import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/components/Layout/index.vue'
import Demo from '@/views/demo/index.vue'

Vue.use(Router)
const project_name = localStorage.mgt_project

export const constantRouterMap = [
  {
    path: '/',
    component: Layout,
    redirect: '/login',
    hidden: true
  },
  {
    path: '/demo',
    component: Demo,
    hidden: true
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/loading',
    component: () => import('@/views/loading'),
    hidden: true
  },
  {
    path: '/index',
    component: Layout,
    name: 'Dashboard',
    hidden: true,
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '仪表盘' }
      }
    ]
  }
]

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

const routerMap = [
  {
    path: '/ticketSetting',
    component: Layout,
    name: 'ticketSetting',
    meta: { title: '票务管理', icon: 'tags' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'ticketType',
        name: 'ticketType',
        component: () => import('@/views/ticketSetting/ticket'),
        meta: { title: '票种管理', permissionName: 'ticket_setting:ticket' }
      },
      {
        path: 'rentTicket',
        name: 'rentTicket',
        component: () => import('@/views/ticketSetting/rentTicket'),
        meta: { title: '租赁票种管理', permissionName: 'ticket_setting:batch' }
      },
      {
        path: 'activityTicket',
        name: 'activityTicket',
        component: () => import('@/views/ticketSetting/activityTicket'),
        meta: { title: '活动票种管理', permissionName: 'ticket_setting:batch' }
      },
      {
        path: 'batchTicket',
        name: 'batchTicket',
        component: () => import('@/views/ticketSetting/batchTicket'),
        meta: { title: '场次票种管理', permissionName: 'ticket_setting:batch' }
      },
      {
        path: 'ticketCategory',
        name: 'ticketCategory',
        component: () => import('@/views/ticketSetting/ticketCategory'),
        meta: { title: '票种分类管理', permissionName: 'ticket_setting:pricecode' }
      },
      {
        path: 'goods',
        name: 'goods',
        component: () => import('@/views/ticketSetting/goods'),
        meta: { title: '商品管理', permissionName: 'ticket_setting:goods' }
      },
      {
        path: 'goodsGroup',
        name: 'goodsGroup',
        component: () => import('@/views/ticketSetting/goodsGroup'),
        meta: { title: '商品组管理', permissionName: 'ticket_setting:batch' }
      },
      {
        path: 'performList',
        name: 'performList',
        component: () => import('@/views/ticketSetting/performList'),
        meta: { title: '节目管理', permissionName: 'ticket_setting:batch' }
      },
      {
        path: 'memberType',
        name: 'memberType',
        component: () => import('@/views/ticketSetting/member'),
        meta: { title: '会员管理', permissionName: 'ticket_setting:member' }
      },
      {
        path: 'scenic',
        name: 'scenic',
        component: () => import('@/views/ticketSetting/scenic'),
        meta: { title: '景点管理', permissionName: 'ticket_setting:scenic' }
      },
      {
        path: 'priceCode',
        name: 'priceCode',
        component: () => import('@/views/ticketSetting/priceCode'),
        meta: { title: '价格码管理', permissionName: 'ticket_setting:pricecode' }
      },
      {
        path: 'batch',
        name: 'batch',
        component: () => import('@/views/ticketSetting/batch'),
        meta: { title: '场次管理', permissionName: 'ticket_setting:batch' }
      },
      {
        path: 'invoice',
        name: 'invoice',
        component: () => import('@/views/ticketSetting/invoice'),
        meta: { title: '发票管理', permissionName: 'ticket_setting:invoice_manager' }
      },
      {
        path: 'invoiceRecord',
        name: 'invoiceRecord',
        component: () => import('@/views/ticketSetting/invoiceRecord'),
        meta: { title: '开票记录', permissionName: 'ticket_setting:invoice_record' }
      },
      {
        path: 'guide',
        name: 'guide',
        component: () => import('@/views/ticketSetting/guide'),
        meta: { title: '导游管理', permissionName: 'ticket_setting:guide' }
      },
      {
        path: 'comments',
        name: 'comments',
        component: () => import('@/views/ticketSetting/comments'),
        meta: { title: '意见反馈', permissionName: 'ticket_setting:comments_feedback' }
      },
      {
        path: 'billingType',
        name: 'billingType',
        component: () => import('@/views/ticketSetting/billingType'),
        meta: { title: '计费类型', permissionName: 'ticket_setting:batch' }
      }
    ]
  },
  {
    path: '/orderSetting',
    component: Layout,
    name: 'orderSetting',
    meta: { title: '订单管理', icon: 'bar-chart' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'order',
        name: 'order',
        component: () => import('@/views/orderSetting/index'),
        meta: { title: '订单查询', permissionName: 'order:weixin' }
      },
      {
        path: 'cancel',
        name: 'cancel',
        component: () => import('@/views/orderSetting/cancel'),
        meta: { title: '退单处理', permissionName: 'order:cancel' }
      },
      {
        path: 'orderTask',
        name: 'orderTask',
        component: () => import('@/views/orderSetting/OrderTask'),
        meta: { title: '批量退单任务列表', permissionName: 'order:cancel' }
      },
      // {
      //   path: 'appOrder',
      //   name: 'appOrder',
      //   component: () => import('@/views/orderSetting/AppOrder'),
      //   meta: { title: '官网订单', permissionName: 'order:term' }
      // },
      // {
      //   path: 'travelAgencyOrder',
      //   name: 'travelAgencyOrder',
      //   component: () => import('@/views/orderSetting/TravelAgencyOrder'),
      //   meta: { title: '旅行社订单', permissionName: 'order:term' }
      // },
      {
        path: 'statisticsByType',
        name: 'statisticsByType',
        component: () => import('@/views/orderSetting/OrderStatistics'),
        meta: { title: '售票统计', permissionName: 'order:stats' }
      },
      {
        path: 'statisticsByChannel',
        name: 'statisticsByChannel',
        component: () => import('@/views/orderSetting/agencyUserStatistics'),
        meta: { title: '售票用户统计', permissionName: 'order:agencyUserStatistics' }
      },
      {
        path: 'agencyOrderStatistics',
        name: 'agencyOrderStatistics',
        component: () => import('@/views/orderSetting/agencyOrderStatistics'),
        meta: { title: '旅行社单统计', permissionName: 'order:agency_order_stats' }
      },
      {
        path: 'comment',
        name: 'comment',
        component: () => import('@/views/orderSetting/Comment'),
        meta: { title: '评论审批', permissionName: 'order:term' }
      },
      {
        path: 'exportReport',
        name: 'exportReport',
        component: () => import('@/views/orderSetting/Report'),
        meta: { title: '导出报表', permissionName: 'order:term' }
      }
    ]
  },
  {
    path: '/reserveSetting',
    component: Layout,
    name: 'reserveSetting',
    meta: { title: '预约管理', icon: 'bar-chart' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'ticketType',
        name: 'ticketType',
        component: () => import('@/views/reserveSetting/batchTicket'),
        meta: { title: '票种管理', permissionName: 'reservation_setting:ticket' }
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('@/views/reserveSetting/index'),
        meta: { title: '订单查询', permissionName: 'reservation_setting:order' }
      }
    ]
  },
  {
    path: '/marketingSetting',
    component: Layout,
    name: 'marketingSetting',
    meta: { title: `${project_name === 'YINXIANGMAZU' ? '分销商管理' : '营销管理'}`, icon: 'shop' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'salesUser',
        name: 'salesUser',
        component: () => import('@/views/marketingSetting/salesUser'),
        meta: { title: '销售管理', permissionName: 'marketing_setting:sales_user' }
      },
      {
        path: 'salesUserClass',
        name: 'salesUserClass',
        component: () => import('@/views/marketingSetting/salesUserClass'),
        meta: { title: '销售分类管理', permissionName: 'marketing_setting:sales_user' }
      },
      {
        path: 'exchangeCard',
        name: 'exchangeCard',
        component: () => import('@/views/marketingSetting/exchangeCard'),
        meta: { title: '兑换卡管理', permissionName: 'marketing_setting:exchange_card' }
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('@/views/marketingSetting/index'),
        meta: { title: '销售订单管理', permissionName: 'marketing_setting:marketing_audit' }
      },
      {
        path: 'statisticsByType',
        name: 'statisticsByType',
        component: () => import('@/views/marketingSetting/OrderStatistics'),
        meta: { title: '销售售票统计', permissionName: 'marketing_setting:marketing_audit' }
      }
    ]
  },
  {
    path: '/saleChannel',
    component: Layout,
    name: 'saleChannel',
    meta: { title: '渠道管理', icon: 'shopping' },
    redirect: 'noredirect',
    alwaysShow: true,
    children: [
      {
        path: 'travelAgency',
        name: 'travelAgency',
        component: () => import('@/views/saleChannel/travelAgency'),
        meta: { title: '旅行社管理', permissionName: 'adv:adv', hidden: project_name === 'YINXIANGMAZU' }
      },
      {
        path: 'travelAgencyRecord',
        name: 'travelAgencyRecord',
        component: () => import('@/views/saleChannel/travelAgencyRecord'),
        meta: { title: '旅行社交易记录', permissionName: 'adv:adv', hidden: project_name === 'YINXIANGMAZU' }
      },
      {
        path: 'weixinConfig',
        name: 'weixinConfig',
        component: () => import('@/views/saleChannel/weixin'),
        meta: { title: '支付配置', permissionName: 'salechannel:weixin' }
      },
      {
        path: 'term',
        name: 'term',
        component: () => import('@/views/saleChannel/term'),
        meta: { title: '自助机管理', permissionName: 'salechannel:term' }
      },
      {
        path: 'statisticsByTerm',
        name: 'statisticsByTerm',
        component: () => import('@/views/saleChannel/OrderStatistics'),
        meta: { title: '自助机取票统计', permissionName: 'salechannel:term' }
      },
      {
        path: 'marqueeList',
        name: 'marqueeList',
        component: () => import('@/views/saleChannel/marqueeList'),
        meta: { title: '滚动字幕', permissionName: 'marquee:marquee' }
      },
      {
        path: 'advPosList',
        name: 'advPosList',
        component: () => import('@/views/saleChannel/advPosList'),
        meta: { title: '广告位管理', permissionName: 'adv:adv' }
      },
      {
        path: 'advList',
        name: 'advList',
        component: () => import('@/views/saleChannel/advList'),
        meta: { title: '广告列表', permissionName: 'adv:adv' }
      }
    ]
  },
  {
    path: '/travelAgencyManage',
    component: Layout,
    name: 'travelAgencyManage',
    meta: { title: '旅行社管理', icon: 'shopping' },
    redirect: 'noredirect',
    hidden: project_name !== 'YINXIANGMAZU',
    children: [
      {
        path: 'travelAgency',
        name: 'travelAgency',
        component: () => import('@/views/saleChannel/travelAgency'),
        meta: { title: '旅行社管理', permissionName: 'adv:adv' }
      },
      {
        path: 'travelAgencyRecord',
        name: 'travelAgencyRecord',
        component: () => import('@/views/saleChannel/travelAgencyRecord'),
        meta: { title: '旅行社交易记录', permissionName: 'adv:adv' }
      }
    ]
  },
  // {
  //   path: '/marquee',
  //   component: Layout,
  //   name: 'marquee',
  //   meta: { title: '字幕管理', icon: 'bar-chart' },
  //   redirect: 'noredirect',
  //   alwaysShow: true,
  //   children: [
  //     // {
  //     //   path: 'marqueeVersion',
  //     //   name: 'marqueeVersion',
  //     //   component: () => import('@/views/marquee/marqueeVersion'),
  //     //   meta: { title: '字幕版本' }
  //     // }
  //   ]
  // },
  // {
  //   path: '/adv',
  //   component: Layout,
  //   name: 'adv',
  //   meta: { title: '广告管理', icon: 'bar-chart' },
  //   redirect: 'noredirect',
  //   alwaysShow: true,
  //   children: [
  //     // {
  //     //   path: 'advPosList',
  //     //   name: 'advPosList',
  //     //   component: () => import('@/views/adv/advPosList'),
  //     //   meta: { title: '广告位管理' }
  //     // },
  //     // {
  //     //   path: 'advVersion',
  //     //   name: 'advVersion',
  //     //   component: () => import('@/views/adv/advVerList'),
  //     //   meta: { title: '广告版本' }
  //     // }
  //   ]
  // },
  {
    path: '/systemSetting',
    component: Layout,
    name: 'systemSetting',
    meta: { title: '系统管理', icon: 'setting' },
    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: 'blacklist',
        name: 'blacklist',
        component: () => import('@/views/systemSetting/blacklist'),
        meta: { title: '黑名单管理', permissionName: 'system_setting:black_list' }
      },
      {
        path: 'greylist',
        name: 'greylist',
        component: () => import('@/views/systemSetting/greyList'),
        meta: { title: '灰名单管理', permissionName: 'system_setting:grey_list_manage' }
      },
      {
        path: 'greyInfolist',
        name: 'greyInfolist',
        component: () => import('@/views/systemSetting/greyInfolist'),
        meta: { title: '灰名单提示管理', permissionName: 'system_setting:grey_list' }
      },
      {
        path: 'stock',
        name: 'stock',
        component: () => import('@/views/systemSetting/stock'),
        meta: { title: '库存信息配置', permissionName: 'system_setting:stock_config' }
      },
      {
        path: 'project',
        name: 'project',
        component: () => import('@/views/systemSetting/project'),
        meta: { title: '项目设置', permissionName: 'system_setting:global_setting' }
      },
      {
        path: 'limitGroup',
        name: 'limitGroup',
        component: () => import('@/views/systemSetting/limitGroup'),
        meta: { title: '限购组管理', permissionName: 'system_setting:order_limit_group' }
      }
    ]
  },
  {
    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
  })
