<template>
  <div id="app">
    <div :class="[pwa ? 'c-pwa' : '', 'c-main']">
      <div class="c-wrap" v-if="toApp">
        <panel></panel>
        <router-view></router-view>
      </div>
      <div v-else class="c-login">
        <login-box></login-box>
      </div>
    </div>
    <div class="c-copyright">
      <span>copyright © 2019 MeeChat.</span>
    </div>
  </div>
</template>

<script>
// import ScatterJS from 'scatter-js/dist/scatter.esm'
import { getUrlParam, noticeManager } from '@/util/util.js'
import { accountLoginMixin } from '@/mixins/login'
import { mapState } from 'vuex'

import loginBox from '@/components/login/loginBox'
import panel from '@/components/panel/panel'

export default {
  name: 'App',
  mixins: [accountLoginMixin],
  components: {
    loginBox,
    panel
  },
  computed: {
    ...mapState({
      isLogin: state => state.chat.isLogin,
      toApp: state => state.chat.toApp,
      curSession: state => state.curSession,
      sessionList: state => state.chat.sessionList
    })
  },
  data () {
    return {
      pwa: getUrlParam('pwa') == '1'
    }
  },
  methods: {
    showUnreadNum () {
      let unreadNum = 0
      this.sessionList.forEach((item) => {
        if (item.is_mute == 0) unreadNum += Number(item.unread)
      })

      noticeManager.changeTitle(unreadNum > 99 ? '99+' : unreadNum)
    }
  },
  async created () {
    this.checkLocalLogin()
  },
  mounted () {
    // 监听浏览器tab切换事件
    let tabTimer = null
    // let lastUnreadNum = 0
    document.addEventListener('visibilitychange', () => {
      if (document.visibilityState !== 'hidden') {
        // 切换回当前页面
        noticeManager.changeTitle(0)
        if (tabTimer) clearInterval(tabTimer)
      } else {
        this.showUnreadNum()
        tabTimer = setInterval(() => {
          this.showUnreadNum()
        }, 2000)
      }
    })

    window.$router = this.$router
  }
}
</script>

<style lang="scss">
@import "@/style/global.scss";
</style>

<style lang="scss" scoped>
.c-main {
  height: 80%;
  min-height: 600px;
  padding-top: 100px;
  -webkit-transition: padding 0.3s linear;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}
@media (max-height: 800px), (max-width: 1000px) {
  .c-main {
    padding-top: 0;
    height: 100%;
  }
  .c-copyright {
    display: none;
  }
}

.c-wrap {
  position: relative;
  // display: flex;
  max-width: 1066px;
  min-width: 800px;
  height: 100%;
  margin: 0 auto;
  border-radius: 3px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  overflow: hidden;
  padding-left: 350px;
  box-sizing: border-box;
  // background: #eee;
}
.c-copyright {
  position: absolute;
  bottom: 30px;
  width: 100%;
  text-align: center;
  font-size: 12px;
  color: #e3e3e3;
}

.c-pwa {
  height: 100%;
  padding-top: 0;
}

.c-pwa .c-wrap {
  max-width: 100%;
}

</style>
