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: '/goodsCenter',
    component: Layout,
    name: '产品中心',
    meta: { title: '产品中心', icon: 'productIcon' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'goodsList',
        name: 'goodsList',
        component: () => import('@/views/goodsCenter/goodsList'),
        meta: { title: '票种管理', permissionName: 'product:goods' }
      },
      {
        path: 'otaList',
        name: 'otaList',
        component: () => import('@/views/goodsCenter/otaList'),
        meta: { title: '分销商管理', permissionName: 'product:ota' }
      },
      {
        path: 'priceList',
        name: 'priceList',
        component: () => import('@/views/goodsCenter/priceList'),
        meta: { title: '价格码管理', permissionName: 'product:pricecode' }
      }
    ]
  },
  {
    path: '/queryReport',
    component: Layout,
    name: '查询统计',
    meta: { title: '查询统计', icon: 'statementIcon' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'refundAudit',
        name: 'refundAudit',
        component: () => import('@/views/queryReport/refundAudit'),
        meta: { title: '退票审核', permissionName: 'stats:cancel_audit' }
      },
      {
        path: 'otaPaymentAmountLog',
        name: 'otaPaymentAmountLog',
        component: () => import('@/views/queryReport/otaPaymentAmountLog'),
        meta: { title: '分销商消费记录', permissionName: 'stats:ota_money_record' }
      },
      {
        path: 'orderList',
        name: 'orderList',
        component: () => import('@/views/queryReport/orderList'),
        meta: { title: '售票查询', permissionName: 'stats:order_query' }
      },
      {
        path: 'salesReport',
        name: 'salesReport',
        component: () => import('@/views/queryReport/salesReport'),
        meta: { title: '售票统计', permissionName: 'stats:order_stats' }
      },
      {
        path: 'orderReconciliation',
        name: 'orderReconciliation',
        component: () => import('@/views/queryReport/orderReconciliation'),
        meta: { title: '销售明细', permissionName: 'stats:order_day_stats' }
      },
      {
        path: 'checkQuery',
        name: 'checkQuery',
        component: () => import('@/views/queryReport/checkQuery'),
        meta: { title: '检票查询', permissionName: 'stats:check_query' }
      },
      {
        path: 'checkReport',
        name: 'checkReport',
        component: () => import('@/views/queryReport/checkReport'),
        meta: { title: '检票统计', permissionName: 'stats:check_stats' }
      },
      {
        path: 'checkAndPrintReport',
        name: 'checkAndPrintReport',
        component: () => import('@/views/queryReport/checkAndPrintReport'),
        meta: { title: '核销统计', permissionName: 'stats:write_off_stats' }
      },
      {
        path: 'checkAndPrintQuery',
        name: 'checkAndPrintQuery',
        component: () => import('@/views/queryReport/checkAndPrintQuery'),
        meta: { title: '核销查询', permissionName: 'stats:write_off_query' }
      },
      {
        path: 'reportExport',
        name: 'reportExport',
        component: () => import('@/views/queryReport/reportExport'),
        meta: { title: '报表导出', permissionName: 'stats:report' }
      },
      {
        path: 'chartStatistics',
        name: 'chartStatistics',
        component: () => import('@/views/queryReport/chartStatistics'),
        meta: { title: '图表统计', permissionName: 'stats:chart' }
      }
    ]
  },
  {
    path: '/systemSetting',
    component: Layout,
    name: '系统管理',
    meta: { title: '系统管理', icon: 'systemSetting' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'account',
        name: 'account',
        component: () => import('@/views/systemSetting/account'),
        meta: { title: '账号管理', permissionName: 'system_setting:users' }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/systemSetting/role'),
        meta: { title: '角色管理', permissionName: 'system_setting:roles' }
      },
      {
        path: 'project',
        name: 'project',
        component: () => import('@/views/systemSetting/project'),
        meta: { title: '景区管理', permissionName: 'system_setting:users' }
      },
      {
        path: 'systemConfig',
        name: 'systemConfig',
        component: () => import('@/views/systemSetting/systemConfig'),
        meta: { title: '系统设置', permissionName: '' }
      }
    ]
  },
  {
    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
  })
