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

    <el-tabs v-model="activeName">
      <el-tab-pane label="消息"
                   name="first">
      </el-tab-pane>
    </el-tabs>
    <el-scrollbar style="height:300px">
      <avue-notice :data="data"
                   :option="option"
                   @page-change="pageChange"></avue-notice>
    </el-scrollbar>
    <div slot="reference">
      <el-badge  :value="badge">
        <i class="el-icon-bell"></i>
      </el-badge>
    </div>
  </el-popover>
</template>

<script>
export default {
  name: "top-notice",
  data () {
    return {
      activeName: 'first',
      option: {
        props: {
          img: 'img',
          title: 'title',
          subtitle: 'subtitle',
          tag: 'tag',
          status: 'status'
        },
      },
      data: [],
      path:"ws://192.168.1.151:8104/websocket?user=1",
      socket:"",
      badge:''
    }
  },
  created () {
    // this.webSocket();
  },
  mounted() {
     this.init();
  },
  methods: {
    //初始化
    init(){
      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
      }
    },
    open() {
      console.log("socket连接成功")
      console.log()
    },
    error() {
      console.log("连接错误")
    },
    getMessage(msg){
      console.log(msg.data)

      // let msgData =  JSON.stringify(msg.data);
      // console.log(msgData)
      // if(msg.data.unRead){
      //   this.badge = msg.data.unReadNum
      // }else{
      //   this.badge = ''
      // }
    },
    send() {
      // this.socket.send(params)
    },
    close() {
      console.log("socket已经关闭")
    },

    pageChange (page, done) {
      setTimeout(() => {
        this.$message.success('页码' + page)
        this.data = this.data.concat(list);
        done();
      }, 1000)

    },
  }
};
</script>

<style lang="scss" scoped>
</style>
