import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';

import { getDictOptions } from '#/utils/dict';

// 字典标识常量
const DICT_KEYS = {
  // 任务类型字典标识
  TASK_TYPE: 'task_type',
  // 岗位类型字典标识
  POST_TYPE: 'post_type',
};

// 获取任务类型字典选项
const getTaskTypeOptions = () => getDictOptions(DICT_KEYS.TASK_TYPE);

// 获取岗位类型字典选项
const getPostTypeOptions = () => getDictOptions(DICT_KEYS.POST_TYPE);

export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 60,
  },
  {
    field: 'postId',
    title: '岗位编号',
  },
  {
    field: 'postName',
    title: '岗位名称',
  },
  {
    field: 'postSort',
    title: '岗位排序',
  },
  {
    field: 'taskType',
    title: '任务类型',
    formatter: (params: {
      cellValue: string | string[];
      column: any;
      row: any;
    }) => {
      const options = getTaskTypeOptions();
      // 处理接口返回的逗号分隔字符串和前端多选的数组
      const cellValue = params.cellValue;
      const values =
        typeof cellValue === 'string'
          ? cellValue.split(',')
          : (Array.isArray(cellValue)
            ? cellValue
            : [cellValue]);
      return values
        .map((value) => {
          const option = options.find((item) => item.value === value);
          return option ? option.label : value;
        })
        .filter(Boolean)
        .join(', ');
    },
  },
  {
    field: 'typeName',
    title: '岗位类型',
    // formatter: (params: { cellValue: string; column: any; row: any }) => {
    //   const options = getPostTypeOptions();
    //   const option = options.find((item) => item.value === params.cellValue);
    //   return option ? option.label : params.cellValue;
    // },
  },
  {
    field: 'status',
    title: '状态',
    slots: { default: 'status' },
  },
  {
    field: 'createTime',
    title: '创建时间',
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
  },
];

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    label: '岗位名称',
    fieldName: 'postName',
  },
  {
    component: 'Select',
    label: '任务类型',
    componentProps: {
      placeholder: '请选择任务类型',
      options: getTaskTypeOptions(),
      multiple: true,
    },
    fieldName: 'taskType',
  },
  {
    component: 'Select',
    label: '岗位类型',
    componentProps: {
      placeholder: '请选择岗位类型',
      options: getPostTypeOptions(),
    },
    fieldName: 'type',
  },
  {
    component: 'Select',
    label: '状态',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
    fieldName: 'status',
  },
];
export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'postId',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    label: '岗位名称',
    componentProps: {
      maxlength: 50,
    },
    fieldName: 'postName',
    rules: 'required',
  },
  {
    component: 'InputNumber',
    label: '岗位排序',
    componentProps: {
      max: 9999,
    },
    fieldName: 'postSort',
    defaultValue: '0',
    rules: 'required',
  },
  {
    component: 'Select',
    label: '任务类型',
    componentProps: {
      placeholder: '请选择任务类型',
      options: getTaskTypeOptions(),
      multiple: true,
    },
    fieldName: 'taskType',
  },
  {
    component: 'Select',
    label: '岗位类型',
    componentProps: {
      placeholder: '请选择岗位类型',
      options: getPostTypeOptions(),
    },
    fieldName: 'type',
  },
  {
    component: 'RadioGroup',
    label: '状态',
    componentProps: {
      isButton: true,
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
    fieldName: 'status',
    defaultValue: '0',
  },
  {
    component: 'Input',
    label: '备注',
    componentProps: {
      type: 'textarea',
      maxlength: 500,
      showWordLimit: true,
    },
    fieldName: 'remark',
  },
];
