<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 {listHandle, 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},
            switchName: {label: '交换机名称', span: 12, type: 'select', eventName: 'change', options:[], rules: [{required: true, message: '请输入硬件SN', trigger: 'blur'}]},
            interfaceName: {label: '接口名称', span: 12, type: 'select', options:[]},
            connectedDeviceType: {label: '接口连接设备类型', span: 12, type: 'select', options:this.dict.type.rm_topology_type},
            serverName: {label: '服务器名称', span: 12, eventName: 'change', options:[], type: 'select'},
            serverPort: {label: '服务器网口', span: 12, options:[], type: 'select'},
          }
        }];
      },
      // 获取交换机下拉
      switchList() {
        resNameBtType({resourceType: 2}).then(val => {
          this.switchNameList = val && val;
          this.formList[0].controls.switchName['options'] = val && val.map(item => {
            if (this.paramsData && this.paramsData.id) {
              if (item.resourceName === this.ruleForm.switchName) {
                this.fnInterFaceNameList({switchIp: item.ipAddress});
              }
            }
            return Object.assign({label: item.resourceName, value: item.resourceName});
          });
        });
      },
      // 接口名称
      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.serverName['options'] = val && val.map(item => {
            if (this.paramsData && this.paramsData.id) {
              if (item.resourceName === this.ruleForm.serverName) {
                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 'switchName':
              let switchIp = '';
              this.switchNameList.find(item => {
                if (item.resourceName === dataVal) {
                  switchIp = item.ipAddress;
                }
              });
              this.fnInterFaceNameList({switchIp: switchIp});
              break;
              case 'serverName':
                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.resourceName === dataVal['switchName'] || item.resourceName === dataVal['switchName']) {
                  dataVal['switchName'] = item.resourceName;
                  dataVal['switchIpAddress'] = item.ipAddress;
                  dataVal['switchSn'] = item.hardwareSn;
                }
              });
              this.serverNameList.find(item => {
                if (item.resourceName === dataVal['serverName'] || item.resourceName === dataVal['serverName']) {
                  dataVal['serverName'] = 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 'cancle':
              this.$router.push("/resource/topology");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
