<template>
    <div class="targetHumidity">
        <div class="table-title">
      <img src="../../assets/img/targetHumidity.png" alt="">{{pageName}}
    </div>
    <div class="title-tips-bold">
      1#目标湿度参数配置
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-60">
        <span>1#时段起始时间：</span>
        <input @blur="blurChange(20032, $event)" v-model="inputData[20032]" @input="change(20032, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20033, $event)" v-model="inputData[20033]" @input="change(20033, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20035, $event)" v-model="inputData[20035]" @input="change(20035, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20034, $event)" v-model="inputData[20034]" @input="changeCount10(20034, $event)"
         type="text" placeholder="0">
        <span>%</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-60">
        <span>2#时段起始时间：</span>
        <input @blur="blurChange(20036, $event)" v-model="inputData[20036]" @input="change(20036, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20037, $event)" v-model="inputData[20037]" @input="change(20037, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20039, $event)" v-model="inputData[20039]" @input="change(20039, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20038, $event)" v-model="inputData[20038]" @input="changeCount10(20038, $event)"
         type="text" placeholder="0">
         <span>%</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-60">
        <span>3#时段起始时间：</span>
        <input @blur="blurChange(20040, $event)" v-model="inputData[20040]" @input="change(20040, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20041, $event)" v-model="inputData[20041]" @input="change(20041, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20043, $event)" v-model="inputData[20043]" @input="change(20043, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20042, $event)" v-model="inputData[20042]" @input="changeCount10(20042, $event)"
         type="text" placeholder="0">
         <span>%</span>
      </div>
    </div>
    <div class="flex-view shrink-none padding-none">
      <div class="input-main-60">
        <span>4#时段起始时间：</span>
        <input @blur="blurChange(20044, $event)" v-model="inputData[20044]" @input="change(20044, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20045, $event)" v-model="inputData[20045]" @input="change(20045, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20047, $event)" v-model="inputData[20047]" @input="change(20047, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20046, $event)" v-model="inputData[20046]" @input="changeCount10(20046, $event)"
         type="text" placeholder="0">
         <span>%</span>
      </div>
    </div>

    <div class="title-tips-bold">
      2#目标湿度参数配置
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-60">
        <span>1#时段起始时间：</span>
        <input @blur="blurChange(20048, $event)" v-model="inputData[20048]" @input="change(20048, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20049, $event)" v-model="inputData[20049]" @input="change(20049, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20051, $event)" v-model="inputData[20051]" @input="change(20051, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20050, $event)" v-model="inputData[20050]" @input="changeCount10(20050, $event)"
         type="text" placeholder="0">
         <span>%</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-60">
        <span>2#时段起始时间：</span>
        <input @blur="blurChange(20052, $event)" v-model="inputData[20052]" @input="change(20052, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20053, $event)" v-model="inputData[20053]" @input="change(20053, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20055, $event)" v-model="inputData[20055]" @input="change(20055, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20054, $event)" v-model="inputData[20054]" @input="changeCount10(20054, $event)"
         type="text" placeholder="0">
         <span>%</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-60">
        <span>3#时段起始时间：</span>
        <input @blur="blurChange(20056, $event)" v-model="inputData[20056]" @input="change(20056, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20057, $event)" v-model="inputData[20057]" @input="change(20057, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20059, $event)" v-model="inputData[20059]" @input="change(20059, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20058, $event)" v-model="inputData[20058]" @input="changeCount10(20058, $event)"
         type="text" placeholder="0">
         <span>%</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-60">
        <span>4#时段起始时间：</span>
        <input @blur="blurChange(20060, $event)" v-model="inputData[20060]" @input="change(20060, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20061, $event)" v-model="inputData[20061]" @input="change(20061, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20063, $event)" v-model="inputData[20063]" @input="change(20063, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标湿度</span>
        <input @blur="blurChange10(20062, $event)" v-model="inputData[20062]" @input="changeCount10(20062, $event)"
         type="text" placeholder="0">
         <span>%</span>
      </div>
    </div>
    </div>
  </template>
  <script>
  export default {
    data(){
        return {
          inputData:[],
      loading:null,
      pageName:'目标湿度'
        }
    },
 
  mounted() {
    const that = this;
    this.loading = this.$loading({
    lock: true,
    text: this.$t('message.loading'),
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
    setTimeout(() => {
      that.dataInit();
    }, 0);
    //input获取焦点后全选
    let inputList = document.querySelectorAll('input');
    for (let index = 0; index < inputList.length; index++) {
      inputList[index].onfocus = this.selectValue;//input放入焦点，全选文本
    }
    if(this.$route.query.name){
      this.pageName=this.$route.query.name
    }
  },
  methods: {
    //失去焦点 计算公式 x/10
    blurChange10(code, el) {
      var store = this.$store.state
      var data = {
        equipmentId: store.equipmentList[store.equipmentIndex - 1].deviceId,
        regAddress: code,
        num: el.target.value * 10,
      }; //避免开启多个计时器
      if (store.nowInput != el.target.value) {
                this.changeData(data);
            }
            // if (this.timer) {
            //     this.timer && clearInterval(this.timer);
            //     this.changeData(data);
            // }

    },
    //反向计算 计算公式 x/10
    changeCount10(code, el) {
      var data = el.target.value * 10
      var sendData = {
        target: {
          value: data
        }
      }
      this.change(code, sendData)
    },
    // 计算展示值 计算公式 x/10
    countData10(data) {
      if (data) {
        return data / 10
      } else {
        return 0
      }
    },
    // 计算展示值 计算公式 (x - 400) / 10
    countData(data) {
      if (data) {
        return (data - 400) / 10
      } else {
        return 0
      }
    },
    //失去焦点
    blurChangeCount(code, el) {
      var store = this.$store.state
      var data = {
        equipmentId: store.equipmentList[store.equipmentIndex - 1].deviceId,
        regAddress: code,
        num: (el.target.value * 10) + 400,
      }; //避免开启多个计时器
      if (store.nowInput != el.target.value) {
                this.changeData(data);
            }
            // if (this.timer) {
            //     this.timer && clearInterval(this.timer);
            //     this.changeData(data);
            // }
    },
    changeCountData(code, el) {
      var data = (el.target.value * 10) + 400
      var sendData = {
        target: {
          value: data
        }
      }
      this.change(code, sendData)
    },
    dataInit() {
      var store = this.$store.state
      var data = {
        deviceId: store.equipmentList[store.equipmentIndex - 1].deviceId,
      };
      this.api.targetValue(data).then(res => {
        this.loading.close()
        if (res.data.code == 200) {
          this.inputData = res.data.data;
          this.inputData[20034] =this.countData10(this.inputData[20034]);
          this.inputData[20038] =this.countData10(this.inputData[20038]);
          this.inputData[20042] =this.countData10(this.inputData[20042]);
          this.inputData[20046] =this.countData10(this.inputData[20046]);
          this.inputData[20050] =this.countData10(this.inputData[20050]);
          this.inputData[20054] =this.countData10(this.inputData[20054]);
          this.inputData[20058] =this.countData10(this.inputData[20058]);
          this.inputData[20062] =this.countData10(this.inputData[20062]);
        }
      })
    },
    //全选文本
    selectValue(e) {
      var store = this.$store.state
      store.nowInput = e.target.value
      e.currentTarget.select();
    },
    //失去焦点
    blurChange(code, el) {
      var store = this.$store.state
      var data = {
        equipmentId: store.equipmentList[store.equipmentIndex - 1].deviceId,
        regAddress: code,
        num: el.target.value,
      }; //避免开启多个计时器
      if (store.nowInput != el.target.value) {
                this.changeData(data);
            }
            // if (this.timer) {
            //     this.timer && clearInterval(this.timer);
            //     this.changeData(data);
            // }

    },
    //修改数据的函数
    changeBtn(code, el) {
      var store = this.$store.state
      var data = {
        equipmentId: store.equipmentList[store.equipmentIndex - 1].deviceId,
        regAddress: code,
        num: el.target.value,
      };
      this.delayTimerBtn(0, data);
    },
    //切换btn的限制 写入停止后j*100毫秒调取函数
    delayTimerBtn(i, data) {
      const that = this;
      //整体接口
      let j = this.$store.state.lateSendBtn;
      //避免开启多个计时器
      this.timer && clearInterval(this.timer);

      this.timer = setInterval(() => {
        ++i;
        if (i == j) {
          that.changeData(data);
          clearInterval(this.timer);
        }
      }, 100);
    },
    change(code, el) {
      var store = this.$store.state
      var data = {
        equipmentId: store.equipmentList[store.equipmentIndex - 1].deviceId,
        regAddress: code,
        num: el.target.value,
      };
      this.delayTimer(0, data);
    },
    //限制 写入停止后j*100毫秒调取函数
    delayTimer(i, data) {
      // const that = this;
      // //整体接口
      // let j = this.$store.state.lateSend;
      // //避免开启多个计时器
      // this.timer && clearInterval(this.timer);

      // this.timer = setInterval(() => {
      //   ++i;
      //   if (i == j) {
      //     that.changeData(data);
      //     clearInterval(this.timer);
      //   }
      // }, 100);
    },
    changeData(data) {
      var store = this.$store.state
      this.api.postControlWrite(data).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          store.nowInput = ''
          // this.dataInit();
        } else {
          this.inputData[data.regAddress] = store.nowInput
          this.$message.error(res.data.msg);
        }
        this.timer = null
      });
    },
  }
  
  }
  </script>
  <style lang="scss">
  
  </style>
  
  