<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: '30px 20px 0', paddingTop: multiTab&&fixedHeader ? '66px' : (!multiTab&&fixedHeader?'45px':'0') }">
        <transition name="page-transition">
          <!-- <route-view v-if="isRouterAlive"/> -->
          <router-view ></router-view>
        </transition>
      </a-layout-content>

      <!-- 视频播放弹窗 -->
      <div v-if="isShowVideoVal">
        <a-modal
          v-model="isShowVideoVal"
          wrapClassName="vidio_cont"
          :footer="null"
          centered
          :maskClosable="false"
          :destroyOnClose="true"
          :width="1000"
          @cancel="closeVido">
          <video-player
            class="video-player vjs-custom-skin"
            ref="videoPlayer"
            :playsinline="true"
            :options="playerOptions"
          ></video-player>
        </a-modal>
      </div>
      <!-- pdf 预览 -->
      <pdfViewModal ref="pdfview" :openModal="showPdfView" @cancel="showPdfView=false"></pdfViewModal>
      <!-- 选择打印机 -->
      <selectPrint ref="selectPrint" :openModal="showSelectPrint" @cancel="showSelectPrint=false"></selectPrint>
      <!-- layout footer -->
      <a-layout-footer>
        <!-- <global-footer /> -->
      </a-layout-footer>

      <!-- Setting Drawer (show in development mode) -->
      <!-- <setting-drawer></setting-drawer> -->
    </a-layout>
  </a-layout>

</template>

<script>
import moment from 'moment'
import { triggerWindowResizeEvent } from '@/utils/util'
import { mapState, mapActions, mapGetters } from 'vuex'
import { mixin, mixinDevice, commonMixin } from '@/utils/mixin'
import config from '@/config/defaultSettings'
import pdfViewModal from '@/views/common/pdfViewModal.vue'
import selectPrint from '@/views/common/selectPrint.vue'
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 { asyncRouterMap } from '@/config/router.config'
import store from '@/store'
export default {
  name: 'BasicLayout',
  mixins: [mixin, mixinDevice, commonMixin],
  provide () {
    return {
      reloadView: this.reloadView,
      setIsHomeNav: this.setIsHomeNav
    }
  },
  components: {
    RouteView,
    SideMenu,
    GlobalHeader,
    GlobalFooter,
    SettingDrawer,
    pdfViewModal,
    selectPrint
  },
  data () {
    return {
      production: config.production,
      collapsed: false,
      menus: [],
      isRouterAlive: true,
      showPdfView: false,
      showSelectPrint: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.routers,
      multiTab: state => state.app.multiTab,
      fixedHeader: state => state.app.fixedHeader
    }),
    ...mapGetters(['nowRoute', 'isShowVideo']),
    isShowVideoVal: {
      get () {
        return this.$store.state.app.isShowVideo
      },
      set (newValue) {
        this.$store.state.app.isShowVideo = newValue
      }
    },
    contentPaddingLeft () {
      if (!this.fixSidebar || this.isMobile()) {
        return '0'
      }
      if (this.sidebarOpened) {
        return '180px'
      }
      return '80px'
    },
    playerOptions () {
      const playerOptions = {
        playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: true, // 如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          type: '', // 这里的种类支持很多种：基本视频格式、直播、流媒体等，具体可以参看git网址项目
          src: '' // url地址
        }],
        poster: '../assets/bg_movie@2x.png', // 你的封面地址
        // width: document.documentElement.clientWidth, //播放器宽度
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true // 全屏按钮
        }
      }
      const isSaleOrder = this.nowRoute.indexOf('salesQuery') > -1 // 当前展示是否为销售单模块
      const isSalesReturn = this.nowRoute.indexOf('salesReturn') > -1 // 当前展示是否为销售退货模块
      const isPurchaseOrder = this.nowRoute.indexOf('purchaseOrder') > -1 // 当前展示是否为采购单管理模块
      const isPurchaseReturn = this.nowRoute.indexOf('purchaseReturn') > -1 // 当前展示是否为采购退货模块
      if (isSaleOrder) {
        playerOptions.sources[0].src = 'https://jianguan-images.oss-cn-beijing.aliyuncs.com/helpvideo/1xiaoshou.mp4'
      }
      if (isSalesReturn) {
        playerOptions.sources[0].src = 'https://jianguan-images.oss-cn-beijing.aliyuncs.com/helpvideo/2xiaoshoutuihuo.mp4'
      }
      if (isPurchaseOrder) {
        playerOptions.sources[0].src = 'https://jianguan-images.oss-cn-beijing.aliyuncs.com/helpvideo/3caigou.mp4'
      }
      if (isPurchaseReturn) {
        playerOptions.sources[0].src = 'https://jianguan-images.oss-cn-beijing.aliyuncs.com/helpvideo/4caigoutuihuo.mp4'
      }
	    return playerOptions
    }
  },
  watch: {
    sidebarOpened (val) {
      this.collapsed = !val
    },
    '$store.state.app.showPdfPrintView' (newValue, oldValue) {
      console.log(newValue)
      if (newValue) {
        this.showPdfView = newValue
        this.$nextTick(() => {
          this.$refs.pdfview.setData(this.$store.state.app.pdfPrintList)
        })
      }
    },
    '$store.state.app.showSelectPrint' (newValue, oldValue) {
      console.log(newValue)
      if (newValue) {
        this.showSelectPrint = 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)
    this.$store.dispatch('ToggleColor', '#2A86F4')
    this.$store.dispatch('ToggleTheme', localStorage.getItem('DEFAULT_THEME') || 'dark')
    this.$store.dispatch('ToggleFontSize', localStorage.getItem('DEFAULT_FONT_SIZE') || 'small')
    // 判断是否是当月25日后
    this.$store.state.app.isLastDayForMonth = moment().date() >= 25
    if (this.$store.state.app.isLastDayForMonth && this.$hasPermissions('M_mothEndTips')) {
      this.$notification.warning({
        message: '提示',
        description: `临到月底了，请尽快处理系统中没有完结的单据！`
      })
    }

    // 打印设置
    this.$store.commit('SET_pdfPrintList', [])
    this.$store.commit('SET_showPdfPrint', false)
    this.$store.commit('SET_showSelectPrint', false)
    this.$store.commit('SET_printLoading', false)
    this.$store.commit('SET_printDefNeedle', localStorage.getItem('DEFAULT_PRINT_NEEDLE') || undefined)
    this.$store.commit('SET_printDefInk', localStorage.getItem('DEFAULT_PRINT_INK') || undefined)
  },
  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']),
    // 关闭视频弹窗
    closeVido () {
      store.commit('IS_ShOW_VIDEO', false)
    },
    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>
