<template>
    <div class="targetCo2">
      <div class="table-title">
      <img src="../../assets/img/targetCo2.png" alt="">{{ pageName }}
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-80">
        <span>1#时段起始时间：</span>
        <input @blur="blurChange(20064, $event)" v-model="inputData[20064]" @input="change(20064, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20065, $event)" v-model="inputData[20065]" @input="change(20065, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20067, $event)" v-model="inputData[20067]" @input="change(20067, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>目标CO2</span>
        <input @blur="blurChange(20066, $event)" v-model="inputData[20066]" @input="change(20066, $event)"
         type="text" placeholder="0">
        <span>ppm</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-80">
        <span>2#时段起始时间：</span>
        <input @blur="blurChange(20068, $event)" v-model="inputData[20068]" @input="change(20068, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20069, $event)" v-model="inputData[20069]" @input="change(20069, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20071, $event)" v-model="inputData[20071]" @input="change(20071, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>目标CO2</span>
        <input @blur="blurChange(20070, $event)" v-model="inputData[20070]" @input="change(20070, $event)"
         type="text" placeholder="0">
        <span>ppm</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-80">
        <span>3#时段起始时间：</span>
        <input @blur="blurChange(20072, $event)" v-model="inputData[20072]" @input="change(20072, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20073, $event)" v-model="inputData[20073]" @input="change(20073, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20075, $event)" v-model="inputData[20075]" @input="change(20075, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>目标CO2</span>
        <input @blur="blurChange(20074, $event)" v-model="inputData[20074]" @input="change(20074, $event)"
         type="text" placeholder="0">
        <span>ppm</span>
      </div>
    </div>
    <div class="flex-view border-none shrink-none padding-none">
      <div class="input-main-80">
        <span>4#时段起始时间：</span>
        <input @blur="blurChange(20076, $event)" v-model="inputData[20076]" @input="change(20076, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20077, $event)" v-model="inputData[20077]" @input="change(20077, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20079, $event)" v-model="inputData[20079]" @input="change(20079, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-80">
        <span>目标CO2</span>
        <input @blur="blurChange(20078, $event)" v-model="inputData[20078]" @input="change(20078, $event)"
         type="text" placeholder="0">
        <span>ppm</span>
      </div>
    </div>

    </div>
  </template>
  <script>
  export default {
    data(){
        return {
          inputData:[],
      loading:null,
      pageName:'目标CO2'
        }
    },
  
  mounted() {
    const that = this;
    this.loading = this.$loading({
    lock: true,
    text: '加载中',
    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;

        }
      })
    },
    //全选文本
    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>
  
  