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

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

    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息',labelWidth: '140px'},
          controls: {
            id: {label: 'ID',hidden: true},
            clientId: {label: '交换机名称', span: 12, type: 'select', eventName: 'change', options:[], required: true},
            interfaceName: {label: '接口名称', span: 12, type: 'select', options:[],required: true},
            connectedDeviceType: {label: '接口连接设备类型', span: 12, type: 'radio', options:this.dict.type.rm_topology_type, required: true},
            serverClientId: {label: '服务器ClientID', span: 12, eventName: 'change', options:[], type: 'select'},
            serverPort: {label: '服务器网口', span: 12, options:[], type: 'select'},
          }
        }];
      },
      // 获取交换机下拉
      switchList() {
        listAllSwitchName({}).then(val => {
          if(val && val.data) {
            this.switchNameList = val && val.data;
            this.formList[0].controls.clientId['options'] = val && val.data.map(item => {
              if (this.paramsData && this.paramsData.id) {
                if (item.clientId === this.ruleForm.clientId) {
                  this.fnInterFaceNameList({switchIp: item.snmpAddress});
                }
              }
              return Object.assign({label: item.switchName, value: item.clientId});
            });
          }
        });
      },
      // 接口名称
      fnInterFaceNameList(val) {
        postInterFaceName(Object.assign({},{resourceType: 2}, 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});
          });
        });
      },
      // 获取服务器下拉
      fnServerNameList(){
        resNameBtType({resourceType: 1}).then(val => {
          this.serverNameList = val && val;
          this.formList[0].controls.serverClientId['options'] = val && val.map(item => {
            if (this.paramsData && this.paramsData.id) {
              if (item.resourceName === this.ruleForm.serverClientId) {
                this.fnServerPortList({serverIp: item.ipAddress});
              }
            }
            return Object.assign({label: item.resourceName, value: item.resourceName});
          });
        });
      },
      // 服务器网口
      fnServerPortList(val) {
        postInterFaceName(Object.assign({},{resourceType: 1}, val)).then(res => {
          this.serverPortList = res;
          this.formList[0].controls.serverPort['options'] = res && res.map(item => {
            return Object.assign({label: item.interfaceName, value: item.id});
          });
        });
      },
      // 获取详情
      getFormDataList(id) {
        getTopology(id).then(val => {
          this.ruleForm = val && val.data;
          this.switchList();
          this.fnServerNameList();
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'clientId':
              this.ruleForm = Object.assign({}, this.ruleForm, this.$refs.formRef.$refs.ruleForm.model);
              let switchIp = '';
              this.switchNameList.find(item => {
                if (item.clientId === dataVal) {
                  switchIp = item.snmpAddress;
                }
              });
              this.ruleForm['interfaceName'] = '';
              this.fnInterFaceNameList({switchIp: switchIp});
              break;
              case 'serverClientId':
                let serverIp = '';
                this.serverNameList.find(item => {
                  if (item.resourceName === dataVal) {
                    serverIp = item.ipAddress;
                  }
                });
                this.fnServerPortList({serverIp: serverIp});
                break;
            case 'submit':
              this.switchNameList.find(item => {
                if (item.clientId === dataVal['clientId']) {
                  dataVal['switchName'] = item.switchName;
                }
              });
              this.serverNameList.find(item => {
                if (item.resourceName === dataVal['serverClientId'] || item.resourceName === dataVal['serverClientId']) {
                  dataVal['serverClientId'] = item.resourceName;
                  dataVal['serverSn'] = item.hardwareSn;
                }
              });
              this.serverPortList.find(item => {
                if (item.id === dataVal['serverPort'] || item.interfaceName === dataVal['serverPort']) {
                  dataVal['serverPort'] = item.interfaceName;
                }
              });
              this.interfaceNameList.find(item => {
                if (item.id === dataVal['interfaceName'] || item.interfaceName === dataVal['interfaceName']) {
                  dataVal['interfaceName'] = item.interfaceName;
                }
              });
              let fnType = addTopology;
              if (dataVal && dataVal.id) {
                fnType = updateTopology;
              }
              if(this.loading) return;
              this.loading = true;
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/resource/topology");
                this.loading = false;
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancel':
              this.$router.push("/resource/topology");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style scoped>
  ::v-deep .el-radio {
    margin-right: 15px!important;
  }
</style>
