DeviceProfileModal.vue 6.56 KB
<template>
  <BasicModal
    v-bind="$attrs"
    width="55rem"
    @register="register"
    :title="getTitle"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <div class="step-form-form">
      <a-steps :current="current">
        <a-step title="设备配置" />
        <a-step title="传输配置" />
        <a-step title="告警配置" />
        <a-step title="告警通知" />
      </a-steps>
    </div>
    <div class="mt-5">
      <DeviceProfileStep1
        ref="DeviceProfileStep1Ref"
        @next="handleStepNext1"
        v-show="current === 0"
      />
      <DeviceProfileStep2
        ref="DeviceProfileStep2Ref"
        @prev="handleStepPrev"
        @next="handleStep2Next"
        v-show="current === 1"
      />
      <DeviceProfileStep3
        ref="DeviceProfileStep3Ref"
        @prev="handleStepPrev"
        @next="handleStep3Next"
        @redo="handleRedo"
        v-if="current === 2"
      />
      <DeviceProfileStep4
        ref="DeviceProfileStep4Ref"
        @prev="handleStepPrev"
        v-show="current === 3"
      />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, getCurrentInstance } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import DeviceProfileStep1 from '/@/views/device/profile/step/DeviceProfileStep1.vue';
  import DeviceProfileStep2 from '/@/views/device/profile/step/DeviceProfileStep2.vue';
  import DeviceProfileStep3 from '/@/views/device/profile/step/DeviceProfileStep3.vue';
  import DeviceProfileStep4 from '/@/views/device/profile/step/DeviceProfileStep4.vue';
  import { Steps } from 'ant-design-vue';
  import { deviceConfigAddOrEdit } from '/@/api/device/deviceConfigApi';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'DeviceModal',
    components: {
      BasicModal,
      DeviceProfileStep1,
      DeviceProfileStep2,
      DeviceProfileStep3,
      DeviceProfileStep4,
      [Steps.name]: Steps,
      [Steps.Step.name]: Steps.Step,
    },
    props: {
      userData: { type: Object },
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const DeviceProfileStep1Ref = ref(null);
      const DeviceProfileStep2Ref = ref(null);
      const DeviceProfileStep3Ref = ref(null);
      const DeviceProfileStep4Ref = ref(null);
      const { createMessage } = useMessage();
      // const getStepData = ref(null);
      const { proxy } = getCurrentInstance();
      const postDeviceConfogData: any = ref({});
      const getStepOneData: any = ref({});
      const getStepTwoData: any = ref({});
      const getStepThreeData: any = ref({});
      const editEchoData: any = ref(null);
      const alarmProfileData: any = ref({});
      const isGetStepThreeData: any = ref({});
      const postEditId = ref('');
      const current = ref(0);
      const isUpdate = ref(true);
      const getTitle = computed(() => (!unref(isUpdate) ? '新增设备配置' : '编辑设备配置'));
      const [register, { closeModal }] = useModalInner((data) => {
        isUpdate.value = !!data?.isUpdate;
        if (!unref(isUpdate)) {
          current.value = 0;
          switch (current.value) {
            case 0:
              proxy.$refs.DeviceProfileStep1Ref.customResetFunc();
              break;
            case 1:
              proxy.$refs.DeviceProfileStep2Ref.customResetAndFunc();
              break;
            case 2:
            // proxy.$refs.DeviceProfileStep3Ref.customResetFunc();
            // break;
            case 3:
              proxy.$refs.DeviceProfileStep4Ref.customResetAndFunc();
              break;
          }
        }
        if (unref(isUpdate)) {
          current.value = 0;
          postEditId.value = data.record.id;
          editEchoData.value = data.record;
          switch (current.value) {
            case 0:
              proxy.$refs.DeviceProfileStep1Ref.resetFieldsFunc(editEchoData.value);
              break;
            case 1:
              proxy.$refs.DeviceProfileStep2Ref.resetFieldsFunc(editEchoData.value);
              break;
            case 2:
            // proxy.$refs.DeviceProfileStep3Ref.resetFieldsFunc(editEchoData.value);
            // break;
            case 3:
              proxy.$refs.DeviceProfileStep4Ref.resetFieldsFunc(editEchoData.value);
              break;
          }
        }
      });
      function handleStepPrev() {
        current.value--;
      }
      function handleStepNext1(v) {
        current.value++;
        getStepOneData.value = v;
      }
      function handleStep2Next(v) {
        current.value++;
        getStepTwoData.value = v;
      }
      function handleStep3Next(v) {
        current.value++;
        getStepThreeData.value = v;
      }
      function handleRedo() {
        current.value = 0;
      }
      const handleSubmit = async () => {
        if (!unref(isUpdate)) {
          isGetStepThreeData.value.profileData = getStepThreeData.value;
          alarmProfileData.value.alarmProfile =
            await proxy.$refs.DeviceProfileStep4Ref.getAllFields();
          Object.assign(
            postDeviceConfogData.value,
            getStepOneData.value,
            getStepTwoData.value,
            isGetStepThreeData.value,
            alarmProfileData.value
          );
          await deviceConfigAddOrEdit(postDeviceConfogData.value);
          createMessage.success('新增设备配置成功');
          closeModal();
          emit('success');
        }
        if (unref(isUpdate)) {
          postDeviceConfogData.value.id = postEditId.value;
          isGetStepThreeData.value.alarmProfile = getStepThreeData.value;
          alarmProfileData.value.alarmProfile =
            await proxy.$refs.DeviceProfileStep4Ref.getAllFields();
          Object.assign(
            postDeviceConfogData.value,
            getStepOneData.value,
            getStepTwoData.value,
            alarmProfileData.value,
            isGetStepThreeData.value
          );
          await deviceConfigAddOrEdit(postDeviceConfogData.value);
          createMessage.success('编辑设备配置成功');
          closeModal();
          emit('success');
        }
      };
      const handleCancel = () => {
        return;
      };
      return {
        DeviceProfileStep2Ref,
        DeviceProfileStep3Ref,
        DeviceProfileStep4Ref,
        DeviceProfileStep1Ref,
        editEchoData,
        // getStepData,
        handleStep3Next,
        handleSubmit,
        handleCancel,
        register,
        getTitle,
        current,
        handleStepPrev,
        handleStepNext1,
        handleStep2Next,
        handleRedo,
      };
    },
  });
</script>