<template>
  <!--修改对话框-->
  <el-dialog
    v-dialogDrag
    width='500px'
    :title="$t('Performance.modifyTitle')"
    @close="close"
    @opened="updatePerformanceMonitorInit"
    :visible.sync="UpdatePerformanceMonitorVisible">
    <el-form ref="form" :model="form" :inline="true">
      <el-form-item prop="performType"
                    style="margin-bottom: -10px;"
                    :label="$t('Performance.performanceEvent') + '：'"
                    :label-width="formLabelWidth">
        <el-input type="text" size='mini' :placeholder="$t('common.placeholder')"
                  :disabled="true"
                  v-model="form.deviceParam"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="nodeType" style="margin-bottom: -10px;" :label="$t('Performance.networkElementType') + '：'" :label-width="formLabelWidth">
        <el-select size='mini' :placeholder="$t('common.selectOption')"
                   :disabled="true"
                   v-model="form.deviceTypeKey"
                   style="width:200px">
          <el-option
            v-for="item in deviceTypeList"
            :key="item.deviceTypeKey"
            :label="item.deviceTypeName"
            :value="item.deviceTypeKey">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item prop="periodTime" style="margin-bottom: -10px;" :label="$t('Performance.monitoringPeriod')+'：'" :label-width="formLabelWidth">-->
<!--        <el-input type="number" size='mini' :placeholder="$t('common.placeholder')"-->
<!--                  v-model="form.period"-->
<!--                  v-on:blur="periodCheck"-->
<!--                  autocomplete="off"-->
<!--                  style="width:200px">-->
<!--          <template slot="append">min</template>-->
<!--        </el-input>-->
<!--      </el-form-item>-->
      <el-form-item prop="lowerLimit0" style="margin-bottom: -10px;" :label="$t('Performance.minorAlarmLowerLimit') + '：'" :label-width="formLabelWidth">
        <el-input type="number" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.lowerLimit0"
                  v-on:blur="lowerLimit0Check"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="upperLimit0" style="margin-bottom: -10px;" :label="$t('Performance.minorAlarmUpperLimit') + '：'":label-width="formLabelWidth">
        <el-input type="number" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.upperLimit0"
                  v-on:blur="upperLimit0Check"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="lowerLimit1" style="margin-bottom: -10px;" :label="$t('Performance.majorAlarmLowerLimit') + '：'" :label-width="formLabelWidth">
        <el-input type="number" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.lowerLimit1"
                  v-on:blur="lowerLimit1Check"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="upperLimit1" style="margin-bottom: -10px;" :label="$t('Performance.majorAlarmUpperLimit') + '：'" :label-width="formLabelWidth">
        <el-input type="number" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.upperLimit1"
                  v-on:blur="upperLimit1Check"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="lowerLimit2" style="margin-bottom: -10px;" :label="$t('Performance.criticalAlarmLowerLimit') + '：'" :label-width="formLabelWidth">
        <el-input type="number" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.lowerLimit2"
                  v-on:blur="lowerLimit2Check"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="upperLimit2" style="margin-bottom: -10px;" :label="$t('Performance.criticalAlarmUpperLimit') + '：'" :label-width="formLabelWidth">
        <el-input type="number" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.upperLimit2"
                  v-on:blur="upperLimit2Check"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' icon="el-icon-circle-check" type="primary" @click="updatePerformanceMonitor">
        {{$t("common.updateBtn")}}
      </el-button>
      <el-button size='mini' icon="el-icon-circle-close" @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>
</template>

<script>

import legitimacyCheck from '../../../utils/legitimacyCheck'
import HelperUtil from '../../../utils/HelperUtil'
import PerformanceMonitorService from '../../../domain/services/PerformanceMonitorService'
import DeviceService from "../../../domain/services/DeviceService";

export default {
  props: ['command'],

  data: function () {
    return {

      initList: this.command.target,
      UpdatePerformanceMonitorVisible: false,
      formLabelWidth: '150px',

      // 弹出框双向绑定
      form: {
        deviceTypeKey: '',
        deviceParam: '',
        deviceModel: '',
        lowerLimit0: '',
        upperLimit0: '',
        lowerLimit1: '',
        upperLimit1: '',
        lowerLimit2: '',
        upperLimit2: '',
        unit: '',
        peformTypeName: '',
        deviceType: '',
        hardCodeKey: ''
      },

      deviceTypeList: [],

      updateList: [],
      // 数据加载过程中，覆盖界面
      fullscreenLoading: false
    }
  },

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

    /**
     * @Description  : 获取网元类型
     * @author       : ls
     * @date         : 2020/5/22 18:25
     * @param        :
     * @return       :
     */
    getAllDeviceType: function () {
      let _this = this
      DeviceService.getAllDeviceTypeCollection().then(result => {
        _this.deviceTypeList = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },

    // ------------------------------------修改开始-------------------------------------------------------------------
    updatePerformanceMonitorInit () {
      this.form.deviceTypeKey = this.initList.deviceTypeKey
      this.form.deviceModel = this.initList.deviceModel
      this.form.deviceParam = this.initList.performTypeName
      this.form.lowerLimit0 = this.initList.lowerLimit0 === -999 ? '' : this.initList.lowerLimit0
      this.form.upperLimit0 = this.initList.upperLimit0 === -999 ? '' : this.initList.upperLimit0
      this.form.lowerLimit1 = this.initList.lowerLimit1 === -999 ? '' : this.initList.lowerLimit1
      this.form.upperLimit1 = this.initList.upperLimit1 === -999 ? '' : this.initList.upperLimit1
      this.form.lowerLimit2 = this.initList.lowerLimit2 === -999 ? '' : this.initList.lowerLimit2
      this.form.upperLimit2 = this.initList.upperLimit2 === -999 ? '' : this.initList.upperLimit2
      this.form.unit = this.initList.unit
      this.form.peformTypeName = this.initList.peformTypeName
      this.form.deviceType = this.initList.deviceType
    },

    /**
     * @Description  : 修改
     * @author       : ls
     * @date         : 2020/5/22 18:20
     * @param        :
     * @return       :
     */
    updatePerformanceMonitor () {
      // if (this.limitCheck() && this.periodCheck()) {
      if (this.limitCheck()) {
        let formRow = {}
        formRow.deviceTypeKey = this.form.deviceTypeKey
        formRow.deviceParam = this.initList.deviceParam
        formRow.deviceModel = this.form.deviceModel
        formRow.lowerLimit0 = this.form.lowerLimit0 === '' ? -999 : this.form.lowerLimit0
        formRow.upperLimit0 = this.form.upperLimit0 === '' ? -999 : this.form.upperLimit0
        formRow.lowerLimit1 = this.form.lowerLimit1 === '' ? -999 : this.form.lowerLimit1
        formRow.upperLimit1 = this.form.upperLimit1 === '' ? -999 : this.form.upperLimit1
        formRow.lowerLimit2 = this.form.lowerLimit2 === '' ? -999 : this.form.lowerLimit2
        formRow.upperLimit2 = this.form.upperLimit2 === '' ? -999 : this.form.upperLimit2
        formRow.unit = this.form.unit
        formRow.peformTypeName = this.form.peformTypeName
        formRow.deviceType = this.form.deviceType
        formRow.hardcodeKey = this.initList.hardcodeKey

        // --------------------------发送请求+命令关闭，修改参数 formRow ---------------------------------
        this.updateList = []
        this.updateList.push(formRow)
        console.log(this.updateList)
        let _this = this
        let loadingInstance = _this.Loading.openLoading()
        PerformanceMonitorService.modifyPerformanceMonitor(this.updateList).then(result => {
          // 成功
          _this.Loading.closeLoading(loadingInstance)
          _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.MODIFY_CODE))
          _this.close()
        }).catch(err => {
          // 失败
          _this.Loading.closeLoading(loadingInstance)
          _this.InfoTip.errorTip(_this, err)
        })
      } else {
        if (!this.limitCheck()) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LIMITS_SET_CODE))
        } else {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PERIOD_AND_LIMITS_SET_CODE))
        }
      }
    },

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

    /***
       * @Description  : 监测周期合法性
       * @author       : zf
       * @date         : 2019/02/26 10:28
       */
    periodCheck: function () {
      if (this.form.period === null || this.form.period === '' || parseInt(this.form.period) <= 0) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PERIOD_SET_CODE))
        return false
      } else {
        return true
      }
    },

    /***
       * @Description  : 一级下限合法性
       * @author       : zf
       * @date         : 2019/02/26 14:39
       */
    lowerLimit0Check: function () {
      return true
    },

    /***
       * @Description  : 一级上限合法性
       * @author       : zf
       * @date         : 2019/02/26 14:39
       */
    upperLimit0Check: function () {
      if (this.form.lowerLimit1 !== '' && this.form.upperLimit0 !== '') {
        this.form.lowerLimit1 = this.form.upperLimit0
      }
    },

    /***
       * @Description  : 二级下限合法性
       * @author       : zf
       * @date         : 2019/02/26 14:39
       */
    lowerLimit1Check: function () {
      if (this.form.upperLimit0 !== '' && this.form.lowerLimit1 !== '') {
        this.form.upperLimit0 = this.form.lowerLimit1
      }
    },

    /***
       * @Description  : 二级上限合法性
       * @author       : zf
       * @date         : 2019/02/26 14:39
       */
    upperLimit1Check: function () {
      if (this.form.lowerLimit2 && this.form.upperLimit1 !== '') {
        this.form.lowerLimit2 = this.form.upperLimit1
      }
    },

    /***
       * @Description  : 三级下限合法性
       * @author       : zf
       * @date         : 2019/02/26 14:39
       */
    lowerLimit2Check: function () {
      if (this.form.upperLimit1 !== '' && this.form.lowerLimit2 !== '') {
        this.form.upperLimit1 = this.form.lowerLimit2
      }
    },

    /***
       * @Description  : 三级上限合法性
       * @author       : zf
       * @date         : 2019/02/26 14:39
       */
    upperLimit2Check: function () {
      return true
    },

    /***
      * @Description  : 校验三组上下线是否符合规范
      * @author       : zf
      * @date         : 2019/05/20 10:56
      */
    limitCheck: function () {
      // 当前表单中门限值
      let limits = []
      if (this.form.lowerLimit0 !== '') {
        limits[0] = parseFloat(this.form.lowerLimit0)
      } else {
        limits[0] = this.form.lowerLimit0
      }
      if (this.form.upperLimit0 !== '') {
        limits[1] = parseFloat(this.form.upperLimit0)
      } else {
        limits[1] = this.form.upperLimit0
      }
      if (this.form.lowerLimit1 !== '') {
        limits[2] = parseFloat(this.form.lowerLimit1)
      } else {
        limits[2] = this.form.lowerLimit1
      }
      if (this.form.upperLimit1 !== '') {
        limits[3] = parseFloat(this.form.upperLimit1)
      } else {
        limits[3] = this.form.upperLimit1
      }
      if (this.form.lowerLimit2 !== '') {
        limits[4] = parseFloat(this.form.lowerLimit2)
      } else {
        limits[4] = this.form.lowerLimit2
      }
      if (this.form.upperLimit2 !== '') {
        limits[5] = parseFloat(this.form.upperLimit2)
      } else {
        limits[5] = this.form.upperLimit2
      }
      let nonNullCount = 0
      let limitsPos = []
      for (let i = 0; i < 6; i++) {
        if (limits[i] !== '') {
          nonNullCount++
          limitsPos.push(i)
        }
      }

      // 表单门限合法性判断
      if (nonNullCount === 0 || nonNullCount === 1) { // 未设门限或仅设置一级门限
        return true
      } else if (nonNullCount === 2) {
        if (limitsPos[1] - limitsPos[0] === 1) { // 设置两个门限值且连续
          if (limitsPos[0] === 0 || limitsPos[0] === 2 || limitsPos[0] === 4) {
            return true
          } else {
            if (limits[limitsPos[0]] === limits[limitsPos[1]]) { // 设置两个门限值相等
              return true
            } else {
              return false
            }
          }
        } else { // 设置两个门限值不连续
          return false
        }
      } else if (nonNullCount === 3) {
        if (limitsPos[2] - limitsPos[0] === 2) {
          if (limitsPos[0] === 0 || limitsPos[0] === 2) {
            if (limits[limitsPos[0]] !== limits[limitsPos[1]] && limits[limitsPos[1]] === limits[limitsPos[2]]) {
              return true
            } else {
              return false
            }
          } else if (limitsPos[0] === 1 || limitsPos[0] === 3) {
            if (limits[limitsPos[0]] === limits[limitsPos[1]] && limits[limitsPos[1]] !== limits[limitsPos[2]]) {
              return true
            } else {
              return false
            }
          } else {
            return false
          }
        } else {
          return false
        }
      } else if (nonNullCount === 4) {
        if (limitsPos[3] - limitsPos[0] === 3) { // 设置门限连续
          if (limitsPos[0] === 1 && limitsPos[3] === 4) { // 设置中间四个门限
            if (limits[1] === limits[2] && limits[3] === limits[4]) { // 一级上限等于二级下限且二级上限等于三级下限
              if (limits[1] < limits[3]) { // 门限递进
                return true
              } else if (limits[1] > limits[3]) { // 门限递进
                return true
              } else {
                return false
              }
            } else { // 门限上下限不等
              return false
            }
          } else if (limitsPos[0] === 0) { // 门限开始从一级开始
            if (limits[1] === limits[2]) {
              if (limits[0] < limits[1] && limits[1] < limits[3]) {
                return true
              } else if (limits[0] > limits[1] && limits[1] > limits[3]) {
                return true
              } else {
                return false
              }
            } else {
              return false
            }
          } else if (limitsPos[3] === 5) {
            if (limits[3] === limits[4]) {
              if (limits[2] < limits[3] && limits[3] < limits[5]) {
                return true
              } else if (limits[2] > limits[3] && limits[3] > limits[5]) {
                return true
              } else {
                return false
              }
            } else {
              return false
            }
          } else {
            return false
          }
        } else { // 门限设置不连续
          return false
        }
      } else if (nonNullCount === 5) {
        if (limitsPos[0] !== 0) {
          if (limits[1] === limits[2] && limits[3] === limits[4]) {
            if (limits[1] < limits[3] && limits[3] < limits[5]) {
              return true
            } else if (limits[1] > limits[3] && limits[3] > limits[5]) {
              return true
            } else {
              return false
            }
          } else {
            return false
          }
        } else {
          if (limitsPos[4] === 4) {
            if (limits[1] === limits[2] && limits[3] === limits[4]) {
              if (limits[0] < limits[1] && limits[1] < limits[3]) {
                return true
              } else if (limits[0] > limits[1] && limits[1] > limits[3]) {
                return true
              } else {
                return false
              }
            } else {
              return false
            }
          } else {
            return false
          }
        }
      } else if (nonNullCount === 6) {
        if ((limits[1] === limits[2]) && (limits[3] === limits[4])) {
          if (limits[0] < limits[2] && limits[2] < limits[4] && limits[4] < limits[5]) {
            return true
          } else if (limits[0] > limits[2] && limits[2] > limits[4] && limits[4] > limits[5]) {
            return true
          } else {
            return false
          }
        } else {
          return false
        }
      } else {
        return false
      }
    }

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

  },

  created () {
    if (window.localStorage.getItem('user-language') === 'en') {
      this.formLabelWidth = '220px'
    } else {
      this.formLabelWidth = '120px'
    }
    this.getAllDeviceType()
  },

  mounted () {
    this.UpdatePerformanceMonitorVisible = true
  }
}
</script>
