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

// 佣金记录列表列配置
export function useCommissionColumns(
  onActionClick?: (params: { code: string; row: AgentApi.AgentCommissionListItem }) => void,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'agent_id',
      title: '代理ID',
      width: 100,
    },
    {
      field: 'order_id',
      title: '订单ID',
      width: 100,
    },
    {
      field: 'amount',
      title: '佣金金额',
      width: 120,
      formatter: ({ cellValue }: { cellValue: number }) =>
        `¥${cellValue.toFixed(2)}`,
    },
    {
      field: 'product_name',
      title: '产品名称',
      width: 150,
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      formatter: ({ cellValue }: { cellValue: number }) => {
        const statusMap: Record<number, string> = {
          0: '已结算',
          1: '冻结中',
          2: '已退款',
        };
        return statusMap[cellValue] || '未知';
      },
    },
    {
      field: 'create_time',
      title: '创建时间',
      width: 160,
      sortable: true,
      sortType: 'string' as const,
    },
    {
      align: 'center',
      cellRender: {
        name: 'CellOperation',
        attrs: {
          nameField: 'id',
          nameTitle: '操作',
          onClick: onActionClick,
        },
        options: [
          {
            code: 'freeze',
            text: '冻结',
            type: 'warning',
            disabled: (row: AgentApi.AgentCommissionListItem) =>
              row?.status !== 0,
            class: (row: AgentApi.AgentCommissionListItem) =>
              row?.status !== 0 ? '!text-gray-400 !cursor-not-allowed' : '',
            tooltip: (row: AgentApi.AgentCommissionListItem) => {
              if (row?.status === 1) return '该佣金已处于冻结中';
              if (row?.status === 2) return '已取消的佣金无法操作';
              return '';
            },
          },
          {
            code: 'unfreeze',
            text: '解冻',
            type: 'primary',
            disabled: (row: AgentApi.AgentCommissionListItem) =>
              row?.status !== 1,
            class: (row: AgentApi.AgentCommissionListItem) =>
              row?.status !== 1 ? '!text-gray-400 !cursor-not-allowed' : '',
            tooltip: (row: AgentApi.AgentCommissionListItem) => {
              if (row?.status === 0) return '已结算的佣金无需解冻';
              if (row?.status === 2) return '已退款的佣金无法操作';
              return '';
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 300,
    },
  ];
}

// 佣金记录搜索表单配置
export function useCommissionFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'InputNumber',
      fieldName: 'order_id',
      label: '订单ID',
      componentProps: {
        placeholder: '请输入订单ID',
        style: { width: '100%' },
      },
    },
    {
      component: 'Input',
      fieldName: 'product_name',
      label: '产品名称',
      componentProps: {
        placeholder: '请输入产品名称（支持模糊搜索）',
      },
    },
    {
      component: 'Select',
      fieldName: 'status',
      label: '状态',
      componentProps: {
        allowClear: true,
        placeholder: '请选择状态',
        options: [
          { label: '已结算', value: 0 },
          { label: '冻结中', value: 1 },
          { label: '已退款', value: 2 },
        ],
      },
    },
  ];
}
