<template>
	<view class="subpackage-host">
		<component v-if="Impl" :is="Impl" :incomingTab="incomingTab" />
		<view v-else class="loading">
			<text v-if="error">{{ error }}</text>
			<text v-else>正在加载接待模块...</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			Impl: null,
			incomingTab: '',
			error: ''
		};
	},
	onLoad() {
		this.loadImpl();
	},
	onShow() {
		// tabBar 的 switchTab 不稳定携带 query，因此从 storage 消费目标tab
		try {
			const allowed = ['status', 'reception', 'tag'];
			const tab = (uni.getStorageSync('workbench-reception-tab') || '').toString();
			if (allowed.includes(tab)) {
				// 先清空再赋值，确保同一个 tabKey 也能触发子组件 watcher
				this.incomingTab = '';
				this.$nextTick(() => {
					this.incomingTab = tab;
				});
				uni.removeStorageSync('workbench-reception-tab');
			}
		} catch (e) {
			// ignore
		}
	},
	methods: {
		loadSubPackagePagesSubpackage() {
			return new Promise((resolve, reject) => {
				if (!uni || !uni.loadSubPackage) {
					resolve();
					return;
				}
				uni.loadSubPackage({
					name: 'pages-subpackage',
					success: resolve,
					fail: () => {
						// 兼容部分平台参数名差异
						uni.loadSubPackage({
							root: 'pages-subpackage',
							success: resolve,
							fail: reject
						});
					}
				});
			});
		},
		async loadImpl() {
			try {
				await this.loadSubPackagePagesSubpackage();
				const mod = await import('@/pages-subpackage/furniture_reception/furniture_reception-impl.vue');
				this.Impl = mod && (mod.default || mod);
			} catch (e) {
				console.error('[FurnitureReceptionHost] loadImpl failed:', e);
				this.error = '接待模块加载失败，请重试';
			}
		}
	}
};
</script>

<style>
.subpackage-host {
	min-height: 100vh;
	background-color: #F5F5F5;
}

.loading {
	min-height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #666;
	font-size: 28rpx;
}
</style>
