import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { HrmEmployeeContractApi } from '#/api/hrm/employee/contract';

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

import { getEmployeeSimpleList } from '#/api/hrm/employee';

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

/** 新增/修改员工合同的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'employeeId',
      label: '员工',
      component: 'ApiSelect',
      componentProps: {
        api: getEmployeeSimpleList,
        labelField: 'name',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) =>
          option.label?.toLowerCase().includes(input.toLowerCase()),
        placeholder: '请选择员工',
      },
      rules: 'selectRequired',
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'contractType',
      label: '合同类型',
      component: 'Select',
      componentProps: {
        placeholder: '请选择合同类型',
        options: getDictOptions(DICT_TYPE.HRM_CONTRACT_TYPE, 'number'),
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'contractTermType',
      label: '期限类型',
      component: 'Select',
      componentProps: {
        placeholder: '请选择期限类型',
        options: getDictOptions(DICT_TYPE.HRM_CONTRACT_TERM_TYPE, 'number'),
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'signCompany',
      label: '签约主体',
      component: 'Input',
      componentProps: {
        placeholder: '请输入签约主体（公司全称）',
      },
    },
    {
      fieldName: 'signDate',
      label: '签订日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择签订日期',
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'startDate',
      label: '开始日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择开始日期',
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
      },
      rules: 'required',
    },
    {
      fieldName: 'endDate',
      label: '结束日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '无固定期限可不填',
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'probationStartDate',
      label: '试用期开始',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择试用期开始日期',
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'probationEndDate',
      label: '试用期结束',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择试用期结束日期',
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'probationSalary',
      label: '试用期工资',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入试用期工资',
        min: 0,
        precision: 2,
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'regularSalary',
      label: '转正工资',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入转正工资',
        min: 0,
        precision: 2,
        style: { width: '100%' },
      },
    },
    {
      fieldName: 'blobIds',
      label: '附件',
      component: 'FileUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        valueKey: 'id',
        maxNumber: 10,
        multiple: true,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
        rows: 2,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'contractNo',
      label: '合同编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入合同编号',
      },
    },
    {
      fieldName: 'employeeId',
      label: '员工',
      component: 'ApiSelect',
      componentProps: {
        api: getEmployeeSimpleList,
        labelField: 'name',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) =>
          option.label?.toLowerCase().includes(input.toLowerCase()),
        allowClear: true,
        placeholder: '请选择员工',
      },
    },
    {
      fieldName: 'contractType',
      label: '合同类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.HRM_CONTRACT_TYPE, 'number'),
        placeholder: '请选择合同类型',
      },
    },
    {
      fieldName: 'contractTermType',
      label: '期限类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.HRM_CONTRACT_TERM_TYPE, 'number'),
        placeholder: '请选择期限类型',
      },
    },
    {
      fieldName: 'terminateStatus',
      label: '解除终止状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.HRM_CONTRACT_TERMINATE_STATUS, 'number'),
        placeholder: '请选择解除终止状态',
      },
    },
    {
      fieldName: 'status',
      label: '合同状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.HRM_CONTRACT_STATUS, 'number'),
        placeholder: '请选择合同状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<HrmEmployeeContractApi.EmployeeContract>['columns'] {
  return [
    { field: 'contractNo', title: '合同编号', minWidth: 160 },
    {
      field: 'parentNo',
      title: '续签自',
      minWidth: 140,
      slots: { default: 'parentNo' },
    },
    { field: 'employeeName', title: '员工姓名', minWidth: 100 },
    { field: 'employeeNo', title: '员工工号', minWidth: 130 },
    { field: 'deptName', title: '部门', minWidth: 120 },
    {
      field: 'contractType',
      title: '合同类型',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.HRM_CONTRACT_TYPE },
      },
    },
    {
      field: 'contractTermType',
      title: '期限类型',
      width: 130,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.HRM_CONTRACT_TERM_TYPE },
      },
    },
    { field: 'signDate', title: '签订日期', width: 110 },
    { field: 'startDate', title: '开始日期', width: 110 },
    { field: 'endDate', title: '结束日期', width: 110 },
    {
      field: 'status',
      title: '合同状态',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.HRM_CONTRACT_STATUS },
      },
    },
    {
      field: 'isCurrent',
      title: '当前合同',
      width: 90,
      slots: { default: 'isCurrent' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
