/*
 * @FilePath: \screenFront\src\store\moduleSocket.ts
 * @Author: 王路平
 * @文件版本: V1.0.0
 * @Date: 2023-02-06 15:58:13
 * @Description: 
 * 
 * 版权信息         : 2023 by ${再登软件}, All Rights Reserved.
 */
import { defineStore } from "pinia";
import { Names } from '@/store/storeName'
import {gettime,clacendTime} from "@/utils/time"
export const useSocketStore = defineStore(Names.socket, {
    // 使用方式
    // const Index= useIndexStore()
    // 1、Index.{数据}++
    // 2、Index.$patch({数据:??})
    // 3、Index.$patch((state)=>{ state.数据=??})
    // 4、通过action修改
    state: () => {
        return {
            noise: {
                listData: [],
                top: {
                    name: null,
                    value: null
                }
            },     //环境常规噪音
            pm: { welding: { two: null, ten: null }, stuff: { two: null, ten: null }, ornaments: { two: null, ten: null } }, //空气质量数据
            humiture: { Humiture: [], bottom: { humidity: null, temp: null, name: null }, top: { humidity: null, temp: null, name: null } }, //温湿度
            verticalNum: { //安全检测
                paintingGas: { limit: null, value: null, icon: 'icon-ranqi', unit: null, devName: null },
                boilerGas: { limit: null, value: null, icon: 'icon-ranqi', unit: null, devName: null },
                canteenGas: { limit: null, value: null, icon: 'icon-ranqi', unit: null, devName: null },
                TVOC_CH2O: { CH2O: { limit: null, value: null, icon: 'icon-app_icons--', unit: 'mg/m3', devName: null }, TVOC: { limit: null, value: null, icon: 'icon-TVOC-Outlined', unit: 'mg/m3', devName: null } },
                flame: { limit: null, value: null, icon: 'icon-weibiaoti1', unit: null, devName: null },
                smoke: { limit: null, value: null, icon: 'icon-yanwubaojingqi', unit: null, devName: null },
            },
            Pressure: { east: 0, west: 0 }, //东西泵房气压
            pipe: { listData: [], top: { name: null, value: null } },
            newVerticalNum: [
                {
                    "type": "TVOC",
                    "icon": "icon-TVOC-Outlined",
                    "title": "TVOC检测",
                    "limit": "0.5",
                    "unit": "mg/m³",
                    "value": [
                        {
                            "name": "精饰车间TVOC/甲醛监测传感器",
                            "val": null,
                            "type": "TVOC_CH2O",
                            "field": "TVOC"
                        }
                    ]
                },
                {
                    "type": "Smoke",
                    "icon": "icon-yanwubaojingqi",
                    "title": "烟雾检测",
                    "limit": "100",
                    "unit": null,
                    "value": [
                        {
                            "name": "精饰车间烟雾传感器",
                            "val": null,
                            "type": "Smoke",
                            "field": "Smoke_Reg"
                        }
                    ]
                },
                {
                    "type": "Methane",
                    "icon": "icon-ranqi",
                    "title": "燃气检测",
                    "limit": "0",
                    "unit": null,
                    "value": [
                        {
                            "name": "精饰车间燃气监测",
                            "val": null,
                            "type": "Methane",
                            "field": "Methane_Reg"
                        },
                        {
                            "name": "食堂燃气监测",
                            "val": null,
                            "type": "Methane",
                            "field": "Methane_Reg"
                        },
                        {
                            "name": "锅炉房燃气监测",
                            "val": null,
                            "type": "Methane",
                            "field": "Methane_Reg"
                        }
                    ]
                },
                {
                    "type": "CH2O",
                    "icon": "icon-app_icons--",
                    "title": "甲醛检测",
                    "limit": "0.08",
                    "unit": "mg/m³",
                    "value": [
                        {
                            "name": "精饰车间TVOC/甲醛监测传感器",
                            "val": null,
                            "type": "TVOC_CH2O",
                            "field": "CH2O"
                        }
                    ]
                },
                {
                    "type": "FIRE",
                    "icon": "icon-weibiaoti1",
                    "title": "明火检测",
                    "limit": "0",
                    "unit": null,
                    "value": [
                        {
                            "name": "精饰车间火花传感器",
                            "val": null,
                            "type": "FIRE",
                            "field": "Fire_Reg"
                        }
                    ]
                }
            ]
        }
    },
    //computed 修改一些值
    //需要使用return将数据抛出
    //getters内可相互使用计算结果
    //使用时可直接放入标签内<div>Index.方法()</div>
    getters: {

    },
    //methods 可同步/异步，提交state
    //actions内获取state数据使用this
    // 使用方式
    // 1、Index.方法()
    //异步方法需要添加async和await
    actions: {
        /**
         * @函数功能: 
         * @param {*} val   环境噪音参数
         * @出口参数: 
         * @函数备注: 
         */
        changenoise(val) {
            // this.noise.listData=val.listData
            if (this.noise.listData.length == 0) {
                this.noise.listData = val.listData
            } else {
                val.listData.forEach(res => {
                    this.noise.listData.forEach(ele => {
                        if (res.name == ele.name) {
                            ele.value = res.value
                        }
                    })
                })
            }

            this.noise.top.name = val.top.name
            this.noise.top.value = val.top.value
        },
        /**
         * @函数功能: 
         * @param {*} val 空气质量参数
         * @出口参数: 
         * @函数备注: 
         */
        changePM(val) {
            if (val.enName == 'welding') {
                this.pm.welding.two = val.pm25
                this.pm.welding.ten = val.pm10
            } else if (val.enName == 'stuff') {
                this.pm.stuff.two = val.pm25
                this.pm.stuff.ten = val.pm10
            } else if (val.enName == 'ornaments') {
                this.pm.ornaments.two = val.pm25
                this.pm.ornaments.ten = val.pm10
            }

        },
        /**
         * @函数功能: 
         * @param {*} val 温湿度数据
         * @出口参数: 
         * @函数备注: 
         */
        changeHumiture(val) {
            if (this.humiture.Humiture?.length == 0) {
                this.humiture = val
            } else {
                val.Humiture.forEach(res => {
                    this.humiture.Humiture.forEach(ele => {
                        if (res.name == ele.name) {
                            ele.humidity = res.humidity
                            ele.temp = res.temp
                        }
                    })
                })
            }

        },
        /**
         * @函数功能: 
         * @param {*} val 喷漆燃气检测
         * @出口参数: 
         * @函数备注: 
         */
        changepaintingGas(val) {
            this.verticalNum.paintingGas.limit = val.paintingGas.limit
            this.verticalNum.paintingGas.value = val.paintingGas.value
            this.verticalNum.paintingGas.devName = val.devName
        },
        /**
         * @函数功能: 
         * @param {*} val 锅炉房燃气检测
         * @出口参数: 
         * @函数备注: 
         */
        changeboilerGas(val) {
            this.verticalNum.boilerGas.limit = val.limit
            this.verticalNum.boilerGas.value = val.value
        },
        /**
         * @函数功能: 
         * @param {*} val 食堂燃气检测
         * @出口参数: 
         * @函数备注: 
         */
        changecanteenGas(val) {
            this.verticalNum.canteenGas.limit = val.limit
            this.verticalNum.canteenGas.value = val.value
        },
        /**
         * @函数功能: 
         * @param {*} val 甲醛/tvoc
         * @出口参数: 
         * @函数备注: 
         */
        changeTVOC_CH2O(val) {

            this.verticalNum.TVOC_CH2O.TVOC.limit = val.TVOC.limit
            this.verticalNum.TVOC_CH2O.TVOC.value = val.TVOC.value
            this.verticalNum.TVOC_CH2O.CH2O.limit = val.CH2O.limit
            this.verticalNum.TVOC_CH2O.CH2O.value = val.CH2O.value
        },
        changeCH2O(val) {
            this.verticalNum.TVOC_CH2O.CH2O.limit = val.CH2O.limit
            this.verticalNum.TVOC_CH2O.CH2O.value = val.CH2O.value
        },
        changeTVOC(val) {
            this.verticalNum.TVOC_CH2O.TVOC.limit = val.TVOC.limit
            this.verticalNum.TVOC_CH2O.TVOC.value = val.TVOC.value
        },
        /**
         * @函数功能:  明火检测
         * @param {*} val
         * @出口参数: 
         * @函数备注: 
         */
        changeflame(val) {
            this.verticalNum.flame.limit = val.limit
            this.verticalNum.flame.value = val.value
        },
        /**
         * @函数功能: 
         * @param {*} val 烟雾报警
         * @出口参数: 
         * @函数备注: 
         */
        changesmoke(val) {
            this.verticalNum.smoke.limit = val.limit
            this.verticalNum.smoke.value = val.value
        },
        changePressure(val) {
            this.Pressure = val
        },
        changePipe(val) {
            console.log(val, "gengxinle ");

            if (this.pipe.listData.length == 0) {
                this.pipe.listData = val.listData
            } else {
                val.listData.forEach(res => {
                    this.pipe.listData.forEach(ele => {
                        if (res.devName == ele.devName) {
                            ele.value = res.value
                        }
                    })
                })
            }

            // this.pipe.listData=val.listData
            this.pipe.top.name = val.top.name
            this.pipe.top.value = val.top.value
        },

        resetRealtimeSecurity() {
            this.verticalNum = { //安全检测
                paintingGas: { limit: null, value: null, icon: 'icon-ranqi', unit: null, devName: null },
                boilerGas: { limit: null, value: null, icon: 'icon-ranqi', unit: null, devName: null },
                canteenGas: { limit: null, value: null, icon: 'icon-ranqi', unit: null, devName: null },
                TVOC_CH2O: { CH2O: { limit: null, value: null, icon: 'icon-app_icons--', unit: 'mg/m3', devName: null }, TVOC: { limit: null, value: null, icon: 'icon-TVOC-Outlined', unit: 'mg/m3', devName: null } },
                flame: { limit: null, value: null, icon: 'icon-weibiaoti1', unit: null, devName: null },
                smoke: { limit: null, value: null, icon: 'icon-yanwubaojingqi', unit: null, devName: null },
            },
                this.Pressure = { east: 0, west: 0 }, //东西泵房气压
                this.pipe = { listData: [], top: { name: null, value: null } }
        },
        //设置新的安全检测数据
        setNewVerticalNum(val) {
            this.newVerticalNum = val
        },
        //更新 新的安全检测数据
        changeNewVerticalNum(val) {
            //{"type":"Smoke","data":{"name":"精饰车间烟雾传感器","val":"100"}}
            let pIndex = this.newVerticalNum.findIndex(item => item.type == val.type)
            let cIndex = null
            if (pIndex != -1) {
                cIndex = this.newVerticalNum[pIndex].value.findIndex(item => item.name == val.data.name)
            }
            if (cIndex != -1) {
                if (val.data.val>this.newVerticalNum[pIndex].limit) {
                    
                    let now = new Date().getTime()
                    if (this.newVerticalNum[pIndex].value[cIndex].date == null) {
                        this.newVerticalNum[pIndex].value[cIndex].date = gettime()
                        this.newVerticalNum[pIndex].value[cIndex].time = now
                    }
                    
                    let time = this.newVerticalNum[pIndex].value[cIndex].time

                    this.newVerticalNum[pIndex].value[cIndex].continuous = clacendTime(now,time)
                } else {
                    this.newVerticalNum[pIndex].value[cIndex].date = null
                    this.newVerticalNum[pIndex].value[cIndex].time = null
                }
                this.newVerticalNum[pIndex].value[cIndex].val = val.data.val
                this.newVerticalNum[pIndex].value[cIndex].status = true
            }
        }
    }
})