<template>
  <div class="app-container">
    <Form :formList="formList" :ruleFormData="ruleForm" :config="this.paramsData && this.paramsData.readonly ? {labelWidth: '140px', buttonGroup: []} : {labelWidth: '140px'}" @fnClick="callback"></Form>
    <el-button v-if="this.paramsData && this.paramsData.readonly" style="float: right;margin-top: 12px;margin-left: 10px;" @click="callback({fnCode: 'cancel'})">返回</el-button>
  </div>
</template>

<script setup>
  import Form from '@/components/form/index.vue';
  import {addMachine, getHandle, updateTopology, getRegistList, bindBusByClient} from "@/api/disRevenue/resource";
  import {listAllBusinessList} from "@/api/disRevenue/earnManage";
  export default {
    name: 'serverRegister_Edit',
    components: {Form},
    dicts: ['rm_register_status','rm_register_online_state'],
    data() {
      return {
        loading: false,
        ruleForm: {},
        formList: [],
        paramsData: {},
        switchNameList: [],
        serverNameList: [],
        serverPortList: [],
        interfaceNameList: [],
        busNameList: {},
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      }
      if (this.paramsData && this.paramsData.type && this.paramsData.type === 'edit') {
        this.fnBusNameList();
      } else {
        this.registList();
      }
      this.fnFormList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        if (this.paramsData && this.paramsData.type && this.paramsData.type === 'edit') {
          this.$set(this.ruleForm, 'deployDevice', this.paramsData.clientIdList);
          this.formList = [{
            config: {title: '基本信息',labelWidth: '140px', colSpan: 'disBlock'},
            controls: {
              id: {label: 'ID',hidden: true},
              businessName: {label: '业务名称', span: 12, type: 'select', options:[], required: true},
              deployDevice: {label: '服务器列表', span: 12, type: 'textarea', rows: 15, required: true},
            }
          }];
        } else if (this.paramsData && this.paramsData.readonly) {
          this.formList = [{
            config: {title: '基本信息',labelWidth: '140px', readonly: true},
            controls: {
              id: {label: 'ID',hidden: true},
              clientId: {label: 'ClientID', span: 12, type: 'select', options:[]},
              hardwareSn: {label: '设备SN', span: 12, type: 'input'},
              ip1Isp: {label: 'IP1-运营商', span: 12, type: 'input'},
              ip1Province: {label: 'IP1-省', span: 12, type: 'input'},
              ip1City: {label: 'IP1-市', span: 12, type: 'input'},
              ip1PublicIp: {label: 'IP1-业务公网', span: 12, type: 'input'},
              ip1InterfaceName: {label: 'IP1-接口名称', span: 12, type: 'input'},
              ip1MacAddress: {label: 'IP1-mac地址', span: 12, type: 'input'},
              ip1InterfaceType: {label: 'IP1-接口类型', span: 12, type: 'input'},
              ip1Ipv4Address: {label: 'IP1-IPv4地址', span: 12, type: 'input'},
              ip1Gateway: {label: 'IP1-网关', span: 12, type: 'input'},
              ip2Isp: {label: 'IP2-运营商', span: 12, type: 'input'},
              ip2Province: {label: 'IP2-省', span: 12, type: 'input'},
              ip2City: {label: 'IP2-市', span: 12, type: 'input'},
              ip2PublicIp: {label: 'IP2-业务公网', span: 12, type: 'input'},
              ip2InterfaceName: {label: 'IP2-接口名称', span: 12, type: 'input'},
              ip2MacAddress: {label: 'IP2-mac地址', span: 12, type: 'input'},
              ip2InterfaceType: {label: 'IP2-接口类型', span: 12, type: 'input'},
              ip2Ipv4Address: {label: 'IP2-IPv4地址', span: 12, type: 'input'},
              ip2Gateway: {label: 'IP2-网关', span: 12, type: 'input'},
              ip3Isp: {label: 'IP3-运营商', span: 12, type: 'input'},
              ip3Province: {label: 'IP3-省', span: 12, type: 'input'},
              ip3City: {label: 'IP3-市', span: 12, type: 'input'},
              ip3PublicIp: {label: 'IP3-业务公网', span: 12, type: 'input'},
              ip3InterfaceName: {label: 'IP3-接口名称', span: 12, type: 'input'},
              ip3MacAddress: {label: 'IP3-mac地址', span: 12, type: 'input'},
              ip3InterfaceType: {label: 'IP3-接口类型', span: 12, type: 'input'},
              ip3Ipv4Address: {label: 'IP3-IPv4地址', span: 12, type: 'input'},
              ip3Gateway: {label: 'IP3-网关', span: 12, type: 'input'},
              mgmtIsp: {label: '管理网-运营商', span: 12, type: 'input'},
              mgmtProvince: {label: '管理网-省', span: 12, type: 'input'},
              mgmtCity: {label: '管理网-市', span: 12, type: 'input'},
              mgmtPublicIp: {label: '管理网-公网IP', span: 12, type: 'input'},
              mgmtInterfaceName: {label: '管理网-接口名称', span: 12, type: 'input'},
              mgmtMacAddress: {label: '管理网-mac地址', span: 12, type: 'input'},
              mgmtInterfaceType: {label: '管理网-接口类型', span: 12, type: 'input'},
              mgmtIpv4Address: {label: '管理网-IPv4地址', span: 12, type: 'input'},
              mgmtGateway: {label: '管理网-网关', span: 12, type: 'input'},
              heartbeatInterval: {label: '心跳时间间隔', span: 12, type: 'input'},
              heartbeatCount: {label: '心跳次数', span: 12, type: 'input'},
              businessName: {label: '业务名称', span: 12, type: 'input'},
              logicalNodeId: {label: '逻辑节点标识', span: 12, type: 'input'},
              onlineStatus: {label: '在线状态', span: 12, type: 'select', options: this.dict.type.rm_register_online_state},
              registrationStatus: {label: '注册状态', span: 12, type: 'select', options: this.dict.type.rm_register_status},
              createTime: {label: '注册时间', span: 12, type: 'input'},
              onboardTime: {label: '上机时间', span: 12, type: 'input'},
              agentVersion: {label: 'agent版本', span: 12, type: 'input'},
              machineCode: {label: '金山machineCode', span: 12, type: 'input'},
            }
          }];
        } else {
          this.formList = [{
            config: {title: '基本信息',labelWidth: '140px', colSpan: 'disBlock'},
            controls: {
              id: {label: 'ID',hidden: true},
              clientId: {label: 'ClientID', span: 12, type: 'select', options:[], required: true},
              machineCode: {label: '金山machineCode', span: 12, type: 'input', required: true},
            }
          }];
        }
      },
      // clientId
      registList() {
        getRegistList({resourceType: 1}).then(res => {
          this.formList[0].controls.clientId['options'] = res && res.data.map(item => {
            return Object.assign({label: item.clientId, value: item.clientId});
          });
        });
      },
      // 业务名称
      fnBusNameList() {
        listAllBusinessList().then(res => {
          this.formList[0].controls.businessName['options'] = res && res.data.map(item => {
            this.busNameList[item.businessName] = item;
            return Object.assign({label: item.businessName, value: item.businessName});
          });
        });
      },
      // 获取详情
      getFormDataList(id) {
        getHandle(id).then(val => {
          this.ruleForm = val && val.data;
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'submit':
              if (this.paramsData && this.paramsData.type && this.paramsData.type === 'edit') {
                dataVal['businessCode'] = this.busNameList[dataVal.businessName].id;
                bindBusByClient(dataVal).then(res => {
                  this.$modal.msgSuccess(res.msg);
                  this.$router.push("/resource/serverRegister");
                });
              } else {
                let content = '<p style="font-size: 1rem;font-weight: 600;">确认添加machineCode</p>' +
                  '<p style="height: 0px;margin-bottom: 50px;">一旦添加machineCode后，将不能修改</p>';
                this.$modal.confirm(content, {submitTitle: '确认添加'}).then(() => {
                  let fnType = addMachine;
                  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/serverRegister");
                    this.loading = false;
                  }).catch(() => {
                    this.$modal.msgError("操作失败")
                  });
                }).catch(() => {});
              }
              break;
            case 'cancel':
              this.$router.push("/resource/serverRegister");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style scoped>
  ::v-deep .el-radio {
    margin-right: 15px!important;
  }
</style>
