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

<script setup name="Handle">
  import Form from '@/components/form/index.vue';
  import {addHandle, getHandle, updateHandle} from "@/api/disRevenue/resource"
  export default {
    name: 'RegisterHandle',
    components: {Form},
    dicts: ['rm_register_resource_type', 'rm_register_protocol', 'rm_register_snmp_detect','rm_register_version', 'rm_register_security_level', 'rm_register_permission', 'rm_register_encryption'],
    data() {
      return {
        ruleForm: {
          resourceType: '1',
          snmpDetect: '1',
          protocol: '2',
          resourceVersion: '1',
          securityLevel: '1',
          rwPermission: '1',
          encryption: '1',
          // agentWeek: '5秒',
          // agentNum: '3次',
          // agentOID: '1.3.6.1.2.1.1.5'
        },
        formList: [],
        paramsData: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      } else {
        this.fnFormList(this.ruleForm);
      }
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息'},
          controls: {
            id: {label: 'ID',hidden: true},
            hardwareSn: {label: '硬件SN', span: 12, type: 'input',
              disabled: objVal && objVal.id ? true : false, required: true},
            resourceName: {label: '资源名称', span: 12, type: 'input',required: true},
            resourceType: {label: '资源类型', span: 12, type: 'radio', options: this.dict.type.rm_register_resource_type, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            ipAddress: {label: 'IP地址', span: 12, type: 'input',required: true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            protocol: {label: '协议', span: 12, type: 'radio', options: this.dict.type.rm_register_protocol, required: true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            resourcePort: {label: '端口', span: 12, type: 'input',required: true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            // otherPortName: {label: ' ', span: 4, type: 'input',
            //   hidden: objVal && objVal.resourcePort === '2' ? false : true},
            snmpDetect: {label: 'SNMP探测', span: 12, type: 'radio', eventName: 'change',required: true, options: this.dict.type.rm_register_snmp_detect, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            resourceVersion: {label: 'SNMP版本', span: 12, type: 'radio',required: true, options: this.dict.type.rm_register_version,hidden: objVal && objVal.snmpDetect === '1' ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            rwPermission: {label: '读写权限', span: 12, type: 'radio', options: this.dict.type.rm_register_permission,hidden: objVal && objVal.snmpDetect === '1' ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            securityLevel: {label: '安全级别', span: 12, type: 'radio', options: this.dict.type.rm_register_security_level,hidden: objVal && objVal.id ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            encryption: {label: '加密方式', span: 12, type: 'radio', options: this.dict.type.rm_register_encryption,hidden: objVal && objVal.id ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            teamName: {label: '团体名称', span: 12, type: 'input', hidden: objVal && objVal.snmpDetect === '1' ? false : true,disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            snmpCollectAddr: {label: 'SNMP采集地址', span: 12, type: 'input',required: true, hidden: objVal && objVal.snmpDetect === '1' ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            snmpCollectPort: {label: 'SNMP采集端口', span: 12, type: 'input',required: true, hidden: objVal && objVal.snmpDetect === '1' ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            resourceUserName: {label: '用户名', span: 12, type: 'input', hidden: objVal && objVal.id ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            resourcePwd: {label: '密码', span: 12, type: 'input', hidden: objVal && objVal.id ? false : true, disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            description: {label: '描述', span: 12, type: 'textarea',disabled: objVal && objVal.registrationStatus === '1' ? true : false},
            agentVersion: {label: 'AGENT版本', span: 12, type: 'input',hidden: objVal && objVal.id ? false : true, disabled: objVal && objVal.id ? true : false},
            // customerName: {label: '设备业务客户', span: 12, type: 'input'},
            // serviceNumber: {label: '业务号', span: 12, type: 'input'},
            // agentWeek: {label: 'Agent与交换机心跳检测周期', span: 12, type: 'input', disabled: true},
            // agentNum: {label: 'Agent与交换机心跳检测次数', span: 12, type: 'input', disabled: true},
            // agentOID: {label: 'Agent与交换机心跳检测OID', span: 12, type: 'input', disabled: true},
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getHandle(id).then(val => {
          this.ruleForm = val && val.data;
          this.fnFormList(this.ruleForm);
        }).catch(() => {
          this.fnFormList(this.ruleForm);
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'snmpDetect':
              if (dataVal === '1') {
                this.formList[0].controls.resourceVersion['hidden'] = false;
                this.formList[0].controls.rwPermission['hidden'] = false;
                this.formList[0].controls.snmpCollectAddr['hidden'] = false;
                this.formList[0].controls.snmpCollectPort['hidden'] = false;
                this.formList[0].controls.teamName['hidden'] = false;
              } else {
                this.formList[0].controls.resourceVersion['hidden'] = true;
                this.formList[0].controls.rwPermission['hidden'] = true;
                this.formList[0].controls.snmpCollectAddr['hidden'] = true;
                this.formList[0].controls.snmpCollectPort['hidden'] = true;
                this.formList[0].controls.teamName['hidden'] = true;
              }
              break;
            case 'submit':
              let fnType = addHandle;
              if (dataVal && dataVal.id) {
                fnType = updateHandle;
              }
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/resource/register")
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancle':
              this.$router.push("/resource/register")
              break;
            default:

          }
        }
      }
    }
  }
</script>
<style>
</style>
