<template>
    <div class="wetFan">
        <div class="table-title">
            <img src="../../assets/img/wetFan.png" alt="">{{ pageName }}
        </div>
        <div class="title-tips">{{$t('text.selection')}}
            <div class="input-btn">
                <div class="btn btn-tips" @click="changeOpen(0)" :class="inputData[22250] == 0 ? 'green' : 'blue'">
                    {{ inputData[22250] == 0 ? $t('text.invalid') : $t('text.valid') }}
                </div>
            </div>
        </div>
        <div class="flex-view">
            <div class="input-btn ">
                <div class="btn" @click="changeOpen(1)" :class="inputData[22250] == 1 ? 'blue' : 'green'">
                    <img src="../../assets/image/irrigateSet2.png" alt="" />{{$t('text.autoRun')}}
                </div>
                <div class="btn" @click="changeOpen(2)" :class="inputData[22250] == 2 ? 'blue' : 'green'">
                    <img src="../../assets/image/irrigateSet3.png" alt="" />{{$t('text.manualOn')}}
                </div>
                <div class="btn" @click="changeOpen(3)" :class="inputData[22250] == 3 ? 'blue' : 'green'">
                    <img src="../../assets/image/irrigateSet1.png" alt="" />{{$t('text.manualOff')}}
                </div>
            </div>
        </div>
        <div class="text-tips">{{$t('heatFan.manualOperationTip')}}</div>
        <div class="flex-view">
            <div class="input-btn ">
                <div class="btn btn-w70" @click="changeIndexOpen1(0)" :class="actList2[0] == '1' ? 'blue' : 'off'">
                    1#
                </div>
                <div class="btn btn-w70" @click="changeIndexOpen1(1)" :class="actList2[1] == '1' ? 'blue' : 'off'">
                    2#
                </div>
                <div class="btn btn-w70" @click="changeIndexOpen1(2)" :class="actList2[2] == '1' ? 'blue' : 'off'">
                    3#
                </div>
                <div class="btn btn-w70" @click="changeIndexOpen1(3)" :class="actList2[3] == '1' ? 'blue' : 'off'">
                    4#
                </div>
            </div>
        </div>

        <div class="flex-view padding-none border-none shrink-none">
                <div class="input-btn">
        <div
          class="btn btn-w70"
          @click="changeIndexOpen3(0)"
          :class="actList1[0] == '1' ? 'blue' : 'off'"
        >
          {{ actList1[0] == "1" ? $t('text.valid') : $t('text.invalid') }}
        </div>
      </div>
            <div class="input-main-60">
                <span>{{$t('heatFan.workingPeriod')}}&nbsp;:</span>
                <input @blur="blurChange(22252, $event)" v-model="inputData[22252]" @input="change(22252, $event)"
                    type="text" placeholder="0" />
                <span>:</span>
                <input @blur="blurChange(22253, $event)" v-model="inputData[22253]" @input="change(22253, $event)"
                    type="text" placeholder="0" />
                <span>-</span>
                <input @blur="blurChange(22254, $event)" v-model="inputData[22254]" @input="change(22254, $event)"
                    type="text" placeholder="0" />
                <span>:</span>
                <input @blur="blurChange(22255, $event)" v-model="inputData[22255]" @input="change(22255, $event)"
                    type="text" placeholder="0" />
            </div>
        </div>
         <div class="flex-view padding-none border-none shrink-none">
            <div class="input-sel input-sel-w180">
                <span>{{$t('heatFan.fanMode')}}</span>
                <el-dropdown @command="handleCommand2($event, 22265)">
                    <div class="el-dropdown-link">
                        <input v-model="inputDataNew[22265]" 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 typeList2" :key="index" :command="item.value">{{
                            item.label }}</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none">
          
            <div class="input-sel input-sel-w120">
                <span>{{$t('heatFan.maxFanGear')}}</span>
                <el-dropdown @command="handleCommand1($event, 22256)">
                    <div class="el-dropdown-link">
                        <input v-model="inputDataNew[22256]" type="text" class="input-input" placeholder="无" />
                        <div class="arrow">
                            <img src="../../assets/image/arrow.png" alt="" />
                        </div>
                    </div>
                    <el-dropdown-menu class="input-menu-120" 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>
                <span>。{{$t('heatFan.max4')}}</span>
            </div>
        </div>
        <div class="text-tips">{{$t('heatFan.heatingMode')}}&nbsp;:</div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-60">
                <span></span><span></span> <span>&nbsp;{{$t('heatFan.tempBelowTarget')}}</span>
                <input @blur="blurChange10(22257, $event)" v-model="inputData[22257]"
                    @input="changeCount10(22257, $event)" type="text" placeholder="0">
                <span>{{$t('heatFan.startHeating')}}</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-60">
                <span></span><span></span> <span>&nbsp;{{$t('heatFan.tempAboveTarget')}}</span>
                <input @blur="blurChange10(22259, $event)" v-model="inputData[22259]"
                    @input="changeCount10(22259, $event)" type="text" placeholder="0">
                <span>{{$t('heatFan.decreaseGear')}}</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none ">
            <div class="input-main-60">
                <span></span><span></span> <span>&nbsp;{{$t('heatFan.cannotReachTarget')}}</span>
                <input @blur="blurChange(22258, $event)" v-model="inputData[22258]" @input="change(22258, $event)"
                    type="text" placeholder="0">
                <span>{{$t('heatFan.increaseGear')}}</span>
            </div>
        </div>
        <!-- <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w70" @click="changeIndexOpen4(0)" :class="inputData['22260'] == '1' ? 'blue' : 'off'">
                    {{ inputData['22260'] == '1' ? $t('text.valid') : $t('text.invalid') }}
                </div>
            </div>
            <div class="input-main-60">
                <span>制冷时，开启风机，湿帘。</span>
            </div>
        </div> -->
        <div class="text-tips">{{$t('heatFan.coolingMode')}}&nbsp;:</div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-60">
                <span></span><span></span> <span>&nbsp;{{$t('heatFan.tempAboveTarget')}}</span>
                <input @blur="blurChange10(22261, $event)" v-model="inputData[22261]"
                    @input="changeCount10(22261, $event)" type="text" placeholder="0">
                <span>{{$t('heatFan.startCooling')}}</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-60">
                <span></span><span></span> <span>&nbsp;{{$t('heatFan.tempBelowTarget')}}</span>
                <input @blur="blurChange10(22263, $event)" v-model="inputData[22263]"
                    @input="changeCount10(22263, $event)" type="text" placeholder="0">
                <span>{{$t('heatFan.decreaseGear')}}</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none ">
            <div class="input-main-60">
                <span></span><span></span> <span>&nbsp;{{$t('heatFan.cannotReachTarget')}}</span>
                <input @blur="blurChange(22262, $event)" v-model="inputData[22262]" @input="change(22262, $event)"
                    type="text" placeholder="0">
                <span>{{$t('heatFan.increaseGear')}}</span>
            </div>
        </div>
        <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w70" @click="changeIndexOpen2(0)" :class="actList3[0] == '1' ? 'blue' : 'off'">
                    {{ actList3[0] == '1' ? $t('text.valid') : $t('text.invalid') }}
                </div>
            </div>
            <div class="input-main-60">
                <span>{{$t('heatFan.operationRules.closeSkylight')}}</span>
            </div>
        </div>
        <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w70" @click="changeIndexOpen2(1)" :class="actList3[1] == '1' ? 'blue' : 'off'">
                    {{ actList3[1] == '1' ? $t('text.valid') : $t('text.invalid') }}
                </div>
            </div>
            <div class="input-main-60">
                <span>{{$t('heatFan.operationRules.closeFanPad')}}</span>
            </div>
        </div>
        <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w70" @click="changeIndexOpen2(2)" :class="actList3[2] == '1' ? 'blue' : 'off'">
                    {{ actList3[2] == '1' ? $t('text.valid') : $t('text.invalid') }}
                </div>
            </div>
            <div class="input-main-60">
                <span>{{$t('heatFan.operationRules.openCirculationFan')}}</span>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            inputData: [],
            typeList1: [
                //  { label:this.$t('heatFan.gear2',{index:0}), value: 0 },
                  { label:this.$t('heatFan.gear2',{index:1}), value: 1 },
                  { label:this.$t('heatFan.gear2',{index:2}), value: 2 },
                  { label:this.$t('heatFan.gear2',{index:3}), value: 3 },
                  { label:this.$t('heatFan.gear2',{index:4}), value: 4 },
            ],
             typeList2: [
                { label: this.$t('realTime.heatingMode'), value: 1 },
                { label:  this.$t('realTime.coolingMode'), value: 2 },
            ],
            inputDataNew: [],
            indexs: 1,
      actList1: [],
            actList2: [],
            actList3: [],
            loading: null,
            pageName: '冷暖风机',
        }
    },
    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();
            // }, 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 = "0";
            this.typeList1.forEach((el, index) => {
                if (el.value == data) {
                    name = el.label;
                }
            });
            return name;
        },
          getData2(data) {
            var name = "";
            this.typeList2.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);
        },
         handleCommand2(command, code) {
            this.typeList2.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(22251, dataNum);
        },
          //开关无效
        changeIndexOpen3(active) {
            if (this.actList1[active] == '1') {
                this.actList1[active] = '0'
            } else {
                this.actList1[active] = '1'
            }
            this.$forceUpdate();
            var revList = this.actList1.slice().reverse()
            var num = revList.join("");
            var dataNum = {
                target: {
                    value: parseInt(num, 2),
                },
            };
            this.changeBtn(22260, dataNum);
        },
        changeIndexOpen4(){
            if (this.inputData['22260'] == '1') {
                this.inputData['22260'] = '0'
            } else {
                this.inputData['22260'] = '1'
            }
            this.$forceUpdate();
            var dataNum = {
                target: {
                    value:  this.inputData['22260'],
                },
            };
            this.changeBtn(22260, dataNum);
        },
        //开关无效
        changeIndexOpen2(active) {
            if (this.actList3[active] == '1') {
                this.actList3[active] = '0'
            } else {
                this.actList3[active] = '1'
            }
            this.$forceUpdate();
            var revList = this.actList3.slice().reverse()
            var num = revList.join("");
            var dataNum = {
                target: {
                    value: parseInt(num, 2),
                },
            };
            this.changeBtn(22264, dataNum);
        },
     getActive1() {
      this.actList1 = [];
      var num = this.inputData[22260]?this.inputData[22260]:0

      var num1 = this.padString(num.toString(2), 1);
      var openListNew = [];
      openListNew = num1.split("");
      this.actList1 = openListNew.reverse();
    },
        getActive2() {
            this.actList2 = []
            var num = this.inputData[22251]?this.inputData[22251]:0

            var num1 = this.padString(num.toString(2), 4)
            var openListNew = []
            openListNew = num1.split("")
            this.actList2 = openListNew.reverse();
        },
        getActive3() {
            this.actList3 = []
            var num = this.inputData[22264]?this.inputData[22264]:0

            var num1 = this.padString(num.toString(2), 3)
            var openListNew = []
            openListNew = num1.split("")
            this.actList3 = openListNew.reverse();
        },
        // 工作方式选择
        changeOpen(index) {
            this.inputData[22250] = index;
            var dataNum = {
                target: {
                    value: index,
                },
            };
            this.changeBtn(22250, dataNum);
        },
        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.heatfan(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.getActive2()
                    this.getActive3()
                     this.getActive1();
                    this.inputData[22257] = this.countData10(this.inputData[22257]);
                    this.inputData[22259] = this.countData10(this.inputData[22259]);
                    this.inputData[22261] = this.countData10(this.inputData[22261]);
                    this.inputData[22263] = this.countData10(this.inputData[22263]);
                    this.inputDataNew[22256] = this.getData1(this.inputData[22256]);
                    this.inputDataNew[22265] = this.getData2(this.inputData[22265]);
                }
            })
        },
        //二进制转换后的补全
        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>