<template>
  <!-- 子网创建对话框 -->
  <el-dialog
    @close="close"
    v-dialogDrag
    :title="$t('LinkConfigurationManagement.updateLinkInfo')"
    style="font-size: 10px;" width='450px'
    :visible.sync="createAndCopyLinkVisible">
    <el-form :model="form" inline="true">
      <el-form-item :label="'*'+$t('LinkConfigurationManagement.linkName')+'：'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' v-model="form.linkName" :placeholder="$t('common.placeholder')" v-on:blur="linkNameCheck"
                  style="width: 173px"></el-input>
      </el-form-item>
      <el-form-item :label="'*'+$t('LinkConfigurationManagement.startDeviceName')+'：'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-select size='mini' v-model="form.startDeviceKey"
                   :placeholder="$t('LinkConfigurationManagement.selectStartDevicePlaceholder')"
                   v-on:change="deviceNameChange(form.startDeviceKey)" disabled>
          <el-option
            v-for="item in usingDeviceList"
            :key="item.deviceKey"
            :label="item.displayName"
            :value="item.deviceKey"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'*'+$t('LinkConfigurationManagement.startDevicePort')+'：'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-select clearable size='mini' v-model="form.startPortName" :placeholder="$t('common.placeholderSelect')" filterable @blur="selectBlur" disabled>
          <el-option v-for="item in portNameList" :key="item.resourceKey" :label="item.portName"
                     :value="item.portName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'*'+$t('LinkConfigurationManagement.endDeviceName')+'：'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-select size='mini' v-model="form.endDeviceKey"
                   :placeholder="$t('LinkConfigurationManagement.selectEndDevicePlaceholder')"
                   v-on:change="deviceNameChange1(form.endDeviceKey)" disabled>
          <el-option v-for="item in usingDeviceList" :key="item.deviceKey" :label="item.displayName"
                     :value="item.deviceKey"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'*'+$t('LinkConfigurationManagement.endDevicePort')+'：'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-select clearable size='mini' v-model="form.endPortName" :placeholder="$t('common.placeholderSelect')" filterable @blur="selectBlur1" disabled>
          <el-option v-for="item in portNameList1" :key="item.resourceKey" :label="item.portName"
                     :value="item.portName">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" icon='el-icon-circle-check' @click="createLink" v-loading.fullscreen.lock="fullscreenLoading">
        {{ $t('common.ok') }}
      </el-button>
      <el-button size='mini' icon='el-icon-circle-close' @click="close">{{ $t('common.cancel') }}</el-button>

    </div>
  </el-dialog>
</template>

<script>
import LinkService from '@/domain/services/LinkService.js'
import DeviceService from '@/domain/services/DeviceService'

import legitimacyCheck from '../../../utils/legitimacyCheck'
import HelperUtil from '../../../utils/HelperUtil'

export default {
  props: ['command'],
  data: function () {
    return {
      initList: this.command.target,
      createAndCopyLinkVisible: true,
      formLabelWidth: '150px',
      deviceList: [],
      deviceList1: null,
      startDevice: [],
      endDevice: [],
      devicePort1: 99,
      devicePort2: 99,
      findDeviceModel1: [],
      findDeviceModel2: [],
      usingDeviceList: [], // 筛选网元
      portNameList: [],
      portNameList1: [],
      linkCheck: null,
      form: {
        'linkKey': '',
        'linkName': '',
        'startDeviceKey': '',
        'startPortName': '',
        'endDeviceKey': '',
        'endPortName': '',
        'linkStatus': 0,
        'linkType': 0,
        'delFlag': 0,
        'modifyTime': ''
      },

      // 数据加载过程中，覆盖界面
      fullscreenLoading: false
    }
  },
  methods: {
    /**
       * @Description  : 支持手动输入
       * @author       : ls
       * @date         : 2020/10/13 17:04
       * @param        :
       * @return       :
       */
    selectBlur: function (e) {
      this.form.startPortName = e.target.value
    },
    selectBlur1: function (e) {
      this.form.endPortName = e.target.value
    },
    /**
       * @Description  : deviceNameChange
       * @author       : ls
       * @date         : 2020/9/21 11:21
       * @param        :
       * @return       :
       */
    deviceNameChange1: function (val) {
      let _this = this
      var flag = false
      _this.portNameList1 = []
      _this.form.endPortName = ''
      var deviceTy = _this.deviceList1[val].deviceTypeKey
      if (deviceTy === 'SERVER' || deviceTy === 'STORAGE') {
        flag = true
      }
      if (flag) {
        _this.portNameList1 = [
          // {
          //   key: 'IPMI',
          //   portName: 'IPMI'
          // },
          // {
          //   key: '网络端口1',
          //   portName: '网络端口1'
          // },
          // {
          //   key: '网络端口2',
          //   portName: '网络端口2'
          // }
        ]
      } else {
        let selectList = []
        selectList.push(
          {
            quaryAttribute: 'deviceKey',
            compareSymbol: '=',
            compareValue: val
          },
          {
            quaryAttribute: 'deviceParam',
            compareSymbol: '=',
            compareValue: 'PORT_NAME'
          }
        )
        DeviceService.getDevicePortStatus(selectList).then(result => {
          _this.portNameList1 = result
        }).catch(err => {
          _this.InfoTip.errorTip(_this, err)
        })
      }
    },
    /**
       * @Description  : deviceNameChange
       * @author       : ls
       * @date         : 2020/9/18 15:40
       * @param        :
       * @return       :
       */
    deviceNameChange: function (val) {
      let _this = this
      var flag = false
      _this.portNameList = []
      _this.form.startPortName = ''
      var deviceTy = _this.deviceList1[val].deviceTypeKey
      if (deviceTy === 'SERVER' || deviceTy === 'STORAGE') {
        flag = true
      }
      if (flag) {
        _this.portNameList = [
          // {
          //   key: 'IPMI',
          //   portName: 'IPMI'
          // },
          // {
          //   key: '网络端口1',
          //   portName: '网络端口1'
          // },
          // {
          //   key: '网络端口2',
          //   portName: '网络端口2'
          // }
        ]
      } else {
        let selectList = []
        selectList.push(
          {
            quaryAttribute: 'deviceKey',
            compareSymbol: '=',
            compareValue: val
          },
          {
            quaryAttribute: 'deviceParam',
            compareSymbol: '=',
            compareValue: 'PORT_NAME'
          }
        )
        DeviceService.getDevicePortStatus(selectList).then(result => {
          _this.portNameList = result
        }).catch(err => {
          _this.InfoTip.errorTip(_this, err)
        })
      }
    },
    /**
       * @Description  :选择网元触发事件，调用接口
       * @author       ::zyh
       * @date         :
       */
    startDeviceName (val) {
      this.form.startPortName = ''
      let _this = this
      this.findDeviceModel1 = []

      for (var i = 0; i < this.deviceList.length; i++) {
        if (this.form.startDeviceKey == this.deviceList[i].deviceKey) {
          this.findDeviceModel1 = this.deviceList[i].deviceModel
        }
      }
      let selectList = []
      selectList.push({
        quaryAttribute: 'deviceModel',
        compareSymbol: '=',
        compareValue: this.findDeviceModel1
      })
      DeviceService.getDevicePortNum(selectList).then(result => {
        // 成功
        if (result.length !== 0) {
          _this.devicePort1 = result[0].portNum
        }
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    endDeviceName (val) {
      this.form.endPortName = ''
      let _this = this
      this.findDeviceModel2 = []
      for (var i = 0; i < this.deviceList.length; i++) {
        if (this.form.endDeviceKey == this.deviceList[i].deviceKey) {
          this.findDeviceModel2 = this.deviceList[i].deviceModel
        }
      }
      let selectList = []
      selectList.push({
        quaryAttribute: 'deviceModel',
        compareSymbol: '=',
        compareValue: this.findDeviceModel2
      })
      DeviceService.getDevicePortNum(selectList).then(result => {
        // 成功
        if (result.length !== 0) {
          this.devicePort2 = result[0].portNum
        }
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },

    close () {
      this.command.done()
    },

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

    /**
       * @Description  :获取全部网元信息
       * @author       ::
       * @date         :
       */
    getAllDeviceInit: function () {
      let _this = this
      console.log('获取全部网元信息:')
      DeviceService.getAllDeviceCollection().then(result => {
        // 成功
        _this.deviceList1 = result
        _this.deviceList = Object.values(result)
        if (_this.initList.startDeviceKey !== '') {
          _this.deviceNameChange(_this.initList.startDeviceKey)
        }
        if (_this.initList.endDeviceKey !== '') {
          _this.deviceNameChange1(_this.initList.endDeviceKey)
        }
        _this.form.startPortName = _this.initList.startPortName
        _this.form.endPortName = _this.initList.endPortName
        _this.handleDevice()
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },

    // ------------------------------------创建开始-------------------------------------------------------------------
    createLinkInit () {
      this.form.linkName = this.initList.linkName
      this.form.startDeviceKey = this.initList.startDeviceKey
      this.form.endDeviceKey = this.initList.endDeviceKey
      this.form.linkStatus = this.initList.linkStatus
      this.form.linkType = this.initList.linkType
      this.form.delFlag = this.initList.delFlag
      this.form.modifyTime = this.initList.modifyTime
    },

    createLink () {
      let _this = this
      if (!this.totalCheck()) {
        return
      }
      let link = {}
      link.linkKey = this.initList.linkKey
      link.linkName = this.form.linkName
      link.startDeviceKey = this.form.startDeviceKey
      link.startPortName = this.form.startPortName
      link.endDeviceKey = this.form.endDeviceKey
      link.endPortName = this.form.endPortName
      link.linkStatus = this.form.linkStatus
      link.linkType = this.form.linkType
      link.delFlag = this.form.delFlag
      link.modifyTime = this.form.modifyTime
      LinkService.modifyLink(link).then(result => {
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.MODIFY_CODE))
        _this.close()
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },

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

    // ------------------------------------验证开始-------------------------------------------------------------------
    /**
       * @Description  ：链路名称校验： 不能为空
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    linkNameCheck: function () {
      if (!this.linkCheck.textNullCheck(this.form.linkName)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('LinkConfigurationManagement.linkName'))

        return false
      }
      if (!this.textLengthCheck(this.form.linkName)) { return false }
      return true
    },
    /**
       * @Description  : startLinkNameCheck
       * @author       : ls
       * @date         : 2020/9/19 12:11
       * @param        :
       * @return       :
       */
    startLinkNameCheck: function () {
      if (!this.linkCheck.textNullCheck(this.form.startDeviceKey)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('LinkConfigurationManagement.startDeviceName'))
        return false
      }
      if (!this.textLengthCheck(this.form.startDeviceKey)) { return false }
      return true
    },
    /**
       * @Description  : endLinkNameCheck
       * @author       : ls
       * @date         : 2020/9/19 12:11
       * @param        :
       * @return       :
       */
    endLinkNameCheck: function () {
      if (!this.linkCheck.textNullCheck(this.form.endDeviceKey)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('LinkConfigurationManagement.endDeviceName'))
        return false
      }
      if (!this.textLengthCheck(this.form.endDeviceKey)) { return false }
      return true
    },

    /**
       * @Description  ：端口名称校验： 不能为空，应数字
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    startPortNameCheck: function () {
      if (!this.linkCheck.textNullCheck(this.form.startPortName)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('LinkConfigurationManagement.startDevicePort'))
        return false
      }
      // else if( parseInt(this.form.startPortName)>99|| parseInt(this.form.startPortName)<0){
      //   this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.START_DEVICE_PORT_BETWEEN_0_99))
      //
      //   return false
      // }
      // else if (parseInt(this.form.startPortName) > this.devicePort1) {
      //   // this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.START_DEVICE_PORT_NUM)+this.devicePort1[0].portNum)
      //
      //   this.$confirm(this.$t('LinkConfigurationManagement.startPort') + this.devicePort1, this.$t('Performance.tip'), {
      //     confirmButtonText: this.$t('common.ok'),
      //     type: 'warning'
      //   })
      //
      //   return false
      // }
      // else {
      //   if (!this.textLengthCheck(this.form.startPortName)) { return false }
      //   if (!this.numCheck(this.form.startPortName)) { return false }
      // }
      return true
    },
    endPortNameCheck: function () {
      if (!this.linkCheck.textNullCheck(this.form.endPortName)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('LinkConfigurationManagement.endDevicePort'))

        return false
      }
      // else if (parseInt(this.form.endPortName) > this.devicePort2) {
      //   // this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.END_DEVICE_PORT_NUM))
      //
      //   this.$confirm(this.$t('LinkConfigurationManagement.endPort') + this.devicePort2, this.$t('Performance.tip'), {
      //     confirmButtonText: this.$t('common.ok'),
      //     type: 'warning'
      //   })
      //   return false
      // }
      // else if( parseInt(this.form.endPortName)>99|| parseInt(this.form.endPortName)<0){
      //   this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.END_DEVICE_PORT_BETWEEN_0_99))
      //
      //   return false
      // }
      // if (!this.textLengthCheck(this.form.endPortName)) { return false }
      // if (!this.numCheck(this.form.endPortName)) { return false }
      return true
    },
    /**
       * @Description  : startEndDeviceNameCheck
       * @author       : ls
       * @date         : 2020/9/19 16:35
       * @param        :
       * @return       :
       */
    startEndDeviceNameCheck: function () {
      if (this.form.startDeviceKey === this.form.endDeviceKey) {
        var obj = {}
        obj.message = '起点网元和终点网元不能相同!'
        this.InfoTip.warningTip(this, obj)
        return false
      }
      return true
    },
    /**
       * @Description  ：校验 限制长度不能大于2位  <99
       * @author       : bjh
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/18 11:12
       */
    portNumCheck: function (portName) {
      if (portName.length < 2) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PORT_ENTERED_MUST_BE_LESS_THAN_99))
      }
    },

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

        return false
      }
      return true
    },

    /**
       * @Description  ：输入数字
       * @author       : bjh
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/1/15 16:09
       */
    numCheck: function (checkNumber) {
      if (!this.linkCheck.numberCheck(checkNumber)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_ENTER_VALID_NUMBER))
        return false
      }
      return true
    },

    totalCheck () {
      if (!this.linkNameCheck()) {
        return false
      }
      if (!this.startLinkNameCheck()) {
        return false
      }
      if (!this.startPortNameCheck()) {
        return false
      }
      if (!this.endLinkNameCheck()) {
        return false
      }
      if (!this.endPortNameCheck()) {
        return false
      }
      if (!this.startEndDeviceNameCheck()) {
        return false
      }
      return true
    }

    // ------------------------------------验证结束-------------------------------------------------------------------

  },

  created () {
    this.linkCheck = legitimacyCheck()
    this.getAllDeviceInit()
    this.createLinkInit()
  },
  mounted () {

  }
}
</script>
