import { setupLayouts } from 'virtual:meta-layouts'
import { createRouter, createWebHistory } from 'vue-router'
import { routes as fileRoutes } from 'vue-router/auto-routes'
import { userInfoAdmin } from '~/api/user/adminUserApi'
declare module 'vue-router' {}

// 重定向 BASE_URL
fileRoutes.flat(Infinity).forEach((route) => {
	route.path = safeResolve(route.path)
})
export const router = createRouter({
	history: createWebHistory(),
	routes: setupLayouts(fileRoutes),
})
// 路由拦截
router.beforeEach((to, from, next) => {

	if (to.fullPath.includes("/front")){
		console.log(to.fullPath)
		console.log(to)
	}
	// 管理员全部限制
	else if (to.fullPath.includes("/admin")){
		console.log("管理员认证~")
		// 	判断有没有登录
		if (userStore().adminToken === null || userStore().adminToken === undefined ||userStore().adminToken == ""){
			next('/login');
		}
	}

	// 在这里编写拦截逻辑
	// 例如，检查用户是否认证
	const  isLogin = userStore().isLogin
	const authList=["/"]
	if (authList.includes(to.fullPath) && !isLogin) {
		// 如果用户未认证，则跳转到登录页面
		next('/login');
	} else {
		// 如果用户已认证，或者不需要认证，则继续
		next();
	}
});

export default router
