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

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

import { getRiskUnitSimpleList } from '#/api/safety/risk/unit';
import { getSimpleUserList, getWorkshopOptions } from '../risk/shared';

/** 作业票状态 */
export const TICKET_STATUS = {
  PENDING: 0,
  APPROVED: 10,
  FINISHED: 20,
  ARCHIVED: 30,
  REJECTED: 90,
};

export function ticketStatusName(status?: number): string {
  switch (status) {
    case 0: return '待审批';
    case 10: return '已批准';
    case 20: return '已完工';
    case 30: return '已归档';
    case 90: return '已驳回';
    default: return '-';
  }
}

export function ticketStatusColor(status?: number): string {
  switch (status) {
    case 0: return 'processing';
    case 10: return 'success';
    case 20: return 'default';
    case 30: return 'default';
    case 90: return 'error';
    default: return 'default';
  }
}

/** 时间格式：兼容后端 LocalDateTime 的 "yyyy-MM-dd HH:mm:ss" */
export const DATETIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '作业票号',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入作业票号' },
    },
    {
      fieldName: 'ticketType',
      label: '作业类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TICKET_TYPE, 'number'),
        placeholder: '请选择作业类型',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TICKET_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'workLocation',
      label: '作业地点',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入作业地点' },
    },
  ];
}

/** 申请/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'code',
      label: '作业票号',
      component: 'Input',
      componentProps: { placeholder: '如 ZY-2026-001' },
      rules: 'required',
    },
    {
      fieldName: 'ticketType',
      label: '作业类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TICKET_TYPE, 'number'),
        placeholder: '请选择作业类型',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'ticketLevel',
      label: '作业级别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TICKET_LEVEL, 'number'),
        placeholder: '请选择作业级别',
      },
    },
    {
      fieldName: 'unitId',
      label: '关联风险单元',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getRiskUnitSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '可关联到具体风险点',
      },
    },
    {
      fieldName: 'workshopId',
      label: '所属车间',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getWorkshopOptions,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择所属车间',
      },
    },
    {
      fieldName: 'workLocation',
      label: '作业地点',
      component: 'Input',
      componentProps: { placeholder: '如 造纸车间东侧管道' },
    },
    {
      fieldName: 'workContent',
      label: '作业内容',
      component: 'Textarea',
      componentProps: { placeholder: '请描述具体作业内容', rows: 2 },
    },
    {
      fieldName: 'planStartTime',
      label: '计划开始',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        showTime: true,
        format: DATETIME_FORMAT,
        valueFormat: DATETIME_FORMAT,
        placeholder: '请选择计划开始时间',
      },
      rules: 'required',
    },
    {
      fieldName: 'planEndTime',
      label: '计划结束',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        showTime: true,
        format: DATETIME_FORMAT,
        valueFormat: DATETIME_FORMAT,
        placeholder: '请选择计划结束时间',
      },
      rules: 'required',
    },
    {
      fieldName: 'guardianUserId',
      label: '监护人',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
        placeholder: '请选择监护人',
      },
    },
    {
      fieldName: 'contractor',
      label: '施工单位',
      component: 'Input',
      componentProps: { placeholder: '外协作业时填写承包商名称' },
    },
    {
      fieldName: 'workerNames',
      label: '作业人员',
      component: 'Input',
      componentProps: { placeholder: '多个用逗号分隔，如 张三,李四' },
    },
    {
      fieldName: 'safetyMeasures',
      label: '安全措施',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '如：1.清理动火点周边可燃物；2.配备灭火器；3.气体检测合格',
        rows: 3,
      },
    },
    {
      fieldName: 'safetyCheckResult',
      label: '安全交底',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '安全交底 / 检查结果', rows: 2 },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '请输入备注', rows: 2 },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesSafetyTicketApi.WorkTicket>['columns'] {
  return [
    { field: 'code', title: '作业票号', width: 140 },
    {
      field: 'ticketType',
      title: '作业类型',
      width: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_TICKET_TYPE },
      },
    },
    {
      field: 'ticketLevel',
      title: '级别',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_TICKET_LEVEL },
      },
    },
    { field: 'workLocation', title: '作业地点', minWidth: 150 },
    {
      field: 'planStartTime',
      title: '计划时间',
      width: 200,
      slots: { default: 'planTime' },
    },
    { field: 'applicantUserName', title: '申请人', width: 100 },
    { field: 'guardianUserName', title: '监护人', width: 100 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      slots: { default: 'status' },
    },
    {
      title: '操作',
      width: 240,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
