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

export const columns: BasicColumn[] = [
  {
    title: '字典项名称',
    dataIndex: 'label',
  },
  {
    title: '字典项编码',
    dataIndex: 'value',
    width: 120,
  },
  {
    title: '颜色',
    dataIndex: 'color',
    width: 120,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 80,
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'dictItemName',
    label: '字典项名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入字典项名称',
    },
  },
  {
    field: 'dictItemCode',
    label: '字典项编码',
    component: 'Input',
    componentProps: {
      placeholder: '请输入字典项编码',
    },
  },
];

export const dataFormSchema: FormSchema[] = [
  {
    field: 'label',
    label: '字典项名称',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入字典项名称',
    },
  },
  {
    field: 'value',
    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: 'color',
    label: '颜色',
    component: 'FormColorPicker',
    componentProps: ({ formModel }) => {
      return {
        onChange: e => {
          formModel.color = e;
        },
      };
    },
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    required: true,
    defaultValue: '1',
    componentProps: {
      placeholder: '请输入排序',
      min: 1,
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
    },
  },
];
