index.vue 9.1 KB
<template>
  <div class="key-style">
    <BasicModal
      v-bind="$attrs"
      width="55rem"
      @register="register"
      :title="getTitle"
      @ok="handleSubmit"
      @cancel="handleCancel"
    >
      <CollapseContainer title="键名筛选器" class="border mb-8">
        <div class="table-style">
          <BasicTable
            :showIndexColumn="false"
            :dataSource="getTableApiData.value"
            @register="registerTable"
          >
            <template #toolbar>
              <a-button type="primary" @click="handleAddKey">新增键名筛选器</a-button>
            </template>
            <template #action="{ record }">
              <TableAction
                :actions="[
                  {
                    label: '编辑',
                    icon: 'clarity:note-edit-line',
                    onClick: handleEdit.bind(null, record),
                  },
                  {
                    label: '删除',
                    icon: 'ant-design:delete-outlined',
                    color: 'error',
                    popConfirm: {
                      title: '是否确认删除',
                      confirm: handleDelete.bind(null, record),
                    },
                  },
                ]"
              />
            </template>
          </BasicTable>
        </div>
      </CollapseContainer>
      <!--      <CollapseContainer title="筛选器预览" class="border mb-8">-->
      <!--        &lt;!&ndash; <p v-for="(item, index) in detailData" :key="index">{{ item }}</p> &ndash;&gt;-->
      <!--        <Description-->
      <!--          v-for="(item, index) in detailData"-->
      <!--          :key="index"-->
      <!--          :column="3"-->
      <!--          :data="item"-->
      <!--          :schema="schema"-->
      <!--        />-->
      <!--      </CollapseContainer>-->
      <BasicForm :showResetButton="false" :showSubmitButton="false" @register="registerForm" />
    </BasicModal>
    <KeyValueModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, reactive, nextTick } from 'vue';
  import { BasicModal } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { formSchema, keyColumns, DescDetailSchema } from './config';
  import { CollapseContainer } from '/@/components/Container/index';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import KeyValueModal from './cpns/index.vue';
  import { Description, DescItem } from '/@/components/Description/index';
  const schema: DescItem[] = [
    {
      field: 'key1',
      label: '键名',
    },
    {
      field: 'operation',
      label: '操作',
      render: (_, data) => {
        const findDay = [
          { label: '等于', value: 'EQUAL' },
          { label: '不等于', value: 'NOT_EQUAL' },
          { label: '开始于', value: 'STARTS_WITH' },
          { label: '结束于', value: 'ENDS_WITH' },
          { label: '包含', value: 'CONTAINS' },
          { label: '不包含', value: 'NOT_CONTAINS' },
          { label: '等于', value: 'EQUAL' },
          { label: '不等于', value: 'NOT_EQUAL' },
          { label: '大于', value: 'GREATER' },
          { label: '小于', value: 'LESS' },
          { label: '大于或等于', value: 'GREATER_OR_EQUAL' },
          { label: '小于或等于', value: 'LESS_OR_EQUAL' },
        ];
        const findRuleByValue = findDay.find((f) => {
          if (f.value == data.operation) {
            return f.label;
          }
        });
        return findRuleByValue?.label;
      },
    },
    {
      field: 'value1',
      label: '值',
    },
  ];

  export default defineComponent({
    name: 'DetailTemplate',
    components: {
      KeyValueModal,
      BasicModal,
      BasicForm,
      CollapseContainer,
      BasicTable,
      TableAction,
      Description,
    },
    props: ['getFatherData'],
    emits: ['success', 'register', 'getAllFieldsRule', 'getLastAllFieldsRule'],
    setup(props, { emit }) {
      let getKeyAndValueChildData = ref<[]>([]);
      const getTableApiData: any = ref([]);
      const detailData: any = ref([]);
      const currentIndex = ref(0);
      const lastValues: any = ref(null);
      const isUpdate = ref(true);
      const watchCurrentAlarmConditionValue = reactive({
        key: 0,
        data: {},
      });

      const [registerForm, { getFieldsValue, resetFields }] = useForm({
        labelWidth: 120,
        schemas: formSchema,
      });
      const [registerModal, { openModal }] = useModal();
      const getTitle = computed(() => (!unref(isUpdate) ? '添加报警规则条件' : '编辑报警规则条件'));
      const [register, { closeModal, openModal: openCondition }] = useModal();
      const [registerTable, { reload, setTableData: setFunc }] = useTable({
        title: '键名筛选器',
        columns: keyColumns,
        bordered: true,
        showIndexColumn: false,
        pagination: false,
        autoCreateKey: true,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });
      const getFieldsValueFunc = () => {
        return watchCurrentAlarmConditionValue.data;
      };
      const handleSubmit = () => {
        lastValues.value = getFieldsValue();
        watchCurrentAlarmConditionValue.key = currentIndex.value;
        let allTableDatas = [];
        getTableApiData.value.forEach((tableObj) => {
          let key = { type: tableObj.type, key: tableObj.key1 };
          let predicate = {
            operation: tableObj.operation,
            type: tableObj.type1,
            value: {
              defaultValue: tableObj.value1,
            },
          };
          allTableDatas.push({
            key: key,
            predicate: predicate,
            value: tableObj.value1,
            valueType: tableObj.type1,
          });
        });
        watchCurrentAlarmConditionValue.data = allTableDatas;
        emit('getAllFieldsRule', watchCurrentAlarmConditionValue, lastValues.value);
        closeModal();
      };
      const resetDataFunc = (j, currentAlarmCondition, update) => {
        isUpdate.value = update;
        openCondition(true);
        currentIndex.value = j;
        resetFields();
        getTableApiData.value = [];
        getTableApiData.value.length = 0;
        detailData.value = [];
        detailData.value.length = 0;
        console.log('father', props.getFatherData);
        let index = 0;
        if (currentAlarmCondition.data.length != undefined) {
          for (let i in currentAlarmCondition.data) {
            let obj = {
              id: index,
              key1: currentAlarmCondition.data[i].key.key,
              type: currentAlarmCondition.data[i].key.type,
              operation: currentAlarmCondition.data[i].predicate.operation,
              value1: currentAlarmCondition.data[i].value,
              type1: currentAlarmCondition.data[i].valueType,
            };
            getTableApiData.value.push(obj);
            index++;
          }
        }
        nextTick(() => {
          setFunc(getTableApiData.value);
        });
      };

      const handleCancel = () => {};
      const handleAddKey = () => {
        setTimeout(() => {
          openModal(true, {
            isUpdate: false,
          });
        }, 10);
      };
      const handleEdit = (record: Recordable) => {
        openModal(true, {
          isUpdate: true,
          record,
        });
        reload();
      };
      const handleDelete = (record: Recordable) => {
        let newGetTableApiData = [];
        getTableApiData.value.forEach((currentTable) => {
          if (currentTable.id != record.id) {
            newGetTableApiData.push(currentTable as never);
          }
        });
        getTableApiData.value = newGetTableApiData;
        setFunc(getTableApiData.value);
        reload();
      };
      const handleSuccess = (v, v1, isUpdate) => {
        if (isUpdate) {
          getTableApiData.value.forEach((tableValue) => {
            if (tableValue.id == v1.id) {
              tableValue.key1 = v1.key1;
              tableValue.operation = v1.operation;
              tableValue.type = v1.type;
              tableValue.type1 = v1.type1;
              tableValue.value1 = v1.value1;
            }
          });
        } else {
          getTableApiData.value.push(v1);
        }
        getKeyAndValueChildData.value = v;
        setFunc(getTableApiData.value);
        reload();
      };
      return {
        handleCancel,
        schema,
        detailData,
        getTableApiData,
        resetDataFunc,
        DescDetailSchema,
        handleSuccess,
        handleEdit,
        handleDelete,
        registerModal,
        registerTable,
        handleAddKey,
        registerForm,
        handleSubmit,
        register,
        getTitle,
        currentIndex,
        getFieldsValueFunc,
      };
    },
  });
</script>
<style lang="less" scoped>
  :deep .ant-table-body {
    overflow-y: auto !important;
    min-height: 90px !important;
    height: 0px !important;
  }
</style>