<template>
  <div class="waterPump wufang">
    <div class="table-title">
      <img src="../../assets/img/uptake.png" alt="" /> 上风口
    </div>
    <div class="title-tips">上风口有效性设置
            <div class="input-btn">
                <div class="btn btn-tips"  @click="changeOpen(0)" :class="openIndex == 0 ? 'blue' : 'green'">
                    {{ openIndex == 0 ? '无效' : '有效' }}
                </div>
            </div>
        </div>
    <div class="flex-view">
      <div class="input-btn">
        <div
          class="btn"
          @click="changeOpen(1)"
          :class="openIndex == 1 ? 'blue' : 'green'"
        >
          <img src="../../assets/image/irrigateSet2.png" alt="" />自动运行
        </div>
        <div
          class="btn"
          @click="changeOpen(2)"
          :class="openIndex == 2 ? 'blue' : 'green'"
        >
          <img src="../../assets/image/irrigateSet3.png" alt="" />手动打开
        </div>
        <div
          class="btn"
          @click="changeOpen(3)"
          :class="openIndex == 3 ? 'blue' : 'green'"
        >
          <img src="../../assets/image/irrigateSet1.png" alt="" />手动关闭
        </div>
        <div
          class="btn"
          @click="changeOpen(4)"
          :class="openIndex == 4 ? 'blue' : 'green'"
        >
          <img src="../../assets/image/irrigateSet1.png" alt="" />停止
        </div>
      </div>
    </div>
    <div class="flex-view">
      <div class="input-main border-right">
        <span>室内1#平均温度达到</span>
        <input
          @blur="blurChangeCount('21532', $event)"
          v-model="inputData['21532']"
          @input="changeCountData('21532', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>℃启动</span>
      </div>
      <div class="input-main border-right">
        <span>滞回带</span>
        <input
          @blur="blurChange10('21533', $event)"
          v-model="inputData['21533']"
          @input="changeCount10('21533', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>℃</span>
      </div>
      <div class="input-main border-right">
        <span>分</span>
        <input
          @blur="blurChange('21534', $event)"
          v-model="inputData['21534']"
          @input="change('21534', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>步打开</span>
      </div>
      <div class="input-main">
        <span>分步打开间隔时间</span>
        <input
          @blur="blurChange('21537', $event)"
          v-model="inputData['21537']"
          @input="change('21537', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>分</span>
      </div>
    </div>
    <div class="flex-view">
      <div class="flex-sel border-right" @click="changeOpenTrue(0)">
        <div v-if="openTrue[0] == 0" class="no-sel"></div>
        <div v-else class="sel"></div>
        计算的最大开度
      </div>
      <div class="flex-sel border-right" @click="changeOpenTrue(1)">
        <div v-if="openTrue[1] == 0" class="no-sel"></div>
        <div v-else class="sel"></div>
        大风保护有效
      </div>
      <div class="flex-sel border-right" @click="changeOpenTrue(2)">
        <div v-if="openTrue[2] == 0" class="no-sel"></div>
        <div v-else class="sel"></div>
        下雨保护
      </div>
      <div class="flex-sel border-right" @click="changeOpenTrue(3)">
        <div v-if="openTrue[3] == 0" class="no-sel"></div>
        <div v-else class="sel"></div>
        有雨量桶检测
      </div>
      <div class="input-main border-right">
        <span>最大开度</span>
        <span>{{ statusNum }}</span>
        <span>%</span>
      </div>
      <div class="input-main">
        <span>开度系数</span>
        <input
          @blur="blurChange('21536', $event)"
          v-model="inputData['21536']"
          @input="change('21536', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
      </div>
    </div>
    <div class="flex-view">
      <div class="input-main">
        <div>上风口的行程时间</div>
        <input
          @blur="blurChange('21538', $event)"
          v-model="inputData['21538']"
          @input="change('21538', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>秒</span>
      </div>
      <div class="input-main">
        <div>打开的通道</div>
        <input
          @blur="blurChange('21539', $event)"
          v-model="inputData['21539']"
          @input="change('21539', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span></span>
      </div>
      <div class="input-main">
        <div>关闭的通道</div>
        <input
          @blur="blurChange('21540', $event)"
          v-model="inputData['21540']"
          @input="change('21540', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span></span>
      </div>
    </div>
    <div class="flex-view border-none">
      <div class="input-main border-right">
        <span>上风口风速大于多少</span>
        <input
          @blur="blurChange10('21562', $event)"
          v-model="inputData['21562']"
          @input="changeCount10('21562', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>m/s,禁止开窗</span>
      </div>
      <div class="input-main border-right">
        <span>大风保护时间</span>
        <input
          @blur="blurChange('21563', $event)"
          v-model="inputData['21563']"
          @input="change('21563', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>分</span>
      </div>
      <div class="input-main">
        <span>下雨检测时间</span>
        <input
          @blur="blurChange('21564', $event)"
          v-model="inputData['21564']"
          @input="change('21564', $event)"
          type="text"
          value="60"
          placeholder="60"
        />
        <span>分</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      openIndex: 1,
      inputData: {},
      openTrue: [],
      statusNum: 0,
      statusInterval: null,
    };
  },
  //如果接口还未调通 跳转到页面后 监听到数据变化就触发数据读取
  watch: {
    "$store.state.ControlData"(newVal) {
      this.dataInit();
    },
  },
  // 当前页面关闭前将数据存入vuex
  beforeDestroy() {
    var store = this.$store.state;
    this.inputData["21530"] = this.openIndex;
    var num = this.myReverse(this.openTrue).join("");
    this.inputData["21531"] = parseInt(num, 2);
    store.ControlData = this.inputData;
  },
  beforeDestroy() {
    clearInterval(this.statusInterval) && this.statusInterval;
  },
  mounted() {
    var store = this.$store.state;
    // this.dataInit();
    if (store.ControlData) {
      this.dataInit();
    }
    //input获取焦点后全选
    let inputList = document.querySelectorAll("input");
    for (let index = 0; index < inputList.length; index++) {
      inputList[index].onfocus = this.selectValue; //input放入焦点，全选文本
    }
  },
  methods: {
    //反转数组 不影响原数组
    myReverse(arr) {
      return [...arr].reverse();
    },
    changeOpenTrue(index) {
      if (this.openTrue[index] == 0) {
        this.openTrue[index] = 1;
      } else {
        this.openTrue[index] = 0;
      }
      this.$forceUpdate();
      var num = this.myReverse(this.openTrue).join("");

      var dataNum = {
        target: {
          value: parseInt(num, 2),
        },
      };
      this.changeBtn("21531", dataNum);
    },
    get21531() {
      var num1 = this.inputData["21531"];
      var num11 = this.padString(num1.toString(2), 4);
      var openList1New;
      openList1New = num11.split("");
      this.openTrue = openList1New.reverse();
    },
    padString(str, length) {
      return str.padStart(length, "0");
    },
    changeOpen(index) {
      this.openIndex = index;
      var dataNum = {
        target: {
          value: this.openIndex,
        },
      };
      this.changeBtn(21530, dataNum);
    },
    //失去焦点 计算公式 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);
    },
    //失去焦点
    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);
    },
    countData(data) {
      if (data) {
        return (data - 400) / 10;
      } else {
        return 0;
      }
    },
    // 计算展示值 计算公式 x/10
    countData10(data) {
      if (data) {
        return data / 10;
      } else {
        return 0;
      }
    },
    dataInit() {
      var store = this.$store.state;
      this.inputData = store.ControlData;
      this.openIndex = this.inputData["21530"];
      this.get21531();
      // this.inputData['21532'] = this.countData(this.inputData['21532'])
      // this.inputData['21533'] = this.countData10(this.inputData['21533'])
      // this.inputData['21562'] = this.countData10(this.inputData['21562'])
      var data = {
        deviceId: store.equipmentList[store.equipmentIndex - 1].deviceId,
      };
      // this.api.readControl_fiveControl(data).then(res => {
      //   if(res.data.code==200){
      //     this.inputData = res.data.data

      //   }

      // })
      this.getStatus(data);
      clearInterval(this.statusInterval) && this.statusInterval;
      this.statusInterval = setInterval(() => {
        this.getStatus(data);
      }, 3000);
    },
    getStatus(data) {
      this.api.getControlGetState(data).then((res) => {
        if (res.data.code == 200) {
          this.statusNum = res.data.data["1015"];
        }
      });
    },
    //全选文本
    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>
  