<template>
  <!--修改对话框-->
  <el-dialog
    v-dialogDrag
    style="font-size: 10px; "
    width='500px'
    :title="$t('Performance.modifyTitle')"
    @close="close"
    @opened="updatePerformanceMonitorInit"
    v-if="UpdatePerformanceMonitorVisible"
    :visible.sync="UpdatePerformanceMonitorVisible">
    <el-form ref="form" :label-position="labelPosition" :model="form" :inline="true" :rules="rules">
      <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.deviceTypeKey"
            :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-item prop="enable" style="margin-bottom: -10px;" :label="$t('Performance.enablementOption') + ':'" :label-width="formLabelWidth">
        <el-checkbox v-model="form.enableChecked"
                     style="width:200px; padding-right: 150px">{{$t('Performance.enable')}}
        </el-checkbox>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" @click="updatePerformanceMonitor">
        {{$t("common.updateBtn")}}
      </el-button>
      <el-button size='mini' @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'

  export default {
    props: ['command'],

    data: function () {
      return {

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

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

        deviceTypeList: [],

        updateList:[],

        //表单输入检测
        rules: {},

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

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

      /**
       * @Description  :获取全部网元类型
       * @author       :zyh
       * @date         :2020/1/7
       */
      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.deviceParam;
        this.form.period = this.initList.period;
        this.form.status = this.initList.status;
        this.form.lowerLimit0 = this.initList.lowerLimit0;
        this.form.upperLimit0 = this.initList.upperLimit0;
        this.form.lowerLimit1 = this.initList.lowerLimit1;
        this.form.upperLimit1 = this.initList.upperLimit1;
        this.form.lowerLimit2 = this.initList.lowerLimit2;
        this.form.upperLimit2 = this.initList.upperLimit2;
        this.form.unit = this.initList.unit;
        this.form.peformTypeName = this.initList.peformTypeName;
        this.form.deviceType = this.initList.deviceType;
        this.form.enableChecked = this.initList.enableChecked
      },

      /***
       * @Description  : 修改性能保存
       * @author       : zf
       * @date         : 2019/02/28 14:34
       */
      updatePerformanceMonitor() {
        if (this.limitCheck() && this.periodCheck()) {
          let formRow = {};
          formRow.deviceTypeKey = this.form.deviceTypeKey;
          formRow.deviceParam = this.form.deviceParam;
          formRow.deviceModel = this.form.deviceModel;
          formRow.period = this.form.period;
          if (this.form.enableChecked === true) {
            this.form.status = 0
          } else {
            this.form.status = 1
          }
          formRow.status = this.form.status;
          formRow.lowerLimit0 = this.form.lowerLimit0;
          formRow.upperLimit0 = this.form.upperLimit0;
          formRow.lowerLimit1 = this.form.lowerLimit1;
          formRow.upperLimit1 = this.form.upperLimit1;
          formRow.lowerLimit2 = this.form.lowerLimit2;
          formRow.upperLimit2 = this.form.upperLimit2;
          formRow.unit = this.form.unit;
          formRow.peformTypeName = this.form.peformTypeName;
          formRow.deviceType = this.form.deviceType;

          //--------------------------发送请求+命令关闭,修改参数 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.periodCheck()) {
            this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LIMITS_SET_CODE))
          }
          else if (this.limitCheck() && !this.periodCheck()) {
            this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PERIOD_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>