<template>
  <div class="targetTemperature">
    <div class="table-title">
      <img src="../../assets/img/targetTemperature.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(20000, $event)" v-model="inputData[20000]" @input="change(20000, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20001, $event)" v-model="inputData[20001]" @input="change(20001, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20003, $event)" v-model="inputData[20003]" @input="change(20003, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20002, $event)" v-model="inputData[20002]" @input="changeCountData(20002, $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(20004, $event)" v-model="inputData[20004]" @input="change(20004, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20005, $event)" v-model="inputData[20005]" @input="change(20005, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20007, $event)" v-model="inputData[20007]" @input="change(20007, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20006, $event)" v-model="inputData[20006]" @input="changeCountData(20006, $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(20008, $event)" v-model="inputData[20008]" @input="change(20008, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20009, $event)" v-model="inputData[20009]" @input="change(20009, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20011, $event)" v-model="inputData[20011]" @input="change(20011, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20010, $event)" v-model="inputData[20010]" @input="changeCountData(20010, $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(20012, $event)" v-model="inputData[20012]" @input="change(20012, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20013, $event)" v-model="inputData[20013]" @input="change(20013, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20015, $event)" v-model="inputData[20015]" @input="change(20015, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20014, $event)" v-model="inputData[20014]" @input="changeCountData(20014, $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(20016, $event)" v-model="inputData[20016]" @input="change(20016, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20017, $event)" v-model="inputData[20017]" @input="change(20017, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
         <input @blur="blurChange(20019, $event)" v-model="inputData[20019]" @input="change(20019, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20018, $event)" v-model="inputData[20018]" @input="changeCountData(20018, $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(20020, $event)" v-model="inputData[20020]" @input="change(20020, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20021, $event)" v-model="inputData[20021]" @input="change(20021, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20023, $event)" v-model="inputData[20023]" @input="change(20023, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20022, $event)" v-model="inputData[20022]" @input="changeCountData(20022, $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(20024, $event)" v-model="inputData[20024]" @input="change(20024, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20025, $event)" v-model="inputData[20025]" @input="change(20025, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20027, $event)" v-model="inputData[20027]" @input="change(20027, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20026, $event)" v-model="inputData[20026]" @input="changeCountData(20026, $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(20028, $event)" v-model="inputData[20028]" @input="change(20028, $event)"
         type="text" placeholder="0">
        <span>:</span>
        <input @blur="blurChange(20029, $event)" v-model="inputData[20029]" @input="change(20029, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>过渡时间(分钟)：</span>
        <input @blur="blurChange(20031, $event)" v-model="inputData[20031]" @input="change(20031, $event)"
         type="text" placeholder="0">
        <span></span>
      </div>
      <div class="input-main-60">
        <span>目标温度</span>
        <input @blur="blurChangeCount(20030, $event)" v-model="inputData[20030]" @input="changeCountData(20030, $event)"
         type="text" placeholder="0">
         <span>℃</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      timer: null,
      inputData: [],
      loading:null,
      pageName:'目标温度'
    }
  },
  watch: {

    '$store.state.equipmentIndex'(newVal, oldVal) {
      this.dataInit();
    },
  },

  mounted() {
    const that = this;

    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() {
      this.loading = this.$loading({
    lock: true,
    text: this.$t('message.loading'),
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
      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[20002] =this.countData(this.inputData[20002]);
          this.inputData[20006] =this.countData(this.inputData[20006]);
          this.inputData[20010] =this.countData(this.inputData[20010]);
          this.inputData[20014] =this.countData(this.inputData[20014]);
          this.inputData[20018] =this.countData(this.inputData[20018]);
          this.inputData[20022] =this.countData(this.inputData[20022]);
          this.inputData[20026] =this.countData(this.inputData[20026]);
          this.inputData[20030] =this.countData(this.inputData[20030]);
        }
      })
    },
    //全选文本
    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>
  
  