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

  @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['orderId']" :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="4">手续费</a-select-option>
            <a-select-option :value="3">钱包账户</a-select-option>
            <a-select-option :value="2">虚拟银行</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>
        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="vdata.searchData['changeMethod']" 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="1">对公转账</a-select-option>
            <a-select-option :value="2">账户提现</a-select-option>
            <a-select-option :value="3">平台户退款</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="vdata.searchData['state']" 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="1">审核中</a-select-option>
            <a-select-option :value="2">打款中</a-select-option>
            <a-select-option :value="3">成功</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 === 'amount'">
            <a-popover placement="top" style="background: #0c2640 !important;">
              <template #content  style="background: #ff0 !important;">
                <p>实付金额：￥{{ (record.finalAmount / 100).toFixed(2) }}</p>
                <p>充值金额：￥{{ (record.amount / 100).toFixed(2) }}</p>
                <p>手续费：￥{{ (record.fee / 100).toFixed(2) }}</p>
              </template>
              <b style="color: #1890ff">￥{{ (record.finalAmount/100).toFixed(2) }} </b>
            </a-popover>
          </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 === 6?'':'volcano'"
            >
              {{ record.changeType === 0?'充值':record.changeType === 1?'提现':record.changeType === 2?'转账':record.changeType === 3?'退款':record.changeType === 4?'抵扣':'未知' }}
            </a-tag>
          </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 === 6?'':'volcano'"
            >
<!--              {{ record.accountType === 4?'手续费账户':record.accountType === 3?'钱包账户':record.accountType === 1?'营销账户':record.accountType === 2?'虚拟银行':record.accountType === 0?'退款账户':'未知' }}-->

              {{record.accountTypeDesc }}
            </a-tag>
          </template>
          <template v-if="column.key === 'changeMethod'">
            <a-tag
                :key="record.changeMethod"
                :color="record.changeMethod === -1?'red':record.changeMethod === 0?'blue':record.changeMethod === 1?'orange':record.changeMethod === 2?'green':record.changeMethod === 6?'orange':'volcano'"
            >
              {{record.changeMethodDesc }}
            </a-tag>
          </template>
          <template v-if="column.key === 'state'">
            <a-tag
                :key="record.state"
                :color="record.state === 0?'blue':record.state === 1?'orange':record.state === 2?'orange':record.state === 3?'green':record.state === -1?'red':'volcano'"
            >

              {{record.stateDesc }}
            </a-tag>
            <a-tooltip v-if="record.state == -1"    :title="record.remark" >
              <info-circle-outlined style="color: red;margin-left: 10px"/>
            </a-tooltip>
          </template>
          <template v-if="column.key === 'operation'">
            <!-- 操作列插槽 -->
            <JeepayTableColumns>
              <a-button type="link" @click="detailFunc(record)">详情</a-button>
            </JeepayTableColumns>
          </template>

        </template>
      </JeepayTable>
    </a-card>
    <!-- 详情页面组件  -->
    <InfoDetail ref="infoDetail" :callback-func="searchFunc" />
  </div>
</template>
<script setup lang="ts">

import {
  API_MCH_ACCOUNT_CHANGE_LOG_LIST,
  API_MCH_ACCOUNT_FUND_LOG_LIST,
  API_MCH_PAY_DEFINES,
  req,
  reqLoad
} from '@/api/manage'
import InfoDetail from './Detail.vue'
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 infoDetail = ref()

const infoTable = ref()

let tableColumns = reactive([
  { key: 'orderId', title: '流水号', dataIndex: 'orderId',  },
  // { key: 'orderIsvId', title: '渠道流水号', dataIndex: 'orderIsvId',  },
  { key: 'accountType', title: '账户类型', dataIndex: 'accountType',  },
  { key: 'changeType', title: '变动类型', dataIndex: 'changeType',  },
  // { key: 'mchNo', title: '用户信息', dataIndex: 'mchNo',  },
  { key: 'changeMethod', title: '变动方式', dataIndex: 'changeMethod' , },
  { key: 'amount', title: '金额', dataIndex: 'amount',  },
  { 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({
  searchData : { queryDateRange: 'today' }
})


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_FUND_LOG_LIST, params)
    }
  function searchFunc () { // 点击【查询】按钮点击事件
      infoTable.value.refTable(true)
    }

  function detailFunc (record: any) { // 钱包流水详情页
      infoDetail.value.show(record)
    }
</script>
