import { BasicColumn, FormSchema } from '@jdlinker/ui';
import { configManageItemCheck } from './config.api';
import { rules } from '@jdlinker/func';

export const columns: BasicColumn[] = [
  {
    title: '配置名称',
    dataIndex: 'configName',
    width: 240
  },
  {
    title: '配置编号',
    dataIndex: 'configCode',
    width: 240
  },
  {
    title: '描述',
    dataIndex: 'description'
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '配置名称',
    field: 'configName',
    component: 'Input'
  },
  {
    label: '配置编码',
    field: 'configCode',
    component: 'Input'
  }
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
  {
    label: '配置名称',
    field: 'configName',
    required: true,
    component: 'Input'
  },
  {
    label: '配置编码',
    field: 'configCode',
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      return !!values.id;
    },
    dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_config_manage', 'config_code', model, schema, true)
  },
  {
    label: '描述',
    field: 'description',
    component: 'Input'
  }
];

export const configItemColumns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'itemText',
    width: 80
  },
  {
    title: '数据值',
    dataIndex: 'itemValue',
    width: 80
  },
  {
    title: '对应值',
    dataIndex: 'itemCode',
    width: 80
  }
];

export const configItemSearchFormSchema: FormSchema[] = [
  {
    label: '名称',
    field: 'itemText',
    component: 'Input'
  },
  {
    label: '状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'dict_item_status',
      stringToNumber: true
    }
  }
];

export const itemFormSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
  {
    label: '名称',
    field: 'itemText',
    required: true,
    component: 'Input'
  },
  {
    label: '数据值',
    field: 'itemValue',
    component: 'Input',
    dynamicRules: ({ values, model }) => {
      return [
        {
          required: true,
          validator: (_, value) => {
            if (!value) {
              return Promise.reject('请输入数据值');
            }
            if (new RegExp("[`~!@#$^&*()=|{}'.<>《》/?！￥（）—【】‘；：”“。，、？]").test(value)) {
              return Promise.reject('数据值不能包含特殊字符！');
            }
            return new Promise<void>((resolve, reject) => {
              const params = {
                configId: values.configId,
                id: model.id,
                itemValue: value
              };
              configManageItemCheck(params)
                .then((res) => {
                  res.success ? resolve() : reject(res.message || '校验失败');
                })
                .catch((err) => {
                  reject(err.message || '验证失败');
                });
            });
          }
        }
      ];
    }
  },
  {
    label: '对应值',
    field: 'itemCode',
    component: 'InputNumber',
    required: true
  },
  {
    label: '描述',
    field: 'description',
    component: 'Input'
  },
  {
    field: 'sortOrder',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 1
  },
  {
    field: 'status',
    label: '是否启用',
    defaultValue: 1,
    component: 'JDictSelectTag',
    componentProps: {
      type: 'radioButton',
      dictCode: 'dict_item_status',
      stringToNumber: true
    }
  }
];
