import VueRouter from "vue-router"
import store from "@/store"

const routes = [
    {
        path: '/',
        name: 'Index',
        component: () => import(/* webpackChunkName: "index" */ '@/pages/index/index'),
        meta: {
            title: '首页',
            keepAlive: true,
            requireAuth: true
        }
    }, {
        path: '/vote',
        name: 'VoteIndex',
        component: () => import(/* webpackChunkName: "index" */ '@/pages/vote/index'),
        meta: {
            title: '推举规则',
            keepAlive: true,
            requireAuth: true
        }
    }, {
        path: '/vote_equal/:id',
        name: 'Vote_equal',
        component: () => import(/* webpackChunkName: "index" */ '@/pages/vote/equal'),
        meta: {
            title: '等额推举',
            keepAlive: true,
            requireAuth: true
        }
    }, {
        path: '/vote_diff/:id',
        name: 'Vote_diff',
        component: () => import(/* webpackChunkName: "index" */ '@/pages/vote/diff'),
        meta: {
            title: '差额推举',
            keepAlive: true,
            requireAuth: true
        }
    }, {
        path: '/files',
        name: 'FileIndex',
        component: () => import(/* webpackChunkName: "index" */ '@/pages/files/index'),
        meta: {
            title: '文件审阅',
            keepAlive: true,
            requireAuth: true
        }
    }, {
        path: '/files/:id',
        name: 'FileShow',
        component: () => import(/* webpackChunkName: "index" */ '@/pages/files/show'),
        meta: {
            title: '审阅',
            keepAlive: true,
            requireAuth: true
        }
    }, {
        path: '/auth',
        component: () => import(/* webpackChunkName: "auth" */ '@/pages/layouts/common'),
        children: [
            {
                path: 'login',
                name: 'AuthLogin',
                component: () => import(/* webpackChunkName: "auth" */ '@/pages/auth/login'),
                meta: {
                    title: '推举系统登录'
                }
            }, {
                path: 'verify',
                name: 'AuthVerify',
                component: () => import(/* webpackChunkName: "auth" */ '@/pages/auth/verify'),
                meta: {
                    title: '验证手机号'
                }
            }
        ]
    }
];

const router = new VueRouter({
    mode: 'history',
    routes
});

router.beforeEach((to, from, next) => {
    let hasLogin = store.state.hasLogin;
    if (to.name === 'AuthLogin') {
        // 如果已经登录，直接跳转到首页去
        if (hasLogin) {
            next({
                name: 'Index'
            });
            return
        }

        let tk = localStorage.getItem('accessToken')

        if (tk) {
            store.commit('login', tk);
            next({
                name: 'Index'
            });
            return
        }
    } else if (to.meta.requireAuth === true) {
        if (hasLogin === false) {
            next({
                name: 'AuthLogin',
                params: {
                    next: to.path
                }
            });
            return
        }
    }
    store.state.showFooter = to.meta.showFooter || false;

    next()
});

export default router