import {createRouter, createWebHistory,createWebHashHistory} from "vue-router";

// const routerHistory = createWebHistory();
const routerHash = createWebHashHistory();

const router = createRouter({
    // history: routerHistory,
    history: routerHash,
    routes: [   
        {
            path: "/",
            name: "Home",
            component: () => import ("../views/Login/Login.vue"),
            meta: {requireAuth: false},
            children:[ 
                {
                    path: "/", // 首页
                    name: "index",
                    component: () => import ("../views/Home/index.vue"),
                    meta: {requireAuth: false}
                }
            ]
        }, {
            path: "/login",
            name: "login",
            component: () => import ("../views/Login/Login.vue"),
            meta: {requireAuth: false}
        }, {
            path: "/cim", // CIM平台
            name: "cim",
            component: () => import ("../views/cim/Cim.vue"),
            meta: {requireAuth: true}
        }, {
            path: "/:pathMatch(.*)*",
            // name: "notFound",
            // component: () =>
            //     // import ("../views/Layer/NotFound.vue")
            //     import ("../views/server_supermarket/ServerSupermarket.vue")
            redirect: '/serverSupermarket'
        }, {
            path: "/register", 
            name: "register",
            component: () => import ("../views/Login/Register.vue"),
            meta: {requireAuth: false}
    }]
});

// 导航守卫
router.beforeEach((to, from, next) => {
    // 跳转到最顶部
    window.scroll(0, 0);

    if (to.meta.requireAuth == true) {
        let token = localStorage.getItem("Token");
        if (token == null || token == "") {
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        } else {
            next();
        }
    } else {
        console.log(to.path);
        if(to.path == '/login'){
            let token = localStorage.getItem("Token");
            if(token == null || token == ""){
                next();
            }else{
                
            }
        }else{
            next();
        }
        
    }

});

export default router;
