import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index'
import request from '@/request'
import { Notification, MessageBox, Message } from 'element-ui'
Vue.use(Router)

let bankAdmin = ['/person-center/bank-record','/person-center/my-bank/index','/person-center/my-collect','/person-center/my-wrong','/person-center/my-bank/bank-detail/','/person-center/my-bank/bank-statistics/','/person-center/my-bank/bank-explain-detail/','/bank-exam-continue/','/bank-exam/','/subject/collect-bank/','/bank-exam-explain/','/bank-report/'];
let canToBank = null;
let courseAdmin = ['/my-course-detail/','/course-exam/']
let canToCourse = null;

const router =  new Router({
  mode:'history',
  scrollBehavior: () => ({ y: 0 }),
  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: '/about',
      name: '走进祥粤',
      component: resolve => require(['@/pages/about/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: '/course-report',
      name: '试卷报告',
      component: resolve => require(['@/pages/course-report/index'],resolve)
    },
    
    {
      path: '/bank-report/:goodsId',
      name: '试卷报告',
      component: resolve => require(['@/pages/bank-report/index'],resolve)
    },
    {
      path: '/course-exam/:goodsId',
      name: '做题',
      component: resolve => require(['@/pages/course-exam/index'],resolve)
    },
    {
      path: '/bank-exam/:goodsId',
      name: '做题',
      component: resolve => require(['@/pages/bank-exam/index'],resolve)
    },
    
    {
      path: '/bank-exam-continue/:goodsId',
      name: '继续做题',
      component: resolve => require(['@/pages/bank-exam-continue/index'],resolve)
    },
    {
      path: '/bank-exam-all-explain/:recordId',
      name: '全部解析',
      component: resolve => require(['@/pages/bank-exam-all-explain/index'],resolve)
    },
    {
      path: '/bank-exam-wrong-explain/:recordId',
      name: '错题解析',
      component: resolve => require(['@/pages/bank-exam-wrong-explain/index'],resolve)
    },
    {
      path: '/bank-exam-explain/:goodsId',
      name: '题目解析',
      component: resolve => require(['@/pages/bank-exam-explain/index'],resolve)
    },
    
    {
      path: '/subject/wrong-type-bank/:type',
      name: '错题',
      component: resolve => require(['@/pages/subject/wrong-type-bank'],resolve)
    },
    {
      path: '/subject/wrong-bank/:examId',
      name: '错题',
      component: resolve => require(['@/pages/subject/wrong-bank'],resolve)
    },
    {
      path: '/subject/collect-type-bank/:type',
      name: '收藏',
      component: resolve => require(['@/pages/subject/collect-type-bank'],resolve)
    },
    {
      path: '/subject/collect-bank/:examId',
      name: '收藏',
      component: resolve => require(['@/pages/subject/collect-bank'],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/:goodsId',
              component: resolve => require(['@/pages/person-center/my-bank/bank-detail/index'],resolve),
              name: '题库详情'
            },
            {
              path: 'bank-statistics/:goodsId',
              component: resolve => require(['@/pages/person-center/my-bank/bank-statistics/index'],resolve),
              name: '做题统计'
            },
            {
              path: 'bank-explain-detail/:goodsId',
              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-collect',
          component: resolve => require(['@/pages/person-center/my-collect/index'],resolve),
          name: '收藏集'
        },
        
        {
          path: 'my-wrong',
          component: resolve => require(['@/pages/person-center/my-wrong/index'],resolve),
          name: '错题集'
        },
        
        {
          path: 'record-list/:goodsId',
          component: resolve => require(['@/pages/person-center/bank-record/record-list/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) => {
  console.log(to,'to')
  console.log(from,'from')
  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')
      }

      // //题库页面要检测是否需要锁定
      // let isBankLock = bankAdmin.some(item => {
      //   return to.path.indexOf(item) != -1
      // })
      // console.log(isBankLock,'isBankLock')
      // if(isBankLock) {
        
      //   //没有执行定时器，开启锁定
      //   if(!canToBank) {
      //     request
      //       .lockLockStatus({
      //         action: "bank",
      //       })
      //       .then((res) => {
      //         //有其他端在操作，不能学习
      //         Message({
      //           type: "warning",
      //           message: res.msg,
      //         });
              
      //         if(from.path=='/') {
      //           next('/')
      //         }

      //       }).catch(err => {
      //         //可以学习，开启锁定
      //           canToBank = 1;
      //         request.lockLockAction({
      //           action:'bank'
      //         }).then(res => {
      //         })
      //         canToBank = setInterval(() => {
      //           request.lockLockAction({
      //             action:'bank'
      //           }).then(res => {
                  
      //           })
      //         }, 10000);
      //         next()
      //       })

      //       return;
      //   } else {
      //     //正在执行定时器，可以直接进入
      //     next()
      //   }
        
      //   return;
      // } else {
      //   //从题库页面离开删除锁定

      //   let isLeaveBank = bankAdmin.some(item => {
      //     return from.path.indexOf(item) != -1
      //   })

      //   if(isLeaveBank) {
          
      //     clearInterval(canToBank)
      //     canToBank = null;
      //     request.lockDelLock({
      //       action:'bank'
      //     }).then(res => {
            
      //     })
      //   }
      // }
      
      // let isCourseLock = courseAdmin.some(item => {
      //   return to.path.indexOf(item) != -1
      // })

      // console.log(isCourseLock,'isCourseLock')
      // if(isCourseLock) {
        
      //   //没有执行定时器，开启锁定
      //   if(!canToCourse) {
      //     request.goodsDetail(to.params.goodsId).then(res => {
      //       //继续教育锁定
      //       if(res.data.educationName == '继续教育') {
      //         request
      //           .lockLockStatus({
      //             action: "jxjy",
      //           })
      //           .then((res) => {
      //             //有其他端在操作，不能学习
      //             Message({
      //               type: "warning",
      //               message: res.msg,
      //             });
                  
      //             if(from.path=='/') {
      //               next('/')
      //             }

      //           }).catch(err => {
      //             //可以学习，开启锁定
      //             canToCourse = 1;
      //             request.lockLockAction({
      //               action:'jxjy'
      //             }).then(res => {
      //             })
      //             canToCourse = setInterval(() => {
      //               request.lockLockAction({
      //                 action:'jxjy'
      //               }).then(res => {
                      
      //               })
      //             }, 10000);
      //             next()
      //           })
      //       } else {
      //         next()
      //       }
      //     })
          

      //       return;
      //   } else {
      //     //正在执行定时器，可以直接进入
      //     next()
      //   }
        
      //   return;
      // } else {
      //   //非题库页面删除锁定
      //   let isLeaveCouese = courseAdmin.some(item => {
      //     return from.path.indexOf(item) != -1
      //   })

      //   if(isLeaveCouese) {
          
      //     clearInterval(canToCourse)
      //     canToCourse = null;
      //     request.lockDelLock({
      //       action:'jxjy'
      //     }).then(res => {
            
      //     })
      //   }
      // }
      
      next()
    } else {
      
      next()
    }

  }
})

export default router;