<template>
	<view>
		<index v-if="curPageCode === 'indexPage'" @changeCurPageCode="changeCurPageCode"></index>
		<goods-manage v-else-if="curPageCode === 'supplyChainPage'"></goods-manage>
		<my-home v-else-if="curPageCode === 'myPage'"></my-home>
		<msg-home v-else-if="curPageCode === 'msgPage'"></msg-home>
		<module-bar ref="moduleBar" :moduleBarInfos="moduleBarInfos" @getCurPageInfo="getCurPageInfo"></module-bar>
		
		<!-- 首页引导 -->
		<vertify-worker-type ref="vertifyWorkerType"></vertify-worker-type>
		<!-- 登录校验弹窗 -->
		<vertify-login ref="vertifyLogin" @reload="reloadForwardPage"></vertify-login>
		<!-- <vertify-phone ref="vertifyPhone" @reload="reloadForwardPage"></vertify-phone> -->
		<!-- 订阅授权 -->
		<vertify-subscribe ref="vertifySubscribe"></vertify-subscribe>
		<vertify-settingSubscribe ref="vertifySettingSubscribe"></vertify-settingSubscribe>
	</view>
</template>

<script>
	import moduleBar from '@/components/custom-bar/module-bar.vue';
	import index from '@/pages/index/index-home.vue';
	import myHome from '@/pages/index/my-home.vue';
	import msgHome from '@/pages/index/msg-home.vue';
	import goodsManage from '@/pages/index/goods-manage.vue';

	export default {
		components: {
			moduleBar,
			index,
			myHome,
			msgHome,
			goodsManage
		},
		data() {
			return {
				moduleBarInfos: [],
				curPageCode: 'indexPage',
				forwardingPageCode: null
			}
		},
		onLoad(options) {
			
			let curUserInfo = this.$request.getCurUserInfo();
			if(!curUserInfo) {
				uni.reLaunch({
					url: '/pages/login/login'
				})
				return
			}
			this.loadData();
			if (options && options.menuCode) {
				this.changeCurPageCode(options.menuCode)
			}
		},
		methods: {
			async loadData() {
				// uni.getSystemInfo({
				// 	success: function(res) {
				// 		console.log('DPR：' + res.pixelRatio)
				// 		console.log('宽度：' + res.screenWidth)
				// 		console.log('高度：' + res.screenHeight)
				// 	}
				// })
				this.moduleBarInfos = await this.$api.data('moduleBarInfos');
				let curUserInfo = this.$request.getCurUserInfo();
				let _this = this;
				
				if (curUserInfo && curUserInfo.phone && curUserInfo.type != null) {
					wx.getSetting({
						withSubscriptions: true,
						success(res) {
							console.log(res.subscriptionsSetting)
							if (res.subscriptionsSetting.mainSwitch && (!res.subscriptionsSetting.itemSettings 
							|| !res.subscriptionsSetting.itemSettings['yqd3p4qsqn1RiyUb8kO4dPqoGKipRQg_y99nGw0jtLE']
							|| !res.subscriptionsSetting.itemSettings['gFoPAYF4J6Y_O5OzzNUbvMxTrsAS7aUWffdNH42xhzM'])) {
								// 提示授权订单进度消息提示
								_this.$refs.vertifySubscribe.showModal();
							} else if (!res.subscriptionsSetting.mainSwitch 
							|| res.subscriptionsSetting.itemSettings['yqd3p4qsqn1RiyUb8kO4dPqoGKipRQg_y99nGw0jtLE'] !== 'accept'
							|| res.subscriptionsSetting.itemSettings['gFoPAYF4J6Y_O5OzzNUbvMxTrsAS7aUWffdNH42xhzM'] !== 'accept') {
								_this.$refs.vertifySettingSubscribe.showModal();
							}
						}
					})
				}
			},
			reloadForwardPage() {
				this.getCurPageInfo({
					curPageCode: this.forwardingPageCode
				})
			},
			async getCurPageInfo(data) {
				let pageCode = data.curPageCode;
				this.forwardingPageCode = pageCode;
				// 登录校验
				if (['supplyChainPage', 'myPage'].indexOf(pageCode) >= 0) {
					let loginRes = await this.authLogin();
					if (!loginRes) {
						return;
					}
				}
				this.forwardingPageCode = null;
				
				if (pageCode === 'publishPage') {
					this.isShowPublish = true;
				} else {
					this.isShowPublish = false;
					this.curPageCode = pageCode;
				}
			},
			async authLogin() {
				// 更新缓存中的userInfo
				// let res = await this.$request.storageExistUser();
				// 获取缓存中的userInfo
				let curUserInfo = this.$request.getCurUserInfo();
				// 校验提示登录
				// if (!curUserInfo || !curUserInfo.openId) {
				// 	this.$refs.vertifyLogin.showModal();
				// 	return false;
				// } else {
				// 	this.$refs.vertifyLogin.hideModal();
				// }
				if (!curUserInfo) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
					return false;
				}
				// 校验是否注册过
				if (!curUserInfo.phone) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
					return false;
				}
				// 校验提示获取手机号码
				// if (!curUserInfo.phone) {
				// 	this.$refs.vertifyPhone.showModal();
				// 	return false;
				// } else {
				// 	this.$refs.vertifyPhone.hideModal();
				// }
				// 提示选择师傅类型
				if (curUserInfo.type == null) {
					this.$refs.vertifyWorkerType.showModal();
					return false;
				}
				return true;
			},
			changeCurPageCode(targetPageCode) {
				// #ifdef MP
				this.$nextTick(() => {
					this.$refs.moduleBar.navChangeByCode(targetPageCode, true);
				})
				// #endif
			}
		}
	}
</script>

<style scoped>
</style>
