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

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

import { getSimpleUserList } from '../risk/shared';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'certNo',
      label: '证书编号',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入证书编号' },
    },
    {
      fieldName: 'certType',
      label: '证书类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_SAFETY_CERT_TYPE, 'number'),
        placeholder: '请选择证书类型',
      },
    },
    {
      fieldName: 'expiringSoon',
      label: '即将到期',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '仅看 30 天内到期（含已过期）', value: true },
          { label: '全部', value: false },
        ],
        placeholder: '到期筛选',
      },
    },
  ];
}

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'userId',
      label: '持证人',
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
        placeholder: '请选择持证人',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'certType',
      label: '证书类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MES_SAFETY_CERT_TYPE, 'number'),
        placeholder: '请选择证书类型',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'certNo',
      label: '证书编号',
      component: 'Input',
      componentProps: { placeholder: '请输入证书编号' },
      rules: 'required',
    },
    {
      fieldName: 'issueOrg',
      label: '发证机关',
      component: 'Input',
      componentProps: { placeholder: '如 新疆应急管理厅' },
    },
    {
      fieldName: 'issueDate',
      label: '发证日期',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        placeholder: '请选择发证日期',
      },
    },
    {
      fieldName: 'expireDate',
      label: '有效期至',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        placeholder: '留空表示长期有效',
      },
    },
    {
      fieldName: 'attachmentUrl',
      label: '证书扫描件',
      component: 'Input',
      componentProps: { placeholder: '扫描件地址（选填）' },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '有效', value: 1 },
          { label: '已作废', value: 0 },
        ],
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '请输入备注', rows: 2 },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesSafetyCertificateApi.Certificate>['columns'] {
  return [
    { field: 'userName', title: '持证人', width: 120 },
    { field: 'deptName', title: '部门', width: 130 },
    {
      field: 'certType',
      title: '证书类型',
      width: 150,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_SAFETY_CERT_TYPE },
      },
    },
    { field: 'certNo', title: '证书编号', minWidth: 160 },
    { field: 'issueOrg', title: '发证机关', minWidth: 150 },
    { field: 'issueDate', title: '发证日期', width: 120 },
    {
      field: 'expireDate',
      title: '有效期至',
      width: 160,
      slots: { default: 'expire' },
    },
    {
      field: 'status',
      title: '状态',
      width: 90,
      slots: { default: 'status' },
    },
    {
      title: '操作',
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
