<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

											let opts = {
												url: '/app/user/info',
												method: 'get'
											}
											that.http.httpTokenRequest(opts, {}).then(res => {
												if (res.data.code == 200) {
													let xcxIndexPath = res.data.data.xcxIndexPath
													console.log(xcxIndexPath,path,'扫一扫进入')
													if (xcxIndexPath) {
														xcxIndexPath = path.replace(xcxIndexPath, '').replace(
															"?", '')
														if (xcxIndexPath.indexOf('scene') >= 0) {
															let scene = xcxIndexPath.replace('scene=', '')
															let param = {
																code: scene
															}
															let opts = {
																url: '/app/user/check/move/code',
																method: 'post'
															}
															that.http.httpTokenRequest(opts, param).then(
																res => {
																	if (res.data.code == 200) {
																		if (res.data.data.bindFlag) {
																			that.gotoMoveCar(scene)
																		} else {
																			that.gotoBindMoveCar(scene)
																		}
																	}else{
																		uni.showToast({
																			title: res.data.msg,
																			icon: 'none',
																		});
																	}
																})
														} else {
															uni.showToast({
																title: '扫码失败',
																icon: 'none',
															})
														}
													} else {
														uni.showToast({
															title: '扫码失败',
															icon: 'none',
														})
													}
												}else{
													uni.showToast({
														title: res.data.msg,
														icon: 'none',
													});
												}
											})
										},
										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
				let param = {
					code: code,
				}
				let opts = {
					url: '/xcx/code/session',
					method: 'post'
				}
				await that.http.httpRequest(opts, param).then(res => {
					if (res.data.code == 200) {
						that.openid = res.data.data.openid
						uni.setStorageSync('openid', that.openid);
					}
				})
			},
			// 获取爱车列表
			getCarList(){
				uni.removeStorageSync('firstCoupon')
				let opts = { url: '/app/vehicleAdmin/list', method: 'get' };
				this.http.httpTokenRequest(opts, {}).then(res => {
					if (res.statusCode == 200) {
						this.carList = res.data
						if(res.data.length > 0){
							let opts = { url: '/app/user/send/coupon', method: 'post' };
							this.http.httpTokenRequest(opts, {}).then(res => {
								if(res.data.code == 200){
									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
							});
						}
					}
				})
			}
		},
		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 opts = {
								url: '/xcx/login',
								method: 'post'
							}
							let openid = uni.getStorageSync('openid')
							that.http.httpTokenRequest(opts, {
								token: token,
								openid: openid
							}).then(res => {
								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) {
										//获取二维码携带的参数
										//扫码后得到的参数
										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)
					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
							});
						}
					})
				}
			}
			
		},
	}
</script>

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