import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { findDictItemByCode } from '/@/api/system/dict';
import { findMessageConfig } from '/@/api/message/config';
import { isMessage } from '/@/views/message/config/config.data';

export const columns: BasicColumn[] = [
  {
    title: '模板名称',
    dataIndex: 'templateName',
    width: 150,
  },
  {
    title: '配置名称',
    dataIndex: 'messageConfig.configName',
    width: 120,
    slots: { customRender: 'config' },
  },
  {
    title: '配置类型',
    dataIndex: 'messageTypeDictText',
    width: 100,
  },
  {
    title: '模板编码',
    dataIndex: 'templateCode',
    width: 180,
  },
  {
    title: '签名信息',
    dataIndex: 'signName',
    width: 180,
  },
  {
    title: '模板用途',
    dataIndex: 'templatePurposeDictText',
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    slots: { customRender: 'status' },
  },
  {
    title: '模板用途',
    dataIndex: 'templatePurpose',
    width: 180,
    ifShow: false,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'templateName',
    label: '模板名称',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 32,
      placeholder: '请输入模板名称',
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 32) {
              return Promise.reject('字数不超过32个字');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    field: 'templateCode',
    label: '模板编码',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 20,
      placeholder: '请输入模板编码',
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 20) {
              return Promise.reject('字数不超过20个字');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: '主键',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
      placeholder: '请输入主键',
    },
  },
  {
    field: 'messageType',
    label: '消息类型',
    required: true,
    component: 'ApiSelect',
    componentProps({ formActionType }) {
      return {
        api: findDictItemByCode,
        params: {
          dictCode: 'message_type',
        },
        labelField: 'itemText',
        valueField: 'itemValue',
        onChange: async (value) => {
          const res = await findMessageConfig({});
          let typeId: Nullable<string> = null;
          const options = res.map((item) => {
            if (item.messageType === value) typeId = item.id;
            return {
              disabled: item.messageType !== value,
              label: item.configName,
              value: item.id,
            };
          });
          await formActionType.setFieldsValue({ messageConfigId: typeId });
          await formActionType.updateSchema({
            field: 'messageConfigId',
            componentProps: {
              options,
            },
          });
        },
      };
    },
  },
  {
    field: 'messageConfigId',
    label: '配置名称',
    required: true,
    component: 'ApiSelect',
    componentProps: {
      api: findMessageConfig,
      params: {
        messageType: ({ values }) => Reflect.get(values, 'messageType'),
      },
      immediate: true,
      labelField: 'configName',
      valueField: 'id',
    },
  },
  {
    field: 'templateName',
    label: '模板名称',
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 32,
      placeholder: '请输入模板名称',
    },
  },
  {
    field: 'templateCode',
    label: '模板编号',
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 20,
      placeholder: '请输入模板编号',
    },

    ifShow: ({ values }) => isMessage(Reflect.get(values, 'messageType')),
  },
  {
    field: 'signName',
    label: '签名',
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 32,
      placeholder: '请输入签名',
    },

    ifShow: ({ values }) => isMessage(Reflect.get(values, 'messageType')),
  },
  {
    field: 'templatePurpose',
    label: '模板用途',
    required: true,
    component: 'ApiSelect',
    helpMessage: [],
    componentProps({ formActionType }) {
      const { updateSchema } = formActionType;
      return {
        onChange(value) {
          if (value === 'FOR_LOGIN') {
            updateSchema({
              field: 'templatePurpose',
              helpMessage: `登录模板平台提供如下参数:
              {
                "code":"验证码"
              }`,
            });
          } else if (value === 'FOR_FORGET_PASSWORD') {
            updateSchema({
              field: 'templatePurpose',
              helpMessage: `忘记密码模板平台提供如下参数:
              {
                "code":"验证码"
              }`,
            });
          } else if (value === 'FOR_ALARM_NOTICE') {
            updateSchema({
              field: 'templatePurpose',
              helpMessage: `告警通知模板平台提供如下参数:
              {
                "type":"告警类型",
                "device_name":"设备名称",
                "severity":"告警等级",
                "organization":"设备所属组织",
                "createTime":告警时间
              }`,
            });
          } else if (value === 'FOR_SET_PASSWORD') {
            updateSchema({
              field: 'templatePurpose',
              helpMessage: `清除密码模板平台提供如下参数:
              {
                "code":"验证码"
              }`,
            });
          }
        },
        api: findDictItemByCode,
        params: {
          dictCode: 'template_purpose',
        },
        labelField: 'itemText',
        valueField: 'itemValue',
      };
    },
  },
  {
    field: 'tenantId',
    label: '租户ID',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
      placeholder: '请输入租户ID',
    },
  },
];
