<template>
  <div :class="classObj" class="app-wrapper">
    <top-nav ref="topnav" />
    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <sidebar v-if="!isIndex" class="sidebar-container" />
    <div :class="{hasTagsView:needTagsView,isIndex:isIndex}" class="main-container scroll-container" @wheel.native.prevent="handleScroll">
      <div :class="{'fixed-header':fixedHeader}">
        <!-- <navbar /> -->
        <tags-view v-if="needTagsView" @changeTopNav="changeTopNav" />
      </div>
      <app-main />
      <right-panel v-if="showSettings">
        <settings />
      </right-panel>
    </div>
  </div>
</template>

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

export default {
  name: 'Layout',
  components: {
    AppMain,
    // Navbar,
    RightPanel,
    Settings,
    Sidebar,
    TagsView,
    TopNav
  },
  mixins: [ResizeMixin],
  computed: {
    ...mapState({
      sidebar: state => state.app.sidebar,
      device: state => state.app.device,
      showSettings: state => state.settings.showSettings,
      needTagsView: state => state.settings.tagsView,
      fixedHeader: state => state.settings.fixedHeader,
      toplist: state => state.user.toplist,
      topnav: state => state.user.topnav
    }),
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    },
    isIndex() {
      var activeList = this.toplist.filter(item => item.id == this.topnav + '')[0]
      return activeList.name == '首页' && this.$route.path == '/dashboard'
	  // return false
    }
  },
  methods: {
    changeTopNav() {
      this.$refs.topnav.setVisibleNumber()
    },
    handleClickOutside() {
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
    }
  }
}
</script>

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

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

    &.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%;
  }
  .isIndex{
    margin-left: 20px !important;
  }
  .mobile .isIndex{
    margin-left: 10px !important;
  }
</style>
