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

import type { AgentApi } from '#/api/agent';

export function useWithdrawalColumns(
  onActionClick?: OnActionClickFn<AgentApi.AgentWithdrawalListItem>,
): VxeTableGridOptions['columns'] {
  return [
    {
      title: '提现单号',
      field: 'withdraw_no',
      width: 180,
    },
    {
      title: '代理ID',
      field: 'agent_id',
      width: 100,
    },
    {
      title: '提现金额',
      field: 'amount',
      width: 120,
      formatter: ({ cellValue }) => `¥${cellValue.toFixed(2)}`,
    },
    {
      title: '实际到账',
      field: 'actual_amount',
      width: 120,
      formatter: ({ cellValue }) => `¥${cellValue?.toFixed(2) || '0.00'}`,
    },
    {
      title: '税费',
      field: 'tax_amount',
      width: 100,
      formatter: ({ cellValue }) => `¥${cellValue?.toFixed(2) || '0.00'}`,
    },
    {
      title: '提现方式',
      field: 'withdrawal_type',
      width: 120,
      formatter: ({ cellValue }: { cellValue: number }) => {
        const methodMap: Record<number, string> = {
          1: '支付宝',
          2: '银行卡',
        };
        return methodMap[cellValue] || '未知';
      },
    },
    {
      title: '收款账户',
      field: 'payee_account',
      width: 180,
    },
    {
      title: '收款人',
      field: 'payee_name',
      width: 120,
    },
    {
      title: '银行卡号',
      field: 'bank_card_no',
      width: 180,
    },
    {
      title: '开户行',
      field: 'bank_name',
      width: 180,
    },
    {
      title: '状态',
      field: 'status',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { value: 1, color: 'warning', label: '待审核' },
          { value: 2, color: 'processing', label: '已通过' },
          { value: 3, color: 'error', label: '已拒绝' },
          { value: 5, color: 'success', label: '已打款' },
        ],
      },
    },
    {
      title: '备注',
      field: 'remark',
      width: 200,
    },
    {
      title: '申请时间',
      field: 'create_time',
      width: 180,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'withdraw_no',
          nameTitle: '提现单号',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'audit',
            text: '审核',
            disabled: (row: AgentApi.AgentWithdrawalListItem) => {
              return row.status !== 1; // 只有待审核状态(status=1)才能审核
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 100,
    },
  ];
}

export function useWithdrawalFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'withdraw_no',
      label: '提现单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入提现单号',
        allowClear: true,
      },
    },
    {
      fieldName: 'agent_id',
      label: '代理ID',
      component: 'Input',
      componentProps: {
        placeholder: '请输入代理ID',
        allowClear: true,
      },
    },
    {
      fieldName: 'withdrawal_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 },
          { label: '已打款', value: 5 },
        ],
        allowClear: true,
      },
    },
  ];
}
