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

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

export { AftersaleReturnStatusEnum } from '@vben/constants';

/** 退货类型选项 */
export const RETURN_TYPE_OPTIONS = [
  { label: '退款退货', value: 1 },
  { label: '换货', value: 2 },
  { label: '仅退款', value: 3 },
];

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

/** 退货申请表单 schema */
export function useReturnFormSchema(
  formType: ReturnFormType,
): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'ticketId',
      label: '售后工单',
      component: 'Input',
      componentProps: {
        disabled: true,
        placeholder: '售后工单',
      },
    },
    {
      fieldName: 'customerName',
      label: '客户',
      component: 'Input',
      componentProps: {
        disabled: true,
        placeholder: '客户',
      },
    },
    {
      fieldName: 'saleOrderId',
      label: '销售订单ID',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    ...(formType === 'detail'
      ? [
          {
            fieldName: 'no',
            label: '退货单号',
            component: 'Input',
            componentProps: {
              disabled: true,
              placeholder: '退货单号',
            },
          },
          {
            fieldName: 'status',
            label: '单据状态',
            component: 'Select',
            componentProps: {
              disabled: true,
              options: getDictOptions(
                DICT_TYPE.AFTERSALE_RETURN_STATUS,
                'number',
              ),
              placeholder: '单据状态',
            },
          },
          {
            fieldName: 'totalReturnPrice',
            label: '应退金额',
            component: 'Input',
            componentProps: {
              disabled: true,
              placeholder: '应退金额',
            },
          },
          {
            fieldName: 'actualRefundPrice',
            label: '实退金额',
            component: 'Input',
            componentProps: {
              disabled: true,
              placeholder: '实退金额',
            },
          },
          {
            fieldName: 'mesReturnCode',
            label: 'MES退货单号',
            component: 'Input',
            componentProps: {
              disabled: true,
              placeholder: 'MES退货单号',
            },
          },
          {
            fieldName: 'qualityResult',
            label: '质检结果',
            component: 'Input',
            componentProps: {
              disabled: true,
              placeholder: '质检结果',
            },
          },
          {
            fieldName: 'qualityReportUrl',
            label: '质检报告',
            component: 'Input',
            componentProps: {
              disabled: true,
              placeholder: '质检报告',
            },
          },
        ]
      : []),
    {
      fieldName: 'returnType',
      label: '退货类型',
      component: 'Select',
      componentProps: {
        options: RETURN_TYPE_OPTIONS,
        placeholder: '请选择退货类型',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'returnReason',
      label: '退货原因',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: {
        placeholder: '请输入退货原因',
        rows: 3,
      },
      rules: 'required',
    },
    {
      fieldName: 'defectCategory',
      label: '缺陷类别',
      component: 'Input',
      componentProps: {
        placeholder: '请输入缺陷类别',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: {
        placeholder: '请输入备注',
        rows: 3,
      },
    },
  ];
}

/** 列表搜索表单 schema */
export function useReturnGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '退货单号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入退货单号',
      },
    },
    {
      fieldName: 'ticketId',
      label: '售后工单',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入售后工单号',
      },
    },
    {
      fieldName: 'customerId',
      label: '客户',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入客户',
      },
    },
    {
      fieldName: 'status',
      label: '单据状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AFTERSALE_RETURN_STATUS, 'number'),
        placeholder: '请选择单据状态',
      },
    },
  ];
}

/** 列表字段 */
export function useReturnGridColumns(): VxeTableGridOptions<AftersalesReturnApi.Return>['columns'] {
  return [
    {
      field: 'no',
      title: '退货单号',
      minWidth: 160,
      slots: { default: 'no' },
    },
    {
      field: 'ticketId',
      title: '售后工单',
      minWidth: 120,
      slots: { default: 'ticketId' },
    },
    {
      field: 'customerName',
      title: '客户名称',
      minWidth: 150,
    },
    {
      field: 'returnType',
      title: '退货类型',
      minWidth: 110,
      slots: { default: 'returnType' },
    },
    {
      field: 'totalReturnPrice',
      title: '应退金额',
      minWidth: 110,
    },
    {
      field: 'actualRefundPrice',
      title: '实退金额',
      minWidth: 110,
    },
    {
      field: 'status',
      title: '单据状态',
      minWidth: 110,
      slots: { default: 'status' },
    },
    {
      field: 'mesReturnCode',
      title: 'MES退货单号',
      minWidth: 140,
    },
    {
      field: 'qualityResult',
      title: '质检结果',
      minWidth: 110,
      slots: { default: 'qualityResult' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDate',
    },
    {
      title: '操作',
      width: 180,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 退货明细子表字段 */
export function useReturnItemGridColumns(): VxeTableGridOptions<AftersalesReturnApi.ReturnItem>['columns'] {
  return [
    {
      field: 'itemCode',
      title: '物料编码',
      minWidth: 120,
    },
    {
      field: 'itemName',
      title: '物料名称',
      minWidth: 140,
    },
    {
      field: 'batchCode',
      title: '批次号',
      minWidth: 120,
    },
    {
      field: 'returnQuantity',
      title: '退货数量',
      width: 100,
    },
    {
      field: 'unitPrice',
      title: '单价',
      width: 100,
    },
    {
      field: 'returnPrice',
      title: '退货金额',
      width: 110,
    },
  ];
}
