// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
const routes = [
    // 添加更多路由信息
    {
        path: "/401",
        component: () => import("@/pages/401/NotFind.vue"),
        meta: {
            name: "401"
        }
    },
    {
        path: "/login",
        component: () => import("@/pages/login/LoginNow.vue"),
        meta: {
            name: "login"
        }
    },
    {
        path: "/77",
        component: () => import("@/pages/mapLoad/FatherMap.vue")
    },
    {
        path:"/draw",
        component:()=>import("@/pages/components/draw/DrawDesigns.vue")
    },
    {
        path: "/home",
        component: () => import("@/pages/home/HomePage.vue"),
        meta: {
            name: "home"
        },
        children: [
            {
                path: "/home/report",
                component: () => import("@/pages/report/ReportVue.vue"),
                meta: {
                    name: "report"
                }
            },
            {
                path: "/home/mission",
                component: () => import("@/pages/mission/MissionVue.vue"),
                meta: {
                    name: "mission"
                }
            },
            {
                path: "/home/int",
                component: () => import("@/pages/intMap/IntMap.vue"),
                meta: {
                    name: "int"
                }
            },
            {
                path: "/home/scdMap",
                component: () => import("@/pages/scdCanLook/ScdCanLook.vue"),
                meta: {
                    name: "scdMap"
                }
            },
            {
                path:"/home/setting",
                component:()=>import("@/pages/system/SystemPage.vue"),
                meta:{
                    name:"setting"
                }
            },
            {
                path:"/home/system",
                component:()=>import("@/pages/setting/SettingPage.vue"),
                meta:{
                    name:"system"
                }
            },
            {
                path:"/home/netStructPicture",
                component:()=>import("@/pages/netStructPicture"),
                meta:{
                    name:"netStructPicture"
                }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})
router.beforeEach((to, from, next) => {
    // 假设您有一个名为 isAuthenticated 的全局变量来表示用户是否已登录
    let isAuthenticated = false; // 请替换为实际的登录状态判断逻辑
    let loginStatus = sessionStorage.getItem("loginStatus")
    if (loginStatus === 'true') {
        isAuthenticated = true
    }
    // 如果用户已登录，正常导航到目标路由
    if (isAuthenticated) {
        next();
    } else {
        ElMessage({
            type: "error",
            message: "您还未登录"
        })
        // 如果用户未登录，重定向到登录页
        if (to.path !== '/login') {
            next('/login');
        } else {
            // 如果用户已经在登录页，继续正常导航
            next();
        }
    }
})
export default router
