/*
 * @FilePath: \wwwd:\code\screenFront\src\store\module\Mechanics.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 { development, production } from "@/utils/devSever";
import { formatter } from "element-plus";
export const useMechanicsStore = defineStore(Names.Mechanics, {
  // 使用方式
  // const Index= useIndexStore()
  // 1、Index.{数据}++
  // 2、Index.$patch({数据:??})
  // 3、Index.$patch((state)=>{ state.数据=??})
  // 4、通过action修改
  state: () => {
    return {
      page: null, //当前页,
      allNum: 0,
      loading: false,
      mechanicsData: [
        {
          id: null,
          index: 1,
          title: "精加",
          titleChild: "机械分厂设备物联图-精加车间",
          titleI18n:'messages.DevDiagram_JingJia',
          type: "精加车间",
          num: null,
          data: null,
          gateway: 1,
          dom: null,
          domName: "bottontip1",
        },
        {
          id: null,
          index: 2,
          title: "机加",
          titleChild: "机械分厂设备物联图-机加车间",
          titleI18n:'messages.DevDiagram_JiJia',
          type: "机加车间",
          num: null,
          data: null,
          gateway: 1,
          dom: null,
          domName: "bottontip2",
        },
        {
          id: null,
          index: 3,
          title: "大件",
          titleChild: "机械分厂设备物联图-大件车间",
          titleI18n:'messages.DevDiagram_DaJian',
          type: "大件车间",
          num: null,
          data: null,
          gateway: 1,
          dom: null,
          domName: "bottontip3",
        },
        {
          id: null,
          index: 4,
          title: "精饰",
          titleChild: "机械分厂设备物联图-精饰车间",
          titleI18n:'messages.DevDiagram_JingShi',
          type: "精饰车间",
          num: null,
          data: null,
          gateway: 1,
          dom: null,
          domName: "bottontip4",
        },
        {
          id: null,
          index: 5,
          title: "焊接",
          titleChild: "机械分厂设备物联图-焊接车间",
          titleI18n:'messages.DevDiagram_HanJie',
          type: "焊接车间",
          num: null,
          data: null,
          gateway: 0,
          dom: null,
          domName: "bottontip5",
        },
      ],
      devstatus: null,
    };
  },
  //computed 修改一些值
  //需要使用return将数据抛出
  //getters内可相互使用计算结果
  //使用时可直接放入标签内<div>Index.方法()</div>
  getters: {},
  //methods 可同步/异步，提交state
  //actions内获取state数据使用this
  // 使用方式
  // 1、Index.方法()
  //异步方法需要添加async和await
  actions: {
    changePage(val: number) {
      this.page = val;
      this.changeImgSrc();
    },
    changegateway(val: any) {
      val.forEach((res) => {
        this.mechanicsData.forEach((ele) => {
          if (ele.type == res.name) {
            ele.id = res.id;
            ele.gateway = res.status;
          }
        });
      });
    },
    changegatewaynum(val: any) {
      this.allNum = 0;
      val.forEach((res) => {
        this.mechanicsData.forEach((ele) => {
          if (ele.type == res.name) {
            ele.num = res.counts;
          }
        });
        this.allNum += res.counts;
      });
    },
    changeMechanicsDatadata(val: any) {
      for (let key in val) {
        val[key].forEach((ele) => {
          //按照环境更改src
          if (process.env.NODE_ENV === "development") {
            ele.data = development.imgxhr + ele.data;
            ele.imgsrc = "";
          } else if (process.env.NODE_ENV === "production") {
            ele.data = production.imgxhr + ele.data; //正式环境+
            ele.imgsrc = "";
          }
        });
        this.mechanicsData.forEach((res) => {
          if (res.type == key) {
            res.data = val[key];
          }
        });
      }
      this.changeImgSrc();   
    },
    socketMechanicsDatadata(val: any) {
      val.forEach((res) => {
        this.mechanicsData.forEach((ele) => {
          if (res.dept == ele.type) {
            ele.data?.forEach((item) => {
              if (item.id == res.id) {
                item.status = res.status;
              }
            });
          }
        });
      });
      // this.changeImgSrc();
    },
    socketDeviceStatusInMF(val: any){
      this.mechanicsData.forEach((ele) => {
          ele.data?.forEach((item) => {
            if (item.id == val.id) {
              
              item.status == val.status?'':item.status = val.status;
            }
          });
      });
      // this.changeImgSrc();
    },
    changeloading(val: boolean) {
      this.loading = val;
    },
    changedevstatus(val: any) {
      let newdata = [];
      for (let key in val) {
        let data = [];
        let status = [0, 0, 0, 0];
        val[key].forEach((ele) => {
          if (ele.name == "H_ON") {
            data.push({
              value: ele.counts,
              name: "messages.work",          
              itemStyle:{
                color:'#91CC75'
              }
            });
            status[0] = 1;
          }
          if (ele.name == "H_IDLE") {
            data.push({
              value: ele.counts,
              name: "messages.halt",
              itemStyle:{
                color:'#AAAAAA'
              }
            });
            status[1] = 1;
          }
          if (ele.name == "H_OFF") {
            data.push({
              value: ele.counts,
              name:"messages.Standby",
              itemStyle:{
                color:'#FAC858'
              }
            });
            status[2] = 1;
          }
          if (ele.name == "H_E_STOP") {
            data.push({
              value: ele.counts,
              name: "messages.STOP",
              itemStyle:{
                color:'#EE6666'
              }
            });
            status[3] = 1;
          }
        });
        if (status[0] == 0) {
          data.push({
            value: null,
            name: "messages.work",
            itemStyle:{
              color:'#91CC75'
            }
          });
        }
        if (status[1] == 0) {
          data.push({
            value: null,
            name: "messages.halt",
            itemStyle:{
              color:'#AAAAAA'
            }
          });
        }
        if (status[2] == 0) {
          data.push({
            value: null,
            name: "messages.Standby",
            itemStyle:{
              color:'#FAC858'
            }
          });
        }
        if (status[3] == 0) {
          data.push({
            value: null,
            name: "messages.halt",
            itemStyle:{
              color:'#EE6666'
            }
          });
        }

        newdata.push({
          name: key,
          data: data,
        });
        data = [];
        status = [0, 0, 0, 0];
      }

      this.devstatus = newdata;
      console.log(newdata);
      
    },
    changeImgSrc() {
      this.mechanicsData.forEach((res) => {
        if (res.data) {
          if (res.index == this.page) {
            res.data.forEach((ele) => {
              ele.imgsrc = ele.data;
            });
          }
        }
      });
    },
  },
});
