import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login'),
    meta: {title: '登录'}
  }, {
    path: '/logout',
    name: 'Logout',
    component: () => import('@/views/logout'),
    meta: {title: '退出登录'}
  }, {
    path: '/',
    redirect: '/data-market/main'
  }, {
    path: '/data-market/main',
    component: () => import('@/views/main'),
    children: [{
      path: '/data-market/main',
      name: 'DataMarketIndex',
      meta: {title: '数据大盘'}
    }]
  }, {
       path: '/system',
       component: () => import('@/views/main'),
       children: [{
         path: '/system/data-acc',
         name: 'AccountIndex',
         component: () => import('@/views/system/dataAcc/list'),
         meta: {title: '账号管理'}
       }]
     }, {
    path: '/system',
    component: () => import('@/views/main'),
    children: [{
      path: '/system/data-dict',
      name: 'dataDict',
      component: () => import('@/views/system/dataDict/list'),
      meta: {title: '系统管理 - 数据字典'}
    },{
               path: '/system/data-acc',
               name: 'AccountIndex',
               component: () => import('@/views/system/dataAcc/list'),
               meta: {title: '账号管理'}
             },{
                              path: '/system/data-paper',
                              name: 'paper',
                              component: () => import('@/views/system/dataPaper2/list'),
                              meta: {title: '账号管理2'}
                            }]
  }]
})

let URL_WHITE_TOKEN_LIST = [
  '/login'
]

router.beforeEach((to, from, next) => {
  NProgress.start() // 每次切换页面时，调用进度条

  next()
  // let token = Cookies.get('token')
  // if (token) {
  //   if (to.matched.length === 0) { // 匹配前往的路由不存在
  //     from.name ? next({name: from.name}) : next('/error') // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
  //   } else {
  //     next()
  //   }
  // } else {
  //   if (URL_WHITE_TOKEN_LIST.indexOf(to.path) !== -1) {
  //     next()
  //   } else {
  //     next(`/login?redirect=${to.path}`)
  //   }
  // }
})

router.afterEach(() => {
  NProgress.done() // 在即将进入新的页面组件前，关闭掉进度条
})

export default router
