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: 'tab1',
        name: 'tab1',
        meta: { myInd: 0 },
        component: () => import('@/views/tab1/index.vue')
      },
      {
        path: 'tab1_look',
        name: 'tab1_look',
        meta: { myInd: 0 },
        component: () => import('@/views/tab1/tab1_look.vue')
      },
      {
        path: 'tab1_add',
        name: 'tab1_add',
        meta: { myInd: 0 },
        component: () => import('@/views/tab1/tab1_add.vue')
      },
      {
        path: 'tab1_edit',
        name: 'tab1_edit',
        meta: { myInd: 0 },
        component: () => import('@/views/tab1/tab1_edit.vue')
      },
      // 文物案例
      {
        path: 'tab2',
        name: 'tab2',
        meta: { myInd: 1 },
        component: () => import('@/views/tab2/index.vue')
      },
      // 场景案例
      {
        path: 'tab3',
        name: 'tab3',
        meta: { myInd: 2 },
        component: () => import('@/views/tab3/index.vue')
      },
      // 用户管理
      {
        path: 'tab4',
        name: 'tab4',
        meta: { myInd: 3 },
        component: () => import('@/views/tab4/index.vue')
      },
      // 操作日志
      {
        path: 'tab5',
        name: 'tab5',
        meta: { myInd: 4 },
        component: () => import('@/views/tab5/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('SWKK_token')
    if (token) next()
    else {
      Message.warning('登录失效，请重新登录')
      next({ name: 'login' })
    }
  }
})

export default router
