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: '登录',
				keepAlive: false
			}
		},
		{
			path: '/address',
			name: 'address',
			component: () => import('./views/Address.vue'),
			meta: {
				title: '地址管理',
				keepAlive: true
			},
			redirect: '/address/dlAddressList',
			children: [
				{
					path: 'dlAddressList',
					name: 'dlAddressList',
					component: () => import('./views/address/dlAddressList.vue'),
					meta: {
						title: '地址列表',
						keepAlive: true
					}
				},
			]
		},
		{
			path: '/settle',
			name: 'settle',
			component: () => import('./views/Settle.vue'),
			meta: {
				title: '地址管理',
				keepAlive: true
			},
			redirect: '/settle/settleOrder',
			children: [
				{
					path: 'settleOrder',
					name: 'settleOrder',
					component: () => import('./views/settle/settleOrder.vue'),
					meta: {
						title: '地址列表',
						keepAlive: true
					}
				},
			]
		},
		{
			path: '/order',
			name: 'order',
			component: () => import('./views/Order.vue'),
			meta: {
				title: '订单',
				keepAlive: true
			},
			children: [
				{
					path: 'orderList',
					name: 'orderList',
					component: () => import('./views/order/orderList.vue'),
					meta: {
						title: '订单列表',
						keepAlive: true
					}
				},
				{
					path: 'dlOrderList',
					name: 'dlOrderList',
					component: () => import('./views/order/dlOrderList.vue'),
					meta: {
						title: '订单列表',
						keepAlive: true
					}
				}
			]
		},
    {
    	path: '/dladmin',
    	name: 'dladmin',
    	component: () => import('./views/Dl.vue'),
    	meta: {
    		title: '结算审核',
    		keepAlive: true
    	},
      redirect: '/dladmin/dljs',
      children: [
        {
          path: 'dljs',
          name: 'dladmindljs',
          component: () => import('./views/dl/dljs.vue'),
          meta: {
          	title: '结算审核',
          	keepAlive: true
          },
        }
      ]
    },
		{
			path: '/user',
			name: 'user',
			component: () => import('./views/User.vue'),
			meta: {
				title: '用户中心',
				keepAlive: true
			},
			redirect: '/user/setUserinfo',
			children: [{
					path: 'setUserinfo',
					name: 'setUserinfo',
					component: () => import('./views/user/setUserinfo.vue'),
					meta: {
						title: '修改个人信息',
						keepAlive: true
					}
				},
				{
					path: 'tokenOrderList',
					name: 'tokenOrderList',
					component: () => import('./views/user/tokenOrderList.vue'),
					meta: {
						title: 'Token',
						keepAlive: true
					}
				},
				{
					path: 'regList',
					name: 'regList',
					component: () => import('./views/user/regList.vue'),
					meta: {
						title: '注册码',
						keepAlive: true
					}
				},
				{
					path: 'money',
					name: 'money',
					component: () => import('./views/user/money.vue'),
					meta: {
						title: '资金流水',
						keepAlive: true
					}
				},
				{
					path: 'rechangeList',
					name: 'rechangeList',
					component: () => import('./views/user/rechangeList.vue'),
					meta: {
						title: '充值记录',
						keepAlive: true
					}
				},
				{
					path: 'takingList',
					name: 'takingList',
					component: () => import('./views/user/takingList.vue'),
					meta: {
						title: '提现记录',
						keepAlive: true
					}
				},
				{
					path: 'transferList',
					name: 'transferList',
					component: () => import('./views/user/transferList.vue'),
					meta: {
						title: '转账记录',
						keepAlive: true
					}
				},
				{
					path: 'userAdmin',
					name: 'userAdmin',
					component: () => import('./views/user/userAdmin.vue'),
					meta: {
						title: '用户管理',
						keepAlive: true
					}
				},
        {
        	path: 'jssq',
        	name: 'jssq',
        	component: () => import('./views/user/jssq.vue'),
        	meta: {
        		title: '结算申请',
        		keepAlive: true
        	}
        }
			]
		},
		{
			path: '/home',
			name: 'home',
			component: () => import('./views/Home.vue'),
			// redirect: '/home/addOrder',
			meta: {
				title: '',
				keepAlive: true
			},
			children: [
				{
					path: 'buyReg',
					name: 'buyReg',
					component: () => import('./views/home/buyReg.vue'),
					meta: {
						title: '购买注册码',
						keepAlive: true
					}
				},
				{
					path: 'abonementList',
					name: 'abonementList',
					component: () => import('./views/home/abonementList.vue'),
					meta: {
						title: '用户列表',
						keepAlive: true
					}
				},
				
				{
					path: 'addOrder',
					name: 'addOrder',
					component: () => import('./views/home/addOrder.vue'),
					meta: {
						title: '添加订单',
						keepAlive: true
					}
				},
				{
					path: 'addressList',
					name: 'addressList',
					component: () => import('./views/home/addressList.vue'),
					meta: {
						title: '地址列表',
						keepAlive: true
					}
				},
				{
					path: 'setAgent',
					name: 'setAgent',
					component: () => import('./views/home/setAgent.vue'),
					meta: {
						title: '设置下级代理',
						keepAlive: true
					}
				},
				{
					path: 'statistics',
					name: 'statistics',
					component: () => import('./views/home/statistics.vue'),
					meta: {
						title: '统计',
						keepAlive: true
					}
				},
				{
					path: 'dlstatistics',
					name: 'dlstatistics',
					component: () => import('./views/home/dlstatistics.vue'),
					meta: {
						title: '统计',
						keepAlive: true
					}
				},
				{
					path: 'setPas',
					name: 'setPas',
					component: () => import('./views/home/setPas.vue'),
					meta: {
						title: '交易密码',
						keepAlive: true
					}
				},
				{
					path: 'setGa',
					name: 'setGa',
					component: () => import('./views/home/setGa.vue'),
					meta: {
						title: '动态口令',
						keepAlive: true
					}
				},
				
			]
		},
		{
			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