import { FormSchema } from '/@/components/Form';
import { findDictItemByCode } from '/@/api/system/dict';
import { h, ref } from 'vue';
import { isExistDataManagerNameApi } from '/@/api/datamanager/dataManagerApi';
import { getDeviceProfile } from '/@/api/alarm/position';
import { BasicColumn, BasicTableProps } from '/@/components/Table';
import { devicePage } from '/@/api/device/deviceManager';
import { Tag } from 'ant-design-vue';
import { DeviceRecord } from '/@/api/device/model/deviceModel';
import { FETCH_SETTING } from '/@/components/Table/src/const';

const typeValue = ref('');
export enum CredentialsEnum {
  IS_ANONYMOUS = 'anonymous',
  IS_BASIC = 'basic',
  IS_PEM = 'pem',
}
export const isBasic = (type: string) => {
  return type === CredentialsEnum.IS_BASIC;
};
export const isPem = (type: string) => {
  return type === CredentialsEnum.IS_PEM;
};

export enum DataSourceType {
  ALL = 'ALL',
  PRODUCT = 'PRODUCTS',
  DEVICE = 'DEVICES',
}

export enum BasicInfoFormField {
  DATA_SOURCE_TYPE = 'datasourceType',
  DATA_SOURCE_PRODUCT = 'datasourceProduct',
  DATA_SOURCE_DEVICE = 'datasourceDevice',
  CONVERT_CONFIG_ID = 'convertConfigId',
}

export enum DeviceStatusEnum {
  OFFLINE = 'OFFLINE',
  ONLINE = 'ONLINE',
  INACTIVE = 'INACTIVE',
}

export enum DeviceStatusNameEnum {
  OFFLINE = '离线',
  ONLINE = '在线',
  INACTIVE = '待激活',
}

export enum DeviceTypeEnum {
  SENSOR = 'SENSOR',
  DIRECT_CONNECTION = 'DIRECT_CONNECTION',
  GATEWAY = 'GATEWAY',
}

export enum DeviceTypeNameEnum {
  SENSOR = '网关子设备',
  DIRECT_CONNECTION = '直连设备',
  GATEWAY = '网关设备',
}

const handleGroupDevice = (options: DeviceRecord[]) => {
  const map = new Map<string, string[]>();
  options.forEach((item) => {
    if (map.has(item.profileId)) {
      const deviceList = map.get(item.profileId)!;
      deviceList.push(item.tbDeviceId);
    } else {
      map.set(item.profileId, [item.tbDeviceId]);
    }
  });
  const value = Array.from(map.entries()).map(([product, devices]) => ({ product, devices }));

  return value;
};

const deviceTableFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '设备名称',
    component: 'Input',
    colProps: { span: 9 },
    componentProps: {
      placeholder: '请输入设备名称',
    },
  },
  {
    field: 'deviceType',
    label: '设备类型',
    component: 'ApiSelect',
    colProps: { span: 9 },
    componentProps: {
      placeholder: '请选择设备类型',
      api: findDictItemByCode,
      params: {
        dictCode: 'device_type',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
    },
  },
];

const deviceTableColumn: BasicColumn[] = [
  {
    title: '状态',
    dataIndex: 'deviceState',
    customRender: ({ text }) => {
      return h(
        Tag,
        {
          color:
            text === DeviceStatusEnum.INACTIVE
              ? 'warning'
              : text === DeviceStatusEnum.OFFLINE
              ? 'error'
              : 'success',
        },
        () => DeviceStatusNameEnum[text]
      );
    },
  },
  {
    title: '设备名称/设备SN',
    dataIndex: 'name',
    customRender: ({ text, record }) => {
      return h('div', [
        h('div', text),
        h('div', { class: 'text-blue-400 truncate', title: record.sn }, record.sn),
      ]);
    },
  },
  {
    title: '设备类型',
    dataIndex: 'deviceType',
    customRender: ({ text }) => {
      return h(Tag, { color: 'success' }, () => DeviceTypeNameEnum[text]);
    },
  },
  {
    title: '所属产品',
    dataIndex: 'deviceProfile.name',
  },
  {
    title: '所属组织',
    dataIndex: 'organizationDTO.name',
  },
];

const TransferTableProps: BasicTableProps = {
  formConfig: {
    layout: 'inline',
    labelWidth: 80,
    schemas: deviceTableFormSchema,
    actionColOptions: { span: 6 },
  },
  size: 'small',
  maxHeight: 240,
  useSearchForm: true,
  columns: deviceTableColumn,
  showIndexColumn: false,
  fetchSetting: FETCH_SETTING,
} as BasicTableProps;

export const modeForm = (submitFn?: Function): FormSchema[] => {
  return [
    {
      field: BasicInfoFormField.CONVERT_CONFIG_ID,
      label: '',
      component: 'Input',
      show: false,
    },
    {
      field: BasicInfoFormField.DATA_SOURCE_TYPE,
      label: '数据源',
      component: 'RadioGroup',
      defaultValue: DataSourceType.ALL,
      componentProps: {
        options: [
          { label: '全部', value: DataSourceType.ALL },
          { label: '产品', value: DataSourceType.PRODUCT },
          { label: '设备', value: DataSourceType.DEVICE },
        ],
      },
    },
    {
      field: BasicInfoFormField.DATA_SOURCE_PRODUCT,
      label: '数据源产品',
      component: 'TransferModal',
      ifShow: ({ model }) => {
        return model[BasicInfoFormField.DATA_SOURCE_TYPE] !== DataSourceType.ALL;
      },
      valueField: 'value',
      changeEvent: 'update:value',
      componentProps: ({ formActionType }) => {
        const { setFieldsValue } = formActionType;
        return {
          api: getDeviceProfile,
          labelField: 'name',
          valueField: 'tbProfileId',
          transferProps: {
            listStyle: { height: '400px' },
            showSearch: true,
            filterOption: (inputValue: string, option: Recordable) => {
              const upperCaseInputValue = inputValue.toUpperCase();
              const upperCaseOptionValue = option.name.toUpperCase();
              return upperCaseOptionValue.includes(upperCaseInputValue);
            },
          },
          onChange: () => {
            setFieldsValue({ [BasicInfoFormField.DATA_SOURCE_DEVICE]: [] });
          },
        };
      },
    },
    {
      field: BasicInfoFormField.DATA_SOURCE_DEVICE,
      label: '数据源设备',
      component: 'TransferTableModal',
      ifShow: ({ model }) => {
        return model[BasicInfoFormField.DATA_SOURCE_TYPE] === DataSourceType.DEVICE;
      },
      valueField: 'value',
      changeEvent: 'update:value',
      componentProps: ({ formActionType }) => {
        const { getFieldsValue } = formActionType;
        const values = getFieldsValue();
        const convertConfigId = Reflect.get(values, BasicInfoFormField.CONVERT_CONFIG_ID);
        const devices = Reflect.get(values, BasicInfoFormField.DATA_SOURCE_DEVICE);

        return {
          labelField: 'name',
          valueField: 'tbDeviceId',
          primaryKey: 'tbDeviceId',
          pendingTableProps: {
            ...TransferTableProps,
            api: devicePage,
            beforeFetch: (params) => {
              const values = getFieldsValue();
              const deviceProfileIds = Reflect.get(values, BasicInfoFormField.DATA_SOURCE_PRODUCT);
              const convertConfigId = Reflect.get(values, BasicInfoFormField.CONVERT_CONFIG_ID);
              if (convertConfigId) {
                Object.assign(params, { convertConfigId, selected: false });
              }
              return { ...params, deviceProfileIds };
            },
          } as BasicTableProps,
          selectedTableProps: {
            ...TransferTableProps,
            // api
            api: !!(convertConfigId && devices) ? devicePage : undefined,
            beforeFetch: (params) => {
              const values = getFieldsValue();
              const deviceProfileIds = Reflect.get(values, BasicInfoFormField.DATA_SOURCE_PRODUCT);
              const convertConfigId = Reflect.get(values, BasicInfoFormField.CONVERT_CONFIG_ID);
              if (convertConfigId) {
                Object.assign(params, { convertConfigId, selected: true });
              }
              return { ...params, deviceProfileIds };
            },
          } as BasicTableProps,
          initSelectedOptions: async ({ setSelectedTotal }) => {
            const values = getFieldsValue();
            const convertConfigId = Reflect.get(values, BasicInfoFormField.CONVERT_CONFIG_ID);
            const deviceProfileIds = Reflect.get(values, BasicInfoFormField.DATA_SOURCE_PRODUCT);
            const devices = Reflect.get(values, BasicInfoFormField.DATA_SOURCE_DEVICE);
            if (convertConfigId && devices) {
              const { items, total } = await devicePage({
                page: 1,
                pageSize: 10,
                convertConfigId: values[BasicInfoFormField.CONVERT_CONFIG_ID],
                deviceProfileIds,
                selected: true,
              });
              setSelectedTotal(total);
              return items;
            }
            return [];
          },
          onSelectedAfter: async () => {
            submitFn && (await submitFn(false));
          },
          onRemoveAfter: async ({ reloadSelected }) => {
            submitFn && (await submitFn(false));
            reloadSelected();
          },
          transformValue: (_selectedRowKeys: string[], selectedRows: DeviceRecord[]) => {
            return handleGroupDevice(selectedRows);
          },
        };
      },
    },
    {
      field: 'type',
      label: '转换方式',
      component: 'ApiSelect',
      required: true,
      colProps: {
        span: 24,
      },
      componentProps({}) {
        return {
          api: findDictItemByCode,
          params: {
            dictCode: 'convert_data_to',
          },
          labelField: 'itemText',
          valueField: 'itemValue',
          onChange(value) {
            typeValue.value = value;
          },
        };
      },
    },
    {
      field: 'remark',
      label: '描述',
      colProps: { span: 24 },
      component: 'Input',
      componentProps: {
        maxLength: 255,
        placeholder: '请输入描述',
      },
    },
  ];
};

export const modeKafkaForm: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入名称',
    },
    dynamicRules: () => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject('请输入名称');
              } else {
                resolve();
              }
            });
          },
        },
      ];
    },
  },
  {
    field: 'topicPattern',
    label: '消息主题',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    defaultValue: 'my-topic',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入消息主题',
    },
  },
  {
    field: 'bootstrapServers',
    label: '服务器',
    colProps: { span: 12 },
    component: 'Input',
    defaultValue: 'localhost:9092',
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: 'localhost:9092',
    },
  },
  {
    field: 'retries',
    label: '重连次数',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: 'batchSize',
    label: '生产者并发',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 16384,
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: 'linger',
    label: '缓存时间',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: 'bufferMemory',
    label: '最大缓存',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 33554432,
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: 'acks',
    component: 'Select',
    label: '响应码',
    colProps: { span: 12 },
    defaultValue: '-1',
    componentProps: {
      placeholder: '请选择响应码',
      options: [
        { label: 'all', value: 'all' },
        { label: '-1', value: '-1' },
        { label: '0', value: '0' },
        { label: '1', value: '1' },
      ],
    },
  },
  {
    field: 'keySerializer',
    label: '键序列化',
    colProps: { span: 24 },
    required: true,
    component: 'Input',
    defaultValue: 'org.apache.kafka.common.serialization.StringSerializer',
    componentProps: {
      maxLength: 255,
      placeholder: 'org.apache.kafka.common.serialization.StringSerializer',
    },
  },
  {
    field: 'valueSerializer',
    label: '值序列化',
    colProps: { span: 24 },
    required: true,
    component: 'Input',
    defaultValue: 'org.apache.kafka.common.serialization.StringSerializer',
    componentProps: {
      maxLength: 255,
      placeholder: 'org.apache.kafka.common.serialization.StringSerializer',
    },
  },
  {
    field: 'otherProperties',
    label: '其他属性',
    colProps: { span: 24 },
    component: 'JAddInput',
    subLabel: '不可重复',
  },
  {
    field: 'addMetadataKeyValuesAsKafkaHeaders',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '将消息的元数据以键值对的方式添加到Kafka消息头中',
  },
  {
    field: 'kafkaHeadersCharset',
    component: 'Select',
    label: '字符集',
    required: true,
    colProps: { span: 12 },
    defaultValue: 'UTF-8',
    componentProps: {
      placeholder: '请选择字符集编码',
      options: [
        { label: 'US-ASCII', value: 'US' },
        { label: 'ISO-8859-1', value: 'ISO-8859-1' },
        { label: 'UTF-8', value: 'UTF-8' },
        { label: 'UTF-16BE', value: 'UTF-16BE' },
        { label: 'UTF-16LE', value: 'UTF-16LE' },
        { label: 'UTF-16', value: 'UTF-16' },
      ],
    },
    ifShow: ({ values }) => {
      return !!values.addMetadataKeyValuesAsKafkaHeaders;
    },
  },
  {
    field: 'description',
    label: '说明',
    colProps: { span: 24 },
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
      rows: 4,
      placeholder: '请输入说明',
    },
  },
];

export const modeMqttForm: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    colProps: { span: 12 },
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入名称',
    },
  },
  {
    field: 'topicPattern',
    label: '主题模式',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    defaultValue: 'my-topic',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Topic pattern',
    },
  },
  {
    field: 'host',
    label: '主机',
    colProps: { span: 12 },
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Host',
    },
  },
  {
    field: 'port',
    label: '端口',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 1883,
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Port',
    },
  },
  {
    field: 'connectTimeoutSec',
    label: '连接超时（秒）',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 10,
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Connection timeout (sec)',
    },
  },
  {
    field: 'clientId',
    label: '客户端ID',
    colProps: { span: 12 },
    component: 'Input',
    componentProps: ({ formActionType }) => {
      const { updateSchema } = formActionType;
      return {
        onChange(e) {
          if (e == null || e == undefined) {
            updateSchema({
              field: 'appendClientIdSuffix',
              show: false,
            });
          } else {
            updateSchema({
              field: 'appendClientIdSuffix',
              show: true,
            });
          }
        },
        maxLength: 8,
        placeholder: '请输入Client ID',
      };
    },
  },
  {
    field: 'appendClientIdSuffix',
    label: '',
    colProps: { span: 12 },
    defaultValue: false,
    component: 'Checkbox',
    renderComponentContent: '将服务ID作为后缀添加到客户端ID',
    show: false,
  },
  {
    field: 'cleanSession',
    label: '是否启用',
    colProps: { span: 12 },
    defaultValue: true,
    component: 'Checkbox',
    renderComponentContent: '清除会话',
  },
  {
    field: 'ssl',
    label: '是否启用',
    colProps: { span: 12 },
    defaultValue: false,
    component: 'Checkbox',
    renderComponentContent: '启用SSL',
  },
  {
    field: 'type',
    component: 'Select',
    label: '凭据类型',
    colProps: { span: 12 },
    defaultValue: 'anonymous',
    componentProps: {
      placeholder: '请选择Credentials',
      options: [
        { label: 'Anonymous', value: 'anonymous' },
        { label: 'Basic', value: 'basic' },
        { label: 'PEM', value: 'pem' },
      ],
    },
  },
  {
    field: 'username',
    label: '用户名',
    colProps: { span: 12 },
    component: 'Input',
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入用户名',
    },
    ifShow: ({ values }) => isBasic(Reflect.get(values, 'type')),
  },
  {
    field: 'password',
    label: '密码',
    colProps: { span: 12 },
    component: 'InputPassword',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入密码',
    },
    ifShow: ({ values }) => isBasic(Reflect.get(values, 'type')),
  },
  {
    field: '4',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'uploadAdd1',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '11',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'showImg1',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '5',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'uploadAdd2',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '1111',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'showImg2',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '6',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'uploadAdd3',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '111111',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'showImg3',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: 'password',
    label: '密码',
    colProps: { span: 12 },
    component: 'InputPassword',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入密码',
    },
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: 'description',
    label: '说明',
    colProps: { span: 24 },
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
      rows: 4,
      placeholder: '请输入说明',
    },
  },
];

export const modeRabbitMqForm: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入名称',
    },
    dynamicRules: () => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject('请输入名称');
              } else {
                resolve();
              }
            });
          },
        },
      ];
    },
  },
  {
    field: 'exchangeNamePattern',
    label: '交换名称模式',
    colProps: { span: 12 },
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入模式',
    },
  },
  {
    field: 'routingKeyPattern',
    label: '路由密钥模式',
    colProps: { span: 12 },
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入模式',
    },
  },
  {
    field: 'messageProperties',
    component: 'Select',
    label: '消息属性',
    colProps: { span: 12 },
    componentProps: {
      placeholder: '请选择消息属性',
      options: [
        { label: 'BASIC', value: 'BASIC' },
        { label: 'TEXT_PLAIN', value: 'TEXT_PLAIN' },
        { label: 'MINIMAL_BASIC', value: 'MINIMAL_BASIC' },
        { label: 'MINIMAL_PERSISTENT_BASIC', value: 'MINIMAL_PERSISTENT_BASIC' },
        { label: 'PERSISTENT_BASIC', value: 'PERSISTENT_BASIC' },
        { label: 'PERSISTENT_TEXT_PLAIN', value: 'PERSISTENT_TEXT_PLAIN' },
      ],
    },
  },
  {
    field: 'host',
    label: '主机',
    colProps: { span: 12 },
    component: 'Input',
    required: true,
    defaultValue: 'localhost',
    componentProps: {
      maxLength: 255,
      placeholder: 'localhost',
    },
  },
  {
    field: 'port',
    label: '端口',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 5672,
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Port',
    },
  },
  {
    field: 'virtualHost',
    label: '虚拟端口（以/开头）',
    colProps: { span: 12 },
    component: 'Input',
    defaultValue: '/',
    componentProps: {
      maxLength: 255,
      placeholder: '/',
    },
  },
  {
    field: 'username',
    label: '用户名',
    colProps: { span: 12 },
    component: 'Input',
    defaultValue: 'guest',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入用户名',
    },
  },
  {
    field: 'password',
    label: '密码',
    colProps: { span: 12 },
    component: 'InputPassword',
    defaultValue: 'guest',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入密码',
    },
  },
  {
    field: 'automaticRecoveryEnabled',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '自动恢复',
  },
  {
    field: 'connectionTimeout',
    label: '连接超时（毫秒）',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 60000,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Connection timeout (ms)',
    },
  },
  {
    field: 'handshakeTimeout',
    label: '握手超时（毫秒）',
    colProps: { span: 12 },
    component: 'InputNumber',
    defaultValue: 10000,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Handshake timeout (ms)',
    },
  },
  {
    field: 'clientProperties',
    label: '客户端属性',
    colProps: { span: 24 },
    component: 'JAddInput',
    subLabel: '不可重复',
  },
  {
    field: 'description',
    label: '说明',
    colProps: { span: 24 },
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
      rows: 4,
      placeholder: '请输入说明',
    },
  },
];

export const modeApiForm: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入名称',
    },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject('请输入名称');
              } else {
                if (values.name) {
                  isExistDataManagerNameApi({
                    name: value,
                    type:
                      typeValue.value == ''
                        ? 'org.thingsboard.rule.engine.rest.TbRestApiCallNode'
                        : typeValue.value,
                  }).then((data) => {
                    if (data == true) {
                      // createMessage.error('名称已存在');
                      resolve();
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
  },
  {
    field: 'restEndpointUrlPattern',
    label: '端点URL模式',
    colProps: { span: 12 },
    required: true,
    defaultValue: 'http://localhost/api',
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入Endpoint URL pattern',
    },
  },
  {
    field: 'requestMethod',
    component: 'Select',
    label: '请求方式',
    colProps: { span: 12 },
    defaultValue: 'POST',
    componentProps: {
      placeholder: '请选择Request method',
      options: [
        { label: 'GET', value: 'GET' },
        { label: 'POST', value: 'POST' },
        { label: 'PUT', value: 'PUT' },
        { label: 'DELETE', value: 'DELETE' },
      ],
    },
  },
  {
    field: 'enableProxy',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '启用代理',
  },

  {
    field: 'proxyHost',
    label: '代理主机',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: 'http或者https开头',
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },
  {
    field: 'proxyPort',
    label: '代理端口',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      maxLength: 255,
      placeholder: 'http或者https开头',
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },
  {
    field: 'proxyUser',
    label: '代理用户',
    colProps: { span: 12 },
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入代理用户',
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },
  {
    field: 'proxyPassword',
    label: '代理密码',
    colProps: { span: 12 },
    required: true,
    component: 'InputPassword',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入代理密码',
    },
    ifShow: ({ values }) => {
      return !!values.enableProxy;
    },
  },

  {
    field: 'useSystemProxyProperties',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '使用系统代理属性',
  },
  {
    field: 'maxParallelRequestsCount',
    label: '最大并行请求数',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !!values.useSystemProxyProperties;
    },
  },
  {
    field: 'ignoreRequestBody',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '无请求正文',
  },
  {
    field: 'readTimeoutMs',
    label: '读取超时（毫秒）',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !values.useSystemProxyProperties;
    },
  },
  {
    field: 'maxParallelRequestsCount',
    label: '最大并行请求数',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !values.useSystemProxyProperties;
    },
  },
  {
    field: 'headers',
    label: 'Headers',
    colProps: { span: 24 },
    defaultValue: { 'Content-Type': 'application/json' },
    component: 'JAddInput',
    subLabel: '不可重复',
  },

  {
    field: 'useRedisQueueForMsgPersistence',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '使用redis队列进行消息持久性',
  },
  {
    field: 'trimQueue',
    label: '是否启用',
    colProps: { span: 12 },
    component: 'Checkbox',
    renderComponentContent: '修剪redis队列',
    ifShow: ({ values }) => {
      return !!values.useRedisQueueForMsgPersistence;
    },
  },
  {
    field: 'maxQueueSize',
    label: 'Redis队列最大数',
    colProps: { span: 12 },
    required: true,
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      maxLength: 255,
    },
    ifShow: ({ values }) => {
      return !!values.useRedisQueueForMsgPersistence;
    },
  },

  {
    field: 'type',
    component: 'Select',
    label: '凭据类型',
    colProps: { span: 12 },
    defaultValue: 'anonymous',
    componentProps: {
      placeholder: '请选择凭据类型',
      options: [
        { label: 'Anonymous', value: 'anonymous' },
        { label: 'Basic', value: 'basic' },
        { label: 'PEM', value: 'pem' },
      ],
    },
  },
  {
    field: 'username',
    label: '用户名',
    colProps: { span: 12 },
    component: 'Input',
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入用户名',
    },
    ifShow: ({ values }) => isBasic(Reflect.get(values, 'type')),
  },
  {
    field: 'password',
    label: '密码',
    colProps: { span: 12 },
    component: 'InputPassword',
    required: true,
    componentProps: {
      maxLength: 255,
      placeholder: '请输入密码',
    },
    ifShow: ({ values }) => isBasic(Reflect.get(values, 'type')),
  },
  {
    field: '1',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'uploadAdd1',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '11',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'showImg1',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '1',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'uploadAdd2',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '1111',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'showImg2',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '1',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'uploadAdd3',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: '111111',
    label: '',
    colProps: { span: 24 },
    component: 'Input',
    slot: 'showImg3',
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },
  {
    field: 'password',
    label: '密码',
    colProps: { span: 12 },
    component: 'InputPassword',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入密码',
    },
    ifShow: ({ values }) => isPem(Reflect.get(values, 'type')),
  },

  {
    field: 'description',
    label: '说明',
    colProps: { span: 24 },
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
      rows: 4,
      placeholder: '请输入说明',
    },
  },
];
