/*
 * @FilePath: \wwwd:\code\screenFront\src\store\module\MechanicalView.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 { valueEquals } from "element-plus";
export const useMechanicalViewStore = defineStore(Names.MechanicalView, {
  // 使用方式
  // const Index= useIndexStore()
  // 1、Index.{数据}++
  // 2、Index.$patch({数据:??})
  // 3、Index.$patch((state)=>{ state.数据=??})
  // 4、通过action修改
  state: () => {
    return {
      sensorList: [],
      iconList: [
        {
          id: 1,
          name: "icon-icon-temperature",
          i18n:'messages.humitureSensor',
          value: "温湿度传感器",
          iconType: "温湿度",
          counts: 0,
          allnum: 0,
        },
        {
          id: 2,
          name: "icon-shengyin",
          i18n:'messages.noiseSensor',
          value: "噪音传感器",
          iconType: "噪音",
          counts: 0,
          allnum: 0,
        },
        {
          id: 3,
          name: "icon-dianliu",
          i18n:'messages.powerSensor',
          value: "电力传感器",
          iconType: "电力",
          counts: 0,
          allnum: 0,
        },
        {
          id: 4,
          name: "icon-weibiaoti1",
          i18n:'messages.sparkSensor',
          value: "火花传感器",
          iconType: "火花",
          counts: 0,
          allnum: 0,
        },
        {
          id: 5,
          name: "icon-fenchen",
          i18n:'messages.dustSensor',
          value: "粉尘传感器",
          iconType: "粉尘",
          counts: 0,
          allnum: 0,
        },
        {
          id: 6,
          name: "icon-app_icons--",
          i18n:'messages.TVOC_CH2OSensor',
          value: "TVOC/甲醛传感器",
          iconType: "TVOC/甲醛",
          counts: 0,
          allnum: 0,
        },
        {
          id: 7,
          name: "icon-yanwubaojingqi",
          i18n:'messages.smokeSensor',
          value: "烟雾传感器",
          iconType: "烟雾",
          counts: 0,
          allnum: 0,
        },
        {
          id: 8,
          name: "icon-ranqi",
          i18n:'messages.gasSensor',
          value: "燃气传感器",
          iconType: "燃气",
          counts: 0,
          allnum: 0,
        },
        {
          id: 9,
          name: "icon-shuiya",
          i18n:'messages.waterPressureSensor',
          value: "水压传感器",
          iconType: "水压",
          counts: 0,
          allnum: 0,
        }, {
          id: 10,
          name: "icon-qiya",
          i18n:'messages.gasPressureSensor',
          value: "气压传感器",
          iconType: "气压",
          counts: 0,
          allnum: 0,
        },
        {
          id: 11,
          name: "icon-beikongshuiwupingtaimenhu-tubiao_zhinengwangguan",
          i18n:'messages.gateway',
          value: "网关",
          iconType: "网关",
          counts: 0,
          allnum: 0,
        },
      ],
      devList: [{
        id: 1,
        name: "icon-shebeiditu",
        i18n:'messages.Dev',
        value: "设备",
        counts: 0,
        allnum: 0,
      },],
      devdataList: [],
      jixiePoint:{}
    };
  },
  //computed 修改一些值
  //需要使用return将数据抛出
  //getters内可相互使用计算结果
  //使用时可直接放入标签内<div>Index.方法()</div>
  getters: {},
  //methods 可同步/异步，提交state
  //actions内获取state数据使用this
  // 使用方式
  // 1、Index.方法()
  //异步方法需要添加async和await
  actions: {
    setDataList(val: any) {

      this.sensorList = []
      for (let key in val) {
        val[key].forEach((res) => {

          this.iconList.forEach((e) => {
            // console.log(e);
            if (e.value.indexOf(key) != -1) {
              e.allnum = val[key].length;
              let sum = 0;
              val[key].forEach((s) => {
                // console.log(s.status);
                if (s.status) {
                  sum++;
                }
              });
              e.counts = sum;
              sum = 0;
            }
          });
          if (res.type == "TVOC/甲醛") {
            res.iconName = "icon-app_icons--";
          }
          if (res.type == "噪音") {
            res.iconName = "icon-shengyin";
          }
          if (res.type == "水压") {
            res.iconName = "icon-shuiya";
          }
          if (res.type == "温湿度") {
            res.iconName = "icon-icon-temperature";
          }
          if (res.type == "火花") {
            res.iconName = "icon-weibiaoti1";
          }
          if (res.type == "烟雾") {
            res.iconName = "icon-yanwubaojingqi";
          }
          if (res.type == "燃气") {
            res.iconName = "icon-ranqi";
          }
          if (res.type == "电力") {
            res.iconName = "icon-dianliu";
          }
          if (res.type == "粉尘") {
            res.iconName = "icon-fenchen";
          }
          if (res.type == "网关") {
            res.iconName =
              "icon-beikongshuiwupingtaimenhu-tubiao_zhinengwangguan";
          }
          if (res.type == "气压") {
            res.iconName =
              "icon-qiya";
          }
          if (res.status) {
            res.status = JSON.parse(res.status)
          }
          res.editStatus = true
          res.judge = "t"
          res.x = res.xfirst
          res.y = res.yfirst
          this.jixiePoint[res.id] = {x:res.x,y:res.y}
          this.sensorList.push(res)
        });
      }
    },
    changeDataList(val: any) {
      this.sensorList.forEach((res) => {
        if (val.id == res.id) {
          if (res.status == JSON.parse(val.status)) {
            res.editStatus = false
          } else {
            // console.log(res,val,res.status==JSON.parse(val.status));
            res.editStatus = true
          }
          if (res.editStatus) {
            res.status = JSON.parse(val.status)
            this.calcListNum();
          }
        } else {
          res.editStatus = false
        }
      });
      // console.log( this.sensorList);
    },
    calcListNum() {
      this.iconList.forEach((e) => {
        let sum = 0;
        this.sensorList.forEach((res) => {
          if (res.type == e.iconType) {
            JSON.parse(res.status) ? sum++ : ''
          }
        });
        e.counts = sum
      });
    },
    setdevList(val: any) {
      let sum = 0
      let all = 0
      this.devdataList = []
      for (let key in val) {
        val[key].forEach(res => {
          res.iconName = "icon-shebeiditu";
          // res.editStatus = true
          if (JSON.parse(res.status)) {
            sum++
          }
          res.x = res.xfirst
          res.y = res.yfirst
          this.jixiePoint[res.id] = {x:res.x,y:res.y}
        })
        this.devdataList.push({
          store: key,
          value: val[key]
        })
        all += val[key].length
      }
      
      this.devList[0].counts = sum
      this.devList[0].allnum = all
      sum = 0
      all = 0
    },
    changedevList(val: any) {
      
      this.devdataList.forEach(res => {
        res.value.forEach(ele => {
          if (val.id == ele.id) {
            if (JSON.parse(ele.status) == JSON.parse(val.status)) {
              ele.editStatus = false
            } else {
              // console.log(res,val,res.status==JSON.parse(val.status));
              ele.editStatus = true
            }
            if (ele.editStatus) {
              ele.status = JSON.parse(val.status)
              // this.calcListNum();
            }
          } else {
            ele.editStatus = false
          }
        })
      })
      this.calcdevListNum()
    },
    calcdevListNum() {
      this.devList.forEach((e) => {
        let sum = 0;
        this.devdataList.forEach((res) => {
          res.value.forEach(ele => {
            JSON.parse(ele.status) ? sum++ : ''
          })


        });
        e.counts = sum
      });
    },
    resetData() {
      this.sensorList = []
      this.iconList = [
        {
          id: 1,
          name: "icon-icon-temperature",
          value: "温湿度传感器",
          iconType: "温湿度",
          counts: 0,
          allnum: 0,
        },
        {
          id: 2,
          name: "icon-shengyin",
          value: "噪音传感器",
          iconType: "噪音",
          counts: 0,
          allnum: 0,
        },
        {
          id: 3,
          name: "icon-dianliu",
          value: "电力传感器",
          iconType: "电力",
          counts: 0,
          allnum: 0,
        },
        {
          id: 4,
          name: "icon-weibiaoti1",
          value: "火花传感器",
          iconType: "火花",
          counts: 0,
          allnum: 0,
        },
        {
          id: 5,
          name: "icon-fenchen",
          value: "粉尘传感器",
          iconType: "粉尘",
          counts: 0,
          allnum: 0,
        },
        {
          id: 6,
          name: "icon-app_icons--",
          value: "TVOC/甲醛传感器",
          iconType: "TVOC/甲醛",
          counts: 0,
          allnum: 0,
        },
        {
          id: 7,
          name: "icon-yanwubaojingqi",
          value: "烟雾传感器",
          iconType: "烟雾",
          counts: 0,
          allnum: 0,
        },
        {
          id: 8,
          name: "icon-ranqi",
          value: "燃气传感器",
          iconType: "燃气",
          counts: 0,
          allnum: 0,
        },
        {
          id: 9,
          name: "icon-shuiya",
          value: "水压传感器",
          iconType: "水压",
          counts: 0,
          allnum: 0,
        }, {
          id: 10,
          name: "icon-qiya",
          value: "气压传感器",
          iconType: "气压",
          counts: 0,
          allnum: 0,
        },
        {
          id: 11,
          name: "icon-beikongshuiwupingtaimenhu-tubiao_zhinengwangguan",
          value: "网关",
          iconType: "网关",
          counts: 0,
          allnum: 0,
        },
      ];
      this.devList = [{
        id: 1,
        name: "icon-shebeiditu",
        value: "设备",
        counts: 0,
        allnum: 0,
      },];
      this.devdataList = [];
    },
  },
});
