<template>
    <div class="irrigateSet sensorSet">
        <div class="table-title">
            <img src="../../assets/img/intrinsicParameter.png" alt=""> {{
                $store.state.equipmentList[$store.state.equipmentIndex - 1].deviceTypeName }}{{ $t('status.equipmentStatus') }}
        </div>
        <div class="flex-view">
            <div class="input-main-60">
                <template v-for="item, index in dataInfo2_list" >
                    <span>{{ item.title }}</span>
                    <input :class="index==2?'w-100':''" 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="" />
                 {{ $t('status.inletPump') }}
            </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>  {{ $t('status.solenoidValveControl') }}</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;">
              {{ $t('status.soilSensorData') }}
        </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>{{ $t('status.soilTempSensor',{index: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>{{ $t('status.soilMoistureSensor',{index: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 {
        computed:{
        dataInfo2_list(){
            var dataInfo2_list=[{
                title: this.$t('status.irrigationRemainingTime'),
                timer: '0',
                unit: 'min'
            },
            {
                title: this.$t('status.currentIrrigationVolume'),
                timer: '0',
                unit: 'L'
            },
            {
                title: this.$t('status.startupMethod'),
                timer: '无',
                unit: ''
            },
            {
                title: this.$t('status.executingGroupNumber'),
                timer: '0',
                unit: ''
            },
            {
                title: this.$t('status.instantFlowRate'),
                timer: '0',
                unit: 'L/h'
            },]
            return dataInfo2_list
        },
             rightList2(){
            var rightList2=[ this.$t('status.externalControlSignal',{index:1}),
                this.$t('status.externalControlSignal',{index:2}),
               this.$t('status.externalControlSignal',{index:3}),
                this.$t('status.externalControlSignal',{index:4}),]
            return rightList2
        },
    },
    data() {
        return {

            inputData: [],
            loading: null,
           
            openType: '',
            right2Index: [],
            centerBottom: [],
          
            timer_:null,
        }
    },

    destroyed() {
        clearInterval(this.timer_)&&this.timer_
    },
    mounted() {
        this.loading = this.$loading({
            lock: true,
            text: this.$t('message.loading'),
            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  this.$t('status.startupMethods.manual')
            } else if (index == 1) {
                return  this.$t('status.startupMethods.externalControl')
            } else if (index == 2) {
                return  this.$t('status.startupMethods.lightAccumulation')
            } else if (index == 3) {
                return  this.$t('status.startupMethods.soilMoistureControl')
            } else if (index == 4) {
                return  this.$t('status.startupMethods.interval')
            } else if (index == 5) {
                return  this.$t('status.startupMethods.timed')
            } else {
                return  this.$t('status.startupMethods.none')
            }
        },
        //什么方式启动的灌溉
        getData29() {
            var num =this.inputData['3011']?this.inputData['3011']:0
            var number = this.padString(num.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
            
        },
        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 num1=inputData['3016']?inputData['3016']:0
            var list1 = this.padString(num1.toString(2), 16)
            var num2=inputData['3017']?inputData['3017']:0
            var list2 = this.padString(num2.toString(2), 16)
            var num3=inputData['3017']?inputData['3017']:0
            var list3 = this.padString(num3.toString(2), 16)
            var num4=inputData['3019']?inputData['3019']:0
            var list4 = this.padString(num4.toString(2), 16)
            var num5=inputData['3020']?inputData['3020']:0
            var list5 = this.padString(num5.toString(2), 16)
            var num6=inputData['3021']?inputData['3021']:0
            var list6 = this.padString(num6.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>
