<template>
  <div :style="auiWrapper" v-loading.fullscreen.lock="loading" :element-loading-text="$t('loading')" class="aui-wrapper all-main-wraper">
    <template v-if="!loading">
      <div class="smp_main-t">
        <main-navbar ref="mainNavbar" v-if="navbarFlag"/>
      </div>
      <div class="smp_main-b" v-if="isNormalSidebar">
        <div class="smp_main-l" v-show="isMainMenuHaveChild">
          <main-sidebar v-if="sidebarFlag" ref="sidebar"/>
        </div>
        <div class="smp_main-r" :class="{'onClose':$store.state.sidebarFold,'main-no-left':!isMainMenuHaveChild}">
          <content-crumb v-if="isMainMenuHaveChild"></content-crumb>
          <main-content />
        </div>
      </div>
      <div class="smp_main-b" v-else>
        <div class="smp_main-r" :class="{'onClose':$store.state.sidebarFold,'main-no-left':true}">
          <basicCom :isSidebar="true"></basicCom>
        </div>
      </div>
    </template>
    <noticePop ref="noticePop"></noticePop>
    <taskPop ref="taskPop"></taskPop>
  </div>
</template>

<script>
import MainNavbar from './main-navbar'
import MainSidebar from './main-sidebar'
import MainContent from './main-content'
import debounce from 'lodash/debounce'
import ContentCrumb from './main-content-crumb'
import noticePop from './notice-pop'
import taskPop from './task-pop'
import basicCom from '@/views/modules/demo/basicComponent/basic-com'
export default {
  provide () {
    return {
      // 刷新
      refresh () {
        this.$store.state.contentIsNeedRefresh = true
        this.$nextTick(() => {
          this.$store.state.contentIsNeedRefresh = false
        })
      }
    }
  },
  data () {
    return {
      loading: true,
      isNormalSidebar: true,
      navbarFlag: false,
      sidebarFlag: false,
      mainContent: 'aui-content__wrapper',
      auiWrapper: '',
      isMainMenuHaveChild: true
    }
  },
  components: {
    MainNavbar,
    MainSidebar,
    MainContent,
    ContentCrumb,
    basicCom,
    noticePop,
    taskPop
  },
  watch: {
    $route: 'routeHandle'
  },
  created () {
    // this.windowResizeHandle()
    // this.routeHandle(this.$route)
    Promise.all([
      this.getUserInfo(),
      this.getPermissions()
    ]).then(() => {
      this.loading = false
    })
    this.loading = false
    this.navbarFlag = window.SITE_CONFIG['isNavbar']
    this.sidebarFlag = window.SITE_CONFIG['isSidebar']
    if (!this.sidebarFlag) {
      this.mainContent = ''
    }
    if (!this.navbarFlag) {
      this.auiWrapper = 'padding-top:0px;'
    }
    if (this.$route.name === 'JCZJ') {
      this.gotoWaterfallSidebar()
    }
  },
  methods: {
    // 窗口改变大小
    // windowResizeHandle () {
    //   this.$store.state.sidebarFold = document.documentElement['clientWidth'] <= 992 || false
    //   window.addEventListener('resize', debounce(() => {
    //     this.$store.state.sidebarFold = document.documentElement['clientWidth'] <= 992 || false
    //   }, 150))
    // },
    // 显示通知弹窗
    showNotice () {
      this.$refs.noticePop.showPop()
    },
    // 显示通知弹窗
    showTask () {
      this.$refs.taskPop.showPop()
    },
    // 切换导航
    gotoNormalSidebar () {
      this.isNormalSidebar = true
      this.$nextTick(() => {
        this.$refs.sidebar.remedyMenuNoChange()
      })
    },
    gotoWaterfallSidebar () {
      this.isNormalSidebar = false
    },
    // 路由, 监听
    routeHandle (route) {
      !this.isNormalSidebar && this.gotoNormalSidebar()
      if (route.name === 'JCZJ') {
        this.gotoWaterfallSidebar()
      }
      // !this.isNormalSidebar && this.gotoNormalSidebar()
      if (!route.meta.isTab) {
        return false
      }
      var tab = this.$store.state.contentTabs.filter(item => item.name === route.name)[0]
      if (!tab) {
        tab = {
          ...window.SITE_CONFIG['contentTabDefault'],
          ...route.meta,
          'name': route.name,
          'params': { ...route.params },
          'query': { ...route.query }
        }
        this.$store.state.contentTabs = this.$store.state.contentTabs.concat(tab)
      }
      this.$store.state.sidebarMenuActiveName = tab.menuId
      this.$store.state.contentTabsActiveName = tab.name
    },
    // 获取当前管理员信息
    getUserInfo () {
      return this.$http.get('/idaas/sso/user/info').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.$store.state.user.name = res.userInfo.displayName
        this.$store.state.user.organizations = res.userInfo.organizations[0].organizationName
      }).catch(() => {})
    },
    // 获取权限
    getPermissions () {
      return this.$http.get('/idaas/ps/user/buttonList').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.$store.dispatch('permission', res.buttonList)
        window.SITE_CONFIG['permissions'] = res.buttonList
      }).catch(() => {})
    }
  }
}
</script>
