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: 'order-center' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      // {
      //   path: 'goodsList',
      //   name: 'goodsList',
      //   component: () => import('@/views/goodsCenter/goodsList'),
      //   meta: { title: '票种管理', permissionName: 'product-center:goods' }
      // },
      {
        path: 'otaList',
        name: 'otaList',
        component: () => import('@/views/goodsCenter/otaList'),
        meta: { title: '分销商管理', permissionName: 'product-center:ota-source-manage' }
      },
      {
        path: 'priceList',
        name: 'priceList',
        component: () => import('@/views/goodsCenter/priceList'),
        meta: { title: '产品管理', permissionName: 'product-center:product-manage' }
      }
      // {
      //   path: 'invoiceList',
      //   name: 'invoiceList',
      //   component: () => import('@/views/goodsCenter/invoiceList'),
      //   meta: { title: '开票管理', permissionName: 'product-center:invoice' }
      // },
      // {
      //   path: 'invoiceChannelList',
      //   name: 'invoiceChannelList',
      //   component: () => import('@/views/goodsCenter/invoiceChannelList'),
      //   meta: { title: '发票渠道管理', permissionName: 'product-center:invoiceChannelList' }
      // },
      // {
      //   path: 'invoiceSellerList',
      //   name: 'invoiceSellerList',
      //   component: () => import('@/views/goodsCenter/invoiceSellerList'),
      //   meta: { title: '发票销售方管理', permissionName: 'product-center:invoiceSellerList' }
      // }
    ]
  },
  {
    path: '/queryReport',
    component: Layout,
    name: '数据中心',
    meta: { title: '数据中心', icon: 'statistics-center' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      // {
      //   path: 'refundAudit',
      //   name: 'refundAudit',
      //   component: () => import('@/views/queryReport/refundAudit'),
      //   meta: { title: '退票审核', permissionName: 'stat-center:cancel_audit' }
      // },
      {
        path: 'otaPaymentAmountLog',
        name: 'otaPaymentAmountLog',
        component: () => import('@/views/queryReport/otaPaymentAmountLog'),
        meta: { title: '分销商消费记录', permissionName: 'stat-center:ota-source-consume-list' }
      },
      {
        path: 'orderList',
        name: 'orderList',
        component: () => import('@/views/queryReport/orderList'),
        meta: { title: '售票查询', permissionName: 'stat-center:sale-list' }
      },
      {
        path: 'salesReport',
        name: 'salesReport',
        component: () => import('@/views/queryReport/salesReport'),
        meta: { title: '售票统计', permissionName: 'stat-center:sale-stat' }
      },
      // {
      //   path: 'orderReconciliation',
      //   name: 'orderReconciliation',
      //   component: () => import('@/views/queryReport/orderReconciliation'),
      //   meta: { title: '销售明细', permissionName: 'stat-center:order_day_stats' }
      // },
      // {
      //   path: 'checkQuery',
      //   name: 'checkQuery',
      //   component: () => import('@/views/queryReport/checkQuery'),
      //   meta: { title: '检票查询', permissionName: 'stat-center:check_query' }
      // },
      // {
      //   path: 'checkReport',
      //   name: 'checkReport',
      //   component: () => import('@/views/queryReport/checkReport'),
      //   meta: { title: '检票统计', permissionName: 'stat-center:check_stats' }
      // },
      {
        path: 'checkAndPrintReport',
        name: 'checkAndPrintReport',
        component: () => import('@/views/queryReport/checkAndPrintReport'),
        meta: { title: '核销统计', permissionName: 'stat-center:check-stat' }
      },
      // {
      //   path: 'checkAndPrintQuery',
      //   name: 'checkAndPrintQuery',
      //   component: () => import('@/views/queryReport/checkAndPrintQuery'),
      //   meta: { title: '核销查询', permissionName: 'stat-center:write_off_query' }
      // },
      {
        path: 'cancelReport',
        name: 'cancelReport',
        component: () => import('@/views/queryReport/cancelReport'),
        meta: { title: '退票统计', permissionName: 'stat-center:cancel-stat' }
      },
      {
        path: 'invoiceList',
        name: 'invoiceList',
        component: () => import('@/views/queryReport/invoiceList'),
        meta: { title: '开票记录', permissionName: 'stat-center:invoice-list' }
      },
      {
        path: 'reportExport',
        name: 'reportExport',
        component: () => import('@/views/queryReport/reportExport'),
        meta: { title: '报表导出', permissionName: 'stat-center:report-list' }
      }
      // {
      //   path: 'chartStatistics',
      //   name: 'chartStatistics',
      //   component: () => import('@/views/queryReport/chartStatistics'),
      //   meta: { title: '图表统计', permissionName: 'stat-center:chart' }
      // }
    ]
  },
  {
    path: '/systemSetting',
    component: Layout,
    name: '系统管理',
    meta: { title: '系统管理', icon: 'system-manage' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'account',
        name: 'account',
        component: () => import('@/views/systemSetting/account'),
        meta: { title: '账号管理', permissionName: 'system-manage:admin-manage' }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/systemSetting/role'),
        meta: { title: '角色管理', permissionName: 'system-manage:role-manage' }
      },
      // {
      //   path: 'project',
      //   name: 'project',
      //   component: () => import('@/views/systemSetting/project'),
      //   meta: { title: '景区管理', permissionName: 'system-manage:users' }
      // },
      {
        path: 'systemConfig',
        name: 'systemConfig',
        component: () => import('@/views/systemSetting/systemConfig'),
        meta: { title: '系统设置', permissionName: 'system-manage:system-set' }
      }
    ]
  },
  {
    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
  })
