import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from "@/views/main/main";

import login from "@/views/login/index";
import {getDeepPrototy} from '@/util/index'

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'login',
        component: login,
        meta: {
            title: ""
        }
    },
    {
        path: "/",
        name: "Main",
        component: Main,
        meta: {
            title: "",
        },
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('@/views/home/home'),
            },
            {
                path: '/404',
                name: '404',
                component: () => import('@/views/error-page/404'),
            },
            {
                path: '/voteActivity',
                name:'voteActivity',
                component:() => import('@/views/vote/index')
            },
            {
                path: '/vote',
                name:'vote',
                component:() => import('@/views/vote/vote')
            },
            {
                path: '/voteActivityInfo',
                name:'voteActivityInfo',
                component:() => import('@/views/vote/voteActivityInfo')
            },
           {
                path: '/notice',
                name:'notice',
                component:() => import('@/views/notice/index')
            },
           {
                path: '/noticeInfo',
                name:'noticeInfo',
                component:() => import('@/views/notice/info')
            },
           {
                path: '/verify',
                name:'verify',
                component:() => import('@/views/verify/index')
            },

            {
                path: '/audio',
                name:'audio',
                component:() => import('@/views/verify/verify')
            },

           {
                path: '/voteResult',
                name:'voteResult',
                component:() => import('@/views/vote/voteResult')
           },
           {
                path: '/suggestions',
                name:'suggestions',
                component:() => import('@/views/suggestions/index')
           },



        ]
    }
]

let routerNameList = getDeepPrototy(routes, 'name');


const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    // LoadingBar.start();
    // console.log(to)
    // console.log(from)
    console.log(to.name);
    if (!routerNameList.includes(to.name)) {

        next({name: '404'})
    }
    if (to.name == 'Main') {
        next({name: 'home'})
    } else {
        next();
    }
});

// 重写vueRouter的push方法，让点击重复路由的情况不会报错 其实这个报错本身也没事
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    console.log(location)
    return originalPush.call(this, location).catch(err => err)
}

export default router
