<script lang="ts" setup>
import { useTemplateRef } from 'vue';
import { NDatePicker, useDialog, useMessage } from 'naive-ui';
import { client, safeClient } from '@/service/api';
import type { TableBaseColumns, TableFetchData, TableInst } from '@/components/table';

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

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

const columns: TableBaseColumns = [
  {
    title: '订单ID',
    key: 'id'
  },
  {
    title: '订单号',
    key: 'orderNo'
  },
  {
    title: '转出用户ID',
    key: 'fromUserId'
  },
  {
    title: '转入用户ID',
    key: 'toUserId'
  },
  {
    title: '资产代码',
    key: 'assetCode'
  },
  {
    title: '转账金额',
    key: 'amount'
  },
  {
    title: '手续费',
    key: 'fee'
  },
  {
    title: '操作',
    fixed: 'right',
    key: 'operation',
    width: 160,
    operations: (row: any) => [
      {
        contentText: '编辑',
        type: 'primary',
        onClick: () => {
          tableInst.value?.reload();
        }
      },
      {
        contentText: '删除',
        type: 'error',
        ghost: true,
        size: 'small',
        onClick: async () => {
          dialog.create({
            title: '提示',
            positiveText: '是',
            negativeText: '否',
            content: '确认删除该银行信息？',
            onPositiveClick: async () => {
              safeClient(() =>
                client.api.admin.deposit.reject({ orderId: row.id as string }).post({
                  reviewNote: '管理员拒绝充值'
                })
              );
              // tableInst.value?.reload();
              message.success('删除成功');
            }
          });
        }
      }
    ]
  }
];

const filterColumns: TableFilterColumns = [
  {
    title: '资产代码',
    key: 'assetCode'
  },
  {
    title: '转账金额',
    key: 'amount',
    component: NDatePicker
  }
];
</script>

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

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