import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  { // 这个是店铺管理界面
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/home',  // 重定向到主页
    children: [
      { path: '403', name: 'NoAuth', meta: { name: '无权限' }, component: () => import('../views/manager/403') },
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home') },
      { path: 'admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/manager/Admin') },
      { path: 'adminPerson', name: 'AdminPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/AdminPerson') },
      { path: 'password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/manager/Password') },
      // { path: 'notice', name: 'Notice', meta: { name: '公告信息' }, component: () => import('../views/manager/Notice') },
      { path: 'shopManager', name: 'ShopManager', meta: { name: '店铺管理' }, component: () => import('../views/manager/Shop/Shop.vue') },
      { path: 'shopOrders', name: 'ShopOrders', meta: { name: '店铺订单' }, component: () => import('../views/manager/Shop/ShopOrders.vue') },
      { path: 'shopProducts', name: 'ShopProducts', meta: { name: '店铺产品' }, component: () => import('../views/manager/Shop/ShopProducts.vue') },
      { path: 'corpGrown', name: 'CorpGrown', meta: { name: '作物生长模块' }, component: () => import('../views/manager/AI/CorpGrown.vue') },
      { path: 'shopStatements', name: 'ShopStatements', meta: { name: '店铺报表' }, component: () => import('../views/manager/AI/CorpGrown.vue') },
      { path: 'monitor', name: 'Monitor', meta: { name: '农村监控模块' }, component: () => import('../views/manager/AI/Monitor.vue') },
      { path: 'pestAnalysis', name: 'PestAnalysis', meta: { name: '害虫识别模块' }, component: () => import('../views/manager/AI/PestAnalysis.vue') },
      // { path: 'audit', name: 'Audit', meta: { name: '店铺申请审核' }, component: () => import('../views/manager/Manager/Audit.vue') },
    ]
  },
  { // 这是管理员界面
    path:"/adminMgr",
    name:'AdminManager',
    component:()=>import('../views/AdminMgr.vue'),
    redirect:'/adminMgr/home',
    children: [
      { path:'/adminMgr/home' , name:'adminHome', meta: {name: '管理员首页' } , component:()=>import('../views/manager/ShopManager/AdminHome.vue')},
      { path: '/adminMgr/notice', name: 'Notice', meta: { name: '公告信息' }, component: () => import('../views/manager/Notice') },
      { path: '/adminMgr/ordersManager' , name: 'Orders' ,meta:{name :'订单管理'} ,component:()=>import('../views/manager/ShopManager/Orders.vue')},
      { path: '/adminMgr/productManager', name:'Products',meta:{name :'产品管理'} ,component:()=>import('../views/manager/ShopManager/Product.vue')},
      { path: '/adminMgr/saleStatementManager',name:'sale',meta:{name:'销量管理'} ,component:()=>import('../views/manager/ShopManager/SaleStatement.vue')},
      { path: '/adminMgr/auditManager'  , name:'Audit'    ,meta:{name:'审核管理'} ,component:()=>import('../views/manager/Manager/Audit.vue')},
      { path: '/adminMgr/productTypeMgr', name:'productType' ,meta:{name:'产品类型管理'} ,component:()=>import('../views/manager/ShopManager/ProductType.vue')},
      { path: '/adminMgr/store', name:'Store' ,meta:{name:'店铺信息管理'} ,component:()=>import('../views/manager/ShopManager/Store.vue')},
      { path: '/adminMgr/user', name:'User' ,meta:{name:'用户信息管理'} ,component:()=>import('../views/manager/ShopManager/User.vue')},
      { path: '/adminMgr/applyForUp', name:'ApplyForUp' ,meta:{name:'申请成为店家'} ,component:()=>import('../views/front/ApplyForUp.vue')},
    ]
  },
  {
    path:'/charts',
    name:"AI",
    component:()=>import('../views/manager/AI/Charts.vue'),
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/Front.vue'),
    children: [
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/front/Home') },
      { path: 'person', name: 'Person', meta: { name: '个人信息' }, component: () => import('../views/front/Person') },
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
  { path: '/register', name: 'Register', meta: { name: '注册' }, component: () => import('../views/Register.vue') },
  { path: '*', name: 'NotFound', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 注：不需要前台的项目，可以注释掉该路由守卫
// 路由守卫
// router.beforeEach((to ,from, next) => {
//   let user = JSON.parse(localStorage.getItem("xm-user") || '{}');
//   if (to.path === '/') {
//     if (user.role) {
//       if (user.role === 'USER') {
//         next('/front/home')
//       } else {
//         next('/home')
//       }
//     } else {
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })

export default router
