import { BasicColumn, FormSchema } from '/@/components/Table';
import { listDictModel } from '/@/api/common';
import { validateStr } from '/@/utils/validate';

export const columns: BasicColumn[] = [
  {
    title: '字典名称',
    dataIndex: 'dictName',
  },
  {
    title: '字典编码',
    dataIndex: 'dictCode',
  },
  {
    title: '字典类型',
    dataIndex: 'dictType',
  },
  {
    title: '状态',
    dataIndex: 'disable',
    width: 80,
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'dictName',
    label: '字典名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入字典名称',
    },
  },
  {
    field: 'dictCode',
    label: '字典编码',
    component: 'Input',
    componentProps: {
      placeholder: '请输入字典编码',
    },
  },
  {
    field: 'dictType',
    label: '字典类型',
    component: 'ApiSelect',
    componentProps: {
      api: listDictModel,
      params: {
        dictCode: 'sys_dict_type',
      },
    },
    defaultValue: '0',
    ifShow: false,
  },
];

export const dataFormSchema: FormSchema[] = [
  {
    field: 'dictName',
    label: '字典名称',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入字典名称',
    },
  },
  {
    field: 'dictCode',
    label: '字典编码',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入字典编码',
    },
    dynamicRules: () => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            if (!value) {
              return Promise.reject('字典项编码不能为空');
            }
            if (validateStr(value)) {
              return Promise.reject('字典项编码为字母或数字组成');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    field: 'dictType',
    label: '字典类型',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: listDictModel,
      params: {
        dictCode: 'sys_dict_type',
      },
    },
  },
  {
    field: 'disable',
    label: '状态',
    component: 'ApiRadioGroup',
    required: true,
    componentProps: {
      api: listDictModel,
      params: {
        dictCode: 'sys_disable_type',
      },
    },
    defaultValue: '0',
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
    },
  },
];
