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

/** 培训类型选项 */
export const trainingTypeOptions = [
  { label: '安全培训', value: 1 },
  { label: '技能培训', value: 2 },
  { label: '管理培训', value: 3 },
  { label: '其他', value: 4 },
];

/** 培训状态选项 */
export const trainingStatusOptions = [
  { label: '草稿', value: 0 },
  { label: '待开展', value: 10 },
  { label: '已完成', value: 20 },
  { label: '已取消', value: 30 },
];

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

/** 表单的配置项 */
export function useFormSchema(formType: FormType): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '培训名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入培训名称',
      },
      rules: 'required',
    },
    {
      fieldName: 'trainingType',
      label: '培训类型',
      component: 'Select',
      componentProps: {
        options: trainingTypeOptions,
        placeholder: '请选择培训类型',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'trainer',
      label: '讲师',
      component: 'Input',
      componentProps: {
        placeholder: '请输入讲师',
      },
    },
    {
      fieldName: 'location',
      label: '培训地点',
      component: 'Input',
      componentProps: {
        placeholder: '请输入培训地点',
      },
    },
    {
      fieldName: 'hours',
      label: '培训学时',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入培训学时',
        min: 0,
        precision: 1,
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'startDate',
      label: '开始日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '选择开始日期',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'endDate',
      label: '结束日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '选择结束日期',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'issueCertificate',
      label: '自动发证',
      component: 'Select',
      componentProps: {
        options: [
          { label: '是', value: 1 },
          { label: '否', value: 0 },
        ],
        placeholder: '是否自动发证',
      },
    },
    {
      fieldName: 'certValidityMonths',
      label: '证书有效期(月)',
      component: 'InputNumber',
      componentProps: {
        placeholder: '自动发证时填写',
        min: 0,
        precision: 0,
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 1, maxRows: 2 },
      },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'trainingNo',
      label: '培训编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入培训编号',
      },
    },
    {
      fieldName: 'name',
      label: '培训名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入培训名称',
      },
    },
    {
      fieldName: 'trainingType',
      label: '培训类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: trainingTypeOptions,
        placeholder: '请选择培训类型',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: trainingStatusOptions,
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<HrmTrainingApi.Training>['columns'] {
  return [
    {
      field: 'trainingNo',
      title: '培训编号',
      width: 140,
    },
    {
      field: 'name',
      title: '培训名称',
      minWidth: 180,
    },
    {
      field: 'trainingTypeName',
      title: '培训类型',
      width: 100,
    },
    {
      field: 'trainer',
      title: '讲师',
      width: 100,
    },
    {
      field: 'location',
      title: '培训地点',
      minWidth: 120,
    },
    {
      field: 'hours',
      title: '学时',
      width: 80,
    },
    {
      field: 'startDate',
      title: '开始日期',
      width: 105,
      formatter: 'formatDate',
    },
    {
      field: 'endDate',
      title: '结束日期',
      width: 105,
      formatter: 'formatDate',
    },
    {
      field: 'participantCount',
      title: '参训人数',
      width: 90,
    },
    {
      field: 'statusName',
      title: '状态',
      width: 90,
      slots: { default: 'status' },
    },
    {
      field: 'action',
      title: '操作',
      width: 240,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}