<template>
  <div class="app-container">
    <Form :formList="formList" :ruleFormData="ruleForm" @fnClick="callback"></Form>
  </div>
</template>

<script setup>
  import Form from '@/components/form/index.vue';
  import {listAllBusinessList, calculateAvg, getBandWidth, updateBandWidth} from "@/api/disRevenue/earnManage"
  import {listAllSwitchName, postInterFaceName} from "@/api/disRevenue/resource"
  export default {
    name: 'Switch_Details',
    components: {Form},
    dicts: ['rm_topology_type'],
    data() {
      return {
        ruleForm: {},
        formList: [],
        switchNameList: [],
        paramsData: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      this.fnFormList();
      this.switchList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息', colSpan: 'disBlock'},
          controls: {
            id: {label: 'ID',hidden: true},
            uplinkSwitch: {label: '交换机名称', span: 12, type: 'select',options:[], eventName: 'change', required: true},
            interfaceName: {label: '接口名称', span: 12, type: 'select',options:[], required: true},
            interfaceRemark: {label: '接口备注', span: 12, type: 'input', disabled: true},
            businessName: {label: '业务名称', span: 12, type: 'select', options:[], eventName: 'change', required: true},
            businessId: {label: '业务代码', span: 12, type: 'input', disabled: true},
            monthTime: {label: '月份选择', span: 12, type: 'month', required: true},
          }
        }];
      },
      // 获取交换机下拉
      switchList() {
        listAllSwitchName({}).then(val => {
          if(val && val.data) {
            this.switchNameList = val && val.data;
            this.formList[0].controls.uplinkSwitch['options'] = val && val.data.map(item => {
              return Object.assign({label: item.switchName, value: item.id});
            });
          }
        });
        listAllBusinessList().then(val => {
          this.formList[0].controls.businessName['options'] = val && val.data.map(item => {
            return Object.assign({label: item.businessName, 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});
          });
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'businessName':
              if (dataVal) {
                formVal.options.forEach(item => {
                  if (item.value === dataVal) {
                    this.$set(this.ruleForm, 'businessName', item.label);
                  }
                });
                this.$set(this.ruleForm, 'businessId', dataVal);
              }
              break;
              case 'uplinkSwitch':
                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.resourceName === dataVal['nodeName']) {
                  dataVal['hardwareSn'] = item.hardwareSn;
                }
              });
              dataVal['resourceType'] = this.paramsData.resourceType;
              calculateAvg(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/earnManage/switch");
                // else {
                //   this.$router.push("/disRevenue/earnManage/switch");
                // }
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancel':
              this.$router.push("/earnManage/switch");
              // else {
              //   this.$router.push("/disRevenue/earnManage/switch");
              // }
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
