import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/components/Layout/index.vue'
import Demo from '@/components/Demo.vue'
import Config from '@/pages/config'
// import agencyName from '@/utils/agencyName'
// import theaterName from '@/utils/theaterName'
// import saleTicketName from '@/utils/saleTicketName'

Vue.use(Router)

export const constantRouterMap = [
  {
    path: '/',
    component: Layout,
    redirect: '/login',
    hidden: true
  },
  {
    path: '/posTool',
    component: require('@/pages/posTool/index.vue').default,
    hidden: true
  },
  {
    path: '/demo',
    component: Demo,
    hidden: true
  },
  {
    path: '/config',
    name: 'config',
    component: Config,
    hidden: true
  },
  {
    path: '/login',
    name: 'login',
    component: require('@/pages/login').default,
    hidden: true
  },
  {
    path: '/index',
    component: Layout,
    name: 'Dashboard',
    hidden: true,
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        // redirect: '/ticketSale/retail',
        component: require('@/pages/dashboard').default,
        meta: { title: '首页' }
      }
    ]
  }
  // ,
  // {
  //   path: '/demo',
  //   component: Layout,
  //   hidden: true,
  //   children: [
  //     {
  //       path: 'index',
  //       name: 'demo',
  //       component: require('@/pages/demo').default,
  //       meta: {title: 'demo'}
  //     }
  //   ]
  // }
]

export default new Router({
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRouterMap
})

// permissionName 与后台获取的权限菜单列表相对应
let routerMap = [
  {
    path: '/ticketSale',
    component: Layout,
    name: '订单中心',
    meta: { title: '订单中心', icon: 'order-center', permissionName: 'order-center' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'retail-ticket',
        name: 'retail-ticket',
        component: require('@/pages/sellManage/retail').default,
        meta: { title: '散客售票', permissionName: 'order-center:single-sale', category: 'ticket' }
      },
      {
        path: 'team-ticket',
        name: 'team-ticket',
        component: require('@/pages/sellManage/retail').default,
        meta: { title: '团体售票', permissionName: 'order-center:group-sale', category: 'ticket', type: 'team' }
      },
      {
        path: 'retail-batch',
        name: 'retail-batch',
        component: require('@/pages/sellManage/retail').default,
        meta: { title: '场次售票', permissionName: 'order-center:batch-sale', category: 'batch' }
      },
      {
        path: 'retail-member',
        name: 'retail-member',
        component: require('@/pages/sellManage/retail').default,
        meta: { title: '年卡次卡', permissionName: 'order-center:member-sale', category: 'member' }
      },
      // {
      //   path: 'salesQuery',
      //   name: 'salesQuery',
      //   component: require('@/pages/queryReport/salesQuery').default,
      //   meta: { title: '售票记录', permissionName: 'order-center:sale-list' }
      // },
      {
        path: 'handOverReport',
        name: 'handOverReport',
        component: require('@/pages/queryReport/handOverReport').default,
        meta: { title: '交班报表', permissionName: 'order-center:hand-over-manage' }
      },
      {
        path: 'handOverList',
        name: 'handOverList',
        component: require('@/pages/queryReport/handOverList').default,
        meta: { title: '交班记录', permissionName: 'order-center:hand-over-list' }
      }
    ]
  },
  {
    path: '/ticketSetting',
    component: Layout,
    name: '票务管理',
    meta: { title: '票务管理', icon: 'ticket-manage', permissionName: 'ticket-manage' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'ticket',
        name: 'ticket',
        component: require('@/pages/ticketSetting/ticket').default,
        meta: { title: '票种管理', permissionName: 'ticket-manage:ticketType-manage' }
      },
      {
        path: 'ticketTypeGroup',
        name: 'ticketTypeGroup',
        component: require('@/pages/ticketSetting/ticketTypeGroup/index').default,
        meta: { title: '票种分组管理', permissionName: 'ticket-manage:ticketTypeGroup-manage' }
      },
      {
        path: 'scenic',
        name: 'scenic',
        component: require('@/pages/ticketSetting/scenic').default,
        meta: { title: '景点管理', permissionName: 'ticket-manage:scenic-manage' }
      },
      {
        path: 'batchConfig',
        name: 'batchConfig',
        component: require('@/pages/ticketSetting/batchConfig').default,
        meta: { title: '场次管理', permissionName: 'ticket-manage:batchConfig-manage' }
      },
      {
        path: 'batch',
        name: 'batch',
        component: require('@/pages/ticketSetting/batch').default,
        meta: { title: '卖场管理', permissionName: 'ticket-manage:batch-manage' }
      },
      {
        path: 'printManage',
        name: 'printManage',
        component: require('@/pages/systemSetting/printManage').default,
        meta: { title: '打印模板配置', permissionName: 'ticket-manage:ticktPrintTemplate-manage' }
      },
      {
        path: 'receiptManage',
        name: 'receiptManage',
        component: require('@/pages/systemSetting/smallTicket').default,
        meta: { title: '小票配置', permissionName: 'ticket-manage:ticketReceiptTemplate-manage' }
      },
      {
        path: 'ticketStorage',
        name: 'ticketStorage',
        component: require('@/pages/ticketSetting/ticketStorage').default,
        meta: { title: '预制门票管理', permissionName: 'ticket-manage:ticketPrepare-manage' }
      },
      {
        path: 'payChannel',
        name: 'payChannel',
        component: require('@/pages/systemSetting/payChannel').default,
        meta: { title: '支付渠道设置', permissionName: 'ticket-manage:payChannel-manage' }
      },
      {
        path: 'invoiceManage',
        name: 'invoiceManage',
        component: require('@/pages/ticketSetting/invoice').default,
        meta: { title: '发票管理', permissionName: 'ticket-manage:invoice-manage' }
      },
      {
        path: 'freeConfig',
        name: 'freeConfig',
        component: require('@/pages/systemSetting/freeConfig').default,
        meta: { title: '免票配置', permissionName: 'ticket-manage:freeConfig-manage' }
      },
      {
        path: 'termManage',
        name: 'termManage',
        component: require('@/pages/checkerManage/termManage').default,
        meta: { title: '自助机设备', permissionName: 'ticket-manage:term-manage' }
      },
      {
        path: 'deviceManage',
        name: 'deviceManage',
        component: require('@/pages/checkerManage/deviceManage').default,
        meta: { title: '检票设备', permissionName: 'ticket-manage:checker-manage' }
      },
      {
        path: 'icCardManage',
        name: 'icCardManage',
        component: require('@/pages/checkerManage/icCardManage').default,
        meta: { title: 'IC卡管理', permissionName: 'ticket-manage:card-manage' }
      },
      {
        path: 'teamManage',
        name: 'teamManage',
        component: require('@/pages/checkerManage/teamManage').default,
        meta: { title: '团队管理', permissionName: 'ticket-manage:team-manage' }
      },
      {
        path: 'guideManage',
        name: 'guideManage',
        component: require('@/pages/checkerManage/guideManage').default,
        meta: { title: '导游管理', permissionName: 'ticket-manage:guide-manage' }
      },
      {
        path: 'subProjectManage',
        name: 'subProjectManage',
        component: require('@/pages/checkerManage/subProjectManage').default,
        meta: { title: '数据权限管理', permissionName: 'ticket-manage:subProject-manage' }
      },
      {
        path: 'statKeyManage',
        name: 'statKeyManage',
        component: require('@/pages/checkerManage/statKeyManage').default,
        meta: { title: '统计字段配置管理', permissionName: 'ticket-manage:statKey-manage' }
      }
    ]
  },
  {
    path: '/queryReport',
    component: Layout,
    name: '数据中心',
    meta: { title: '数据中心', icon: 'statistics-center', permissionName: 'statistics-center' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'salesQuery',
        name: 'salesQuery',
        component: require('@/pages/queryReport/salesQuery').default,
        meta: { title: '售票记录', permissionName: 'statistics-center:sale-center-list' }
      },
      {
        path: 'orderStatistic',
        name: 'orderStatistic',
        component: require('@/pages/queryReport/OrderStatistic').default,
        meta: { title: '售票统计', permissionName: 'statistics-center:order-statistics' }
      },
      {
        path: 'osSaleStatistic',
        name: 'osSaleStatistic',
        component: require('@/pages/queryReport/osSaleStatistic').default,
        meta: { title: '分账统计', permissionName: 'statistics-center:order-scenic-split-statistics' }
      },
      {
        path: 'ghSplitRecordStatistic',
        name: 'ghSplitRecordStatistic',
        component: require('@/pages/queryReport/ghSplitRecordStatistic').default,
        meta: { title: '工行商户统计', permissionName: 'statistics-center:gh-mer-split-statistics' }
      },
      {
        path: 'checkQuery',
        name: 'checkQuery',
        component: require('@/pages/queryReport/checkQuery').default,
        meta: { title: '核销记录', permissionName: 'statistics-center:use-list' }
      },
      {
        path: 'checkStatistic',
        name: 'checkStatistic',
        component: require('@/pages/queryReport/checkStatistic').default,
        meta: { title: '核销统计', permissionName: 'statistics-center:check-statistics' }
      },
      {
        path: 'refundQuery',
        name: 'refundQuery',
        component: require('@/pages/queryReport/refundQuery/RefundQuery').default,
        meta: { title: '退票记录', permissionName: 'statistics-center:cancel-list' }
      },
      {
        path: 'refundStatistic',
        name: 'refundStatistic',
        component: require('@/pages/queryReport/refundStatistic').default,
        meta: { title: '退票统计', permissionName: 'statistics-center:cancel-statistics' }
      },
      {
        path: 'freeQuery',
        name: 'freeQuery',
        component: require('@/pages/queryReport/freeQuery').default,
        meta: { title: '免票记录', permissionName: 'statistics-center:free-list' }
      },
      {
        path: 'freeStatistic',
        name: 'freeStatistic',
        component: require('@/pages/queryReport/freeStatistic').default,
        meta: { title: '免票统计', permissionName: 'statistics-center:free-statistics' }
      },
      {
        path: 'visitorQuery',
        name: 'visitorQuery',
        component: require('@/pages/queryReport/visitorQuery').default,
        meta: { title: '游客列表', permissionName: 'statistics-center:customer-list' }
      },
      {
        path: 'visitorStatistic',
        name: 'visitorStatistic',
        component: require('@/pages/visitor/visitorStatistic').default,
        meta: { title: '游客画像', permissionName: 'statistics-center:customer-statistics' }
      },
      {
        path: 'checkerStatistic',
        name: 'checkerStatistic',
        component: require('@/pages/queryReport/checkerStatistic').default,
        meta: { title: '设备统计', permissionName: 'statistics-center:checker-statistics' }
      },
      {
        path: 'reportList',
        name: 'reportList',
        component: require('@/pages/queryReport/reportList').default,
        meta: { title: '报表列表', permissionName: 'statistics-center:export-list' }
      },
      {
        path: 'wlkData',
        name: 'wlkData',
        component: require('@/pages/queryReport/wlkData').default,
        meta: { title: '文旅卡数据', permissionName: 'statistics-center:wlk-statistics' }
      }
    ]
  },
  {
    path: '/systemSetting',
    component: Layout,
    name: '系统管理',
    meta: { title: '系统管理', icon: 'system-manage', permissionName: 'system-manage' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'commonSetting',
        name: 'commonSetting',
        component: require('@/pages/systemSetting/commonSetting').default,
        meta: { title: '参数配置', permissionName: 'system-manage:system-config-manage' }
      },
      {
        path: 'account',
        name: 'account',
        component: require('@/pages/systemSetting/account').default,
        meta: { title: '账号管理', permissionName: 'system-manage:admin-manage' }
      },
      {
        path: 'role',
        name: 'role',
        component: require('@/pages/systemSetting/role').default,
        meta: { title: '角色管理', permissionName: 'system-manage:role-manage' }
      },
      {
        path: 'operationLog',
        name: 'operationLog',
        component: require('@/pages/systemSetting/operationLog').default,
        meta: { title: '操作日志', permissionName: 'system-manage:business-manage' }
      },
      {
        path: 'loginLog',
        name: 'loginLog',
        component: require('@/pages/systemSetting/loginLog').default,
        meta: { title: '登录日志', permissionName: 'system-manage:login-log-manage' }
      }
    ]
  },
  {
    path: '/error',
    component: Layout,
    redirect: 'noredirect',
    name: 'errorPages',
    hidden: true,
    meta: {
      title: 'errorPages',
      icon: '404'
    },
    children: [
      { path: '401', component: require('@/pages/401').default, name: 'page401', meta: { title: 'page401', noCache: true } },
      { path: '404', component: require('@/pages/404').default, name: 'page404', meta: { title: 'page404', noCache: true } }
    ]
  },
  {
    path: '*',
    redirect: '/error/404',
    hidden: true
  }
]

// 印象妈祖客户端定制化去掉一些不用的菜单入口
// 优化票务管理页面，部分未使用功能按钮优化或隐藏，（如场次管理、场次管理剧院等重复按钮）
// const appName = process.env.VUE_APP_PROJECT_NAME // '印象妈祖票务系统'
// if (appName.indexOf('妈祖') > 0) {
//   const titlesToDelete = ['场次管理', '场次管理-'] // 添加要删除的标题 []
//   console.log('印象妈祖客户端定制化删除部分菜单', titlesToDelete)
//   routerMap.forEach(route => {
//     if (route.children) {
//       route.children = route.children.filter(child => {
//         return !titlesToDelete.some(title => child.meta.title.includes(title))
//       })
//     }
//   })
// }

// 动态将子权限附加到父级
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
  })
