<script lang="ts" setup>
import { h, useTemplateRef } from 'vue';
import { useDateFormat } from '@vueuse/core';
import { useDialog, useMessage } from 'naive-ui';
import { client, safeClient } from '@/service/api';
import type { TableBaseColumns, TableFetchData, TableFilterColumns, TableInst } from '@/components/table';
import { WithdrawMethodEnum, WithdrawStatusEnum } from '@/enum';
import Complete from './components/complete.vue';

const dialog = useDialog();
const message = useMessage();
const tableInst = useTemplateRef<TableInst>('tableInst');

const fetchData: TableFetchData = ({ pagination, filter }) => {
  return safeClient(() =>
    client.api.admin.withdraw.approved.get({
      query: {
        ...pagination,
        ...filter
      }
    })
  );
};

const columns: TableBaseColumns = [
  {
    title: 'ID',
    key: 'id'
  },
  {
    title: '资产账户',
    key: 'assetCode'
  },
  {
    title: '提现金额',
    key: 'amount',
    render: row => {
      return Number(row.amount).toFixed(2);
    }
  },
  {
    title: '实际到账金额',
    key: 'actualAmount',
    render: row => {
      return Number(row.actualAmount).toFixed(2);
    }
  },
  {
    title: '提现方式',
    key: 'withdrawMethod',
    render: row => {
      return WithdrawMethodEnum[row.withdrawMethod as keyof typeof WithdrawMethodEnum];
    }
  },
  {
    title: '手续费',
    key: 'fee',
    render: row => {
      return Number(row.fee).toFixed(2);
    }
  },
  {
    title: '状态',
    key: 'status',
    render: row => {
      return WithdrawStatusEnum[row.status as keyof typeof WithdrawStatusEnum];
    }
  },
  {
    title: '创建时间',
    key: 'createdAt',
    render: (row: any) => {
      return useDateFormat(row.createdAt, 'YYYY-MM-DD HH:mm').value;
    }
  },
  {
    title: '操作',
    fixed: 'right',
    key: 'operation',
    width: 160,
    operations: (row: any) => [
      {
        contentText: '完成',
        type: 'primary',
        ghost: true,
        onClick: () => {
          const dialogInstance = dialog.create({
            title: '提示',
            style: { width: '600px' },
            content: () =>
              h(Complete, {
                data: row,
                onClose: () => {
                  dialogInstance.destroy();
                  tableInst.value?.reload();
                }
              })
          });
        }
      },
      {
        contentText: '拒绝',
        type: 'error',
        ghost: true,
        onClick: async () => {
          dialog.create({
            title: '提示',
            positiveText: '是',
            negativeText: '否',
            content: '确定拒绝该提现申请吗？',
            onPositiveClick: async () => {
              await safeClient(() =>
                client.api.admin.deposit.reject({ orderId: row.id as string }).post({
                  reviewNote: '管理员拒绝充值'
                })
              );
              tableInst.value?.reload();
              message.success('拒绝成功');
            }
          });
        }
      }
    ]
  }
];

const filterColumns: TableFilterColumns = [
  {
    title: '提现金额',
    key: 'amount'
  }
];
</script>

<template>
  <TableBase
    ref="tableInst"
    show-header-operation
    :columns="columns"
    :filter-columns="filterColumns"
    :fetch-data="fetchData"
  />
</template>

<style lang="css" scoped></style>
