config.ts 2.8 KB
import { doQueryRuleChainList } from '/@/api/device/deviceConfigApi';
import { FormSchema, useComponentRegister } from '/@/components/Form';
import { OrgTreeSelect } from '/@/views/common/OrgTreeSelect';
import { useI18n } from '/@/hooks/web/useI18n';

const { t } = useI18n(); // 加载国际化
useComponentRegister('OrgTreeSelect', OrgTreeSelect);

export enum FormFieldsEnum {
  NAME = 'name',
  ORGANIZATION_ID = 'organizationId',
  DESCRIPTION = 'description',
  RULE_CHAIN_ID = 'ruleChainId',
}

type GetFormSchemasParams = {
  handleOnOrganizationChange: Fn;
  handleOnOrganizationOptionsChange: Fn;
  handleOnRuleChainChange: Fn;
  handleOnRuleChainOptionsChange: Fn;
};

export const getFormSchemas = ({
  handleOnOrganizationChange,
  handleOnOrganizationOptionsChange,
  handleOnRuleChainChange,
  handleOnRuleChainOptionsChange,
}: GetFormSchemasParams): FormSchema[] => {
  return [
    {
      field: FormFieldsEnum.NAME,
      component: 'Input',
      label: t('rule.linkedge.index.name'),
      required: true,
      componentProps: {
        placeholder: `${t('rule.linkedge.index.pleaseEnter')}${t('rule.linkedge.index.name')}`,
        maxLength: 36,
      },
    },
    {
      field: FormFieldsEnum.ORGANIZATION_ID,
      component: 'OrgTreeSelect',
      label: t('rule.linkedge.index.affiliatedOrganization'),
      required: true,
      componentProps: {
        placeholder: `${t('rule.linkedge.index.pleaseEnter')}${t(
          'rule.linkedge.index.affiliatedOrganization'
        )}`,
        onChange: handleOnOrganizationChange,
        onOptionsChange: handleOnOrganizationOptionsChange,
      },
    },
    {
      field: FormFieldsEnum.RULE_CHAIN_ID,
      label: t('deviceManagement.product.ruleChainText'),
      component: 'ApiSelect',
      required: true,
      componentProps: {
        api: async () => {
          const result = await doQueryRuleChainList();
          return result.map((item) => ({ ...item, label: item.name, value: item.id.id }));
        },
        placeholder: `${t('common.chooseText')}${t('deviceManagement.product.ruleChainText')}`,
        onChange: handleOnRuleChainChange,
        onOptionsChange: handleOnRuleChainOptionsChange,
      },
    },
    {
      field: FormFieldsEnum.DESCRIPTION,
      component: 'InputTextArea',
      label: t('rule.linkedge.index.description'),
      componentProps: {
        placeholder: `${t('rule.linkedge.index.pleaseEnter')}${t(
          'rule.linkedge.index.description'
        )}`,
      },
    },
    {
      field: 'flipFlop',
      component: 'Input',
      label: '',
      slot: 'flipFlop',
    },
    {
      field: 'executionCondition',
      component: 'Input',
      label: '',
      slot: 'executionCondition',
    },
    {
      field: 'executionAction',
      component: 'Input',
      label: '',
      slot: 'executionAction',
    },
  ];
};