<template>
  <div class="step2">
    <div>
      <div v-show="isWhereComp == 'org.thingsboard.rule.engine.kafka.TbKafkaNode'">
        <TransferConfigKafka ref="refTransferConfigKafka" @prev="getSonPrev" />
      </div>
      <div v-show="isWhereComp == 'org.thingsboard.rule.engine.mqtt.TbMqttNode'">
        <TransferConfigMqtt ref="refTransferConfigMqtt" @prev="getSonPrev" />
      </div>
      <div v-show="isWhereComp == 'org.thingsboard.rule.engine.rabbitmq.TbRabbitMqNode'">
        <TransferConfigRabbitMq ref="refTransferConfigRabbitMq" @prev="getSonPrev" />
      </div>
      <div v-show="isWhereComp == 'org.thingsboard.rule.engine.rest.TbRestApiCallNode'">
        <TransferConfigApi ref="refTransferConfigApi" @prev="getSonPrev" />
      </div>
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, watch, ref, getCurrentInstance, nextTick } from 'vue';
  import TransferConfigKafka from '../cpns/cpns/transferConfigKafka.vue';
  import TransferConfigMqtt from '../cpns/cpns/transferConfigMqtt.vue';
  import TransferConfigRabbitMq from '../cpns/cpns/transferConfigRabbitMq.vue';
  import TransferConfigApi from '../cpns/cpns/transferConfigApi.vue';
  import { Alert, Divider, Descriptions } from 'ant-design-vue';

  export default defineComponent({
    components: {
      [Alert.name]: Alert,
      [Divider.name]: Divider,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
      TransferConfigKafka,
      TransferConfigMqtt,
      TransferConfigRabbitMq,
      TransferConfigApi,
    },
    // eslint-disable-next-line vue/require-prop-types
    props: ['getModeSelect', 'defineClearFuncProp'],
    emits: ['prevSon'],
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance() as any;
      const getTransferConfigKafkaValue = ref({});
      const refTransferConfigKafka = ref(null);
      const refTransferConfigMqtt = ref(null);
      const refTransferConfigRabbitMq = ref(null);
      const refTransferConfigApi = ref(null);
      const isWhereComp = ref('');

      const getSonPrev = () => {
        emit('prevSon');
      };
      watch(
        () => props.getModeSelect,
        (val) => {
          isWhereComp.value = val.type;
        }
      );
      const clearSonValueDataFunc = () => {
        nextTick(() => {
          try {
            proxy.$refs.refTransferConfigKafka?.customClearStepTwoValueFunc();
            proxy.$refs.refTransferConfigMqtt?.customClearStepTwoValueFunc();
            proxy.$refs.refTransferConfigRabbitMq?.customClearStepTwoValueFunc();
            proxy.$refs.refTransferConfigApi?.customClearStepTwoValueFunc();
          } catch (e) {
            return e;
          }
        });
      };
      const clearSonValueValidateFunc = () => {
        try {
          proxy.$refs.refTransferConfigKafka?.clearValidateFunc();
          // proxy.$refs.refTransferConfigMqtt?.customClearStepTwoValueFunc();
          // proxy.$refs.refTransferConfigRabbitMq?.customClearStepTwoValueFunc();
          // proxy.$refs.refTransferConfigApi?.customClearStepTwoValueFunc();
        } catch (e) {
          return e;
        }
      };
      const getSonValueDataFunc = () => {
        if (isWhereComp.value == 'org.thingsboard.rule.engine.kafka.TbKafkaNode') {
          getTransferConfigKafkaValue.value = proxy.$refs.refTransferConfigKafka.getSonValueFunc();
        } else if (isWhereComp.value == 'org.thingsboard.rule.engine.mqtt.TbMqttNode') {
          getTransferConfigKafkaValue.value = proxy.$refs.refTransferConfigMqtt.getSonValueFunc();
        } else if (isWhereComp.value == 'org.thingsboard.rule.engine.rabbitmq.TbRabbitMqNode') {
          getTransferConfigKafkaValue.value =
            proxy.$refs.refTransferConfigRabbitMq.getSonValueFunc();
        } else if (isWhereComp.value == 'org.thingsboard.rule.engine.rest.TbRestApiCallNode') {
          getTransferConfigKafkaValue.value = proxy.$refs.refTransferConfigApi.getSonValueFunc();
        }
        return getTransferConfigKafkaValue.value;
      };
      const editSonValueDataFunc = (v) => {
        try {
          if (v.type == 'org.thingsboard.rule.engine.kafka.TbKafkaNode') {
            isWhereComp.value = v.type;
            proxy.$refs.refTransferConfigKafka.setStepTwoFieldsValueFunc(
              v.configuration,
              v.name,
              v?.additionalInfo?.description
            );
          } else if (v.type == 'org.thingsboard.rule.engine.mqtt.TbMqttNode') {
            isWhereComp.value = v.type;
            proxy.$refs.refTransferConfigMqtt.setStepTwoFieldsValueFunc(
              v.configuration,
              v.name,
              v?.additionalInfo?.description
            );
          } else if (v.type == 'org.thingsboard.rule.engine.rabbitmq.TbRabbitMqNode') {
            isWhereComp.value = v.type;
            proxy.$refs.refTransferConfigRabbitMq.setStepTwoFieldsValueFunc(
              v.configuration,
              v.name,
              v?.additionalInfo?.description
            );
          } else if (v.type == 'org.thingsboard.rule.engine.rest.TbRestApiCallNode') {
            isWhereComp.value = v.type;
            proxy.$refs.refTransferConfigApi.setStepTwoFieldsValueFunc(
              v.configuration,
              v.name,
              v?.additionalInfo?.description
            );
          }
        } catch (e) {
          return e;
        }
      };
      return {
        clearSonValueValidateFunc,
        clearSonValueDataFunc,
        editSonValueDataFunc,
        refTransferConfigKafka,
        getSonValueDataFunc,
        getSonPrev,
        isWhereComp,
        refTransferConfigMqtt,
        refTransferConfigRabbitMq,
        refTransferConfigApi,
      };
    },
  });
</script>
<style lang="less" scoped></style>
