import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home.vue'

import { getXToken, configWx } from '@/utils/aCommon'
import mineRoute from  './mine'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'mainSale',
    component: () => import('@/views/mainSale/active/main/index')
  },
  {
    path: '/turntableDetail',
    name: 'turntableDetail',
    component: () => import('../views/active/turntableDetail.vue')
  },
  {
    path: '/couponDetail',
    name: 'couponDetail',
    component: () => import('../views/active/couponDetail.vue')
  },
  {
    path: '/createActive',
    name: 'createActive',
    component: () => import('../views/active/createActive.vue')
  },
  {
    path: '/activeList',
    name: 'activeList',
    component: () => import('../views/active/activeList.vue')
  },
  {
    path: '/createBigWheelActive',
    name: 'createBigWheelActive',
    component: () => import('../views/active/createBigWheelActive.vue')
  },
  {
    path: '/createSuccess',
    name: 'createSuccess',
    component: () => import('@/views/active/createSuccess')
  },
  {
    path: '/activeDetail',
    name: 'activeDetail',
    component: () => import('@/views/active/activeDetail')
  },
  {
    path: '/mainSale',
    name: 'MainSale',
    component: () => import('@/views/mainSale/active/main/index')
  },
  {
    path: '/changeCounter',
    name: 'changeCounter',
    component: () => import('@/views/counter/changeCounter')
  },
  {
    path: '/counterInfo',
    name: 'counterInfo',
    component: () => import('@/views/counter/counterInfo')
  },
  {
    path: '/counterMaintain',
    name: 'counterMaintain',
    component: () => import('@/views/counter/counterMaintain')
  },
  {
    path: '/counterHome',
    name: 'counterHome',
    component: () => import('@/views/counter/counterHomePage')
  },
  {
    path: '/counterEdit',
    name: 'counterEdit',
    component: () => import('@/views/counter/counterEdit')
  },
  {
    path: '/chargeCode',
    name: 'chargeCode',
    component: () => import('@/views/counter/chargeCode')
  },
  {
    path: '/pushStoreInfo',
    name: 'pushStoreInfo',
    component: () => import('@/views/counter/pushStoreInfo')
  },
  {
    path: '/goodManage',
    name: 'goodManage',
    component: () => import('@/views/goods/manage')
  },
  {
    path: '/goodManage1',
    name: 'goodManage1',
    component: () => import('@/views/goods/goodManage')
  },
  {
    path: '/goodGrounding1',
    name: 'goodGrounding',
    component: () => import('@/views/goods/goodGrounding')
  },
  {
    path: '/profitList',
    name: 'profitList',
    component: () => import('@/views/profit/index')
  },
  {
    path: '/actTemplate',
    name: 'ActTemplate',
    component: () => import('@/views/mainSale/active/template/index')
  },
  {
    path: '/me',
    name: 'Me',
    component: () => import('@/views/mainSale/me/main/index')
  },
  {
    path: '/task',
    name: 'Task',
    component: () => import('@/views/mainSale/me/task/index')
  },
  {
    path: '/taskList',
    name: 'taskList',
    component: () => import('@/views/task/taskList')
  },
  {
    path: '/newMemberTask',
    name: 'newMemberTask',
    component: () => import('@/views/task/newMemberTask')
  },
  {
    path: '/case',
    name: 'case',
    component: () => import('@/views/case/index')
  },
  {
    path: '/caseEdit',
    name: 'caseEdit',
    component: () => import('@/views/case/caseEdit')
  },
  {
    path: '/caseDetail',
    name: 'caseDetail',
    component: () => import('@/views/case/caseDetail')
  },
  {
    path: '/newInfo',
    name: 'newInfo',
    component: () => import('@/views/message/newInfo')
  },
  {
    path: '/voucherInfo',
    name: 'VoucherInfo',
    component: () => import('@/views/voucherInfo/index')
  },
  {
    path: '/qrCode',
    name: 'qrCode',
    component: () => import('@/views/qrCode/index')
  },
  {
    path: '/noSet',
    name: 'noSet',
    component: () => import('@/views/counter/noSet')
  },
  {
    path: '/coupon',
    name: 'coupon',
    component: () => import('@/views/coupon/list')
  },
  {
    path: '/couponAdd',
    name: 'couponAdd',
    component: () => import('@/views/coupon/addCoupon')
  },
  {
    path: '/yhqDetail',
    name: 'yhqDetail',
    component: () => import('@/views/coupon/detail')
  },
   ...mineRoute
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const whiteList = ['/discount', 'discount']

router.beforeEach((to, from ,next) => {
  // alert(`--->route: beforeEach: to =${JSON.stringify(to)}. from =${from}. next =${JSON.stringify(next)}`)
  if(whiteList.indexOf(to.path) != -1) {
    // alert(`--->route: beforeEach: If: UnIn whiteList.`)
    if(sessionStorage.getItem('zConfigWx') === 'Pass'){
      next()
    } else {
      configWx(to)
    }

  } else {
    // alert(`--->route: beforeEach: If: In whiteList.`)

    next()
  }

  // if (whiteList.indexOf(to.path) !== -1) {
  //   next()
  // } else {
  //   if(sessionStorage.getItem('unionid')) {
  //     next()
  //   } else {
  //     getUnionId(to)
  //     // next()
  //   }
  // }
})

// async function getUnionId (to) {
//   await getXToken()
//   configWx(to)
// }

export default router
