<template>
  <router-view v-if="isRoutesReady"></router-view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { usePermissionStore } from '@/stores/permission'
import { useRouter } from 'vue-router'
import { GetDictionaryGetPage } from '@/api/system/index'
const isRoutesReady = ref(false)
const permissionStore = usePermissionStore()
const router = useRouter()

const getDictionaryGetPage = () => {
  const params = {
    LambdaExp: 'IsDeleted==false&&dicName.Contains("")',
    SelectSorts: '[{"FieldName":"CreateTime","IsAsc":false}]',
    PageNum: 1,
    PageSize: 10000
  }
  GetDictionaryGetPage(params).then(res => {
    if (res.StatusCode == 200) {
      sessionStorage.setItem('dictionarys', JSON.stringify(res.Data))
    }
  })
}

onMounted(async () => {
  getDictionaryGetPage()
  try {
    if (!permissionStore.isRoutesGenerated) {
      sessionStorage.removeItem('tabs')
      await permissionStore.generateRoutes()
    }
    // 检查当前路由是否存在
    if (router.currentRoute.value.matched.length === 0 &&
      router.currentRoute.value.path !== '/404') {
      router.replace('/home')
    }
    isRoutesReady.value = true
  } catch (error) {
    console.error('Failed to generate routes:', error)
    router.replace('/404')
  }
})
</script>