import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 解决ElementUi导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push

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

const router = new Router({
  routes: [
    {
      path: '/',
      // name: 'home',
      component: () => import('../views/home.vue'),
      title: '一键发单',
      icon: '/static/image/order-icon.png',
      children: [
        {
          path: '/',
          name: 'HomeIndex',
          title: '订单列表',
          component: () => import('../components/Home.vue')
        },
        {
          path: '/orderSearch',
          name: 'orderSearch',
          title: '订单查询',
          component: () => import('../components/orderSearch.vue')
        },
        {
          path: '/manualCreate',
          name: 'manualCreate',
          title: '手动发单',
          component: () => import('../components/manualCreate.vue')
        },
        {
          path: '/shop/shopList',
          name: 'shopList',
          title: '我的店铺',
          component: () => import('../components/shopCompoents/shopList.vue')
        },
        {
          path: '/platformAccount/index',
          name: 'platformAccount',
          title: '账号管理',
          component: () => import('../components/platformAccount/index')
        },
        {
          path: '/account/wallet',
          name: 'wallet',
          title: '我的钱包',
          component: () => import('../components/accountCompoents/wallet.vue')
        },
        {
          path: '/account/walletDetail',
          name: 'walletDetail',
          title: '收支明细',
          component: () => import('../components/accountCompoents/shopAccount.vue')
        },
        {
          path: '/account/coupon',
          name: 'coupon',
          title: '优惠券',
          component: () => import('../components/accountCompoents/coupon.vue')
        },

        {
          path: '/platform/bindTakeOut',
          name: 'bindTakeOut',
          title: '绑定外卖平台',
          component: () => import('../components/shopCompoents/bindTakeOut.vue')
        },
        {
          path: '/platform/bindDelivery',
          name: 'bindDelivery',
          title: '绑定配送平台',
          component: () => import('../components/shopCompoents/bindDelivery.vue')
        },
        {
          path: '/set/waimai',
          name: 'waimai',
          title: '外卖设置',
          component: () => import('../components/settingComponents/waimai.vue')
        },
        {
          path: '/set/print',
          name: 'print',
          title: '打印设置',
          component: () => import('../components/settingComponents/print.vue')
        },
        {
          path: '/set/system',
          name: 'system',
          title: '系统设置',
          component: () => import('../components/settingComponents/systemSetting.vue')
        },
        {
          path: '/help/commonProblem',
          name: 'commonProblem',
          title: '常见问题',
          component: () => import('../components/help/help.vue')
        },
        {
          path: '/help/customerService',
          name: 'customerService',
          title: '客服中心',
          component: () => import('../components/help/customerService.vue')
        },
        {
          path: '/help/download',
          name: 'download',
          title: '下载中心',
          component: () => import('../components/help/downloadCenter.vue')
        },
        {
          path: '/help/feedback',
          name: 'feedback',
          title: '意见反馈',
          component: () => import('../components/help/feedback.vue')
        },
        {
          path: '/help/about',
          name: 'about',
          title: '关于我们',
          component: () => import('../components/help/about.vue')
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue'),
      title: '登录',
      authority: false
    },
    {
      path: '/resetPassword',
      name: 'resetPassword',
      component: () => import('../views/resetPassword.vue'),
      title: '重置密码',
      authority: false
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    // localStorage.clear()
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
    localStorage.removeItem('usbPrinter')
    next()
  } else {
    const token = localStorage.getItem('token') || null;
    // console.log('这是一个token:',token);
    if (token) {
      next()
    } else if (to.path !== '/resetPassword') {
      router.push('/login')
    } else {
      next()
    }
  }
})

export default router
