<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <sidebar class="sidebar-container" />
    <div :class="{hasTagsView:needTagsView}" class="main-container">
      <div id="fixedHeader" :class="{'fixed-header':fixedHeader}">
        <navbar />
        <tags-view v-if="needTagsView" />
      </div>
      <app-main />
      <right-panel v-if="showSettings">
        <settings />
      </right-panel>
    </div>
  </div>
</template>

<script>
import RightPanel from '@/components/RightPanel'
import { AppMain, Navbar, Settings, Sidebar, TagsView } from './components'
import ResizeMixin from './mixin/ResizeHandler'

export default {
  name: 'Layout',
  components: {
    AppMain,
    Navbar,
    RightPanel,
    Settings,
    Sidebar,
    TagsView
  },
  mixins: [ResizeMixin],
  computed: {
    sidebar() {
      return this.$store.state.setting.sidebar
    },
    device() {
      return this.$store.state.setting.device
    },
    showSettings() {
      return this.$store.state.setting.settingBar.opened
    },
    needTagsView() {
      return this.$store.state.setting.multipage
    },
    fixedHeader() {
      return this.$store.state.setting.fixHeader
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    },
    websocketMsg() {
	      return this.$store.getters.STAFF_UPDATE.msg
	  },
  },
  watch: {
	  	websocketMsg(curVal, oldVal) {
	      	let obj = JSON.parse(curVal)
	      	this.openMessageTips(obj)
	    }
	},
  mounted() {
    // 判断长连接，是否已经开启      
	  	if(this.$store.state.websocket.websock == null){
	  		// 页面刚进入时开启长连接
    		this.$store.dispatch('STAFF_WEBSOCKET')	        
	  	}      
  },
  methods: {
    handleClickOutside() {
      this.$store.commit('setting/closeSidebar', { withoutAnimation: false })
    },
    openMessageTips(data){      //排除数字大屏         
      if(this.$route.fullPath.indexOf("/largeScreen")<0){
        //console.log("接收websocket的推送信息 == ", data)        
        if(data!=null && data.type == 'PUSH_TYPE_GLOBAL_EXCEPTION'){          
          this.$nextTick(()=> {
              let datas = data.data.warnMap.warnData.records              
              datas.forEach((data)=>{
                  let msg = data.feedback ? data.feedback : " 响应超时 ";
                  //let url = data.taskNodeId ? "#/dispatchMgr/exception":"#/developer/warnLong";
                  let url = "#/developer/warnLong"
                  let content = "<a href='"+url+"'>"+(data.resourceName? "["+ data.resourceName +"] " : "" )+(data.procedureName? data.procedureName : "")
                   + (data.instructionName? "("+data.instructionName+")" : "") + msg+"</a>";                  
                  this.$notification.error(content, { messageIsHTML: true, timer:10, title:"异常警报", showCloseIcn:true });
              })
          });
        }
      }
    },
    // 组件销毁的时候，关闭websocket连接
	  websocketClose() { 
	      this.$store.getters.STAFF_UPDATE.lockReconnect = true
	      this.$store.getters.STAFF_UPDATE.websock.close() // 离开路由之后断开websocket连接
	      clearTimeout(this.$store.getters.STAFF_UPDATE.reconnectData) // 离开清除 timeout
	      clearTimeout(this.$store.getters.STAFF_UPDATE.timeoutObj) // 离开清除 timeout
	      clearTimeout(this.$store.getters.STAFF_UPDATE.serverTimeoutObj) // 离开清除 timeout
	    },
	  	// websocket信息变更
	  onmessage() {
	      this.$store.getters.STAFF_UPDATE.websock.onmessage = function(evt) {	       
		     	console.log("websocket获取数据==="+evt)
	      }
	  },
  }
}
</script>

<style lang="scss" scoped>
  @import "~@/styles/mixin.scss";
  @import "~@/styles/variables.scss";

  .app-wrapper {
    @include clearfix;
    position: relative;
    height: 100%;
    width: 100%;

    &.mobile.openSidebar {
      position: fixed;
      top: 0;
    }
  }

  .drawer-bg {
    background: #000;
    opacity: 0.3;
    width: 100%;
    top: 0;
    height: 100%;
    position: absolute;
    z-index: 999;
  }

  .fixed-header {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 9;
    width: calc(100% - #{$sideBarWidth});
    transition: width 0.28s;
  }

  .hideSidebar .fixed-header {
    width: calc(100% - 54px)
  }

  .mobile .fixed-header {
    width: 100%;
  }

  .dialog-notice{
    position: fixed;
    bottom: 85px;
    display: inline-block;
    max-width: 300px;
    left: 0;
    right: 0;
    margin: 0 auto 10px auto;
    background-color: #000;
    text-align: center;
    z-index: 9999;
}
</style>
