import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesProWorkOrderApi } from '#/api/mes/pro/workorder';

import { markRaw } from 'vue';

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

import CrmCustomerSelect from '#/components/crm-customer-select.vue';
import { getRangePickerDefaultProps } from '#/utils';
import { MdItemSelect } from '#/views/mes/md/item/components';

/** 工单中心搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '订单编码',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入订单编码' },
    },
    {
      fieldName: 'name',
      label: '订单名称',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入订单名称' },
    },
    {
      fieldName: 'productId',
      label: '产品',
      component: markRaw(MdItemSelect),
      componentProps: { placeholder: '请选择产品' },
    },
    {
      fieldName: 'clientId',
      label: '客户',
      component: markRaw(CrmCustomerSelect),
      componentProps: { placeholder: '请选择客户' },
    },
    {
      fieldName: 'requestDate',
      label: '需求日期',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 工单中心列表字段 */
export function useGridColumns(): VxeTableGridOptions<MesProWorkOrderApi.WorkOrder>['columns'] {
  return [
    {
      field: 'code',
      title: '订单编码',
      fixed: 'left',
      width: 200,
      treeNode: true,
      slots: { default: 'code' },
    },
    {
      field: 'name',
      title: '订单名称',
      minWidth: 150,
    },
    {
      field: 'productCode',
      title: '产品编码',
      width: 120,
    },
    {
      field: 'productName',
      title: '产品名称',
      minWidth: 120,
    },
    {
      field: 'productSpecification',
      title: '规格型号',
      width: 120,
    },
    {
      field: 'unitMeasureName',
      title: '单位',
      width: 80,
    },
    {
      field: 'quantity',
      title: '生产数量',
      width: 100,
    },
    {
      field: 'quantityScheduled',
      title: '已排产',
      width: 100,
    },
    {
      field: 'quantityProduced',
      title: '已生产',
      width: 100,
    },
    {
      field: 'clientName',
      title: '客户名称',
      width: 120,
    },
    {
      field: 'requestDate',
      title: '需求日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_PRO_WORK_ORDER_STATUS },
      },
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 进度中的任务列表字段 */
export function useTaskGridColumns(): VxeTableGridOptions<any>['columns'] {
  return [
    {
      field: 'taskCode',
      title: '任务编码',
      width: 180,
    },
    {
      field: 'workstationName',
      title: '工作站',
      width: 140,
    },
    {
      field: 'quantity',
      title: '排产数量',
      width: 100,
    },
    {
      field: 'producedQuantity',
      title: '已生产',
      width: 100,
    },
    {
      field: 'progressPercent',
      title: '进度',
      width: 120,
      slots: { default: 'taskProgress' },
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_PRO_TASK_STATUS },
      },
    },
    {
      title: '操作',
      width: 100,
      fixed: 'right',
      slots: { default: 'taskActions' },
    },
  ];
}
