import type { VbenFormApi, VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesQcNcrApi } from '#/api/mes/qc/ncr';

import { h, markRaw } from 'vue';

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

import { Button } from 'ant-design-vue';

import { getSimpleUserList } from '#/api/system/user';
import { DeptTreeSelect } from '#/views/system/dept/components';
import { MdItemSelect } from '#/views/mes/md/item/components';

/** NCR 状态枚举 */
export const NCR_STATUS = {
  PENDING_REVIEW: 0,
  REVIEWED: 1,
  DISPOSED: 2,
  CLOSED: 3,
} as const;

export const NCR_STATUS_OPTIONS = [
  { label: '待评审', value: 0 },
  { label: '已评审', value: 1 },
  { label: '已处置', value: 2 },
  { label: '已关闭', value: 3 },
];

/** 检验类型枚举 */
export const QC_TYPE_OPTIONS = [
  { label: 'IQC', value: 1 },
  { label: 'IPQC', value: 2 },
  { label: 'OQC', value: 3 },
  { label: 'RQC', value: 4 },
];

/** 新增/修改的表单 */
export function useFormSchema(
  formApi?: VbenFormApi,
): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'qcId',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'status',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: 'NCR单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入NCR单号',
      },
      rules: 'required',
      suffix: () =>
        h(
          Button,
          {
            type: 'default',
            onClick: async () => {
              const now = new Date();
              const pad = (n: number) => String(n).padStart(2, '0');
              const code = `NCR${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
              await formApi?.setFieldValue('code', code);
            },
          },
          { default: () => '自动生成' },
        ),
    },
    {
      fieldName: 'name',
      label: 'NCR名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入NCR名称',
      },
      rules: 'required',
    },
    {
      fieldName: 'qcType',
      label: '检验类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: QC_TYPE_OPTIONS,
        placeholder: '请选择检验类型',
      },
    },
    {
      fieldName: 'itemId',
      label: '不合格物料',
      component: markRaw(MdItemSelect),
      componentProps: {
        placeholder: '请选择不合格物料',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'defectLevel',
      label: '缺陷等级',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_DEFECT_LEVEL, 'number'),
        placeholder: '请选择缺陷等级',
      },
    },
    {
      fieldName: 'unqualifiedQuantity',
      label: '不合格数量',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        placeholder: '请输入不合格数量',
        precision: 2,
      },
    },
    {
      fieldName: 'defectDescription',
      label: '不合格描述',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入不合格描述',
        rows: 3,
      },
    },
    {
      fieldName: 'rootCause',
      label: '根本原因',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入根本原因',
        rows: 2,
      },
    },
    {
      fieldName: 'correctiveAction',
      label: '纠正措施',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入纠正措施',
        rows: 2,
      },
    },
    {
      fieldName: 'preventiveAction',
      label: '预防措施',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入预防措施',
        rows: 2,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        rows: 2,
      },
    },
  ];
}

/** 评审表单 */
export function useReviewFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'disposition',
      label: '处置方式',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_NCR_DISPOSITION, 'number'),
        placeholder: '请选择处置方式',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'defectLevel',
      label: '缺陷等级',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_DEFECT_LEVEL, 'number'),
        placeholder: '请选择缺陷等级',
      },
    },
    {
      fieldName: 'responsibleDeptId',
      label: '责任部门',
      component: markRaw(DeptTreeSelect),
      componentProps: {
        placeholder: '请选择责任部门',
      },
    },
    {
      fieldName: 'reviewOpinion',
      label: '评审意见',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入评审意见',
        rows: 3,
      },
    },
  ];
}

/** 处置表单 */
export function useDisposeFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'handlerUserId',
      label: '处理人',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        placeholder: '请选择处理人',
        valueField: 'id',
      },
    },
    {
      fieldName: 'handleResult',
      label: '处理结果说明',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入处理结果说明',
        rows: 3,
      },
    },
  ];
}

/** 关闭表单 */
export function useCloseFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'closeOpinion',
      label: '关闭意见',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入关闭意见',
        rows: 3,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: 'NCR单号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入NCR单号',
      },
    },
    {
      fieldName: 'qcType',
      label: '检验类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: QC_TYPE_OPTIONS,
        placeholder: '请选择检验类型',
      },
    },
    {
      fieldName: 'itemId',
      label: '不合格物料',
      component: markRaw(MdItemSelect),
      componentProps: {
        placeholder: '请选择物料',
      },
    },
    {
      fieldName: 'defectLevel',
      label: '缺陷等级',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_DEFECT_LEVEL, 'number'),
        placeholder: '请选择缺陷等级',
      },
    },
    {
      fieldName: 'disposition',
      label: '处置方式',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_NCR_DISPOSITION, 'number'),
        placeholder: '请选择处置方式',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: NCR_STATUS_OPTIONS,
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        class: '!w-full',
        placeholder: '请选择创建时间',
        showTime: true,
        type: 'date',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesQcNcrApi.Ncr>['columns'] {
  return [
    {
      field: 'code',
      title: 'NCR单号',
      width: 200,
    },
    {
      field: 'qcCode',
      title: '来源检验单编号',
      width: 180,
    },
    {
      field: 'itemName',
      title: '物料名称',
      minWidth: 150,
    },
    {
      field: 'itemSpecification',
      title: '规格型号',
      width: 130,
    },
    {
      field: 'qcType',
      title: '检验类型',
      width: 100,
      slots: { default: 'qcType' },
    },
    {
      field: 'defectLevel',
      title: '缺陷等级',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_DEFECT_LEVEL },
      },
    },
    {
      field: 'unqualifiedQuantity',
      title: '不合格数量',
      width: 110,
    },
    {
      field: 'disposition',
      title: '处置方式',
      width: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_NCR_DISPOSITION },
      },
    },
    {
      field: 'status',
      title: '状态',
      width: 90,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_NCR_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 220,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
