<template>
	<view class="main_content">
		<index v-if="tab=='index'"></index>
		<moveCar v-if="tab=='moveCar'"></moveCar>
		<shop v-if="tab=='shop'" ref="shop" :page="page"></shop>
		<personal v-if="tab=='personal'" ref="personal"></personal>
		<u-gap bgColor="transparent" height="055"></u-gap>
		<view class="tabbar">
			<v-tabbar ref="myTab" :list="tabbar" style="position:fixed;bottom:0;width:100%;left:0;right:0;"
				@tabChange="tabChange"></v-tabbar>
		</view>
	</view>
</template>

<script src="https://gias.jd.com/js/td.js"></script>
<script>
	import {
		mapState
	} from 'vuex';
	import tabbar from '@/common/hans-tabbar/hans-tabbar'
	// import index from './home'
	import index from './home-new.vue'
	import moveCar from './moveCar'
	import shop from './shop'
	import personal from './personal'
	let plugin = requirePlugin('loginPlugin');

	export default {
		components: {
			index,
			moveCar,
			personal,
			shop,
			'v-tabbar': tabbar
		},
		data() {
			return {
				tab: "index",
				page: 1,
				scene: null, // 二维码携带参数
				scanClick: true, //扫一扫节流
			}
		},
		computed: {
			...mapState({
				tabbarTextColor: "tabbarTextColor",
				tabbarTextActiveColor: "tabbarTextActiveColor",
				tabbar: "tabbar"
			})
		},
		onShareAppMessage(res) {
			console.log(res, 111111)
			if (res.from === 'menu') {
				return {
					title: '京东汽车挪车牌',
					path: '/pages/main',
					imageUrl: ""
				};
			}
		},
		onShow: function() {
			uni.hideHomeButton()
			if (this.tab == 'personal') {
				this.$refs.personal.getUserInfo();
			}
			// 领券判断
			let firstCoupon = uni.getStorageSync('firstCoupon') || false
			if (firstCoupon) {
				this.getCarList()
			}
		},
		onReachBottom() {
			let refresh = uni.getStorageSync("refresh")
			if (refresh == 0) {
				if (this.tab == 'shop') {
					this.page++;
				}
			}
		},
		methods: {
			tabChange(index) {
				let that = this
				switch (index) {
					case 0:
						this.tab = 'index'
						break;
					case 1:
						this.tab = 'moveCar'
						break;
					case 2:
						let token = uni.getStorageSync('userToken');
						if (token) {
							if (this.scanClick) {
								this.scanClick = false
								setTimeout(() => {
									uni.scanCode({
										success: (res) => {
											this.scanClick = true
											let path = res.path
											
											if (path) {
												let paths = path.split('?')[1]
												let scene = paths.split("=")[1]
												// this.$http.request({
												// 	url:'/app/user/check/move/code',
												// 	method:'post',
												// 	data:{
												// 		code: scene
												// 	},
												// }).then(res => {
												// 	console.log(res, "扫码通知车主")
												// 	if (res.bindFlag) {
												// 		that.gotoMoveCar(scene)
												// 	} else {
												// 		that.gotoBindMoveCar(scene)
												// 	}
												// })
												this.goScene(scene)
											} else {
												uni.showToast({
													title: "扫码失败!",
													icon: 'error',
												})
											}
										},
										fail: () => {
											this.scanClick = true
										}
									});
								}, 300)
							}
						} else {
							let returnPage = '/pages/main';
							let pageType = "reLaunch"
							uni.navigateTo({
								url: "/pages/login/index/index?returnPage=" + returnPage + '&pageType=' + pageType
							})
						}
						break;
					case 3:
						this.tab = 'shop'
						break;
					case 4:
						this.tab = 'personal'
						break;
				}
				that.$forceUpdate();
			},
			//跳转到挪车页面
			gotoMoveCar(s) {
				let that = this;
				uni.navigateTo({
					url: '/pagesB/pages/move-car/oneMoveCar?scene=' + s
				});
			},
			//跳转到绑定页面
			gotoBindMoveCar(s) {
				let that = this;
				uni.navigateTo({
					url: '/pagesB/pages/move-car/scan?moveCode=' + s
				});
			},
			wechatLogin() {
				wx.login({
					success: (res) => {
						this.wechatSession(res.code)
					}
				})
			},
			//获取session_key
			async wechatSession(code) {
				let that = this
				that.$http.request({
					url: '/xcx/code/session',
					method: 'post',
					data: {
						code: code,
					}
				}).then(res => {
					that.openid = res.openid
					uni.setStorageSync('openid', that.openid);
				})
			},
			// 获取爱车列表
			getCarList() {
				uni.removeStorageSync('firstCoupon')
				this.$http.request({
					url:'/app/vehicleAdmin/list',
					method:'get',
					data:{},
					isFactory:false,
				}).then(res => {
					if (res.statusCode == 200) {
						this.carList = res.data
						if (res.data.length > 0) {
							this.$http.request({
								url:'/app/user/send/coupon',
								method:'post',
								data:{}
							}).then(res => {
								uni.showToast({
									title: "领取成功！",
									icon: 'none',
								});
								let timer = setTimeout(() => {
									uni.navigateToMiniProgram({
										appId: 'wx91d27dbf599dff74', // 跳转目标小程序的id
										path: 'pages/item/detail/detail?sku=100023585323', // 目标小程序的页面路径
										extraData: { // 需要携带的参数
												
										},
										success: (res) => {
											// 打开成功
											clearTimeout(timer)
										}
									})
								}, 600)
							})
						} else {
							uni.showToast({
								title: "请添加爱车后再领取！",
								icon: 'none',
								duration: 1500
							});
						}
					}
				})
			},
			// 扫一扫
			goScene(scene){
				this.$http.request({
					url:'/app/user/check/move/code',
					method:'post',
					data:{
						code: scene
					},
				}).then(res => {
					console.log(res, "扫码通知车主")
					if (res.bindFlag) {
						this.gotoMoveCar(scene)
					} else {
						this.gotoBindMoveCar(scene)
					}
					uni.removeStorageSync('scene')
				})
			}
		},
		onLoad(option) {
			// 静默授权获取openid
			let openid = uni.getStorageSync('openid');
			if (!openid) {
				this.wechatLogin()
			}


			let that = this
			if (option && option.index) {
				that.$nextTick(() => {
					let i = parseInt(option.index);
					if (!i) {
						i = 0;
					}
					that.tabChange(i)
					that.$refs.myTab.current = i;
				});
			}


			let token = uni.getStorageSync('userToken')
			if (!token) {
				let ptKey = plugin.getPtKey();
				if (ptKey) {
					uni.showLoading({
						title: '登录中',
						mask: true
					});
					plugin.isvObfuscator({
						url: 'https://wxapplogin2.m.jd.com/cgi-bin/login/isv/isvObfuscator'
					}).then(res => {
						let {
							token,
							message
						} = res;
						if (!token) {
							console.log(res, "pin_key失效")
							uni.navigateTo({
								url: "/pages/login/index/index"
							})
							uni.hideLoading()
							// uni.showToast({
							// 	title: `京东登录失败:${message}`,
							// 	icon: 'none',
							// 	duration: 2000
							// });
						} else {
							let openid = uni.getStorageSync('openid')
							that.$http.request({
								url:'/xcx/login',
								method:'post',
								data:{
									token: token,
									openid: openid
								},
								isFactory:false,
								load:false
							}).then(res => {
								console.log(res,'88888888')
								if (res.data.code == 200) {
									let token = res.data.data.token
									uni.setStorageSync('userToken', token)
									uni.hideLoading()
									uni.showToast({
										title: "登录成功",
										icon: 'none',
										duration: 2000
									});
								
									// 判断是否扫码进入
									that.scene = uni.getStorageSync('scene')
									console.log(that.scene, "微信扫码进入")
									if (that.scene) {
										//获取二维码携带的参数
										//扫码后得到的参数
										this.goScene(that.scene)
										// that.http.httpTokenRequest({
										// 	url: '/app/user/check/move/code',
										// 	method: 'post'
										// }, {
										// 	code: that.scene
										// }).then(res => {
										// 	if (res.data.code == 200) {
										// 		if (res.data.data.bindFlag) {
										// 			that.gotoMoveCar(that.scene);
										// 		} else {
										// 			that.gotoBindMoveCar(that.scene);
										// 		}
										// 		uni.removeStorageSync('scene')
										// 	} else {
										// 		uni.showToast({
										// 			title: res.data.msg,
										// 			icon: 'none',
										// 			duration: 2000
										// 		});
										// 	}
										// })
									}
								
								} else {
									uni.hideLoading()
									uni.showToast({
										title: res.data.msg,
										icon: "error",
									});
								}
							})
						}

					}).catch(err => {
						console.log('err', err)
					})
				} else {
					// 判断是否扫码进入
					console.log("扫码进入但是未登录")
					if (option && option.scene) {
						uni.setStorageSync('scene', decodeURIComponent(option.scene))
						let returnPage = '/pages/main';
						let pageType = "reLaunch"
						uni.navigateTo({
							url: "/pages/login/index/index?returnPage=" + returnPage + '&pageType=' + pageType
						})
					}
				}
			} else {
				// 判断是否扫码进入
				if (option && option.scene) {
					console.log(option.scene, "扫码一登录")
					//获取二维码携带的参数
					//扫码后得到的参数
					that.scene = decodeURIComponent(option.scene)
					uni.setStorageSync('scene', that.scene)
					this.goScene(that.scene)
				}
			}

		},
	}
</script>

<style lang="scss" scoped>
	.main_content {
		height: 100%;
		overflow: hidden;
	}
</style>
