<template>
	<view>
		<sell-container ref="sellContainer"></sell-container>
		<ActivityHost ref="activityHost" :enable-upsell="false"></ActivityHost>
		<policy-consent-host />
	</view>
</template>

<script>
import ActivityHost from "@/components/activity-host.vue";
import PolicyConsentHost from "@/components/policy-consent-host.vue";
import tabbarBlockMixin from "@/utils/tabbar-block-mixin.js";
import { eventBus } from "@/utils/event-bus";
import { consumeHomeRefreshAfterPolicy } from "@/utils/policy-consent-manager.js";

export default {
	mixins: [tabbarBlockMixin],
	components: {
		ActivityHost,
		PolicyConsentHost,
	},
	data() {
		return {
			pendingActivityOptions: {},
			pendingPolicyRefresh: false,
			policyAcceptedHandler: null,
			loginSuccessHandler: null,
		}
	},
	onLoad(options) {
		this.pendingActivityOptions = options || {};
		this.bindPolicyRefreshListeners();
	},
	onUnload() {
		this.unbindPolicyRefreshListeners();
	},
	onReady() {
		this.$nextTick(() => {
			if (this.hasActivityEntryOptions(this.pendingActivityOptions)) {
				this.$refs.activityHost?.handlePageOptions(this.pendingActivityOptions);
			}
			this.pendingActivityOptions = {};
		});
	},
	onShow() {
		this.$updateCartBadge();
		// 在其他 Tab 同意协议后切回首页时补刷数据
		if (consumeHomeRefreshAfterPolicy() && uni.getStorageSync("token")) {
			this.refreshHomePageData();
		}
	},
	onShareAppMessage(res) {
		return this.$refs.activityHost?.getShareAppMessage(res) || {
			title: "书嗨",
			path: "/pages/sell/index",
		};
	},
	onShareTimeline(res) {
		return this.$refs.activityHost?.getShareTimeline(res) || {
			title: "书嗨",
			path: "/pages/sell/index",
		};
	},
	onPullDownRefresh() {
		if (this.$refs.sellContainer) {
			this.$refs.sellContainer.hasShareList();
			this.$refs.sellContainer.getIndexCateInfo();
		}
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	onPageScroll(e) {
		const comp = this.$refs.sellContainer;
		if (!comp) return;
		comp.scrollTop = e.scrollTop;
	},
	methods: {
		bindPolicyRefreshListeners() {
			if (!this.policyAcceptedHandler) {
				this.policyAcceptedHandler = () => {
					this.pendingPolicyRefresh = true;
				};
				eventBus.on("policyAccepted", this.policyAcceptedHandler);
			}
			if (!this.loginSuccessHandler) {
				this.loginSuccessHandler = () => {
					if (!this.pendingPolicyRefresh) return;
					this.pendingPolicyRefresh = false;
					consumeHomeRefreshAfterPolicy();
					this.refreshHomePageData();
				};
				eventBus.on("loginSuccess", this.loginSuccessHandler);
			}
		},
		unbindPolicyRefreshListeners() {
			if (this.policyAcceptedHandler) {
				eventBus.off("policyAccepted", this.policyAcceptedHandler);
				this.policyAcceptedHandler = null;
			}
			if (this.loginSuccessHandler) {
				eventBus.off("loginSuccess", this.loginSuccessHandler);
				this.loginSuccessHandler = null;
			}
		},
		refreshHomePageData() {
			this.$nextTick(() => {
				this.$refs.sellContainer?.refreshPageData?.();
			});
		},
		hasActivityEntryOptions(options = {}) {
			return !!(options.scene || options.reduceCode || options.upsellCode);
		}
	}
}
</script>

<style>
/* Empty style as content is in the component */
</style>
