import * as echarts from "echarts";
import { msgBoradTabel, msgBoradNum, responMsg } from "../../api/messageBoard"

export default {
  data() {
    return {
      tableData: [

      ],
      msg: "",
      dialogVisible: false,
      socketData: null,
      intervalName: [{
        value: "1m",
        label: "1分钟K线"
      },
      {
        value: "3m",
        label: "3分钟K线"
      },
      {
        value: "5m",
        label: "5分钟K线"
      },
      {
        value: "15m",
        label: "15分钟K线"
      },
      {
        value: "30m",
        label: "30分钟K线"
      },
      {
        value: "1h",
        label: "1小时K线"
      },
      {
        value: "2h",
        label: "2小时K线"
      },
      {
        value: "4h",
        label: "4小时K线"
      },
      {
        value: "6h",
        label: "6小时K线"
      },
      {
        value: "8h",
        label: "8小时K线"
      },
      {
        value: "12h",
        label: "12小时K线"
      },
      {
        value: "1d",
        label: "1天K线"
      },
      {
        value: "3d",
        label: "3天K线"
      },
      {
        value: "1w",
        label: "周K线"
      },
      {
        value: "1M",
        label: "月K线"
      },

      ],
      tablesName: [{
        value: "btcusdt",
        label: "U本位BTC合约",
      },
      {
        value: "btcstocks",
        label: "BTC现货",
      },
      {
        value: "btcusd",
        label: "币本位BTC合约",
      },
      {
        value: "ethusdt",
        label: "U本位ETH合约",
      },
      {
        value: "ethstocks",
        label: "ETH现货",
      },
      {
        value: "ethusd",
        label: "币本位ETH合约",
      },

      ],
      tables: ["btcstocks", "btcusd", "btcusdt", "ethstocks", "ethusd", "ethusdt"],
      tablesZh: ["BTC现货", "币本位BTC合约", "U本位BTC合约", "ETH现货", "币本位ETH合约", "U本位ETH合约"],
      ws: null,
      tableData2: [],
      timer:null,
      heartbeat:0,
      heartbeatDetection:null,//心跳检测


    }
  },
  mounted() {


  
    this.handelWebSocket()
  },
  methods: {
   
    // 连接WebSocket 处理数据
    handelWebSocket() {

      this.ws = new WebSocket(`ws://18.139.234.105:12345`);
      //连接成功
      this.ws.onopen = () => {
        console.log("连接成功");      
        //连接成功发送消息
        let obj = {
          id: 1,
          channel: "check"
        }
        this.ws.send(JSON.stringify(obj))
        //清除定时器
        clearInterval(this.timer)
        clearTimeout(this.heartbeatDetection)
        //心跳
        this.timer = setInterval(() => {
          this.ws.send('ping');
          this.heartbeat = 0
           //心跳检测
        this.heartbeatDetection=setTimeout(()=>{        
          if (this.heartbeat===0) {//心跳断开 重连
            this.ws = new WebSocket(`ws://18.139.234.105:12345`);
          }
        },3000)
        }, 900*1000); // 15分钟 发送一个心跳包      


      }
      //连接失败重连
      this.ws.onerror = () => {        
        this.heartbeat = 0
          //清除定时器
          clearInterval(this.timer)
          clearTimeout(this.heartbeatDetection)
          this.ws = new WebSocket(`ws://18.139.234.105:12345`);
      }
      this.ws.onmessage = (data) => {//优化做数据验证
        console.log(data, 65623);
        if (data.data == "pong") {
            this.heartbeat = 1 //心跳未断开 赋值
        }else{
           this.socketData = JSON.parse(data.data)
           if (this.socketData.update_time) {//确认有值 在初始化结构对象
            //初始化数据结构对象
            this.handelTables()
          }
  
          this.tableData.forEach(item => {//赋值显示
            if (item.interval == this.socketData.interval) {
              item.newTime = this.socketData.update_interval //更新时间
              item.time = this.socketData.update_time//查询时间
              item.update_interval = this.socketData.update_interval//更新间隔
              this.socketData.tables.forEach(subItem => {
                if (subItem.name == item.currency) {
                  item.tableTime = subItem.time
                }
  
              })
  
  
            }
            //状态赋值
            switch (item.interval) {
              case "1m":
                let queryTime
                let updateTime
                let interval
                interval = 60 * 1000
                queryTime = new Date(item.time).getTime();
                updateTime = new Date(item.tableTime).getTime();
  
                if (item.time && (updateTime + interval) > queryTime) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "3m":
  
                let interval3 = 3 * 60 * 1000
  
                if (item.time && (new Date(item.tableTime).getTime() + interval3) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "5m":
                let interval5 = 5 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval5) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "15m":
                let interval15 = 15 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval15) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "30m":
                let interval30 = 30 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval30) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "1h":
                let interval1h = 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval1h) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "2h":
                let interval2h = 2 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval2h) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "4h":
  
                let interval4h = 4 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval4h) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "6h":
                let interval6h = 6 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval6h) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "8h":
                let interval8h = 8 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval8h) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "12h":
                let interval12h = 12 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval12h) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "1d":
                let interval1d = 24 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval1d) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "3d":
                let interval3d = 3 * 24 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval3d) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "1w":
                let interval1w = 7 * 24 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval1w) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
  
                break;
              case "1M":
                let interval1M = 30 * 24 * 60 * 60 * 1000
                if (item.time && (new Date(item.tableTime).getTime() + interval1M) > new Date(item.time).getTime()) {
                  item.state = "正常"
                } else {
                  item.state = "异常"
                }
                break;
              default:
  
                break;
            }
            //异常状态排序
            let state = "异常"
            this.tableData.sort((a, b) => {
              if (a.time && b.time) {//保证有值得情况下在排序
                if (a.state === state && b.state !== state) {
                  return -1; // a 在前
                }
                if (a.state !== state && b.state === state) { // 如果 b 的 state 值等于指定值，而 a 的不等于
                  return 1; // b 在前
                }
  
                return 0; // 保持原来的顺序
              }
  
            })
  
          })
          //过滤空数据
          this.tableData2 = this.tableData.filter(item => item.newTime)
        
        }
       
     
        // 判断消息类型，如果是心跳包则进行处理
        // if (this.socketData.type === 'ping') {
        //   const pongData = { type: 'pong' };
        //   socket.send(JSON.stringify(pongData));
        //   lastHeartbeatTime = Date.now();
        // }
        
        


      }



    },
    //处理表格对象动态生成
    handelTables() {
      if (this.tableData.length > 0) return //判断有值不在重复初始化
      this.intervalName.forEach((item, index) => {
        for (let i = 0; i < this.tables.length; i++) {
          let obj = {}
          obj[this.tables[i]] = ""
          obj.name = `${this.tablesZh[i]} ${item.label}`
          obj.time = ""
          obj.interval = item.value
          obj.newTime = ""
          obj.state = ""
          obj.update_interval = ""
          obj.currency = this.tables[i]
          obj.tableTime = ""

          this.tableData.push(obj)
        }

      });

    },

    cellStyle(row) {//改变正常异常颜色
      if (row.row.state == "正常" && row.column.label === "状态") {
        return "color:#000"
      } else if (row.row.state == "异常" && row.column.label === "状态") {
        return "color:red"
      }
    },
    //定时向服务器发送心跳信息
    onSend(socket) {
      const heartbeatInterval = 10000; // 心跳间隔，单位毫秒
      const pingData = { type: 'ping' }; // 心跳包数据

       let  lastHeartbeatTime; // 上次心跳时间

      const sendPing = () => {
        socket.send(JSON.stringify(pingData));
        lastHeartbeatTime = Date.now();
      };
      setInterval(sendPing, heartbeatInterval);
    }

  },
  beforeDestroy() {
    //关闭WebSocket 
    if (this.ws) {
      this.ws.close();

    }
  },





}