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

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

import { getRangePickerDefaultProps } from '#/utils';

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

/** 计划状态常量 */
export const PLAN_STATUS = {
  DRAFT: 0,
  ISSUED: 10,
  FINISHED: 20,
  CANCELLED: 30,
};

/** 表单的配置项 */
export function useFormSchema(formType: FormType): VbenFormSchema[] {
  const isReadonly = formType === 'detail';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '计划编码',
      component: 'Input',
      componentProps: {
        placeholder: '系统自动生成',
        disabled: true,
      },
    },
    {
      fieldName: 'name',
      label: '计划名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入计划名称',
        disabled: isReadonly,
      },
      rules: 'required',
    },
    {
      fieldName: 'planType',
      label: '计划类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MES_PRO_MAINTENANCE_PLAN_TYPE, 'number'),
        placeholder: '请选择计划类型',
        disabled: isReadonly,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'station',
      label: '站点',
      component: 'Input',
      componentProps: {
        placeholder: '请输入站点',
        disabled: isReadonly,
      },
    },
    {
      fieldName: 'line',
      label: '线路',
      component: 'Input',
      componentProps: {
        placeholder: '请输入线路',
        disabled: isReadonly,
      },
    },
    {
      fieldName: 'planDate',
      label: '计划日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '选择计划日期',
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        disabled: isReadonly,
      },
      rules: 'required',
    },
    {
      fieldName: 'teamId',
      label: '班组',
      component: 'Input',
      componentProps: {
        disabled: true,
        placeholder: '由计划执行分配',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 1, maxRows: 1 },
        disabled: isReadonly,
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'items',
      label: '备件/物资需求',
      component: 'Input',
      formItemClass: 'col-span-3',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '计划编码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入计划编码',
      },
    },
    {
      fieldName: 'name',
      label: '计划名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入计划名称',
      },
    },
    {
      fieldName: 'planType',
      label: '计划类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_PRO_MAINTENANCE_PLAN_TYPE, 'number'),
        placeholder: '请选择计划类型',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '草稿', value: PLAN_STATUS.DRAFT },
          { label: '已下发', value: PLAN_STATUS.ISSUED },
          { label: '已完成', value: PLAN_STATUS.FINISHED },
          { label: '已取消', value: PLAN_STATUS.CANCELLED },
        ],
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'planDate',
      label: '计划日期',
      component: 'RangePicker',
      componentProps: getRangePickerDefaultProps(),
    },
  ];
}

/** 列表的表格列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 50 },
    { type: 'seq', title: '序号', width: 60 },
    {
      field: 'code',
      title: '计划编码',
      minWidth: 150,
      slots: { default: 'code' },
    },
    {
      field: 'name',
      title: '计划名称',
      minWidth: 180,
    },
    {
      field: 'planType',
      title: '计划类型',
      width: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_PRO_MAINTENANCE_PLAN_TYPE },
      },
    },
    {
      field: 'station',
      title: '站点',
      minWidth: 140,
    },
    {
      field: 'line',
      title: '线路',
      minWidth: 140,
    },
    {
      field: 'teamName',
      title: '班组',
      width: 110,
    },
    {
      field: 'planDate',
      title: '计划日期',
      width: 170,
      formatter: 'formatDateTime',
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      slots: { default: 'status' },
    },
    {
      field: 'workOrderCode',
      title: '生成的工单',
      minWidth: 150,
    },
    {
      field: 'action',
      title: '操作',
      width: 300,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
