<template>
    <div class="irrigateSet sensorSet">
        <div class="table-title">
            <img src="../../assets/img/intrinsicParameter.png" alt=""> {{
                $store.state.equipmentList[$store.state.equipmentIndex - 1].deviceTypeName }}状态显示
        </div>
        <div class="flex-view">
            <div class="input-main-60">
                <template v-for="item, index in dataInfo2_list">
                    <span>{{ item.title }}</span>
                    <input  v-model="item.timer" type="text" value="60" disabled placeholder="60" />
                    <span>{{ item.unit }}</span>
                </template>
            </div>
        </div>
        <div class="flex-view">
            <div class="open-btn" :class="inputData['3012'] ? 'active' : ''">
                <img v-if="inputData['3012']" src="../../assets/image/irrigateSet-icon2-act.png" alt="" />
                <img v-else src="../../assets/image/irrigateSet-icon2.png" alt="" />
                进水泵
            </div>
        </div>
        <div class="flex-view">
            <div class="open-btn w-250" :class="right2Index.indexOf(index) != -1 ? 'active' : ''"
                v-for="(item, index) in rightList2" >{{ item }}
            </div>
            <!-- <div class="open-btn w-250" :class="right2Index.indexOf(index) != -1 ? 'active' : ''"
                v-for="(item, index) in rightList2.slice(4, 8)">{{ item }}
            </div> -->
        </div>
        <div class="flex-view">
            <div class="input-main-60">
                <span>电磁阀控制通道</span>
                <template v-for="(item, index) in 16 * 6">
                    <input v-if="centerBottom.indexOf(index) != -1"  disabled :value="`${index + 1}#`"
                        type="text" placeholder="0" />
                </template>
            </div>
        </div>
        <div style="padding: 10px 10px 0;">
            土壤温湿度传感器数据
        </div>
        <div class="flex-view">
            <div>
                <template v-for="item,index in 40">
                    <div class="input-main-60" v-if="index<(inputData[3102]?inputData[3102]:0)" :key="index">
                    <span>{{ index + 1 }}#土壤温度传感器</span>
                    <input   disabled :value="inputData[3103+index]?(inputData[3103+index]-400)/10:0"
                        type="text" placeholder="0" />
                    </div>
                </template>
            </div>
            <div>
                <template v-for="item,index in 40">
                    <div class="input-main-60" v-if="index<(inputData[3102]?inputData[3102]:0)" :key="index">
                    <span>{{ index + 1 }}#土壤湿度传感器</span>
                    <input   disabled :value="inputData[3062+index]?inputData[3062+index]/10:0"
                        type="text" placeholder="0" />
                    </div>
                </template>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {

            inputData: [],
            loading: null,
            dataInfo2_list: [{
                title: '灌溉剩余时长',
                timer: '0',
                unit: 'min'
            },
            {
                title: '本次灌溉量',
                timer: '0',
                unit: 'L'
            },
            {
                title: '启动方式',
                timer: '无',
                unit: ''
            },
            {
                title: '执行灌溉组号',
                timer: '0',
                unit: ''
            },
            {
                title: '瞬时流量',
                timer: '0',
                unit: 'L/h'
            },
            ],
            openType: '',
            right2Index: [],
            centerBottom: [],
            rightList2: ['外控信号1#',
                '外控信号2#',
                '外控信号3#',
                '外控信号4#',
                // '高液位信号',
                // '中液位信号',
                // '低液位信号',
                // '电机信号',
            ],
            timer_:null,
        }
    },
    destroyed() {
        clearInterval(this.timer_)&&this.timer_
    },
    mounted() {
        this.loading = this.$loading({
            lock: true,
            text: '加载中',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        this.dataInit()
			let that = this
			this.timer_ = setInterval(function() {
				that.dataInit();
			}, 5000);
    },
    methods: {
        dataInit() {
            var store = this.$store.state
            var data = {
                deviceId: store.equipmentList[store.equipmentIndex - 1].deviceId,

            };
            this.api.getFI_getState(data).then((res) => {
                this.loading.close()
                if (res.data.code == 200) {
                    this.inputData = res.data.data;
                    this.getDataInfo2()
                    this.getcenterBottom() //获取电磁阀
                    this.getData29() //数据29 启动方式/信号
                } else {
                    this.$message.error(res.data.msg);
                }
            });
        },
        getDataInfo2() {
            var inputData = this.inputData
            this.dataInfo2_list[0].timer = this.countData(inputData['3013'])
            this.dataInfo2_list[1].timer = this.countData(inputData['3014'])
            this.dataInfo2_list[2].timer = this.countData(inputData['3011'])
            this.dataInfo2_list[3].timer = this.countData(inputData['3015'])
            this.dataInfo2_list[4].timer = this.countData(inputData['3010'])
        },
        getType29(index) {
            if (index == 0) {
                return '手动开启动'
            } else if (index == 1) {
                return '外控模式启动'
            } else if (index == 2) {
                return '光照积累启动'
            } else if (index == 3) {
                return '土壤湿度控制'
            } else if (index == 4) {
                return '间隔启动'
            } else if (index == 5) {
                return '定时启动'
            } else {
                return '无'
            }
        },
        //什么方式启动的灌溉
        getData29() {
            var number = this.padString(this.inputData['3011'].toString(2), 14)
            var index = 6
            var typeNumber = -1
            for (var i = 0; i < index; i++) {
                if (Number(number[i])) {
                    typeNumber = i
                }
            }

            this.openType = this.getType29(typeNumber)
            this.dataInfo2_list[2].timer = this.getType29(typeNumber)
            var statusIndex = 8
            let list = []
            for (var j = 0; j < statusIndex; j++) {
                if (Number(number[number.length - 1 - j])) {
                    if (j == 0) {
                        list.push(1)
                    } else if (j == 1) {
                        list.push(0)
                    } else {
                        list.push(j)
                    }
                }
            }
            this.right2Index = list
            console.log(this.right2Index);
            
        },
        padString(str, length) {
            return str.padStart(length, '0');
        },
        countData(data) {
            if (data) {
                return data
            } else {
                return 0
            }
        },
        getcenterBottom() {
            // 96个电磁阀
            this.centerBottom = []
            var inputData = this.inputData
            var list1 = this.padString(inputData['3016'].toString(2), 16)
            var list2 = this.padString(inputData['3017'].toString(2), 16)
            var list3 = this.padString(inputData['3018'].toString(2), 16)
            var list4 = this.padString(inputData['3019'].toString(2), 16)
            var list5 = this.padString(inputData['3020'].toString(2), 16)
            var list6 = this.padString(inputData['3021'].toString(2), 16)
            var number = 16
            for (var i = 0; i < number; i++) {
                if (Number(list1[list1.length - 1 - i])) {
                    this.centerBottom.push(i)
                }
            }
            for (var j = 0; j < number; j++) {
                if (Number(list2[list2.length - 1 - j])) {
                    this.centerBottom.push(j + 16)
                }
            }
            for (var k = 0; k < number; k++) {
                if (Number(list3[list3.length - 1 - k])) {
                    this.centerBottom.push(k + 32)
                }
            }
            for (var l = 0; l < number; l++) {
                if (Number(list4[list4.length - 1 - l])) {
                    this.centerBottom.push(l + 48)
                }
            }
            for (var m = 0; m < number; m++) {
                if (Number(list5[list5.length - 1 - m])) {
                    this.centerBottom.push(m + 64)
                }
            }
            for (var n = 0; n < number; n++) {
                if (Number(list6[list6.length - 1 - n])) {
                    this.centerBottom.push(n + 80)
                }
            }
        },
    },

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