<script setup lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
  import { AppRoute, NotFoundRoute, EnumPage } from '@/router/base';
  import { authRoutes } from '@/router/auth';
  import { filterAuthHandle } from '@/router/utils'
  import { AppStore } from '@/store/modules/app';
  import 'ant-design-vue/es/message/style';
  const router = useRouter()
  const route = useRoute()
  const store = AppStore()
  const isLogin = computed(() => store.isLogin)
  watch(isLogin, (val) => {
    if (![EnumPage.Login as string, EnumPage.Error as string].includes(route.path)) {
      if (!val) {
        router.push(EnumPage.Login)
        store.updateLoading(false)
      } else {
        // 此处模拟接口请求获取权限列表
        store.updateLoading(true)
        setTimeout(() => {
          const authFilterRoutes = filterAuthHandle([1], authRoutes)
          console.log(authFilterRoutes);
          const childrenRoutes = [...authFilterRoutes, ...AppRoute.children as []]
          router.removeRoute(AppRoute.name as string)
          router.addRoute({
            ...AppRoute,
            children: childrenRoutes
          })
          router.addRoute(NotFoundRoute)
          store.updateLoading(false)
          store.updateAuthRoutes(authFilterRoutes)
          router.replace(route.fullPath)
        }, 2e3);
      }
    }
  }, {
    immediate: true
  })
</script>

<template>
  <init-loading>
    <RouterView />
  </init-loading>
</template>

<style>
@import './style/index.css';
@import './style/global.css';
</style>
