import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('../views/layout/index.vue'),
    children: [
      {
        path: 'tab2',
        name: 'tab2',
        meta: { myInd: 2 },
        component: () => import('../views/tab2/index.vue')
      },
      {
        path: 'tab2Add',
        name: 'tab2Add',
        meta: { myInd: 2 },
        component: () => import('../views/tab2/add.vue')
      },
      {
        path: 'tab3',
        name: 'tab3',
        meta: { myInd: 3 },
        component: () => import('../views/tab3/index.vue')
      },
      {
        path: 'tab1',
        name: 'tab1',
        meta: { myInd: 1 },
        component: () => import('../views/tab1/index.vue')
      }

    ]
  }
]

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

router.beforeEach((to, from, next) => {
  // 如果是去登录页，不需要验证，直接下一步
  if (to.name === 'login') next()
  // 否则要有token值才能下一步，不然就返回登录页
  else {
    const token = localStorage.getItem('HNBWY_token')
    if (token) next()
    else {
      Message.warning('登录失效，请重新登录')
      next({ name: 'login' })
    }
  }
})

export default router
