<template>
    <div class="synthesis-con">
        <div class="table-title">
            <img src="../../assets/image/synthesis.png" alt=""> {{ $t('synthesisCon.title') }}
        </div>
        <div class="synthesis-content">
            <div class="left-table">
                <div class="table-header">{{ $t('synthesisCon.category') }}</div>
                <div class="table-tr">
                    <div v-for="item, index in leftList" :key="index">{{ item }}</div>
                </div>
            </div>
            <div class="right-table scroll">
                <div>
                    <div class="table-header">
                        <div class="" v-for="item, index in 16">{{ index + 1 }}</div>
                    </div>
                    <div class="table-tr">
                        <div v-for="item, index in rightList" :key="index">
                            <div v-for="item1, index1 in item">
                                <div :class="item1 == '1' ? 'sel' : 'no-sel'" @click="changeType(index, index1)"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            inputData: [],
            nowSel: {
                index: -1,
                value: 0,
            },
            rightList: ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
      loading:null,

        }
    },
    computed:{
        leftList(){
            var data=[ this.$t('alarm.temperature.average',{index:1}),
                 this.$t('alarm.temperature.average',{index:2}),
               this.$t('alarm.humidity.average', {index:1}),
               this.$t('alarm.humidity.average', {index:2}),
               this.$t('synthesisCon.illuminance'),
                'CO2',
               this.$t('synthesisCon.soilTemp'),
               this.$t('synthesisCon.soilMoisture'),
               this.$t('synthesisCon.nutrientPH'),
               this.$t('synthesisCon.nutrientEC'),
               this.$t('synthesisCon.waterTemp'),
            ]
            return data
        }
    },
    watch:{
        '$store.state.equipmentIndex'(newVal, oldVal) {
  this.dataInit();
},
    },
    mounted() {
 
        this.dataInit()
    },
    methods: {
        checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }

            return i;
        },
        padString(str, length) {
            return str.padStart(length, '0');
        },
        getList(data) {
            var num = data?data:0
            var num1 = this.padString(num.toString(2), 16)
            var openListNew = num1.split("")
            var list = openListNew.reverse()
            return list
        },
        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.getReadControlSensorInputSynthesis(data).then((res) => {
                this.loading.close()
                if (res.data.code == 200) {
                    this.inputData = res.data.data;
                    this.leftList.forEach((el, index) => {

                        this.rightList[index] = this.getList(this.inputData['212' + this.checkTime(index)])
                    })
                    this.$forceUpdate();
                } else {
                    this.$message.error(res.data.msg);
                }
            });
        },
        //反转数组 不影响原数组
        myReverse(arr) {
            return [...arr].reverse()
        },
        changeType(index, index1) {
            var store = this.$store.state
            const that = this
            if (this.rightList[index][index1] == '1') {
                this.rightList[index][index1] = '0'
            } else {
                this.rightList[index][index1] = '1'
            }
            this.$forceUpdate();
            var num = this.myReverse(this.rightList[index]).join("");
            var dataNum = {
                target: {
                    value: parseInt(num, 2),
                },
            };

            if (this.nowSel.index == -1) {
                this.changeBtn('212' + this.checkTime(index), dataNum)
                this.nowSel.index = index
                this.nowSel.value = parseInt(num, 2)
            } else {
                if (this.nowSel.index == index) {
                    this.changeBtn('212' + this.checkTime(index), dataNum)
                    this.nowSel.index = index
                    this.nowSel.value = parseInt(num, 2)
                } else {

                    var code = '212' + this.checkTime(this.nowSel.index)
                    var data = {
                        equipmentId: store.equipmentList[store.equipmentIndex - 1].deviceId,
                        regAddress: code,
                        num: this.nowSel.value,
                    };
                    this.nowSel.index = -1
                    this.nowSel.value = 0
                    that.changeData(data);
                    this.changeBtn('212' + this.checkTime(index), dataNum)
                }
            }

        },
        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) {
            this.api.postControlWrite(data).then((res) => {
                if (res.data.code == 200) {
                    this.$message({
                        message: res.data.msg,
                        type: "success",
                    });
                    // this.dataInit();

                } else {
                    this.$message.error(res.data.msg);
                }
                if (this.nowSel.index != -1) {
                    this.timer = null
                    this.nowSel.index = -1
                    this.nowSel.value = 0
                }

            });
        },
    },

}
</script>
<style lang="scss"></style>
  