<template>
  <div :class="{'has-logo':showLogo}">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu :default-active="activeMenu" :collapse="isCollapse" :background-color="variables.menuBg"
        :text-color="variables.menuText" :unique-opened="true" :active-text-color="variables.menuActiveText"
        :collapse-transition="false" mode="vertical">
        <sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
  import {
    mapGetters
  } from 'vuex'
  import Logo from './Logo'
  import SidebarItem from './SidebarItem'
  import variables from '@/styles/variables.scss'

  export default {
    components: {
      SidebarItem,
      Logo
    },
    computed: {
      ...mapGetters([
        'sidebar'
      ]),
      routes() {
        console.log("----------------------", this.$router.options.routes)
        // this.socketInfo()
        return this.$router.options.routes
      },
      socketInfo() {
        // var staffId = JSON.parse(localStorage.getItem('winseaview-userInfo')).content.staffId
        var staffId = "84f62127b7384dcdbaeaddfe460329fc"

        // getAdminId().toPromise().then(response => {console.log(11111)})
        this.loading = false
        // WebSocket
        // this.$store.dispatch('setAdminId', response.data.data)
        if ('WebSocket' in window) {
          if (process.env.NODE_ENV === 'production') {
            this.websocket = new WebSocket('wss://www.zthymaoyi.com/wss/websocket/' + staffId)
          } else {
            //  this.websocket = new WebSocket('ws://192.168.1.115:8090/commonUser/api/onOpen?adminId=84f62127b7384dcdbaeaddfe460329fc' )
            // this.websocket = new WebSocket('ws://192.168.1.119:9100/websocket/' + staffId)
            this.websocket = new WebSocket('ws://192.168.1.114:8090/websocket/' + staffId)
          }
          console.log("knjhdfkhedfkh",process.env.NODE_ENV)
          this.initWebSocket()
        } else {
          alert('当前浏览器不支持websocket')
        }
      },
      initWebSocket() {
        console.log("有该方法·",this.websocket)
        // 连接错误
        this.websocket.onerror = this.setErrorMessage
        // 连接成功
        this.websocket.onopen = this.setOnopenMessage

        // 收到消息的回调
        this.websocket.onmessage = this.setOnmessageMessage

        // 连接关闭的回调
        this.websocket.onclose = this.setOncloseMessage

        // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = this.onbeforeunload
      },
      setErrorMessage() {
        console.log('WebSocket连接发生错误   状态码：' + this.websocket.readyState)
      },
      setOnopenMessage() {
        // console.log(this.websocket)
        console.log('WebSocket连接成功    状态码：' + this.websocket.readyState)
      },
      setOnmessageMessage(event) {
        // console.log(3333)
        // 根据服务器推送的消息做自己的业务处理
        console.log('服务端返回：' + event.data)
        // var msg = event.data.split('$')
        // var that = this

        // this.$notify.warning({
        //   title: '新消息提醒',
        //   message: msg[0],
        //   duration: 0,
        //   onClick() {
        //     that.$router.push({
        //       path: msg[1]
        //     }) // 你要跳转的路由 还可以传参 当然也可以通过其他方式跳转
        //   }
        // })
        // var currentPage = that.$router.history.current.path
        // that.$router.push({
        //   path: '/'
        // })
        // that.$router.push({
        //   path: currentPage
        // })
      },
      setOncloseMessage() {
        // console.log(this.websocket)
        console.log('WebSocket连接关闭    状态码：' + this.websocket.readyState)
      },
      activeMenu() {
        const route = this.$route
        const {
          meta,
          path
        } = route
        // if set path, the sidebar will highlight the path you set
        if (meta.activeMenu) {
          return meta.activeMenu
        }
        return path
      },
      showLogo() {
        return this.$store.state.settings.sidebarLogo
      },
      variables() {
        return variables
      },
      isCollapse() {
        return !this.sidebar.opened
      }
    }
  }
</script>
