import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/components/Layout.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { public: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/customization/hsbc/equity-products',
    children: [
      {
        path: 'customization/hsbc/pages',
        redirect: 'customization/hsbc/pages/home',
        meta: { title: '页面管理' }
      },
      {
        path: 'customization/hsbc/pages/home',
        name: 'PageHomeConfig',
        component: () => import('@/views/PageHomeConfig.vue'),
        meta: { title: '权益介绍页' }
      },
      {
        path: 'customization/hsbc/pages/zone',
        name: 'PageZoneConfig',
        component: () => import('@/views/PageZoneConfig.vue'),
        meta: { title: '权益首页' }
      },
      {
        path: 'customization/hsbc/pages/assembly',
        name: 'PageAssembly',
        component: () => import('@/views/PageAssembly.vue'),
        meta: { title: '组装页' }
      },
      {
        path: 'customization/hsbc/equity-products',
        name: 'EquityProducts',
        component: () => import('@/views/EquityProducts.vue'),
        meta: { title: '借记卡权益', icon: 'el-icon-goods' }
      },
      {
        path: 'customization/hsbc/credit-card-equity',
        name: 'CreditCardEquity',
        component: () => import('@/views/CreditCardEquity.vue'),
        meta: { title: '信用卡权益', icon: 'el-icon-bank-card' }
      },
      {
        path: 'customization/hsbc/crowd-tasks',
        name: 'CrowdTaskList',
        component: () => import('@/views/CrowdTaskList.vue'),
        meta: { title: '客群任务管理', icon: 'el-icon-user' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/add',
        name: 'CrowdTaskAdd',
        component: () => import('@/views/CrowdTaskEdit.vue'),
        meta: { title: '新增客群规则任务' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/edit/:id',
        name: 'CrowdTaskEdit',
        component: () => import('@/views/CrowdTaskEdit.vue'),
        meta: { title: '编辑客群规则任务' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/copy/:id',
        name: 'CrowdTaskCopy',
        component: () => import('@/views/CrowdTaskEdit.vue'),
        meta: { title: '复制客群规则任务' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/view/:id',
        name: 'CrowdTaskView',
        component: () => import('@/views/CrowdTaskView.vue'),
        meta: { title: '客群规则任务详情' }
      },
      {
        path: 'customization/hsbc/crowd-name-lists',
        name: 'CrowdNameList',
        component: () => import('@/views/CrowdNameList.vue'),
        meta: { title: '客群名单管理', icon: 'el-icon-document' }
      },
      {
        path: 'customization/hsbc/crowd-name-lists/add',
        name: 'CrowdNameListAdd',
        component: () => import('@/views/CrowdNameListEdit.vue'),
        meta: { title: '新增客群' }
      },
      {
        path: 'customization/hsbc/crowd-name-lists/edit/:id',
        name: 'CrowdNameListEdit',
        component: () => import('@/views/CrowdNameListEdit.vue'),
        meta: { title: '编辑客群' }
      },
      {
        path: 'customization/hsbc/rule-groups',
        name: 'RuleGroupList',
        component: () => import('@/views/RuleGroupList.vue'),
        meta: { title: '规则组管理', icon: 'el-icon-set-up' }
      },
      {
        path: 'customization/hsbc/rule-groups/add',
        name: 'RuleGroupAdd',
        component: () => import('@/views/RuleGroupEdit.vue'),
        meta: { title: '新增规则组' }
      },
      {
        path: 'customization/hsbc/rule-groups/edit/:id',
        name: 'RuleGroupEdit',
        component: () => import('@/views/RuleGroupEdit.vue'),
        meta: { title: '编辑规则组' }
      },
      {
        path: 'customization/hsbc/rule-groups/view/:id',
        name: 'RuleGroupView',
        component: () => import('@/views/RuleGroupView.vue'),
        meta: { title: '规则组详情' }
      },
      {
        path: 'customization/hsbc/equity-products/add',
        name: 'AddEquityProduct',
        component: () => import('@/views/AddEquityProduct.vue'),
        meta: { title: '添加权益', icon: 'el-icon-edit' }
      },
      {
        path: 'customization/hsbc/equity-products/view/:id',
        name: 'ViewEquityProduct',
        component: () => import('@/views/ViewEquityProduct.vue'),
        meta: { title: '查看权益', icon: 'el-icon-view' }
      },
      {
        path: 'customization/hsbc/equity-products/edit/:id',
        name: 'EditEquityProduct',
        component: () => import('@/views/EditEquityProduct.vue'),
        meta: { title: '编辑权益', icon: 'el-icon-edit' }
      },
      {
        path: 'customization/hsbc/equity-products/audit/:id',
        name: 'AuditEquityProduct',
        component: () => import('@/views/AuditEquityProduct.vue'),
        meta: { title: '审核权益', icon: 'el-icon-check' }
      },
      {
        path: 'customization/hsbc/equity-products/coupons/:id',
        name: 'EquityProductCoupons',
        component: () => import('@/views/EquityProductCoupons.vue'),
        meta: { title: '券码管理', icon: 'el-icon-files' }
      },
      {
        path: 'customization/hsbc/tags',
        name: 'TagList',
        component: () => import('@/views/TagList.vue'),
        meta: { title: '标签管理', icon: 'el-icon-collection-tag' }
      },
      {
        path: 'customization/hsbc/tags/add',
        name: 'TagAdd',
        component: () => import('@/views/TagEdit.vue'),
        meta: { title: '新建标签', icon: 'el-icon-plus' }
      },
      {
        path: 'customization/hsbc/tags/edit/:id',
        name: 'TagEdit',
        component: () => import('@/views/TagEdit.vue'),
        meta: { title: '编辑标签', icon: 'el-icon-edit' }
      },
      {
        path: 'customization/hsbc/tags/view/:id',
        name: 'TagView',
        component: () => import('@/views/TagView.vue'),
        meta: { title: '查看标签', icon: 'el-icon-view' }
      },
      {
        path: 'customization/hsbc/sms-templates',
        name: 'SmsTemplateList',
        component: () => import('@/views/SmsTemplateList.vue'),
        meta: { title: '短信模板管理', icon: 'el-icon-message' }
      },
      {
        path: 'customization/hsbc/sms-send-records',
        name: 'SmsSendRecord',
        component: () => import('@/views/SmsSendRecord.vue'),
        meta: { title: '发送记录查询', icon: 'el-icon-document-copy' }
      },
      {
        path: 'customization/hsbc/equity-grant',
        name: 'CustomerEquityGrant',
        component: () => import('@/views/CustomerEquityGrant.vue'),
        meta: { title: '客户权益发放', icon: 'el-icon-s-data' }
      },
      {
        path: 'customization/hsbc/cash-discount-grant',
        name: 'CashDiscountGrant',
        component: () => import('@/views/CashDiscountGrant.vue'),
        meta: { title: '立减金发放明细', icon: 'el-icon-s-finance' }
      },
      {
        path: 'customization/hsbc/points-detail',
        name: 'PointsDetail',
        component: () => import('@/views/PointsDetail.vue'),
        meta: { title: '积分明细查询', icon: 'el-icon-s-order' }
      },
      {
        path: 'customization/hsbc/channel',
        name: 'ChannelManagement',
        component: () => import('@/views/ChannelManagement.vue'),
        meta: { title: 'Channel管理' }
      },
      {
        path: 'customization/hsbc/campaign',
        name: 'CampaignManagement',
        component: () => import('@/views/CampaignManagement.vue'),
        meta: { title: 'Campaign管理' }
      }
    ]
  },
  { path: '*', redirect: '/' }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta && to.meta.public) {
    if (token && to.path === '/login') return next('/')
    return next()
  }
  if (!token) {
    return next({ path: '/login', query: { redirect: to.fullPath } })
  }
  next()
})

export default router
