<!--
  历史记录的通用组件， 可抽屉可 页面

  @author terrfly
  @site https://www.jeequan.com
  @date 2022/03/23 20:23
-->
<template>
  <div>
    <a-card class="table-card">
      <JeepaySearchForm v-if="props.showSearch" :searchFunc="searchFunc" :resetFunc="resetFunc">
        <a-form-item label="" class="table-search-item">
          <JeepayDateRangePicker
              ref="dateRangePicker"
              v-model:value="vdata.searchData['queryDateRange']"
              customDateRangeType="dateTime"
          />
        </a-form-item>
        <jeepay-text-up v-model:value="vdata.searchData['flowNo']" :placeholder="'流水号'" />
        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="vdata.searchData['accountType']" placeholder="选择账户类型">
            <a-select-option :value="''">全部</a-select-option>
            <a-select-option :value="0">退款账户</a-select-option>
            <a-select-option :value="1">营销账户</a-select-option>
            <a-select-option :value="3">钱包账户</a-select-option>
            <a-select-option :value="2">虚拟银行</a-select-option>
            <a-select-option :value="4">手续费</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="vdata.searchData['changeType']" placeholder="选择账变类型">
            <a-select-option :value="''">全部</a-select-option>
            <a-select-option :value="0">充值</a-select-option>
            <a-select-option :value="1">提现</a-select-option>
            <a-select-option :value="2">转账</a-select-option>
            <a-select-option :value="3">退款</a-select-option>
            <a-select-option :value="4">抵扣</a-select-option>
          </a-select>
        </a-form-item>

      </JeepaySearchForm>
      <!-- 列表渲染 -->
      <JeepayTable
          ref="infoTable"
          :init-data="false"
          :req-table-data-func="reqTableDataFunc"
          :table-columns="tableColumns"
          :search-data="vdata.searchData"
          row-key="hid"
      >
        <template #bodyCell="{ column, record }">

          <template v-if="column.key === 'beforeAmt'">
               ￥{{ record.beforeAmt ? (record.beforeAmt/100).toFixed(2) : 0 }}
          </template>
          <template v-if="column.key === 'changeAmt'">
            ￥{{ record.changeAmt ? (record.changeAmt/100).toFixed(2) : 0 }}
          </template>
          <template v-if="column.key === 'afterAmt'">
            ￥{{ record.afterAmt ? (record.afterAmt/100).toFixed(2) : 0 }}
          </template>

          <template v-if="column.key === 'accountType'">
            <a-tag
                :key="record.accountType"
                :color="record.accountType === 0?'red':record.accountType === 1?'orange':record.accountType === 2?'blue':record.accountType === 4?'blue':'volcano'"
            >
              {{ record.accountType === 3?'钱包账户':record.accountType === 1?'营销账户':record.accountType === 2?'虚拟银行':record.accountType === 0?'退款账户':record.accountType === 4?'手续费账户':'未知' }}
            </a-tag>
          </template>
          <template v-if="column.key === 'changeType'">
            <a-tag
                :key="record.changeType"
                :color="record.changeType === 0?'blue':record.changeType === 1?'orange':record.changeType === 2?'green':record.changeType === 3?'red':record.changeType === 4?'orange':'volcano'"
            >
              {{ record.changeType === 0?'充值':record.changeType === 1?'提现':record.changeType === 2?'转账':record.changeType === 3?'退款':record.changeType === 4?'抵扣':'未知' }}
            </a-tag>
          </template>
          <template v-if="column.key === 'state'">
<!--            1处理中 2成功  -1失败-->
            <a-tag v-if="record.state == 1" color="warning">{{ record.stateDesc }}</a-tag>
            <a-tag v-if="record.state == 2" color="success">{{ record.stateDesc }}</a-tag>
            <a-tag v-if="record.state == 3" color="processing">{{ record.stateDesc }}</a-tag>
            <a-tag v-if="record.state == -1" color="error">{{ record.stateDesc }}</a-tag>
            <a-tooltip v-if="record.state == -1"    :title="record.remark" >
              <info-circle-outlined style="color: #FFD080;margin-left: 10px"/>
            </a-tooltip>
          </template>

          <template v-if="column.key === 'operation'">
            <!-- 操作列插槽 -->
            <JeepayTableColumns>
              <a-button type="link" @click="changeFunc(record)" v-if="record.changeType == 1">详情</a-button>
            </JeepayTableColumns>
          </template>

        </template>
      </JeepayTable>
    </a-card>
    <!-- 详情页面组件  -->
  </div>
  <a-modal
      v-model:visible="vdata.visibleStatus"
      title="审核"
      ok-text="确认"
      cancel-text="取消"
      @ok="hideModal"
  >
    <a-form ref="infoFormModel" :model="vdata.searchFrom"  class="edit-box">
      <a-row  justify="space-between" type="flex">
        <a-col :span="24">
          <a-form-item label="审核状态">
            <a-radio-group v-model:value="vdata.info.state">
              <a-radio value="2" checked>通过</a-radio>
              <a-radio value="-1">驳回</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="24" v-if="vdata.info.changeType === 1">
          <a-form-item label="打款方式">
            <a-radio-group v-model:value="vdata.info.mark">
              <a-radio value="0">手动打款</a-radio>
              <a-radio value="1">灵工打款</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注">
            <a-input v-model:value="vdata.info.remark" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>

  <template>
    <a-drawer
        width="50%"
        placement="right"
        :closable="true"
        :visible="vdata.visibleDetailStatus"
        :title="'详情'"
        @close="onClose"
    >
      <a-row justify="space-between" type="flex">
        <a-col :sm="12">
          <a-descriptions>
            <a-descriptions-item label="结算人名称">
              {{ vdata.detailData.settleName}}
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
        <a-col :sm="12">
          <a-descriptions>
            <a-descriptions-item label="结算银行卡账号">
              {{ vdata.detailData.settleNo }}
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
        <a-col :sm="12">
          <a-descriptions>
            <a-descriptions-item label="开户行省市县">
              {{ vdata.detailData.bankCardAreaName }}
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
        <a-col :sm="12">
          <a-descriptions>
            <a-descriptions-item label="开户银行名称">
              {{ vdata.detailData.bankName }}
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
        <a-col :sm="12">
          <a-descriptions>
            <a-descriptions-item label="提现金额">
              ￥{{ (vdata.detailData.cashAmt / 100).toFixed(2) }}
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
      </a-row>
    </a-drawer>
  </template>
</template>
<script setup lang="ts">

import {
  API_MCH_ACCOUNT_CHANGE_LOG_LIST,
  $getAccountChangeConfirm,
  req,
  reqLoad,
  API_URL_PRODUCT_EXAMINEE_CHECK, $getMchAppConfigAppCapability
} from '@/api/manage'

import { ref, reactive, defineProps, getCurrentInstance, onMounted } from 'vue'
const { $infoBox,$access } = getCurrentInstance()!.appContext.config.globalProperties

// 定义父组件的传值
const props = defineProps({
  defaultQueryCondition: { type: Object, default: () => {}  }, // 查询集合的条件
  showSearch: { type: Boolean, default: true } //是否显示搜索面板
})


const infoTable = ref()

let tableColumns = reactive([
  { key: 'flowNo', title: '流水号', dataIndex: 'flowNo',  },
  { key: 'accountType', title: '账户类型', dataIndex: 'accountType',  },
  { key: 'accountNo', title: '账户号', dataIndex: 'accountNo',  },
  { key: 'beforeAmt', title: '变动前账户余额', dataIndex: 'beforeAmt',  },
  { key: 'changeAmt', title: '变动金额', dataIndex: 'changeAmt',  },
  { key: 'afterAmt', title: '变动后账户余额', dataIndex: 'afterAmt' ,  },
  { key: 'changeType', title: '变动类型', dataIndex: 'changeType',  },
  // { key: 'state', title: '状态', dataIndex: 'state' , },
  { key: 'remark', title: '备注',dataIndex: 'remark' },
  { key: 'updatedAt', title: '时间', dataIndex: 'updatedAt' , width: 200,},
  // { key: 'operation', title: '操作',  fixed: 'right', align: 'center'}
])
const dateRangePicker = ref()
const vdata : any = reactive({
  visibleDetailStatus:false,
  visibleStatus:false,
  info:{},
  searchData : {} as any,
  detailData:{} as any,
})


onMounted(() => {
  Object.assign(vdata.searchData, props.defaultQueryCondition)
  infoTable.value.refTable(true)
})

function resetFunc() {
  dateRangePicker.value.returnSelectModel()
  vdata.searchData = { queryDateRange: 'today', opAccountType: 1 }
}

// 请求table接口数据
function reqTableDataFunc(params: any) {
  return req.list(API_MCH_ACCOUNT_CHANGE_LOG_LIST, params)
}
function searchFunc () { // 点击【查询】按钮点击事件
  infoTable.value.refTable(true)
}

function hideModal (){
  checkSave()
  vdata.visibleStatus = false;
}

function checkFunc(data){
    vdata.info = {}
    vdata.info = {
      flowNo:data.flowNo,
      state:data.state,
      remark:data.remark,
      changeType:data.changeType
    }
    vdata.visibleStatus = true;
}

function checkSave () {
  $getAccountChangeConfirm(vdata.info).then((res) => {
    $infoBox.message.success('保存成功')
    searchFunc()
  })
}
function onClose () {
  vdata.visibleDetailStatus = false
}
function changeFunc(record){
  vdata.detailData = {}
  vdata.detailData = record.extra
  console.log(vdata.detailData,'record.extrarecord.extrarecord.extra')
  vdata.visibleDetailStatus = true
}
</script>
