import {createRouter, createWebHistory} from "vue-router";
import Login from "./components/Login.vue";
import Home from "./components/home.vue"
import Panel from "./components/panel.vue";
import Page1 from "./components/page1.vue";
import Page2 from "./components/page2.vue";
import Page3 from "./components/page3.vue";

const routes = [
    { path: "/", redirect: "/login" },
    { path: "/login", component: Login },
    {
        path: "/panel",
        component: Panel,
        redirect: "/panel/home",
        children: [
            { path: "home", component:Home},
            { path: "page1", component: Page1 },
            { path: "page2", component: Page2 },
            { path: "page3", component: Page3 },
        ],
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

router.beforeEach((to, from, next) => {
    const token = localStorage.getItem("bm_token");
    if (to.meta['requiresAuth'] && !token) {
        return next('/login');
    }
    if (to.path === "/login" && token) {
        return next('/panel');
    }
    next()
})



export default router;