/*
 * @Author       : yuanrunwei
 * @Date         : 2020-05-18 12:05:37
 * @LastEditors: XuTongZhang
 * @LastEditTime: 2020-08-03 15:09:54
 * @FilePath     : \ambre-admin-noob\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
// import cookiesDeploy from '../common/utils/cookiesDeploy'
Vue.use(VueRouter)
// let list

const routes = [
  {
    path: '/info',
    name: 'info',
    component: () => import('@/views/info.vue')
  },
  {
    path: '/contest',
    name: 'contest',
    component: () => import('@/views/contest.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('@/views/index.vue')
  },
  // error
  {
    path: '*',
    name: 'error',
    component: resolve => require(['@/views/error'], resolve)
  },
  // login
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue')
  },
  // redirect
  {
    path: '/',
    redirect: '/info'
  }
]

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

let info = JSON.parse(localStorage.getItem('user'))
const whiteList = ['/info']
router.beforeEach((to, from, next) => {
  if (info && info.isRegister) {
    whiteList.indexOf(to.path) !== -1
      ? next({
        name: 'contest'
      })
      : next()
  } else {
    // whiteList.indexOf(to.path) !== -1
    to.path === '/contest'
      ? next({
        name: 'info'
      })
      : next()
      // : next({
      //   name: 'info'
      // })
  }
})

export default router
