import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import store from '@/store'
import { getToken, setToken, removeToken } from '@/utils/auth'
Vue.use(Router)
//不管是超级管理员还是普通用户都可以看到的页面
export const constantRoutes = [
    // 登录页
    {
        path: '/login',
        component: () => import('@/views/login/index'),
        hidden: true
    },
]

/**
 * 异步路由
 * asyncRoutes
 */
export const asyncRoutes = [
    {
        path: '/',
        name: 'Layout',
        redirect: '/login',
        alwaysShow: true,
        component: Layout,
        children: [
            {
                path: 'car',
                name: 'car',
                meta: {
                    isAuth: true,
                    title: '主页',
                    keepAlive: true, //缓存组件
                    requireAuth: true
                },
                component: () => import('@/views/car'),
            },
            {
                path: 'affiche',
                name: 'affiche',
                meta: {
                    isAuth: true, title: '公告管理',
                    requireAuth: true
                },
                component: () => import('@/views/affiche')
            },
            {
              path: 'carcome',
              name: 'carcome',
              meta: {
                  isAuth: true, title: '出入统计',
                  requireAuth: true
              },
              component: () => import('@/views/carcome')
          },
            {
                path: 'role',
                name: 'role',
                meta: { isAuth: true, title: '角色管理', requireAuth: true },
                component: () => import('@/views/role')
            }

        ]
    },
]

export const anyRoute = { path: '*', redirect: '/404', hidden: true }
const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}


//这里设置路由拦截
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) { //判断是否需要登录验证
        const token = getToken()
        if (token && store.state.user.token) {
            if (store.state.user.token == token) { //判断token等于时候就跳转路由
                next();//这个跳转是上面路由跳转
            }
            else { //token不一样时候就返回登录页面
                next({
                    path: '/login',//返回登录界面
                    // query: {redirect: to.fullPath}
                })
            }
        } else {
            next({
                path: '/login',//返回登录界面
                // query: {redirect: to.fullPath}
            })
        }
    }
    else {
        //如果不需要登录权限就直接跳转到该路由
        next();
    }
})


export default router

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
// router.beforeEach((to, from, next) => {
//     //如果路由需要跳转
//     // if (to.meta.isAuth) {
//     //     //判断 如果cook是否存在，可以进去
//     //     if (Cookies.get('cook')) {
//     //         next()  //放行
//     //     } else {
//     //         // alert('登录身份失效,请重新登录')
//     //         next({ path: "/login" });
//     //     }
//     // } else {
//     //     // 否则，放行
//     //     next()
//     // }
// })
