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

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

export const DATETIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';

/** 培训状态 */
export const TRAINING_STATUS = {
  PLANNED: 0,
  ONGOING: 10,
  FINISHED: 20,
  CANCELED: 30,
};

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

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '培训编号',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入培训编号' },
    },
    {
      fieldName: 'title',
      label: '培训主题',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入培训主题' },
    },
    {
      fieldName: 'trainingType',
      label: '培训类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TRAINING_TYPE, 'number'),
        placeholder: '请选择培训类型',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TRAINING_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'code',
      label: '培训编号',
      component: 'Input',
      componentProps: { placeholder: '如 PX-2026-001' },
      rules: 'required',
    },
    {
      fieldName: 'title',
      label: '培训主题',
      component: 'Input',
      componentProps: { placeholder: '如 特种作业安全操作培训' },
      rules: 'required',
    },
    {
      fieldName: 'trainingType',
      label: '培训类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TRAINING_TYPE, 'number'),
        placeholder: '请选择培训类型',
      },
    },
    {
      fieldName: 'trainingLevel',
      label: '培训层级',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TRAINING_LEVEL, 'number'),
        placeholder: '三级安全教育时选择层级',
      },
    },
    {
      fieldName: 'trainer',
      label: '讲师',
      component: 'Input',
      componentProps: { placeholder: '请输入讲师姓名' },
    },
    {
      fieldName: 'trainerOrg',
      label: '讲师单位',
      component: 'Input',
      componentProps: { placeholder: '外部讲师时填写' },
    },
    {
      fieldName: 'location',
      label: '培训地点',
      component: 'Input',
      componentProps: { placeholder: '如 一楼培训室' },
    },
    {
      fieldName: 'startTime',
      label: '开始时间',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        showTime: true,
        format: DATETIME_FORMAT,
        valueFormat: DATETIME_FORMAT,
        placeholder: '请选择培训开始时间',
      },
    },
    {
      fieldName: 'endTime',
      label: '结束时间',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        showTime: true,
        format: DATETIME_FORMAT,
        valueFormat: DATETIME_FORMAT,
        placeholder: '请选择培训结束时间',
      },
    },
    {
      fieldName: 'durationHours',
      label: '学时',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        precision: 1,
        placeholder: '如 4',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: getDictOptions(DICT_TYPE.MES_SAFETY_TRAINING_STATUS, 'number'),
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'content',
      label: '培训内容',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '请输入培训内容', rows: 3 },
    },
    {
      fieldName: 'material',
      label: '培训资料',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '教材/资料名称或链接' },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '请输入备注', rows: 2 },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesSafetyTrainingApi.Training>['columns'] {
  return [
    { field: 'code', title: '培训编号', width: 140 },
    { field: 'title', title: '培训主题', minWidth: 200 },
    {
      field: 'trainingType',
      title: '类型',
      width: 130,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_TRAINING_TYPE },
      },
    },
    {
      field: 'trainingLevel',
      title: '层级',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_TRAINING_LEVEL },
      },
    },
    { field: 'trainer', title: '讲师', width: 100 },
    { field: 'startTime', title: '开始时间', width: 170 },
    { field: 'durationHours', title: '学时', width: 80, align: 'right' },
    {
      field: 'attendeeCount',
      title: '参训/实到',
      width: 120,
      align: 'center',
      slots: { default: 'attendee' },
    },
    {
      field: 'avgScore',
      title: '平均分',
      width: 90,
      align: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_TRAINING_STATUS },
      },
    },
    {
      title: '操作',
      width: 220,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
