<template>
  <a-layout :class="['layout', device]">
    <!-- SideMenu -->
    <a-drawer
      v-if="isMobile()"
      placement="left"
      :wrapClassName="`drawer-sider ${navTheme}`"
      :closable="false"
      :visible="collapsed"
      @close="drawerClose"
    >
      <side-menu
        mode="inline"
        :menus="menus"
        :theme="navTheme"
        :collapsed="false"
        :collapsible="true"
        @menuSelect="menuSelect"
      ></side-menu>
    </a-drawer>

    <side-menu
      v-else-if="isSideMenu()"
      mode="inline"
      :menus="menus"
      :theme="navTheme"
      :collapsed="collapsed"
      :collapsible="true"
    ></side-menu>

    <a-layout :class="[layoutMode, `content-width-${contentWidth}`]" :style="{ paddingLeft: contentPaddingLeft, minHeight: '100vh' }">
      <!-- layout header -->
      <global-header
        :mode="layoutMode"
        :menus="menus"
        :theme="navTheme"
        :collapsed="collapsed"
        :device="device"
        @toggle="toggle"
      />
      <!-- layout content -->
      <a-layout-content :style="{ height: '100%', margin: '31px 20px 0', paddingTop: multiTab&&fixedHeader ? '61px' : (!multiTab&&fixedHeader?'33px':'0') }">
        <transition name="page-transition">
          <!-- <route-view v-if="isRouterAlive"/> -->
          <router-view ></router-view>
        </transition>
      </a-layout-content>
      <!-- pdf 预览 -->
      <pdfViewModal ref="pdfview" :openModal="showPdfView" @cancel="showPdfView=false"></pdfViewModal>
      <!-- layout footer -->
      <a-layout-footer>
        <!-- <global-footer /> -->
      </a-layout-footer>
      <div id="print"></div>
      <!-- Setting Drawer (show in development mode) -->
      <!-- <setting-drawer v-if="!production"></setting-drawer> -->
    </a-layout>
  </a-layout>

</template>

<script>
import { triggerWindowResizeEvent } from '@/utils/util'
import { mapState, mapActions } from 'vuex'
import { mixin, mixinDevice } from '@/utils/mixin'
import config from '@/config/defaultSettings'

import RouteView from './RouteView'
import SideMenu from '@/components/Menu/SideMenu'
import GlobalHeader from '@/components/GlobalHeader'
import GlobalFooter from '@/components/GlobalFooter'
// import SettingDrawer from '@/components/SettingDrawer'
import pdfViewModal from '@/views/common/pdfViewModal.vue'
import { asyncRouterMap } from '@/config/router.config'

export default {
  name: 'BasicLayout',
  mixins: [mixin, mixinDevice],
  provide () {
    return {
      reloadView: this.reloadView
    }
  },
  components: {
    RouteView,
    SideMenu,
    GlobalHeader,
    GlobalFooter,
    // SettingDrawer,
    pdfViewModal
  },
  data () {
    return {
      production: config.production,
      collapsed: false,
      menus: [],
      isRouterAlive: true,
      showPdfView: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.routers,
      multiTab: state => state.app.multiTab,
      fixedHeader: state => state.app.fixedHeader
    }),
    contentPaddingLeft () {
      if (!this.fixSidebar || this.isMobile() || this.layoutMode == 'topmenu') {
        return '0'
      }
      if (this.sidebarOpened) {
        return '160px'
      }
      return '80px'
    }
  },
  watch: {
    sidebarOpened (val) {
      this.collapsed = !val
    },
    // 打印预览
    '$store.state.app.showPdfPrintView' (newValue, oldValue) {
      console.log(newValue)
      if (newValue) {
        this.showPdfView = newValue
      }
    }
  },
  created () {
    if (this.mainMenu) {
      this.menus = this.mainMenu.find((item) => item.path === '/').children
    } else {
      // 没有权限时只显示首页
      const noqx = asyncRouterMap.find((item) => item.path === '/').children
      this.menus = [noqx.find(item => item.path === '/home')]
    }
    this.collapsed = !this.sidebarOpened
    this.$store.dispatch('ToggleMultiTab', true)
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }
  },
  methods: {
    ...mapActions(['setSidebar']),
    toggle () {
      this.collapsed = !this.collapsed
      this.setSidebar(!this.collapsed)
      triggerWindowResizeEvent()
    },
    paddingCalc () {
      let left = ''
      if (this.sidebarOpened) {
        left = this.isDesktop() ? '160px' : '80px'
      } else {
        left = (this.isMobile() && '0') || ((this.fixSidebar && '80px') || '0')
      }
      return left
    },
    menuSelect () {
    },
    drawerClose () {
      this.collapsed = false
    },
    reloadView () {
      this.isRouterAlive = false
      this.$nextTick(() => {
        this.isRouterAlive = true
      })
    }
  }
}
</script>

<style lang="less">
@import url('../components/global.less');
</style>
