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

// const _import = require('./_import_' + process.env.NODE_ENV)//只有在正式环境下才使用懒加载（这个还没有解决）
// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading

// 有时候我们想把某个路由下的所有组件都打包在同个异步块 (chunk) 中。Webpack 会将任何一个异步模块与相同的块名称组合到相同的异步块中。
// const Foo = () => import(/* webpackChunkName: "group-foo" */ './Foo.vue')

Vue.use(Router)

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

/**
* hidden: true  //当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
* 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
* 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
* 若你想不管路由下面的 children 声明的个数都显示你的根路由
* 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
* alwaysShow: true
* redirect: noredirect           //当设置 noredirect 的时候该路由在面包屑导航中不可被点击
* name:'router-name'             //设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
* meta : {
*   permission: ['HY_add', 'HY_edit'] //设置该路由的 按钮权限
* 	title: 'title' //设置该路由在侧边栏和面包屑中展示的名字
*  	icon: 'svg-name' //设置该路由的图标
*  	noCache: true //如果设置为true ,则不会被 <keep-alive> 缓存(默认 false)
*  }
**/

// 所有权限通用路由表
// 如首页和登录页和一些不用权限的公用页面
export const constantRouterMap = [
  {
    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: '/404', component: () => import('@/views/404'), hidden: true },
  {
    path: '',
    component: Layout,
    redirect: 'dashboard', // 重定向地址，在面包屑中点击会重定向去的地址
    name: '首页',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'shouye', noCache: true }
    }]
  },
  {
    path: '',
    component: Layout,
    name: '来电弹屏',
    children: [{
      path: 'callScreen',
      // path: 'callScreen',
      component: () => import('@/views/callScreen/index'),
      name: 'callScreen',
      meta: { title: '来电弹屏', noCache: true }
    }],
    hidden: true
  },
  {
    path: '',
    component: Layout,
    name: '自动外呼弹屏',
    children: [{
      path: 'autoCallScreen',
      // path: 'autoCallScreen',
      component: () => import('@/views/autoCallScreen/index'),
      name: 'autoCallScreen',
      meta: { title: '自动外呼弹屏', noCache: false }
    }],
    hidden: true
  },
  {
    path: '',
    component: Layout,
    name: '点击外呼弹屏',
    children: [{
      path: 'clickCallScreen/:callid(\\d+)',
      // path: 'clickCallScreen',
      component: () => import('@/views/clickCallScreen/index'),
      name: 'clickCallScreen',
      meta: { title: '点击外呼弹屏', noCache: false }
    }],
    hidden: true
  },
  {
    path: '',
    component: Layout,
    name: '个人资料',
    children: [{
      path: 'personalInfo',
      component: () => import('@/views/personalInfo/index'),
      name: 'personalInfo',
      meta: { title: '个人资料', noCache: true }
    }],
    hidden: true
  },
  // {
  //   path: '',
  //   component: Layout,
  //   name: '自动外呼监控',
  //   children: [{
  //     path: 'autoCallMonitor',
  //     component: () => import('@/views/autoCallMonitor/index'),
  //     name: 'autoCallMonitor',
  //     meta: { title: '自动外呼监控', noCache: true }
  //   }],
  //   hidden: true
  // }
  // 	{
  // 		path: '',
  // 		component: Layout,
  // 		name: '系统设置',
  // 		meta: { title: '系统设置', icon: 'msnui-sys-set' },
  // 		children: [
  // 			{
  // 				path: '',
  // 				name: '权限',
  // 				component: () => import('@/views/systemSetup/roleSeting'),
  // 				meta: { title: '权限'},
  // 				children: [
  // 					{
  // 						path: 'systemSetup/roleSeting/menuSetup/index',
  // 						name: '菜单设置',
  // 						component: () => import('@/views/systemSetup/roleSeting/menuSetup/index'),
  // 						meta: { title: '菜单设置'},
  // 					},
  // 					{
  // 						path: 'systemSetup/roleSeting/roleSetup/index',
  // 						name: '权限设置',
  // 						component: () => import('@/views/systemSetup/roleSeting/roleSetup/index'),
  // 						meta: { title: '权限设置'}
  // 					}
  // 				]
  // 			},
  //
  // 		],
  // 		hidden: true
  // 	},
]

// scrollBehavior:
// - 这个功能只在支持 history.pushState 的浏览器中可用。
// - defaults to no scroll behavior
// - return false to prevent scroll
const scrollBehavior = (to, from, savedPosition) => {
  if (savedPosition) {
    // savedPosition is only available for popstate navigations.
    return savedPosition
  } else {
    const position = {}
    // new navigation.
    // scroll to anchor by returning the selector
    if (to.hash) {
      position.selector = to.hash
    }
    // check if any matched route config has meta that requires scrolling to top
    if (to.matched.some(m => m.meta.scrollToTop)) {
      // cords will be used if no selector is provided,
      // or if the selector didn't match any element.
      position.x = 0
      position.y = 0
    }
    // if the returned position is falsy or an empty object,
    // will retain current scroll position.
    return position
  }
}

// 实例化vue的时候只挂载 constantRouter
export default new Router({
  mode: 'history', // 后端支持可开
  // scrollBehavior: () => ({ y: 0 }),
  scrollBehavior,
  routes: constantRouterMap
})

