<template>
  <el-popover placement="bottom"
              width="350"
              trigger="click">

    <el-tabs v-model="activeName" style="align-items: center" @tab-click="tabsHandle">
      <el-tab-pane label="未读"
                   name="unread">
      </el-tab-pane>
      <el-tab-pane label="已读"
                   name="read">
      </el-tab-pane>
    </el-tabs>
    <el-scrollbar style="height:300px">
      <avue-notice :data="data"
                   :option="option"
                   @click="goUrl"
                   @page-change="pageChange"
                   v-loading="loading"
      >
      </avue-notice>
    </el-scrollbar>
    <div slot="reference" @click="init">
      <el-badge  :value="badge">
        <i class="el-icon-bell"></i>
      </el-badge>
    </div>
  </el-popover>
</template>

<script>
  import { getMsgLogs , getMsgDetail,getMessage } from "@/api/logs";
export default {
  name: "top-notice",
  data () {
    return {
      activeName: 'unread',
      intercept: 'unread',
      option: {
        props: {
          img: 'img',
          title: 'messageBody',
          subtitle: 'createTime',
          tag: 'tag',
          status: 'messageType'
        },
      },
      data: [],
      unreadData: [], // 未读的
      readData: [], // 已读的
      path:"",
      socket:"",
      badge:'',
      loading: false
    }
  },
  created () {
    // this.webSocket();
  },
  mounted() {
    //60秒轮训消息
    window.setInterval(() => {
      setTimeout(() => {
        this.assistMessage()
      },0)
    },60000)
    this.path = "wss://trade.tubaosoft.com/api/blade-client/websocket?user=" + this.$store.getters.userInfo.user_id
     this.init();
  },
  methods: {
    //初始化
    init(){
      this.loading = true
      getMsgLogs().then(res=>{
        this.afterData(res.data.data)
        this.loading = false
      })
      if(typeof(WebSocket) === "undefined"){
        alert("您的浏览器不支持socket")
      }else{
        // 实例化socket
        this.socket = new WebSocket(this.path)
        // 监听socket连接
        this.socket.onopen = this.open
        // 监听socket错误信息
        this.socket.onerror = this.error
        // 监听socket消息
        this.socket.onmessage = this.getMessage
        this.assistMessage()
      }
    },
    open() {
      // console.log("socket连接成功")
    },
    error() {
      console.log("连接错误")
    },
    getMessage(msg){
      let msgData = JSON.parse(msg);
      if(msgData.data.unRead){
        this.badge = msgData.data.unReadNum
      }else{
        this.badge = ''
      }
    },
    send() {
      // this.socket.send(params)
    },
    close() {
      console.log("socket已经关闭")
    },
    assistMessage(){
      //辅助消息接口
      getMessage().then(res=>{
        this.getMessage(JSON.stringify(res))
      })
    },
    goUrl(row){
      if(row.url){
        this.$router.push({
          path: row.url,
          query: {params:row.parameter},
        });
      }
      getMsgDetail(row.id).then(res=>{
        this.$set(row,"isRead",res.data.data.isRead)
        this.afterData(this.data)
        this.assistMessage()
      })
    },
    afterData(data){
      if(data.length === 0 ) return this.data = []
      if (this.activeName == 'unread') {
        this.unreadData = data.filter(item => item.isRead == 0)
        this.unreadData.forEach(item => {
          item.tag = "未读"
          item.messageType = 1//状态  0 灰色  1 蓝色 2 橙色 3 红色 4 绿色
        })
        this.data = this.unreadData
      } else {
        this.readData = data.filter(item => item.isRead != 0)
        this.readData.forEach(item => {
          item.tag = "已读"
          item.messageType = 0
        })
        this.data = this.readData
      }
      // this.data = data.map(item =>{
      //   if(item.isRead == 0){
      //     item.tag = "未读"
      //     item.messageType = 1//状态  0 灰色  1 蓝色 2 橙色 3 红色 4 绿色
      //   }else{
      //     item.tag = "已读"
      //     item.messageType = 0
      //   }
      //   return item
      // })
    },
    pageChange (page, done) {

    },
    tabsHandle(data) {
      if (this.intercept == data.name) return
      this.intercept = data.name
      this.loading = true;
      getMsgLogs().then(res=>{
        this.afterData(res.data.data)
        this.loading = false;
      })
    }
  }
};
</script>

<style lang="scss" scoped>
  ::v-deep .avue-notice__more {
    display: none;
  }
  ::v-deep .avue-notice__title {
    display: flex;
    align-items: center;
  }
  ::v-deep .avue-notice__tag {
    margin-left: 15px;
  }
</style>
