<template>
	<view>
		<index v-if="curPageCode === 'indexPage'"></index>
		<worker-circle v-if="curPageCode === 'workerCirclePage'"></worker-circle>
		<personal-center v-if="curPageCode === 'myPage'"></personal-center>
		<!-- <publish-home v-if="curPageCode === 'publishPage'"></publish-home> -->
		<msg-page v-if="curPageCode === 'msgPage'"></msg-page>
		<module-bar ref="moduleBar" :moduleBarInfos="moduleBarInfos" @getCurPageInfo="getCurPageInfo"></module-bar>

		<!-- 登录校验弹窗 -->
		<vertify-login ref="vertifyLogin" @reload="reloadForwardPage"></vertify-login>
		<vertify-phone ref="vertifyPhone" @reload="reloadForwardPage"></vertify-phone>
		<!-- 订阅授权 -->
		<vertify-subscribe ref="vertifySubscribe"></vertify-subscribe>

		<!-- 发布 -->
		<view class="cu-modal content-mask" :class="isShowPublish?'show':''">
			<view class="cu-dialog bottom-dialog margin-bottom-with-bar">
				<view class="padding-xl">
					<view class="flex justify-start margin-tb-xl">
						<view class="cu-avatar round middle-avatar first-avatar">
							<view class="cuIcon-formfill"></view>
						</view>
						<view class="flex flex-column-around text-left margin-left-sm text-white"
							@click="showPage('/pages/publish/publish-task')">
							<view class="text-xl">发布任务</view>
							<view>公司、家居家政雇佣上门服务</view>
						</view>
					</view>
					<!-- <view class="flex justify-start margin-tb-xl">
						<view class="cu-avatar round middle-avatar second-avatar">
							<view class="cuIcon-cameraaddfill"></view>
						</view>
						<view class="flex flex-column-around text-left margin-left-sm text-white">
							<view class="text-xl">发闲置</view>
							<view class="text-gray">30s发布宝贝</view>
							<view class="text-sm">手机/家电卖出/非上门类</view>
						</view>
					</view> -->
					<!-- <view class="flex justify-start margin-tb-xl">
						<view class="cu-avatar round middle-avatar third-avatar">
							<view class="cuIcon-footprint"></view>
						</view>
						<view class="flex flex-column-around text-left margin-left-sm text-white">
							<view class="text-xl">社区跑腿</view>
							<view>同楼盘跑腿服务</view>
						</view>
					</view> -->
					<!-- <view class="flex justify-start margin-tb-xl">
						<view class="cu-avatar round middle-avatar fourth-avatar">
							<view class="cuIcon-shopfill"></view>
						</view>
						<view class="flex flex-column-around text-left margin-left-sm text-white">
							<view class="text-xl">品牌厂商发布</view>
							<view>销售商电商雇佣</view>
						</view>
					</view> -->
				</view>
			</view>
			<view class="cu-bar tabbar margin-bottom-xl fixed-bottom-bar">
				<view class="action add-action">
					<button class="cu-btn bg-gray cuIcon-close" @click="hidePublish"></button>
					<text class="mask-close-text">关闭</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import moduleBar from '@/components/custom-bar/module-bar.vue';
	import index from '@/pages/index/home.vue';
	import workerCircle from '@/pages/index/worker-home.vue';
	import personalCenter from '@/pages/index/my-home.vue';
	import msgPage from '@/pages/index/msg-home.vue';
	// import publishHome from '@/pages/index/publish-home.vue';

	export default {
		components: {
			moduleBar,
			index,
			workerCircle,
			personalCenter,
			msgPage
			// publishHome
		},
		data() {
			return {
				moduleBarInfos: [],
				curPageCode: 'indexPage',
				isShowPublish: false,
				forwardingPageCode: null,
				curUserInfo: null,
				inByShare: false,
				inParam: null
			}
		},
		async onLoad(option) {
			this.moduleBarInfos = await this.$api.data('moduleBarInfos');
			if (option) {
				// 跳转逻辑
				if (option.menuCode) {
					this.changeCurPageCode(option.menuCode);
					return;
				}
				if (option.scene) {
					let paraStr = decodeURIComponent(option.scene);
					let kvStrArr = paraStr.split("&");
					kvStrArr.forEach((kvStr) => {
						let kvArr = kvStr.split("=");
						option[kvArr[0]] = kvArr[1];
					});
				}
			}
			this.inParam = option;
			this.loadData(option);
		},
		async onShareAppMessage(e) {
			let shareInfo = null;
			if (e && e.target && e.target.dataset) {
				shareInfo = e.target.dataset.shareInfo;
			}
			if (!this.curUserInfo) {
				this.curUserInfo = await this.$request.getCurUserNoCache();
				if (!this.curUserInfo) {
					uni.showToast({
						title: '请前往“我的”完成登录',
						icon: 'none'
					})
					return;
				}
			}
			if (!shareInfo) {
				shareInfo = {
					title: '家政服务就找工圈子',
					path: '/pages/index/index?distributor=' + this.curUserInfo.customerId,
					imageUrl: 'http://gqz.opsoul.com/sys/group-selfie.png'
				}
			}
			return shareInfo;
		},
		methods: {
			async loadData(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['yqd3p4qsqn1RiyUb8kO4dPqoGKipRQg_y99nGw0jtLE'] !== 'accept') {
							// 提示授权订单进度消息提示
							_this.$refs.vertifySubscribe.showModal();
						}
					}
				})
			},
			changeCurPageCode(targetPageCode) {
				// #ifdef MP
				this.$nextTick(() => {
					this.$refs.moduleBar.navChangeByCode(targetPageCode, true);
				})
				// #endif
			},
			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>
