import Vue from 'vue'
import VueRouter from 'vue-router'
// 加载条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import VueAMap from 'vue-amap';

Vue.use(VueRouter);
// Vue.use(VueAMap);

// VueAMap.initAMapApiLoader({
//     key: 'your amap key',
//     plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
//     // 默认高德 sdk 版本为 1.4.4
//     v: '1.4.4'
// });

const routes = [
    {
        path: '/',
        component: () => import( '../views/Index.vue'),
        children: [
            {
                path: '/',
                name: 'Wait',
                component: () => import('@/views/Wait.vue'),
                meta: {
                    title: "等待",
                    clmid: "1",
                }
            },{
                path: '/main',
                name: 'Main',
                component: () => import('@/views/Main.vue'),
                meta: {
                    title: "团课",
                    clmid: "1",
                }
            },{
                path: '/rank',
                name: 'Rank',
                component: () => import('@/views/Rank.vue'),
                meta: {
                    title: "排名",
                    clmid: "2",
                }
            },{
                path: '/pk',
                name: 'pk',
                component: () => import('@/views/pk.vue'),
                meta: {
                    title: "pk",
                    clmid: "3",
                }
            },{
                path: '/2pkRank',
                name: '2pkRank',
                component: () => import('@/views/2pkRank.vue'),
                meta: {
                    title: "2pkRank",
                    clmid: "5",
                }
            },{
                path: '/threepk',
                name: 'threepk',
                component: () => import('@/views/threepk.vue'),
                meta: {
                    title: "threepk",
                    clmid: "6",
                }
            },{
                path: '/3pkRank',
                name: '3pkRank',
                component: () => import('@/views/3pkRank.vue'),
                meta: {
                    title: "3pkRank",
                    clmid: "6",
                }
            },{
                path: '/test',
                name: 'test',
                component: () => import('@/views/test.vue'),
                meta: {
                    title: "test",
                    clmid: "6",
                }
            },{
                path: '/actionScore',
                name: 'actionScore',
                component: () => import('@/components/actionScore.vue'),
                meta: {
                    title: "test",
                    clmid: "6",
                }
            },
        ]
    }, {
        path: '*',
        name: '404',
        component: () => import( '../views/404.vue')
    },
];

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};

const router = new VueRouter({
    // mode: 'history',
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
});

// 路由守卫
router.beforeEach((to,from,next)=>{
    NProgress.start();
    const isLogin = localStorage.token? true : false;
    if(to.path == '/login' || to.path == '/register'){//'login'和'register'相当于是路由白名单
        next();
    }else{
        //如果token存在，就正常跳转，如果不存在，则说明未登陆，则跳转到'login'
        // isLogin? next() : next("/login");
        next();
    }
});

router.afterEach(() => {
    NProgress.done()
});

export default router
