import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

export function useWithdrawalColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      title: '代理ID',
      field: 'agent_id',
      width: 100,
    },
    {
      title: '提现金额',
      field: 'amount',
      width: 120,
      formatter: ({ cellValue }) => `¥${cellValue?.toFixed(2) || '0.00'}`,
    },
    {
      title: '扣税比例',
      field: 'tax_rate',
      width: 90,
      formatter: ({ cellValue }) =>
        cellValue != null ? `${(Number(cellValue) * 100).toFixed(1)}%` : '-',
    },
    {
      title: '扣税金额',
      field: 'tax_amount',
      width: 120,
      formatter: ({ cellValue }) => `¥${cellValue?.toFixed(2) || '0.00'}`,
    },
    {
      title: '实际转账金额',
      field: 'actual_amount',
      width: 120,
      formatter: ({ cellValue }) => `¥${cellValue?.toFixed(2) || '0.00'}`,
    },
    {
      title: '提现类型',
      field: 'withdraw_type',
      width: 100,
      formatter: ({ cellValue }) => {
        const typeMap: Record<number, string> = {
          1: '支付宝',
          2: '银行卡',
        };
        return typeMap[cellValue] || '未知';
      },
    },
    {
      title: '收款信息',
      field: 'payee_info',
      width: 200,
      formatter: ({ row }) => {
        if (row.withdraw_type === 1) {
          // 支付宝提现
          return row.payee_account || '-';
        } else if (row.withdraw_type === 2) {
          // 银行卡提现
          if (row.bank_card_no) {
            const cardNo = row.bank_card_no.replaceAll(/\s/g, '');
            const masked =
              cardNo.length > 8
                ? `${cardNo.slice(0, 4)} **** **** ${cardNo.slice(
                    Math.max(0, cardNo.length - 4),
                  )}`
                : cardNo;
            return `${masked}${row.bank_name ? ` (${row.bank_name})` : ''}`;
          }
          return '-';
        }
        return '-';
      },
    },
    {
      title: '收款人',
      field: 'payee_name',
      width: 120,
      formatter: ({ cellValue, row }) => {
        if (row.withdraw_type === 2 && cellValue) {
          return cellValue;
        }
        return '-';
      },
    },
    {
      title: '状态',
      field: 'status',
      width: 100,
      formatter: ({ cellValue }) => {
        const statusMap: Record<number, string> = {
          1: '申请中',
          2: '成功',
          3: '失败',
        };
        return statusMap[cellValue] || '未知';
      },
      cellRender: {
        name: 'VxeTag',
        props: ({ row }) => {
          const statusConfig: Record<
            number,
            { content: string; type: string }
          > = {
            1: { type: 'warning', content: '申请中' },
            2: { type: 'success', content: '成功' },
            3: { type: 'error', content: '失败' },
          };
          const config = statusConfig[row.status] || {
            type: 'info',
            content: '未知',
          };
          return {
            type: config.type,
            content: config.content,
          };
        },
      },
    },
    {
      title: '申请时间',
      field: 'create_time',
      width: 180,
    },
    {
      title: '备注',
      field: 'remark',
      width: 200,
      formatter: ({ cellValue }) => cellValue || '-',
    },
    {
      align: 'center',
      slots: { default: 'operation' },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}

export function useWithdrawalFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'withdraw_type',
      label: '提现类型',
      component: 'Select',
      componentProps: {
        options: [
          { label: '支付宝', value: 1 },
          { label: '银行卡', value: 2 },
        ],
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '申请中', value: 1 },
          { label: '成功', value: 2 },
          { label: '失败', value: 3 },
        ],
        allowClear: true,
      },
    },
  ];
}
