<template>
	<view>
		<!-- 订阅授权 -->
		<vertify-subscribe ref="vertifySubscribe"></vertify-subscribe>
		<vertify-settingSubscribe ref="vertifySettingSubscribe"></vertify-settingSubscribe>
	</view>
</template>

<script>

	export default {
		data() {
			return {
				curPageCode: 'indexPage',
				isShowPublish: false,
				forwardingPageCode: null,
				curUserInfo: null,
				inByShare: false,
				inParam: null
			}
		},
		// async onLoad(option) {
			// this.inParam = option;
			// this.loadData(option);
		// },
		methods: {
			async loadData(option) {
				this.inParam = option;
				this.curUserInfo = this.$request.getCurUserInfo();
				// 更新分销人信息
				if (option && option.distributor) {
					let loginRes = await this.authLogin();
					if (!loginRes) {
						this.inByShare = true;
						return;
					}
					
					this.inByShare = false;
					let res = await this.$request.updateUser({
						customerPlace: option.distributor,
						customerId: this.curUserInfo.customerId
					});
					if (res && res.code === 0) {
						this.inParam = null;
					}
				}
				let _this = this;
				
				// 提示授权订单进度消息提示
				wx.getSetting({
					withSubscriptions: true,
					success(res) {
						console.log(res.subscriptionsSetting)
						if (res.subscriptionsSetting.mainSwitch && (!res.subscriptionsSetting.itemSettings 
						|| !res.subscriptionsSetting.itemSettings['JtsGFPDjYhL2GbHfKxvTJaR_lLp8xLyw8VeR01Y0JHM'])) {
							// 提示授权订单进度消息提示
							_this.$refs.vertifySubscribe.showModal();
						} else if (!res.subscriptionsSetting.mainSwitch 
						|| res.subscriptionsSetting.itemSettings['JtsGFPDjYhL2GbHfKxvTJaR_lLp8xLyw8VeR01Y0JHM'] !== 'accept') {
							_this.$refs.vertifySettingSubscribe.showModal();
						}
					}
				})
			},
			reloadForwardPage() {
				if (this.inByShare) {
					this.loadData(this.inParam);
				} else {
					this.getCurPageInfo({
						curPageCode: this.forwardingPageCode
					})
				}
			},
			async getCurPageInfo(data) {
				let pageCode = data.curPageCode;
				this.forwardingPageCode = pageCode;
				// 登录校验
				if (['publishPage', '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;
				}
			},
			hidePublish() {
				this.isShowPublish = false;
			},
			showPage(url) {
				uni.navigateTo({
					url: url
				})
			},
			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.phone) {
					this.$refs.vertifyPhone.showModal();
					return false;
				} else {
					this.$refs.vertifyPhone.hideModal();
				}
				return true;
			}
		}
	}
</script>

<style scoped>
	.content-mask {
		z-index: 9999;
		background: rgba(0, 0, 0, 0.9);
	}

	.mask-close-text {
		visibility: hidden;
	}

	.middle-avatar {
		width: 120rpx;
		height: 120rpx;
	}

	.middle-avatar [class*="cuIcon-"] {
		font-size: 53rpx !important;
	}

	.cu-avatar.first-avatar {
		background-color: #0081ff;
		color: #ffffff;
	}

	.cu-avatar.second-avatar {
		background-color: #fbbd08;
		color: #ffffff;
	}

	.cu-avatar.third-avatar {
		background-color: #f37b1d;
		color: #ffffff;
	}

	.cu-avatar.fourth-avatar {
		background-color: #1cbbb4;
		color: #ffffff;
	}

	.cu-dialog.bottom-dialog {
		background-color: unset;
		vertical-align: bottom;
	}
</style>
