import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesSafetyRiskUnitApi } from '#/api/safety/risk/unit';

import { DICT_TYPE } from '#/packages/constants/src';
import { getDictOptions } from '#/packages/effects/hooks/src';

import { z } from '#/adapter/form';
import { getSimpleUserList, getWorkshopOptions } from '../shared';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '单元编码',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入风险单元编码' },
    },
    {
      fieldName: 'name',
      label: '单元名称',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入风险单元名称' },
    },
    {
      fieldName: 'riskCategory',
      label: '风险类别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_RISK_CATEGORY, 'number'),
        placeholder: '请选择风险类别',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_UNIT_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'code',
      label: '单元编码',
      component: 'Input',
      componentProps: { placeholder: '如 FX-001' },
      rules: 'required',
    },
    {
      fieldName: 'name',
      label: '单元名称',
      component: 'Input',
      componentProps: { placeholder: '如 造纸车间1#烘缸' },
      rules: 'required',
    },
    {
      fieldName: 'workshopId',
      label: '所属车间',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getWorkshopOptions,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择所属车间',
      },
    },
    {
      fieldName: 'location',
      label: '具体位置',
      component: 'Input',
      componentProps: { placeholder: '如 造纸车间东侧' },
    },
    {
      fieldName: 'riskCategory',
      label: '风险类别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_RISK_CATEGORY, 'number'),
        placeholder: '请选择风险类别',
      },
    },
    {
      fieldName: 'chargeUserId',
      label: '责任人',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
        placeholder: '请选择责任人',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: getDictOptions(DICT_TYPE.MES_SAFETY_UNIT_STATUS, 'number'),
      },
      rules: z.number().default(1),
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '请输入备注', rows: 3 },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesSafetyRiskUnitApi.RiskUnit>['columns'] {
  return [
    { field: 'code', title: '单元编码', width: 130 },
    { field: 'name', title: '单元名称', minWidth: 180 },
    { field: 'workshopName', title: '所属车间', width: 120 },
    { field: 'location', title: '具体位置', minWidth: 150 },
    {
      field: 'riskCategory',
      title: '风险类别',
      width: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_RISK_CATEGORY },
      },
    },
    { field: 'chargeUserName', title: '责任人', width: 100 },
    {
      field: 'riskCount',
      title: '风险条数',
      width: 100,
      align: 'right',
      slots: { default: 'riskCount' },
    },
    {
      field: 'status',
      title: '状态',
      width: 90,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_UNIT_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
