<template>
  <div class="app-container">
    <Form :formList="formList" :ruleFormData="ruleForm" :config="{labelWidth: '140px'}" @fnClick="callback"></Form>
  </div>
</template>

<script setup>
  import Form from '@/components/form/index.vue';
  import {listAllSwitchName, addSwitchInterface, getSwitchInterface, updateSwitchInterface, postInterFaceName} from "@/api/disRevenue/resource"
  export default {
    name: 'PortRemarks',
    components: {Form},
    dicts: ['rm_topology_type'],
    data() {
      return {
        loading: false,
        ruleForm: {},
        formList: [],
        paramsData: {},
        switchNameList: [],
        interfaceNameList: [],
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      } else {
        this.switchList();
      }
      this.fnFormList();

    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息',labelWidth: '140px', colSpan: 'disBlock'},
          controls: {
            id: {label: 'ID',hidden: true},
            switchName: {label: '交换机名称', span: 18, type: 'select', eventName: 'change', options:[], required: true},
            interfaceName: {label: '交换机接口名称', span: 18, type: 'select', options:[], required: true},
            interfaceRemark: {label: '接口备注', span: 18, type: 'textarea', required: true},
          }
        }];
      },
      // 获取交换机下拉
      switchList() {
        listAllSwitchName({}).then(val => {
          if(val && val.data) {
            this.switchNameList = val && val.data;
            this.formList[0].controls.switchName['options'] = val && val.data.map(item => {
              return Object.assign({label: item.switchName, value: item.id});
            });
          }
        });
      },
      // 接口名称
      fnInterFaceNameList(val) {
        postInterFaceName(val).then(res => {
          this.interfaceNameList = res;
          this.formList[0].controls.interfaceName['options'] = res && res.map(item => {
            return Object.assign({label: item.interfaceName, value: item.id});
          });
        });
      },
      // 获取详情
      getFormDataList(id) {
        getSwitchInterface(id).then(val => {
          this.ruleForm = val && val.data;
          this.switchList();
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'switchName':
              let switchIp = '';
              this.switchNameList.find(item => {
                if (item.id === dataVal) {
                  switchIp = item.snmpAddress;
                }
              });
              this.fnInterFaceNameList({switchIp: switchIp});
              break;
            case 'submit':
              this.switchNameList.find(item => {
                if (item.id === dataVal['switchName']) {
                  dataVal['switchName'] = item.switchName;
                  dataVal['clientId'] = item.clientId;
                  dataVal['switchSn'] = item.hardwareSn;
                }
              });
              this.interfaceNameList.find(item => {
                if (item.id === dataVal['interfaceName'] || item.interfaceName === dataVal['interfaceName']) {
                  dataVal['interfaceName'] = item.interfaceName;
                }
              });
              let fnType = addSwitchInterface;
              if (dataVal && dataVal.id) {
                fnType = updateSwitchInterface;
              }
              if(this.loading) return;
              this.loading = true;
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/resource/switchRegister")
                this.loading = false;
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancel':
              this.$router.push("/resource/switchRegister");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
