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: 1 },
        component: () => import('../views/tab1/index.vue')
      },
      {
        path: 'tab1Add',
        name: 'tab1Add',
        meta: { myInd: 1 },
        component: () => import('../views/tab1/tab1Add.vue')
      },
      {
        path: 'tab1Add2',
        name: 'tab1Add2',
        meta: { myInd: 1 },
        component: () => import('../views/tab1/tab1Add2.vue')
      },
      {
        path: 'tab1Look',
        name: 'tab1Look',
        meta: { myInd: 1 },
        component: () => import('../views/tab1/tab1Look.vue')
      },
      {
        path: 'tab2',
        name: 'tab2',
        meta: { myInd: 2 },
        component: () => import('../views/tab2/index.vue')
      },
      {
        path: 'tab2Add',
        name: 'tab2Add',
        meta: { myInd: 2 },
        component: () => import('../views/tab2/tab2Add.vue')
      },
      {
        path: 'tab2Look',
        name: 'tab2Look',
        meta: { myInd: 2 },
        component: () => import('../views/tab2/tab2Look.vue')
      },
      {
        path: 'tab3',
        name: 'tab3',
        meta: { myInd: 3 },
        component: () => import('../views/tab3/index.vue')
      },
      {
        path: 'tab3Add',
        name: 'tab3Add',
        meta: { myInd: 3 },
        component: () => import('../views/tab3/tab3Add.vue')
      },
      {
        path: 'tab3Look',
        name: 'tab3Look',
        meta: { myInd: 3 },
        component: () => import('../views/tab3/tab3Look.vue')
      },
      {
        path: 'tab4',
        name: 'tab4',
        meta: { myInd: 4 },
        component: () => import('../views/tab4/index.vue')
      },
      {
        path: 'tab4Add',
        name: 'tab4Add',
        meta: { myInd: 4 },
        component: () => import('../views/tab4/tab4Add.vue')
      },
      {
        path: 'tab4Look',
        name: 'tab4Look',
        meta: { myInd: 4 },
        component: () => import('../views/tab4/tab4Look.vue')
      },
      {
        path: 'tab5',
        name: 'tab5',
        meta: { myInd: 5 },
        component: () => import('../views/tab5/index.vue')
      },
      {
        path: 'tab5Add',
        name: 'tab5Add',
        meta: { myInd: 5 },
        component: () => import('../views/tab5/tab5Add.vue')
      },
      {
        path: 'tab5Look',
        name: 'tab5Look',
        meta: { myInd: 5 },
        component: () => import('../views/tab5/tab5Look.vue')
      },
      {
        path: 'tab6',
        name: 'tab6',
        meta: { myInd: 6 },
        component: () => import('../views/tab6/index.vue')
      },
      {
        path: 'tab6Add',
        name: 'tab6Add',
        meta: { myInd: 6 },
        component: () => import('../views/tab6/tab6Add.vue')
      },
      {
        path: 'tab6Look',
        name: 'tab6Look',
        meta: { myInd: 6 },
        component: () => import('../views/tab6/tab6Look.vue')
      },
      {
        path: 'tab7',
        name: 'tab7',
        meta: { myInd: 7 },
        component: () => import('../views/tab7/index.vue')
      },
      {
        path: 'tab8',
        name: 'tab8',
        meta: { myInd: 8 },
        component: () => import('../views/tab8/index.vue')
      },
      {
        path: 'tab8Look',
        name: 'tab8Look',
        meta: { myInd: 8 },
        component: () => import('../views/tab8/tab8Look.vue')
      },
      {
        path: 'tab9',
        name: 'tab9',
        meta: { myInd: 9 },
        component: () => import('../views/tab9/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('CQLJXU_token')
    if (token) next()
    else {
      Message.warning('登录失效，请重新登录')
      next({ name: 'login' })
    }
  }
})

export default router
