import Vue from 'vue'
import Router from 'vue-router'
import { getCookie } from '@/utils/cookie'

Vue.use(Router)

const router = new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    {
      path: '/',
      redirect: '/deck'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login/IndexPage.vue')
    },
    {
      path: '/deck',
      name: 'Deck',
      component: () => import('../views/deck/IndexPage.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log(to)
  let userToken = getCookie('cmnw_token')
  if(!userToken&&to.name!=='Login') {
    next('/login')
  } else {
    next()
  }
})

export default router
