import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index'
Vue.use(Router)

const router =  new Router({
  mode:'history',
  routes: [
    {
      path: '*',
      redirect: '/home',
      component: resolve => require(['@/pages/home/index'],resolve)
    },
    {
      path: '/home',
      name: '首页',
      component: resolve => require(['@/pages/home/index'],resolve)
    },
    {
      path: '/login',
      name: '登录',
      component: resolve => require(['@/pages/login/index'],resolve)
    },
    {
      path: '/cart',
      name: '购物车',
      component: resolve => require(['@/pages/cart/index'],resolve)
    },
    {
      path: '/payment',
      name: '支付',
      component: resolve => require(['@/pages/payment/index'],resolve)
    },
    {
      path: '/payment-success',
      name: '支付成功',
      component: resolve => require(['@/pages/payment-success/index'],resolve)
    },
    {
      path: '/course-list',
      name: '课程列表',
      component: resolve => require(['@/pages/course-list/index'],resolve)
    },
    {
      path: '/bank-list',
      name: '题库列表',
      component: resolve => require(['@/pages/bank-list/index'],resolve)
    },
    {
      path: '/course-detail/:goodsId',
      name: '课程详情',
      component: resolve => require(['@/pages/goods-detail/course-detail'],resolve)
    },
    {
      path: '/bank-detail/:goodsId',
      name: '题库详情',
      component: resolve => require(['@/pages/goods-detail/bank-detail'],resolve)
    },
    {
      path: '/my-course-detail/:goodsId',
      name: '课程详情',
      component: resolve => require(['@/pages/course-detail/index'],resolve)
    },
    {
      path: '/bank-report/:bankId',
      name: '试卷报告',
      component: resolve => require(['@/pages/bank-report/index'],resolve)
    },
    {
      path: '/question-bank/:courseId',
      name: '做题',
      component: resolve => require(['@/pages/question-bank/index'],resolve)
    },
    {
      path: '/person-center',
      name: '个人中心',
      component: resolve => require(['@/pages/person-center/index'],resolve),
      beforeEnter:(to,from,next) => {
        let token = window.localStorage.getItem('token');

        if(token == null || token == '' || token === undefined){

          next('/login')

        }else{

          next();

        }
      },
      children:[
        {
          path: '/',
          redirect: 'my-course',
        },
        {
          path: 'my-course',
          component: resolve => require(['@/pages/person-center/my-course/index'],resolve),
          name: '我的课程'
        },
        {
          path: 'play-record',
          component: resolve => require(['@/pages/person-center/play-record/index'],resolve),
          name: '播放记录'
        },
        {
          path: 'my-bank',
          component: resolve => require(['@/pages/person-center/my-bank/index'],resolve),
          name:'我的题库',
          children:[
            {
              path: '/',
              redirect: 'index',
            },
            {
              path: 'index',
              component: resolve => require(['@/pages/person-center/my-bank/index/index'],resolve),
              name: '我的题库',
            },
            {
              path: 'bank-detail/:bankId',
              component: resolve => require(['@/pages/person-center/my-bank/bank-detail/index'],resolve),
              name: '题库详情'
            },
            {
              path: 'bank-statistics/:bankId',
              component: resolve => require(['@/pages/person-center/my-bank/bank-statistics/index'],resolve),
              name: '做题统计'
            },
            {
              path: 'bank-explain-detail/:bankId',
              component: resolve => require(['@/pages/person-center/my-bank/bank-explain-detail/index'],resolve),
              name: '详情'
            },
          ]
        },
        {
          path: 'bank-record',
          component: resolve => require(['@/pages/person-center/bank-record/index'],resolve),
          name: '做题记录'
        },
        {
          path: 'my-order',
          component: resolve => require(['@/pages/person-center/my-order/index'],resolve),
          name: '我的订单'
        },
        {
          path: 'my-examination',
          component: resolve => require(['@/pages/person-center/my-examination/index'],resolve),
          name: '我的考试'
        },
        {
          path: 'my-message',
          component: resolve => require(['@/pages/person-center/my-message/index'],resolve),
          name: '我的消息'
        },
        {
          path: 'my-info',
          component: resolve => require(['@/pages/person-center/my-info/index'],resolve),
          name: '我的资料'
        },
        {
          path: 'my-invoice',
          name: '我的发票',
          component: resolve => require(['@/pages/person-center/my-invoice/index'],resolve),
          children:[
            
            {
              path: '/',
              redirect: 'index',
            },
            {
              path: 'index',
              component: resolve => require(['@/pages/person-center/my-invoice/index/index'],resolve),
              name: '我的发票',
            },
            {
              path: 'add',
              component: resolve => require(['@/pages/person-center/my-invoice/add/index'],resolve),
              name: '申请发票',
            },
          ]
        },
        {
          path: 'my-classhour',
          component: resolve => require(['@/pages/person-center/my-classhour/index'],resolve),
          name: '我的学时',
          children:[
            {
              path: '/',
              redirect: 'index',
            },
            {
              path: 'index',
              component: resolve => require(['@/pages/person-center/my-classhour/index/index'],resolve),
              name: '我的学时',
            },
            {
              path: 'appointment',
              component: resolve => require(['@/pages/person-center/my-classhour/appointment/index'],resolve),
              name: '预约考试',
            },
            {
              path: 'appointment-add',
              component: resolve => require(['@/pages/person-center/my-classhour/appointment-add/index'],resolve),
              name: '预约考试',
            },
            {
              path: 'appointment-add-kp',
              component: resolve => require(['@/pages/person-center/my-classhour/appointment-add-kp/index'],resolve),
              name: '预约考试',
            }
          ]
        },
      ]
    }
  ]
})

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

router.beforeEach((to,from,next) => {

  if(to.path === '/login'){

    next();

  }else{

    let token = window.localStorage.getItem('token');

    //进入页面有token获取用户信息
    if(token){
      store.token = token;
      if(!store.state.userInfo) {
        //获取用户信息
        store.dispatch('getUserInfo')
      }
      
      next()
    } else {
      
      next()
    }

  }
})

export default router;