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

import { markRaw } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import CrmCustomerSelect from '#/components/crm-customer-select.vue';

import AfterSaleTicketSelect from './modules/ticket-select.vue';

/** 表单类型 */
export type FormType = 'create' | 'detail' | 'edit';

/** 审批状态常量 */
export const AUDIT_STATUS = {
  DRAFT: 0,
  PROCESS: 10,
  APPROVE: 20,
  REJECT: 30,
  CANCEL: 40,
};

/** 表单的配置项 */
export function useFormSchema(formType: FormType, formApi?: VbenFormApi): VbenFormSchema[] {
  const isReadonly = formType === 'detail';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'compensationNo',
      label: '赔付单号',
      component: 'Input',
      componentProps: {
        placeholder: '系统自动生成',
        disabled: true,
      },
    },
    {
      fieldName: 'compensationType',
      label: '赔付类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AFTER_SALE_COMPENSATION_TYPE, 'number'),
        placeholder: '请选择赔付类型',
        disabled: isReadonly,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'ticketId',
      label: '关联售后工单',
      component: markRaw(AfterSaleTicketSelect),
      componentProps: {
        placeholder: '请选择售后工单',
        disabled: isReadonly,
      },
    },
    {
      fieldName: 'customerId',
      label: '客户',
      component: markRaw(CrmCustomerSelect),
      componentProps: {
        placeholder: '请选择客户',
        disabled: isReadonly,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'saleOrderId',
      label: '关联销售订单',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 0,
        placeholder: '请输入销售订单编号',
        disabled: isReadonly,
      },
    },
    {
      fieldName: 'amount',
      label: '赔付金额',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 2,
        placeholder: '请输入赔付金额',
        disabled: isReadonly,
      },
      rules: 'required',
    },
    {
      fieldName: 'payee',
      label: '赔付对象',
      component: 'Input',
      componentProps: {
        placeholder: '如：张三',
        disabled: isReadonly,
      },
    },
    {
      fieldName: 'reason',
      label: '赔付原因',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: {
        placeholder: '请输入赔付原因',
        rows: 3,
        disabled: isReadonly,
      },
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 1, maxRows: 1 },
        disabled: isReadonly,
      },
      formItemClass: 'col-span-3',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'compensationNo',
      label: '赔付单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入赔付单号',
      },
    },
    {
      fieldName: 'compensationType',
      label: '赔付类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AFTER_SALE_COMPENSATION_TYPE, 'number'),
        placeholder: '请选择赔付类型',
      },
    },
    {
      fieldName: 'customerId',
      label: '客户',
      component: markRaw(CrmCustomerSelect),
      componentProps: {
        placeholder: '请选择客户',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审批状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '未提交', value: AUDIT_STATUS.DRAFT },
          { label: '审批中', value: AUDIT_STATUS.PROCESS },
          { label: '审核通过', value: AUDIT_STATUS.APPROVE },
          { label: '审核不通过', value: AUDIT_STATUS.REJECT },
          { label: '已取消', value: AUDIT_STATUS.CANCEL },
        ],
        placeholder: '请选择审批状态',
      },
    },
  ];
}

/** 列表的表格列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 50 },
    { type: 'seq', title: '序号', width: 60 },
    {
      field: 'compensationNo',
      title: '赔付单号',
      minWidth: 160,
      slots: { default: 'compensationNo' },
    },
    {
      field: 'compensationType',
      title: '赔付类型',
      width: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AFTER_SALE_COMPENSATION_TYPE },
      },
    },
    {
      field: 'customerName',
      title: '客户名称',
      minWidth: 150,
    },
    {
      field: 'amount',
      title: '赔付金额',
      width: 110,
    },
    {
      field: 'payee',
      title: '赔付对象',
      minWidth: 110,
    },
    {
      field: 'reason',
      title: '赔付原因',
      minWidth: 200,
      showOverflow: true,
    },
    {
      field: 'auditStatus',
      title: '审批状态',
      width: 110,
      slots: { default: 'auditStatus' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 170,
      formatter: 'formatDateTime',
    },
    {
      field: 'action',
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
