import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { HrmAttendanceHolidayApi } from '#/api/hrm/attendance/holiday';

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

/** 表单类型 */
export type FormType = 'create' | 'update' | '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: 'date',
      label: '日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '选择日期',
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
      },
      rules: 'required',
    },
    {
      fieldName: 'type',
      label: '类型',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.HRM_HOLIDAY_TYPE, 'number'),
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'isLegal',
      label: '法定节假日',
      component: 'Checkbox',
      componentProps: {
        // @ts-ignore
        trueValue: 1,
        falseValue: 0,
      },
      help: '勾选表示为法定节假日',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
        rows: 2,
        maxlength: 500,
        showCount: true,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '节假日名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入节假日名称',
      },
    },
    {
      fieldName: 'date',
      label: '日期',
      component: 'RangePicker',
      componentProps: {
        allowClear: true,
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'type',
      label: '类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.HRM_HOLIDAY_TYPE, 'number'),
        placeholder: '请选择类型',
      },
    },
    {
      fieldName: 'isLegal',
      label: '法定节假日',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '是', value: 1 },
          { label: '否', value: 0 },
        ],
        placeholder: '请选择',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<HrmAttendanceHolidayApi.Holiday>['columns'] {
  return [
    { field: 'name', title: '节假日名称', minWidth: 150 },
    { field: 'date', title: '日期', width: 120 },
    {
      field: 'type',
      title: '类型',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.HRM_HOLIDAY_TYPE },
      },
    },
    {
      field: 'isLegal',
      title: '法定节假日',
      width: 100,
      slots: {
        default: 'isLegal',
      },
    },
    { field: 'remark', title: '备注', minWidth: 200 },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 150,
      fixed: 'right',
      slots: {
        default: 'actions',
      },
    },
  ];
}