import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router' 
import Home from '../views/Home/Home.vue'
import { isLogin } from '../utils/index'

const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/home'
        },
        {
            path: '/home',
            name: 'home',
            meta: {
                needAuth: true
            },
            component: Home 
        },
        {
            path: '/user',
            meta: {
                needAuth: true
            },
            name: 'user',
            component: () => import('../views/User/User.vue')
        },
        {
            path: '/examPreOrder',
            meta: {
                needAuth: true
            },
            name: 'examPreOrder',
            component: () => import('../views/ExamPreOrder/ExamPreOrder.vue')
        },
        {
            path: '/examReport',
            meta: {
                needAuth: true
            },
            name: 'examReport',
            component: () => import('../views/ExamReport/ExamReport.vue')
        },
        {
            path: '/myPreOrder',
            meta: {
                needAuth: true
            },
            name: 'myPreOrder',
            component: () => import('../views/MyPreOrder/MyPreOrder.vue')
        },
        {
            path: '/careerExam',
            meta: {
                needAuth: true
            },
            name: 'careerExam',
            component: () => import('../views/CareerExam/CareerExam.vue')
        },
        {
            path: '/preOrderSucceed',
            meta: {
                needAuth: true
            },
            name: 'preOrderSucceed',
            component: () => import('../views/PreOrderSucceed/PreOrderSucceed.vue')
        },
        {
            path: '/personalInfo',
            meta: {
                needAuth: true
            },
            name: 'personalInfo',
            component: () => import('../views/PersonalInfo/PersonalInfo.vue')
        },
        {
            path: '/msgNotice',
            meta: {
                needAuth: true
            },
            name: 'msgNotice',
            component: () => import('../views/MsgNotice/MsgNotice.vue')
        },
        {
            path: '/accountLogin',
            name: 'accountLogin',
            component: () => import('../views/AccountLogin/AccountLogin.vue')
        },
        {
            path: '/forgetPassword',
            name: 'forgetPassword',
            component: () => import('../views/ForgetPassword/ForgetPassword.vue')
        },
        {
            path: '/msgLogin',
            name: 'msgLogin',
            component: () => import('../views/MsgLogin/MsgLogin.vue')
        },
        {
            path: '/preOrderReceipt',
            meta: {
                needAuth: true
            },
            name: 'preOrderReceipt',
            component: () => import('../views/PreOrderReceipt/PreOrderReceipt.vue')
        },
        {
            path: '/checkPdf',
            meta: {
                needAuth: true
            },
            name: 'checkPdf',
            component: () => import('../views/CheckPdf/CheckPdf.vue')
        }
    ]
})

/* 
    路由前置守卫
    对于路由静态信息中设置needAuth的路由加以鉴权
    访问没有携带token的页面需要跳转到账号登录页面
*/
router.beforeEach((to: any) => {
    if (to.meta.needAuth) {
        if (!isLogin()) {
            return {
                path: '/accountLogin'
            }
        }
    }
})

export default router