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

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
    proxy: false
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  // 登录
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  // 首页显示的页面
  {
    path: '/',
    component: Layout,
    redirect: '/login'
  },
  // 修改密码
  {
    component: Layout,
    path: '/password',
    name: 'Password',
    redirect: 'edit',
    hidden: true,
    meta: { title: '密码管理' },
    children: [
      {
        path: 'edit',
        component: () => import('@/views/password/index'),
        name: 'Edit',
        meta: { title: '修改密码' }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  // 供应商管理页面
  {
    path: '/supplier',
    component: Layout,
    alwaysShow: true,
    name: 'Supplier',
    redirect: '/supplier/list',
    meta: { title: '供应商管理', icon: 'el-icon-s-custom', roles: ['admin'], noCache: true },
    children: [
      {
        path: 'list',
        component: () => import('@/views/supplier/user/index'),
        name: 'SupplierList',
        meta: { title: '供应商列表', icon: 'el-icon-menu', affix: true, roles: ['admin'], noCache: true }
      },
      {
        hidden: true,
        path: 'add',
        component: () => import('@/views/supplier/user/add'),
        name: 'SupplierAdd',
        meta: { title: '添加供应商', icon: 'el-icon-menu', roles: ['admin'], noCache: true }
      },
      {
        hidden: true,
        path: 'edit',
        component: () => import('@/views/supplier/user/edit'),
        name: 'SupplierAdd',
        meta: { title: '修改供应商', icon: 'el-icon-menu', roles: ['admin'], noCache: true }
      }
    ]
  },
  // 授权管理页面
  {
    path: '/auth',
    component: Layout,
    alwaysShow: true,
    redirect: '/auth/list',
    name: 'Auth',
    meta: { title: '授权管理', icon: 'el-icon-s-promotion', roles: ['admin', 'normal'], noCache: true, proxy: true },
    children: [
      {
        path: 'list',
        component: () => import('@/views/supplier/auth/index'),
        name: 'AuthList',
        meta: { title: '授权列表', icon: 'el-icon-menu', roles: ['admin', 'normal'], affix: true, noCache: true, proxy: true }
      }
    ]
  },
  // 商品管理
  {
    path: '/product',
    component: Layout,
    alwaysShow: true,
    redirect: '/product/list',
    hidden: true,
    name: 'Auth',
    meta: { title: '商品管理', icon: 'el-icon-s-shop', roles: ['admin', 'normal'], noCache: true, proxy: true },
    children: [
      {
        hidden: true,
        path: 'list',
        component: () => import('@/views/supplier/product/index'),
        name: 'Product',
        meta: { title: '商品列表', icon: 'el-icon-menu', roles: ['admin', 'normal'], noCache: true, proxy: true }
      },
      {
        hidden: true,
        path: 'add',
        component: () => import('@/views/supplier/product/add'),
        name: 'AddProduct',
        meta: { title: '添加商品', icon: 'el-icon-menu', roles: ['admin', 'normal'], noCache: true, proxy: true }
      },
      {
        hidden: true,
        path: 'edit',
        component: () => import('@/views/supplier/product/edit'),
        name: 'EditProduct',
        meta: { title: '修改商品', icon: 'el-icon-menu', roles: ['admin', 'normal'], noCache: true, proxy: true }
      }
    ]
  },
  // 审核页面
  {
    path: '/review',
    component: Layout,
    alwaysShow: true,
    redirect: '/review/list',
    name: 'Review',
    meta: { title: '品牌授权审核', icon: 'el-icon-s-check', roles: ['admin'], noCache: true },
    children: [
      {
        path: 'list',
        component: () => import('@/views/supplier/review/index'),
        name: 'ReviewList',
        meta: { title: '审核列表', icon: 'el-icon-menu', roles: ['admin'], noCache: true }
      },
      {
        path: 'auth-list',
        hidden: true,
        component: () => import('@/views/supplier/review/authList'),
        name: 'AuthList',
        meta: { title: '授权机构审核列表', icon: 'el-icon-menu', roles: ['admin'], noCache: true }
      },
      {
        path: 'shop-list',
        hidden: true,
        component: () => import('@/views/supplier/review/shopList'),
        name: 'ShopList',
        meta: { title: '商品审核列表', icon: 'el-icon-menu', roles: ['admin'], noCache: true }
      },
      {
        path: 'shop-detail',
        hidden: true,
        component: () => import('@/views/supplier/review/shopDetail'),
        name: 'ShopDetail',
        meta: { title: '商品审核详情', icon: 'el-icon-menu', roles: ['admin'], noCache: true }
      }
    ]
  },
  // 404页面
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
