import Vue from 'vue'
import Router from 'vue-router'

const routerPush = Router.prototype.push

Router.prototype.push = function push(location) {

	// if(typeof(location)=="string"){
	//   var Separator = "&";
	//   if(location.indexOf('?')==-1) { Separator='?'; }
	//   location = location + Separator + "random=" + Math.random();
	// }

	return routerPush.call(this, location).catch(error => error)
}

Vue.use(Router)

let router = new Router({
	// mode: 'history',
	base: process.env.BASE_URL,
	routes: [{
			path: '/',
			redirect: '/home'
		},
		{
			path: '/login',
			name: 'login',
			component: () => import('./views/Login.vue'),
			meta: {
				title: '登录'
			}
		},
		{
			path: '/home',
			name: 'home',
			component: () => import('./views/Home.vue'),
			redirect: '/home/statistics',
			meta: {
				title: ''
			},
			children: [{
					path: 'regList',
					name: 'regList',
					component: () => import('./views/home/regList.vue'),
					meta: {
						title: 'Token列表'
					}
				},
				{
					path: 'buyReg',
					name: 'buyReg',
					component: () => import('./views/home/buyReg.vue'),
					meta: {
						title: '购买注册码'
					}
				},
				{
					path: 'abonementList',
					name: 'abonementList',
					component: () => import('./views/home/abonementList.vue'),
					meta: {
						title: '用户列表'
					}
				},
				{
					path: 'orderList',
					name: 'orderList',
					component: () => import('./views/home/orderList.vue'),
					meta: {
						title: '订单列表'
					}
				},
				{
					path: 'addOrder',
					name: 'addOrder',
					component: () => import('./views/home/addOrder.vue'),
					meta: {
						title: '添加订单'
					}
				},
				{
					path: 'addressList',
					name: 'addressList',
					component: () => import('./views/home/addressList.vue'),
					meta: {
						title: '地址列表'
					}
				},
				{
					path: 'rechangeList',
					name: 'rechangeList',
					component: () => import('./views/home/rechangeList.vue'),
					meta: {
						title: '充值记录'
					}
				},
				{
					path: 'rechange',
					name: 'rechange',
					component: () => import('./views/home/rechange.vue'),
					meta: {
						title: '充值'
					}
				},
				{
					path: 'taking',
					name: 'taking',
					component: () => import('./views/home/taking.vue'),
					meta: {
						title: '提现'
					}
				},
				{
					path: 'takingList',
					name: 'takingList',
					component: () => import('./views/home/takingList.vue'),
					meta: {
						title: '提现记录'
					}
				},
				{
					path: 'transferList',
					name: 'transferList',
					component: () => import('./views/home/transferList.vue'),
					meta: {
						title: '转账记录'
					}
				},
				{
					path: 'transfer',
					name: 'transferList',
					component: () => import('./views/home/transfer.vue'),
					meta: {
						title: '转账'
					}
				},
				{
					path: 'setAgent',
					name: 'setAgent',
					component: () => import('./views/home/setAgent.vue'),
					meta: {
						title: '设置下级代理'
					}
				},
				{
					path: 'statistics',
					name: 'statistics',
					component: () => import('./views/home/statistics.vue'),
					meta: {
						title: '统计'
					}
				},
				{
					path: 'dlstatistics',
					name: 'dlstatistics',
					component: () => import('./views/home/dlstatistics.vue'),
					meta: {
						title: '统计'
					}
				},
				{
					path: 'setPas',
					name: 'setPas',
					component: () => import('./views/home/setPas.vue'),
					meta: {
						title: '交易密码'
					}
				},
				{
					path: 'setGa',
					name: 'setGa',
					component: () => import('./views/home/setGa.vue'),
					meta: {
						title: '动态口令'
					}
				},
				{
					path: 'setUserinfo',
					name: 'setUserinfo',
					component: () => import('./views/home/setUserinfo.vue'),
					meta: {
						title: '修改个人信息'
					}
				},
				{
					path: 'money',
					name: 'money',
					component: () => import('./views/home/money.vue'),
					meta: {
						title: '资金流水'
					}
				},
				{
					path: 'tokenOrderList',
					name: 'tokenOrderList',
					component: () => import('./views/home/tokenOrderList.vue'),
					meta: {
						title: '资金流水'
					}
				},
			]
		},
		{
			path: '*',
			redirect: '/home'
		}
	]
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.localStorage.getItem('token')
  console.log(tokenStr,'tokenStr');
  if (!tokenStr) return next('/login')
  next()
})

export default router