<template>
  <el-container class="app-wrapper" :class="[{ hideSidebar: isCollapsed }, { 'font-big': isFontBig }, { 'expire-top': expire }]" @click="setTime">
    <SystemTip />
    <CommonHeader :match-path="matchPath"></CommonHeader>
    <el-container style="overflow: hidden">
      <Sidebar v-if="showSidebar" @toggleCollapsed="toggleCollapsed" :isCollapsed="isCollapsed" :menuData="menuData" :isLimitLevel="true" :matchPath="matchPath" :basePath="basePath"></Sidebar>
      <el-container class="main-container">
        <ThirdMenu :menuData="thirdMenuData" v-if="thirdMenuData.children"></ThirdMenu>
        <el-main class="main-body">
          <el-scrollbar class="custom-scrollbar" style="flex: 1">
            <transition enter-active-class="fadeInUp" mode="out-in">
              <!-- <router-view v-if="$route.path === '/404'"></router-view> -->
              <div id="subapp-viewport" class="app-view-box"></div>
            </transition>
            <!-- <el-backtop target=".custom-scrollbar .el-scrollbar__wrap" :right="5"></el-backtop> -->
          </el-scrollbar>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { watch, ref, computed, unref } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { trimStart, cloneDeep } from 'lodash-es'
import Sidebar from './components/sidebar/Sidebar.vue'
import ThirdMenu from './components/ThirdMenu.vue'
import CommonHeader from './components/Header.vue'
import SystemTip from './components/SystemTip.vue'
import useRouteItem from '@/hooks/useRouteItem'

// import startApp from '@/core/register'
export default {
  components: {
    CommonHeader,
    SystemTip,
    Sidebar,
    ThirdMenu
  },
  data() {
    return {
      matchPath: ''
    }
  },
  setup() {
    const route = useRoute()
    const store = useStore()
    // 初始化
    function init() {
      store.commit('permission/SET_BUTTONS')
      store.dispatch('GetSystemConfigs')
    }
    init()
    // onMounted(() => {
    //   console.log('micro apps register')
    //   startApp()
    // })
    // 获取三级菜单数据
    const thirdMenuData = ref({})
    function getThirdMenu(data, matchPath) {
      for (const items of data) {
        if (matchPath.includes(items.path) && items.children) {
          if (matchPath === items.path) {
            const thirdMenu = cloneDeep(items)
            // 对三级菜单隐藏的数据做剔除
            const children = []
            items.children = items.children || []
            items.children.forEach(_ => {
              if (!_.hidden) children.push(_)
            })
            thirdMenuData.value = Object.assign(thirdMenu, { children: children.length ? children : null })
          } else {
            getThirdMenu(items.children, matchPath)
          }
          break
        }
      }
    }
    // 截取当前路径
    function getLevelPath(level) {
      return `/${trimStart(route.path, '/').split('/', level).join('/')}`
    }
    // 处理路径，获取匹配的路由以及获取第三级菜单的数据
    function handlePath() {
      const matchPath = getLevelPath(3)
      // 确保刷新先更新menuData再去获取三级菜单数据
      setTimeout(() => {
        const pathLen = route.path.split('/').length
        thirdMenuData.value = {}
        if (pathLen >= 6) {
          // 寻找四级菜单
          getThirdMenu(addRoutes.value, getLevelPath(4))
        } else if (pathLen >= 5) {
          // 寻找三级菜单
          getThirdMenu(addRoutes.value, matchPath)
        }
      })
    }
    watch(
      () => route.path,
      () => {
        handlePath()
      },
      {
        immediate: true
      }
    )
    watch(thirdMenuData, (current, pre) => {
      // 当三级菜单数据有变化是触发, 有三级菜单默认收缩主菜单
      if (!current.children === !pre.children) return
      store.commit('SET_COLLAPSED', !!thirdMenuData.value.children)
    })
    const toggleCollapsed = (value, source) => {
      if (typeof value === 'boolean') {
        // 窗口改变时，发现有三级菜单直接返回
        if (source === 'resize' && thirdMenuData.value.children) return
        store.commit('SET_COLLAPSED', value)
      } else {
        store.commit('TOGGLE_SIDEBAR')
      }
    }
    const addRoutes = computed(() => store.state.permission.addRoutes)
    const menuData = computed(() => store.state.app.sideMenuData)
    const { routeItem } = useRouteItem()
    const showSidebar = computed(() => {
      return routeItem.value && !routeItem.value.meta.hiddenSide && unref(menuData).length
    })
    function setTime() {
      store.commit('SET_OPERATETIME')
    }
    return {
      isCollapsed: computed(() => store.state.app.isCollapsed),
      basePath: computed(() => store.state.app.basePath),
      expire: computed(() => store.state.app.expire),
      isFontBig: computed(() => store.state.app.pageConfig.contentFontSize === 'big'),
      addRoutes,
      menuData,
      showSidebar,
      thirdMenuData,
      toggleCollapsed,
      setTime
    }
  },
  methods: {
    // 选择菜单处理，主要处理当前菜单二次点击
    selectItem() {
      // const matchPath = [path, `${path}/list`]
      // if (matchPath.includes(this.$route.path)) {
      //   setTimeout(() => {
      //     this.$router.push({ name: 'Redirect', query: { path: path } })
      //   })
      // }
    }
  }
}
</script>
<style lang="scss" scoped>
.app-wrapper {
  background: #e7f4ff;
  flex-direction: column;
  min-width: 1200px;
  overflow: hidden;
}

.main-container {
  position: relative;
  height: 100%;
  background: #f5f7f9;
  .main-body {
    padding: 0 16px 12px 16px;
    display: flex;
    flex-direction: column;
  }
}
</style>
