import Vue from 'vue'
import Router from 'vue-router'
/* Layout */
import Layout from '../views/layout/Layout'

Vue.use(Router)

export const constantRouterMap = [
  // {
  //   path: '/chartsAnalysis',
  //   component: Layout,
  //   name: '图表分析',
  //   meta: {title: '图表分析', icon: 'tableIcon'},
  //   redirect: 'noredirect',
  //   alwaysShow: true,
  //   children: [
  //     {
  //       path: 'salesAnalysisCharts',
  //       name: 'salesAnalysisCharts',
  //       component: () => import('@/views/chartsAnalysis/salesAnalysisCharts')
  //     }, {
  //       path: 'channelAnalysisCharts',
  //       name: 'channelAnalysisCharts',
  //       component: () => import('@/views/chartsAnalysis/channelAnalysisCharts')
  //     }, {
  //       path: 'guestAnalysisCharts',
  //       name: 'guestAnalysisCharts',
  //       component: () => import('@/views/chartsAnalysis/guestAnalysisCharts')
  //     }, {
  //       path: 'salesForecastCharts',
  //       name: 'salesForecastCharts',
  //       component: () => import('@/views/chartsAnalysis/salesForecastCharts')
  //     }, {
  //       path: 'salesReportCharts',
  //       name: 'salesReportCharts',
  //       component: () => import('@/views/chartsAnalysis/salesReportCharts')
  //     }
  //   ]
  // },
  {
    path: '/',
    component: Layout,
    redirect: '/login',
    hidden: true
  },
  {
    path: '/login',
    component: () =>
      import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/loading',
    component: () =>
      import('@/views/loading'),
    hidden: true
  },
  {
    path: '/404',
    component: () =>
      import('@/views/404'),
    hidden: true
  }
]

export default new Router({
  // mode: 'history', //后端支持可开
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRouterMap
})

// permissionName 与后台获取的权限菜单列表相对应
let routerMap = [
  // {
  //   path: '/index',
  //   component: Layout,
  //   name: 'Dashboard',
  //   hidden: true,
  //   children: [
  //     {
  //       path: 'dashboard',
  //       name: 'dashboard',
  //       component: () => import('@/views/dashboard/index'),
  //       meta: { title: '仪表盘', permissionName: 'home_page_query' }
  //     }
  //   ]
  // },
  {
    path: '/systemSetting',
    component: Layout,
    name: '系统管理',
    meta: { title: '系统管理', icon: 'systemSetting' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'project',
        name: 'project',
        component: () => import('@/views/systemSetting/project'),
        meta: { title: '项目管理', permissionName: 'system_setting:users' }
      },
      {
        path: 'otaSource',
        name: 'otaSource',
        component: () => import('@/views/systemSetting/otaSource'),
        meta: { title: '分销商管理', permissionName: 'system_setting:users' }
      },
      {
        path: 'product',
        name: 'product',
        component: () => import('@/views/systemSetting/product'),
        meta: { title: '分销产品管理', permissionName: 'system_setting:users' }
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('@/views/systemSetting/order'),
        meta: { title: '公共订单管理', permissionName: 'system_setting:users' }
      },
      {
        path: 'file',
        name: 'file',
        component: () => import('@/views/systemSetting/file'),
        meta: { title: '应用下载', permissionName: 'system_setting:roles' }
      },
      {
        path: 'systemConfig',
        name: 'systemConfig',
        component: () => import('@/views/systemSetting/systemConfig'),
        meta: { title: '平台系统总配置', permissionName: '' }
      },
      {
        path: 'xcAccountParam',
        name: 'xcAccountParam',
        component: () => import('@/views/systemSetting/xcAccountParam'),
        meta: { title: '携程账号信息管理', permissionName: 'system_setting:users' }
      }
    ]
  },
  {
    path: '/error',
    component: Layout,
    redirect: 'noredirect',
    name: 'errorPages',
    hidden: true,
    meta: {
      title: 'errorPages',
      icon: '404'
    },
    children: [
      { path: '401', component: () => import('@/views/401'), name: 'page401', meta: { title: 'page401', noCache: true } },
      { path: '404', component: () => import('@/views/404'), name: 'page404', meta: { title: 'page404', noCache: true } }
    ]
  },
  {
    path: '*',
    redirect: '/error/404',
    hidden: true
  }
]

// 动态将子权限附加到父级
export const asyncRouterMap = routerMap
  .map(item => {
    if (item.children) {
      !item.meta && (item.meta = {})

      item.meta.permissionName = item.children.map(i => {
        if (i.meta && i.meta.permissionName) {
          return i.meta.permissionName
        } else {
          return 'all' // 所有人可见
        }
      })
    }

    return item
  })
