import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesQcInspectionLedgerApi } from '#/api/mes/qc/ledger';

import { markRaw } from 'vue';

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

import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';
import { MdmItemSelect } from '#/views/basicData/mdm/components';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'qcType',
      label: '分类',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_QC_TYPE, 'number'),
        placeholder: '请选择分类',
      },
    },
    {
      fieldName: 'inspectDate',
      label: '检测日期',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
      },
    },
    {
      fieldName: 'code',
      label: '检验单编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入检验单编号',
      },
    },
    {
      fieldName: 'itemId',
      label: '产品物料',
      component: markRaw(MdmItemSelect),
      componentProps: {
        placeholder: '请选择产品物料',
      },
    },
    {
      fieldName: 'batchNo',
      label: '批次号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入批次号',
      },
    },
    {
      fieldName: 'checkResult',
      label: '检测结果',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_QC_CHECK_RESULT, 'number'),
        placeholder: '请选择检测结果',
      },
    },
    {
      fieldName: 'inspectorUserId',
      label: '检测人员',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        placeholder: '请选择检测人员',
        valueField: 'id',
      },
    },
  ];
}

/** 列表的字段（一行一个检测项结果） */
export function useGridColumns(): VxeTableGridOptions<MesQcInspectionLedgerApi.Ledger>['columns'] {
  return [
    {
      field: 'qcType',
      title: '分类',
      width: 130,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_QC_TYPE },
      },
    },
    {
      field: 'code',
      title: '检验单编号',
      width: 170,
    },
    {
      field: 'name',
      title: '检验单名称',
      minWidth: 180,
      showOverflow: 'tooltip',
    },
    {
      field: 'itemCode',
      title: '物料编码',
      width: 130,
    },
    {
      field: 'itemName',
      title: '物料名称',
      minWidth: 150,
      showOverflow: 'tooltip',
    },
    {
      field: 'itemSpecification',
      title: '规格型号',
      minWidth: 140,
      showOverflow: 'tooltip',
    },
    {
      field: 'batchNo',
      title: '批次号',
      width: 130,
    },
    {
      field: 'indicatorName',
      title: '检测项',
      minWidth: 140,
      showOverflow: 'tooltip',
    },
    {
      field: 'indicatorValue',
      title: '检测值',
      minWidth: 120,
      showOverflow: 'tooltip',
    },
    {
      field: 'judgementBasis',
      title: '判定依据',
      minWidth: 120,
      showOverflow: 'tooltip',
    },
    {
      field: 'checkResult',
      title: '检测结果',
      width: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_QC_CHECK_RESULT },
      },
    },
    {
      field: 'checkQuantity',
      title: '检测数量',
      width: 100,
    },
    {
      field: 'qualifiedQuantity',
      title: '合格数量',
      width: 100,
    },
    {
      field: 'unqualifiedQuantity',
      title: '不合格数量',
      width: 110,
    },
    {
      field: 'inspectorName',
      title: '检测人员',
      width: 110,
    },
    {
      field: 'inspectDate',
      title: '检测日期',
      width: 170,
      formatter: 'formatDateTime',
    },
    {
      field: 'status',
      title: '单据状态',
      width: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_ORDER_STATUS },
      },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 160,
      showOverflow: 'tooltip',
    },
  ];
}
