<template>
  <div class="wetFan">
    <div class="table-title">
      <img src="../../assets/img/targetCo2.png" alt="" />{{ pageName }}
    </div>
    <div class="title-tips">
      工作方式选择
      <div class="input-btn">
        <div
          class="btn btn-tips"
          @click="changeOpen(0)"
          :class="
            inputData[this.startList[this.indexs - 1]] == 0 ? 'blue' : 'green'
          "
        >
          {{
            inputData[this.startList[this.indexs - 1]] == 0 ? "无效" : "有效"
          }}
        </div>
      </div>
    </div>
    <div class="flex-view">
      <div class="input-btn">
        <div
          class="btn"
          @click="changeOpen(1)"
          :class="
            inputData[this.startList[this.indexs - 1]] == 1 ? 'blue' : 'green'
          "
        >
          <img src="../../assets/image/irrigateSet2.png" alt="" />自动运行
        </div>
        <div
          class="btn"
          @click="changeOpen(2)"
          :class="
            inputData[this.startList[this.indexs - 1]] == 2 ? 'blue' : 'green'
          "
        >
          <img src="../../assets/image/irrigateSet3.png" alt="" />手动打开
        </div>
        <div
          class="btn"
          @click="changeOpen(3)"
          :class="
            inputData[this.startList[this.indexs - 1]] == 3 ? 'blue' : 'green'
          "
        >
          <img src="../../assets/image/irrigateSet1.png" alt="" />手动关闭
        </div>
      </div>
    </div>
    <div class="flex-view padding-none border-none shrink-none">
      <div class="input-sel input-sel-w180">
        <span>光照来源选择</span>
        <el-dropdown
          @command="handleCommand1($event, startList[indexs - 1] + 2)"
        >
          <div class="el-dropdown-link">
            <input
              v-model="inputDataNew[startList[indexs - 1] + 2]"
              type="text"
              class="input-input"
              placeholder="无"
            />
            <div class="arrow">
              <img src="../../assets/image/arrow.png" alt="" />
            </div>
          </div>
          <el-dropdown-menu class="input-menu-180" slot="dropdown">
            <el-dropdown-item
              v-for="(item, index) in typeList1"
              :key="index"
              :command="item.value"
              >{{ item.label }}</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div class="flex-view padding-none border-none shrink-none">
      <div class="input-main-80">
        <span>光照确定时间</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 3, $event)"
          v-model="inputData[startList[indexs - 1] + 3]"
          @input="change(startList[indexs - 1] + 3, $event)"
          type="text"
          placeholder="0"
        />
        <span>分钟</span>
      </div>
    </div>
    <div class="flex-view padding-none border-none shrink-none">
      <div class="input-main-80">
        <span>补气工作限制</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 4, $event)"
          v-model="inputData[startList[indexs - 1] + 4]"
          @input="change(startList[indexs - 1] + 4, $event)"
          type="text"
          placeholder="0"
        />
        <span>:</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 5, $event)"
          v-model="inputData[startList[indexs - 1] + 5]"
          @input="change(startList[indexs - 1] + 5, $event)"
          type="text"
          placeholder="0"
        />
        <span></span>
        <input
          @blur="blurChange(startList[indexs - 1] + 6, $event)"
          v-model="inputData[startList[indexs - 1] + 6]"
          @input="change(startList[indexs - 1] + 6, $event)"
          type="text"
          placeholder="0"
        />
        <span>:</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 7, $event)"
          v-model="inputData[startList[indexs - 1] + 7]"
          @input="change(startList[indexs - 1] + 7, $event)"
          type="text"
          placeholder="0"
        />
      </div>
    </div>
    <div class="flex-view padding-none border-none shrink-none">
      <div class="input-btn">
        <div
          class="btn btn-w80"
          @click="changeIndexOpen1(0)"
          :class="actList2[0] == '1' ? 'blue' : 'off'"
        >
          {{ actList2[0] == "1" ? "有效" : "无效" }}
        </div>
      </div>
      <div class="input-main-80">
        <span>光照大于</span>
        <input
          @blur="blurChange10(startList[indexs - 1] + 8, $event)"
          v-model="inputData[startList[indexs - 1] + 8]"
          @input="changeCount10(startList[indexs - 1] + 8, $event)"
          type="text"
          placeholder="0"
        />
        <span>klux,允许补气</span>
      </div>
    </div>
    <div class="flex-view padding-none border-none shrink-none">
      <div class="input-btn">
        <div
          class="btn btn-w80"
          @click="changeIndexOpen1(1)"
          :class="actList2[1] == '1' ? 'blue' : 'off'"
        >
          {{ actList2[1] == "1" ? "有效" : "无效" }}
        </div>
      </div>
      <div class="input-main-80">
        <span>室内温度大于</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 9, $event)"
          v-model="inputData[startList[indexs - 1] + 9]"
          @input="change(startList[indexs - 1] + 9, $event)"
          type="text"
          placeholder="0"
        />
        <span>℃，允许补气</span>
      </div>
    </div>
    <div class="flex-view padding-none border-none shrink-none">
      <div class="input-main-80">
        <span>二氧化碳浓度低于</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 10, $event)"
          v-model="inputData[startList[indexs - 1] + 10]"
          @input="change(startList[indexs - 1] + 10, $event)"
          type="text"
          placeholder="0"
        />
        <span>ppm,开始补气。</span>
      </div>
    </div>
    <div class="flex-view padding-none border-none shrink-none">
      <div class="input-main-80">
        <span>二氧化碳浓度高于</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 11, $event)"
          v-model="inputData[startList[indexs - 1] + 11]"
          @input="change(startList[indexs - 1] + 11, $event)"
          type="text"
          placeholder="0"
        />
        <span>ppm,停止补气。</span>
      </div>
    </div>
     <div class="flex-view padding-none border-none shrink-none">
      <div class="input-main-80">
        <span>开启</span>
        <input
          @blur="blurChange(startList[indexs - 1] + 12, $event)"
          v-model="inputData[startList[indexs - 1] + 12]"
          @input="change(startList[indexs - 1] + 12, $event)"
          type="text"
          placeholder="0"
        />
        <span>秒 ，关闭多少</span>
          <input
          @blur="blurChange(startList[indexs - 1] + 13, $event)"
          v-model="inputData[startList[indexs - 1] + 13]"
          @input="change(startList[indexs - 1] + 13, $event)"
          type="text"
          placeholder="0"
        />
        <span>秒，进行间隔补气</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputData: [],
      inputDataNew: [],
      indexs: 1,
      actList1: [],
      actList2: [],
      startList: [22360, 22378],
      loading: null,
      pageName: "CO2补气",
      typeList1: [
        { label: "室外光照", value: 0 },
        { label: "室内光照", value: 1 },
      ],
    };
  },
  watch: {
    $route(newVal, oldVal) {
      const that = this;
      this.indexs = this.$route.query.id ? this.$route.query.id : 1;
      if (this.$route.query.name) {
        this.pageName = this.$route.query.name;
      }
      setTimeout(() => {
        that.getActive1();
        that.getActive2();
      }, 0);
    },
    "$store.state.equipmentIndex"(newVal, oldVal) {
      this.dataInit();
    },
  },
  mounted() {
    const that = this;
    this.indexs = this.$route.query.id ? this.$route.query.id : 1;

    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);
    },
    getData1(data) {
      var name = "";
      this.typeList1.forEach((el, index) => {
        if (el.value == data) {
          name = el.label;
        }
      });
      return name;
    },
    handleCommand1(command, code) {
      this.typeList1.forEach((el, index) => {
        if (el.value == command) {
          this.inputDataNew[code] = el.label;
        }
      });
      this.inputData[code] = command;
      var dataNum = {
        target: {
          value: command,
        },
      };
      this.changeBtn(code, dataNum);
    },

    //开关无效
    changeIndexOpen1(active) {
      if (this.actList2[active] == "1") {
        this.actList2[active] = "0";
      } else {
        this.actList2[active] = "1";
      }
      this.$forceUpdate();
      var revList = this.actList2.slice().reverse();
      var num = revList.join("");
      var dataNum = {
        target: {
          value: parseInt(num, 2),
        },
      };
      this.changeBtn(this.startList[this.indexs - 1] + 1, dataNum);
    },
    getActive1() {
      this.actList1 = [];
      var num = this.inputData[this.startList[this.indexs - 1]];

      var num1 = this.padString(num.toString(2), 4);
      var openListNew = [];
      openListNew = num1.split("");
      this.actList1 = openListNew.reverse();
    },
    getActive2() {
      this.actList2 = [];
      var num = this.inputData[this.startList[this.indexs - 1] + 1];

      var num1 = this.padString(num.toString(2), 2);
      var openListNew = [];
      openListNew = num1.split("");
      this.actList2 = openListNew.reverse();
    },
    // 工作方式选择
    changeOpen(index) {
      this.inputData[this.startList[this.indexs - 1]] = index;
      var dataNum = {
        target: {
          value: index,
        },
      };
      this.changeBtn(this.startList[this.indexs - 1], dataNum);
    },
    dataInit() {
      this.loading = this.$loading({
        lock: true,
        text: "加载中",
        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.co2replenishment(data).then((res) => {
        this.loading.close();
        if (res.data.code == 200) {
          this.inputData = res.data.data;
          this.inputDataNew = JSON.parse(JSON.stringify(res.data.data));
          this.getActive1();
          this.getActive2();
          this.startList.forEach((el, index) => {
            this.inputData[el + 8] = this.countData10(this.inputData[el + 8]);
            this.inputDataNew[el + 2] = this.getData1(this.inputData[el + 2]);
          });
        }
      });
    },
    //二进制转换后的补全
    padString(str, length) {
      return str.padStart(length, "0");
    },
    //反转数组 不影响原数组
    myReverse(arr) {
      return [...arr].reverse();
    },
    //全选文本
    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>