import {createRouter, createWebHashHistory} from 'vue-router'

const routes = [
    {
        path: '/',
        redirect: '/Login'
    },
    {
        path: '/Home',
        name: 'Home',
        redirect: '/BorrowRoom',
        component: () => import('../views/Home.vue'),
        //---------------------教室借用相关-----------------------------
        children: [
            {
                path: '/BorrowRoom',
                name: 'BorrowRoom',
                component: () => import('../views/BorrowRoom/BorrowRoom.vue')
            },
            {
                path: '/RecordList',
                name: 'RecordList',
                component: () => import('../views/BorrowRoom/RecordList.vue')
            },
            //---------------------管理员部分-----------------------------
            {
                path: '/Admit',
                name: 'Admit',
                component: () => import('../views/Admin/Admit.vue')
            },
            {
                path: '/UserManage',
                name: 'UserManage',
                component: () => import('../views/Admin/UserManage.vue')
            },
            {
                path: '/System',
                name: 'System',
                component: () => import('../views/Admin/System.vue')
            },
            {
                path: '/Personal',
                name: 'Personal',
                component: () => import('../views/Personal.vue')
            },
            //---------------------教师--------------------------------------------------------------------------
            {
                path: '/ApplyExperiment',
                name: 'ApplyExperiment',
                component: () => import('../views/ApplyExperiment/ApplyExperiment.vue')
            }, {
                path: '/ExperimentCheck',
                name: 'ExperimentCheck',
                component: () => import('../views/ApplyExperiment/ExperimentCheck.vue')
            },
            {
                path: '/Sub',
                name: 'Sub',
                component: () => import('../views/Student/Sub.vue')
            }, {
                path: '/Log',
                name: 'Log',
                component: () => import('../views/Admin/Log.vue')
            }
        ]
    },
    //---------------------用户/Admin--------------------------------------------------------------------
    {
        path: '/Login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },


]

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

//导航守卫
//开启前先做白名单
router.beforeEach((to, from, next) => {
    //页面拦截
    if (to.name !== 'Login') {
        let role = window.sessionStorage.getItem("role");
        // let token = window.sessionStorage.getItem("token");
        if (!role) {
            // return next({ name: 'Login' });
        }
        return next();
    } else {
        return next();
    }


})

export default router
