<template>
	<view>
		<index v-if="loadPage.indexOf('home')!=-1" v-show="channel=='home'" ref="home"></index>
		<service v-if="loadPage.indexOf('service')!=-1" v-show="channel=='service'" ref="service"></service>
		<user v-if="loadPage.indexOf('user')!=-1" v-show="channel=='user'" ref="user"></user>
		<cart v-if="loadPage.indexOf('cart')!=-1" v-show="channel=='cart'" ref="cart"></cart>
		<!-- #ifdef H5 -->
		<!-- <wx-share /> -->
		<wx-share ref="wxshare" />
		<!-- #endif -->
	</view>
</template>

<script>
	const app = getApp();
	const req = require("../../utils/request.js");
	import index from '../index/index.vue';
	import service from '../../pages/service/index.vue';
	import user from '../../pages/user/user.vue';
	import cart from '../../pages/cart/cart.vue';
	export default {
		components: {
			index,
			service,
			user,
			cart
		},
		props: {},
		data() {
			return {
				channel: '',
				loadPage: [],
				options: {},
				pageScroll: {},
				shareContent: ''
			}
		},
		async onLoad(options) {
			// this.wxOauth();
			// #ifdef H5
			var appId = req.getStorage('appId');
			if (appId && options.appId == appId||options.appId=="undefined"||!options.appId) {
				
			} else {
				console.log('appId》》》》》》',options.appId);
				req.setStorage('appId', options.appId);
				await this.getwxConfig();
				window.location.reload();
			}
			// #endif
			this.options = req.getStorage('optionsPageDate')
			req.removeStorage('optionsPageDate')
			if (this.options && this.options.pageChannel) {
				this.channel = this.options.pageChannel
			} else {
				this.channel = 'home'
			}
			this.init(this.options)
			uni.$on('tabClick', (data) => {
				console.log('onLoad', data)
				this.channel = data
				this.options = req.getStorage('optionsPageDate')
				this.init(this.options)
			})
		},
		onShow() {
			this.options = req.getStorage('optionsPageDate')
			req.removeStorage('optionsPageDate')
			console.log('onShow optionsPageDate', this.options)
			if (this.options && this.options.pageChannel) {
				if (this.channel == this.options.pageChannel) {
					return false
				} else {
					this.channel = this.options.pageChannel
					this.init(this.options)
				}
			}
			try {
				this.$refs[this.channel].onShowPage()
			} catch (e) {
				//TODO handle the exception
			}
		},
		onShareAppMessage: function() {
			try {
				return this.$refs[this.channel].onShareAppMessagePage()
			} catch (e) {
				//TODO handle the exception
			}
		},
		onShareTimeline() {
			try {
				return this.$refs[this.channel].onShareTimelinePage()
			} catch (e) {
				//TODO handle the exception
			}
		},
		onReachBottom(e) {
			this.$nextTick(() => {
				try {
					this.$refs[this.channel].onReachBottomPage(e)
				} catch (e) {
					//TODO handle the exception
				}
			})
		},
		onPageScroll(e) {
			this.pageScroll[this.channel] = e.scrollTop
			this.pageScroll = JSON.parse(JSON.stringify(this.pageScroll))
			try {
				this.$refs[this.channel].onPageScrollPage(e)
			} catch (e) {
				//TODO handle the exception
			}
		},

		onReady() {
			// #ifdef H5
			var that = this;
			//初始化分享内容
			setTimeout(function() {
				that.shareContent = that.$refs[that.channel].onShareAppMessagePage();
				if (that.shareContent) {
					that.shareContent.path = window.location.origin + that.shareContent.path;
				}
				console.log('分享内容》》》》》', that.shareContent);
				that.$refs.wxshare.init(that.shareContent);
			}, 7 * 1000);
			// #endif
		},

		methods: {
			getwxConfig() {
				return new Promise((resolve, reject) => {
					req.getwxConfig(data => {
						resolve();
					});
				});
			},

			init(options) {
				console.log('init', this._data)
				console.log(this.loadPage.indexOf(this.channel))
				//已加载的页面不再调onload
				if (this.loadPage.indexOf(this.channel) == -1) {
					this.loadPage.push(this.channel)
					this.$nextTick(() => {
						//已加载的页面不再调onload
						try {
							this.$refs[this.channel].onLoadPage(options)
						} catch (e) {
							//TODO handle the exception
						}
					})
					// this.$refs[this.channel].onLoadPage(options)
				}
				this.$nextTick(() => {
					try {
						if (this.pageScroll[this.channel]) {
							uni.pageScrollTo({
								duration: 0,
								scrollTop: this.pageScroll[this.channel]
							})
						} else {
							uni.pageScrollTo({
								duration: 0,
								scrollTop: 0
							})
						}
						this.$refs[this.channel].onShowPage()
					} catch (e) {
						//TODO handle the exception
					}
				});


			}
		},
	}
</script>

<style>
</style>