<template>
  <!-- 网络物理连接创建对话框 -->
  <el-dialog
    @close="close"
    @opened="createNetworkPhysicsLinkInit"
    v-dialogDrag
    :title=title
    style="font-size: 10px;" width='450px'
    :visible.sync="CreateNetworkPhysicsLinkVisible">
    <el-form :model="form" inline="true">
      <el-form-item :label="'*'+$t('NetworkPhysicsLinkConfigurationManagement.nodeName')+'：'" :label-width="formLabelWidth" style="margin-bottom: -10px;">
        <el-input size = 'mini' :placeholder="$t('common.placeholder')" v-model="form.nodeName" v-on:blur="nodeNameCheck" autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item :label="'*'+$t('NetworkPhysicsLinkConfigurationManagement.accessBusiness')+'：'" :label-width="formLabelWidth" style="margin-bottom: -10px;">
        <el-input size = 'mini' :placeholder="$t('common.placeholder')" v-model="form.description" v-on:blur="descriptionCheck" autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item :label="'*'+$t('NetworkPhysicsLinkConfigurationManagement.deviceKey')+'：'" :label-width="formLabelWidth" style="margin-bottom: -10px;">
        <el-select v-model="form.deviceKey" size="mini" :placeholder="$t('common.selectOption')" v-on:change="deviceKeyCheck" autocomplete="off" style="width: 200px">
          <el-option
            v-for="item in usingDeviceList"
            :key="item.deviceKey"
            :label="item.deviceName"
            :value="item.deviceKey">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'*'+$t('NetworkPhysicsLinkConfigurationManagement.port')+'：'" :label-width="formLabelWidth" style="margin-bottom: -10px;">
        <el-input size = 'mini' :placeholder="$t('common.placeholder')" v-model="form.port" v-on:blur="portNameCheck" autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size = 'mini' type="primary" @click="createNetworkPhysicsLink" v-loading.fullscreen.lock="fullscreenLoading">{{$t('common.createBtn')}}</el-button>
      <el-button size = 'mini' @click="close">{{$t('common.cancel')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import NetworkPhysicsService from '@/domain/services/NetworkPhysicsService.js'
  import DeviceService from '@/domain/services/DeviceService'
  import legitimacyCheck from "../../../utils/legitimacyCheck";
  import HelperUtil from "../../../utils/HelperUtil";

  export default {
    name: "CreateNetworkPhysicsLinkDialog",
    props: ['command'],
    data: function(){
      return{
        title: this.command.title,
        initList: this.command.target,
        CreateNetworkPhysicsLinkVisible: true,
        formLabelWidth: '120px',
        deviceList: [],
        usingDeviceList: [],

        form:{
          "nodeName": "",
          "description": "",
          "deviceKey": "",
          "port": "",
        },
        fullscreenLoading:false,
      }

    },
    methods:{
      close() {
        this.command.done();
      },

      /**
       * @Description  ：根据设备状态，过滤设备集合
       * @author       : zyh
       */
      handleDevice() {
        let _this = this;
        this.deviceList.forEach(function (device) {
          if (device.deviceStatus === 0) {
            _this.usingDeviceList.push(device)
          }
        })
      },

      /**
       * @Description  :获取全部网元信息
       * @author       : zyh
       */
      getAllDeviceInit: function () {
        let _this = this;
        DeviceService.getAllDeviceCollection().then(result => {
          //成功
          _this.deviceList = Object.values(result)
          _this.handleDevice();
        }).catch(err => {
          //失败
          _this.InfoTip.errorTip(_this,err);
        })
      },
    //------------------------------------创建开始-------------------------------------------------------------------

      createNetworkPhysicsLinkInit(){
        this.form.nodeName = this.initList.nodeName;
        this.form.description = this.initList.description;
        this.form.deviceKey = this.initList.deviceKey;
        this.form.port = this.initList.port;
      },

      createNetworkPhysicsLink(){
        let _this = this
        if(!this.totalCheck()){

        }
        else{
          let netPhy={};
          netPhy.nodeName = this.form.nodeName;
          netPhy.description = this.form.description;
          netPhy.deviceKey = this.form.deviceKey;
          netPhy.port = this.form.port;

          NetworkPhysicsService.addNetPhyLink(netPhy).then(result => {
            //成功
            _this.InfoTip.successTip(_this,HelperUtil.getStatusCodeObjectByCode(_this.successCode.ADD_CODE));
            _this.close();
          }).catch(err => {
            _this.InfoTip.errorTip(_this, err);
//            this.command.done();
          });
        }

      },

      //------------------------------------创建结束-------------------------------------------------------------------

      //------------------------------------验证开始-------------------------------------------------------------------

      /**
       * @Description  ：节点名称校验： 不能为空
       * @author       :
       */
      nodeNameCheck: function(){
        let netPhyCheck = legitimacyCheck()
        if (!netPhyCheck.textNullCheck(this.form.nodeName)) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE),this.$t('NetworkPhysicsLinkConfigurationManagement.nodeName'))
          return false
        }
        if(!this.textLengthCheck(this.form.nodeName)){return false}
        return true
      },

      /**
       * @Description  ：接入业务校验： 不能为空
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      descriptionCheck: function(){
        let netPhyCheck = legitimacyCheck()
        if (!netPhyCheck.textNullCheck(this.form.description)) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE),this.$t('NetworkPhysicsLinkConfigurationManagement.description'))
          return false
        }
        if(!this.textLengthCheck(this.form.description)){return false}
        return true
      },

      /**
       * @Description  ：设备名称校验： 不能为空
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      deviceKeyCheck: function(){
        let netPhyCheck = legitimacyCheck()
        if (!netPhyCheck.textNullCheck(this.form.deviceKey)) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE),this.$t('NetworkPhysicsLinkConfigurationManagement.deviceKey'))
          return false
        }
        if(!this.textLengthCheck(this.form.deviceKey)){return false}
        return true
      },

      /**
       * @Description  ：端口名称校验： 不能为空，应数字
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      portNameCheck: function () {
        let netPhyCheck = legitimacyCheck()
        if (!netPhyCheck.textNullCheck(this.form.port)) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE),this.$t('NetworkPhysicsLinkConfigurationManagement.port'))
          return false
        }else if( parseInt(this.form.port)>99|| parseInt(this.form.port)<0){
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PORT_BETWEEN_0_99))
          return false
        }
        if(!this.textLengthCheck(this.form.port)){return false}
        if(!this.numCheck(this.form.port)){return false}
        return true
      },


      /**
       * @Description  ：文本框所输入的字符长度校验： 控制在20之内
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      textLengthCheck: function (checkString) {
        let netPhyCheck = legitimacyCheck()
        if (!netPhyCheck.textLengthCheck(checkString)) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LENGTH_CODE))

          return false
        }
        return true
      },

      /**
       * @Description  ：输入数字
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      numCheck: function (checkNumber) {
        let netPhyCheck = legitimacyCheck()
        if (!netPhyCheck.numberCheck(checkNumber)) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_ENTER_VALID_NUMBER))

          return false
        }
        return true
      },

      totalCheck(){
        if( this.nodeNameCheck()&&
          this.descriptionCheck()&&
          this.deviceKeyCheck()&&
          this.portNameCheck())
          return true
        else{
          return false
        }
      },

      //------------------------------------验证结束-------------------------------------------------------------------
    },
    beforeCreate:function(){

    },
    created(){
      this.getAllDeviceInit()
      this.createNetworkPhysicsLinkInit()
    },
    mounted(){

    },
  }
</script>

<style scoped>

</style>
