<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 {listAllResourList} from "@/api/disRevenue/resource"
  export default {
    name: 'ServerDetails',
    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: '基本信息'},
          controls: {
            id: {label: 'ID',hidden: true},
            clientId: {label: 'ClientID', span: 12, type: 'select',options:[],
              rules: [{required: true, message: '请选择节点名称', trigger: 'change'}]},
            businessName: {label: '业务名称', span: 12, style: 'display: block;', type: 'select', options:[], eventName: 'change',
              rules: [{required: true, message: '请选择业务名称', trigger: 'change'}]},
            businessId: {label: '业务代码', span: 12, style: 'display: block;', type: 'input', disabled: true,
              rules: [{required: true, message: '请输入业务代码', trigger: 'blur'}]},
            monthTime: {label: '月份选择', span: 12, type: 'month',
              rules: [{required: true, message: '请选择月份', trigger: 'change'}]},
          }
        }];
      },
      // 获取交换机下拉
      switchList() {
        listAllResourList({resourceType: this.paramsData.resourceType}).then(val => {
          this.switchNameList = val && val;
          this.formList[0].controls.nodeName['options'] = val && val.map(item => {
            return Object.assign({label: item.resourceName, value: item.resourceName});
          });
        });
        listAllBusinessList().then(val => {
          this.formList[0].controls.businessName['options'] = val && val.data.map(item => {
            return Object.assign({label: item.businessName, 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 '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);
                if (this.paramsData && this.paramsData.resourceType === '1') {
                  this.$router.push("/earnManage/server");
                }
                // else {
                //   this.$router.push("/disRevenue/earnManage/switch");
                // }
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancel':
              if (this.paramsData && this.paramsData.resourceType === '1') {
                this.$router.push("/earnManage/server");
              }
              // else {
              //   this.$router.push("/disRevenue/earnManage/switch");
              // }
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
