<template>
    <div class="wetFan">
        <div class="table-title">
            <img src="../../assets/img/wetFan.png" alt="">{{ pageName }}
        </div>
        <div class="title-tips">工作方式选择
            <div class="input-btn">
                <div class="btn btn-tips" @click="changeOpen(0)" :class="inputData[22250] == 0 ? 'blue' : 'green'">
                    {{ inputData[22250] == 0 ? '无效' : '有效' }}
                </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="" />自动运行
                </div>
                <div class="btn" @click="changeOpen(2)" :class="inputData[22250] == 2 ? 'blue' : 'green'">
                    <img src="../../assets/image/irrigateSet3.png" alt="" />手动打开
                </div>
                <div class="btn" @click="changeOpen(3)" :class="inputData[22250] == 3 ? 'blue' : 'green'">
                    <img src="../../assets/image/irrigateSet1.png" alt="" />手动关闭
                </div>
            </div>
        </div>
        <div class="text-tips">手动打开时冷暖风机启动</div>
        <div class="flex-view">
            <div class="input-btn ">
                <div class="btn btn-w80" @click="changeIndexOpen1(0)" :class="actList2[0] == '1' ? 'blue' : 'off'">
                    1#
                </div>
                <div class="btn btn-w80" @click="changeIndexOpen1(1)" :class="actList2[1] == '1' ? 'blue' : 'off'">
                    2#
                </div>
                <div class="btn btn-w80" @click="changeIndexOpen1(2)" :class="actList2[2] == '1' ? 'blue' : 'off'">
                    3#
                </div>
                <div class="btn btn-w80" @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-main-80">
                <span>工作时段</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 shrink-none">
          
            <div class="input-sel input-sel-w180">
                <span>冷暖风机最高档位</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-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>
                <span>。（最大为4档）</span>
            </div>
        </div>
        <div class="text-tips">加热模式:</div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-80">
                <span></span><span></span> <span>&nbsp;温度低于目标值</span>
                <input @blur="blurChange10(22257, $event)" v-model="inputData[22257]"
                    @input="changeCount10(22257, $event)" type="text" placeholder="0">
                <span>℃时，启动1档加热。</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-80">
                <span></span><span></span> <span>&nbsp;温度高于目标值</span>
                <input @blur="blurChange10(22259, $event)" v-model="inputData[22259]"
                    @input="changeCount10(22259, $event)" type="text" placeholder="0">
                <span>℃时，减档。</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none ">
            <div class="input-main-80">
                <span></span><span></span> <span>&nbsp;达不到目标值，</span>
                <input @blur="blurChange(22258, $event)" v-model="inputData[22258]" @input="change(22258, $event)"
                    type="text" placeholder="0">
                <span>秒加档。</span>
            </div>
        </div>
        <!-- <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w80" @click="changeIndexOpen4(0)" :class="inputData['22260'] == '1' ? 'blue' : 'off'">
                    {{ inputData['22260'] == '1' ? '有效' : '无效' }}
                </div>
            </div>
            <div class="input-main-80">
                <span>制冷时，开启风机，湿帘。</span>
            </div>
        </div> -->
        <div class="text-tips">制冷模式:</div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-80">
                <span></span><span></span> <span>&nbsp;温度高于目标值</span>
                <input @blur="blurChange10(22261, $event)" v-model="inputData[22261]"
                    @input="changeCount10(22261, $event)" type="text" placeholder="0">
                <span>℃时，启动1档制冷。</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none border-none">
            <div class="input-main-80">
                <span></span><span></span> <span>&nbsp;温度低于目标值</span>
                <input @blur="blurChange10(22263, $event)" v-model="inputData[22263]"
                    @input="changeCount10(22263, $event)" type="text" placeholder="0">
                <span>℃时，减档。</span>
            </div>
        </div>
        <div class="flex-view padding-none shrink-none ">
            <div class="input-main-80">
                <span></span><span></span> <span>&nbsp;达不到目标值，</span>
                <input @blur="blurChange(22262, $event)" v-model="inputData[22262]" @input="change(22262, $event)"
                    type="text" placeholder="0">
                <span>秒加档。</span>
            </div>
        </div>
        <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w80" @click="changeIndexOpen2(0)" :class="actList3[0] == '1' ? 'blue' : 'off'">
                    {{ actList3[0] == '1' ? '有效' : '无效' }}
                </div>
            </div>
            <div class="input-main-80">
                <span>冷暖风机运行时，关闭天窗。</span>
            </div>
        </div>
        <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w80" @click="changeIndexOpen2(1)" :class="actList3[1] == '1' ? 'blue' : 'off'">
                    {{ actList3[1] == '1' ? '有效' : '无效' }}
                </div>
            </div>
            <div class="input-main-80">
                <span>冷暖风机运行时，关闭风机，湿帘。</span>
            </div>
        </div>
        <div class="flex-view padding-none border-none shrink-none">
            <div class="input-btn">
                <div class="btn btn-w80" @click="changeIndexOpen2(2)" :class="actList3[2] == '1' ? 'blue' : 'off'">
                    {{ actList3[2] == '1' ? '有效' : '无效' }}
                </div>
            </div>
            <div class="input-main-80">
                <span>冷暖风机运行时，开启环流风扇。</span>
            </div>
        </div>



    </div>
</template>
<script>
export default {
    data() {
        return {
            inputData: [],
            typeList1: [
            { label: "0", value: 0 },
                { label: "1档", value: 1 },
                { label: "2档", value: 2 },
                { label: "3档", value: 3 },
                { label: "4档", value: 4 },
            ],
            inputDataNew: [],
            indexs: 1,
            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 = "";
            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(22251, 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);
        },

        getActive2() {
            this.actList2 = []
            var num = this.inputData[22251]

            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]

            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: '加载中',
                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.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]);
                }
            })
        },
        //二进制转换后的补全
        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>