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

Vue.use(Router)

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

/**
 * 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: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '系统面板', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/system',
    component: Layout,
    redirect: '/system',
    alwaysShow: true,
    name: 'System',
    meta: {
      title: '系统管理',
      icon: 'el-icon-s-home',
      roles: ['admin']
    },
    children: [
      {
        path: 'admin',
        component: () => import('@/views/admin/index'),
        name: 'Admin',
        meta: {
          title: '管理员',
          roles: ['admin']
        }
      },
      {
        path: 'works',
        component: () => import('@/views/admin/works'),
        name: 'Works',
        meta: {
          title: '排班表',
          roles: ['admin']
        }
      },
      {
        path: 'onlines',
        component: () => import('@/views/onlines/online.vue'),
        name: 'onlines',
        meta: {
          title: '在线客服',
          roles: ['admin']
        }
      }/*,
      {
        path: 'shortcut',
        component: () => import('@/views/shortcut/shortcutContent.vue'),
        name: 'shortcut',
        meta: {
          title: '快捷内容设置',
          roles: ['admin']
        }
      },
      {
        path: 'teams',
        component: () => import('@/views/admin/teams'),
        name: 'Teams',
        meta: {
          title: '团队',
          roles: ['admin']
        }
      }*/
    ]
  },

  {
    path: '/order',
    component: Layout,
    redirect: '/order/index',
    alwaysShow: true,
    name: 'Orders',
    meta: {
      title: '订单管理',
      icon: 'money',
      roles: ['order_index', 'editor']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/order/index'),
        name: 'OrderList',
        meta: {
          title: '订单列表',
          roles: ['order_pub', 'editor']
        }
      },
      /* {
        path: 'pub',
        component: () => import('@/views/order/pub'),
        name: 'OrderPub',
        meta: {
          title: '公海订单',
          roles: ['order_pub','editor']
        }
      },*/
      {
        path: 'back',
        component: () => import('@/views/order/back'),
        name: 'OrderBack',
        meta: {
          title: '流转订单',
          roles: ['order_back', 'editor']
        }
      },
      {
        path: 'product',
        component: () => import('@/views/order/product'),
        name: 'productNameList',
        meta: {
          title: '产品统计',
          roles: ['order_pub', 'editor']
        }
      },
    ]
  },
  {
    path: '/log',
    component: Layout,
    redirect: '/log/index',
    alwaysShow: true,
    name: 'Log',
    meta: {
      title: '日志记录',
      icon: 'nested',
      roles: ['follow_index', 'log_index', 'editor']
    },
    children: [
      {
        path: 'follow',
        component: () => import('@/views/log/follow'),
        name: 'Follow',
        meta: {
          title: '跟进记录',
          roles: ['follow_index', 'editor']
        }
      },
      {
        path: 'index',
        component: () => import('@/views/log/index'),
        name: 'LogIndex',
        meta: {
          title: '日志记录',
          roles: ['log_index']
        }
      }
    ]
  },

  {
    path: '/data',
    component: Layout,
    redirect: '/data/index',
    alwaysShow: true,
    name: 'Data',
    meta: {
      title: '数据统计',
      icon: 'chart',
      roles: ['data_index']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/data/index'),
        name: 'Index',
        meta: {
          title: '跟进统计',
          roles: ['data_index']
        }
      },
      /* {
        path: 'online',
        component: () => import('@/views/data/online'),
        name: 'Index',
        meta: {
          title: '在线时长',
          roles: ['data_online']
        }
      },
      {
        path: 'anchor',
        component: () => import('@/views/data/anchor'),
        name: 'Anchor',
        meta: {
          title: '主播概况',
          roles: ['data_anchor']
        }
      },*/
      {
        path: 'sale',
        component: () => import('@/views/data/sale'),
        name: 'Sale',
        meta: {
          title: '销售统计',
          roles: ['data_sale']
        }
      }
    ]
  },

  {
    path: '/icon',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/icons/index'),
        name: 'Icons',
        meta: { title: 'Icons', icon: 'icon', noCache: true }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

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

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
