<template>
	<view class="chat-root">
		<view v-if="booting" class="chat-boot">
			<view class="chat-boot__spinner" />
			<text class="chat-boot__text">加载中...</text>
		</view>
		<EnterpriseHome v-else ref="enterpriseHome" :active="true" />
	</view>
</template>

<script>
import EnterpriseHome from '@/packageA/components/home/EnterpriseHome.vue'
import store from '@/common/store.js'
import { getToken } from '@/utils/request.js'

export default {
	components: { EnterpriseHome },
	data() {
		return {
			booting: true,
			pendingDeepLink: null
		}
	},
	onLoad(options) {
		this.pendingDeepLink = options || null
		this.bootstrap()
	},
	async onShow() {
		if (this.booting) return
		try {
			await store.syncProfile({ showError: false })
		} catch (e) {
			// 忽略：仍按本地状态刷新进度卡内认证面板
		}
		const home = this.$refs.enterpriseHome
		if (home && typeof home.refreshWorkflowProcessPanels === 'function') {
			home.refreshWorkflowProcessPanels()
		}
	},
	onShareAppMessage() {
		const home = this.$refs.enterpriseHome
		if (home && typeof home.getShareAppMessage === 'function') {
			return home.getShareAppMessage()
		}
		return {
			title: '用工邀请',
			path: '/pages/worker/job-offer?scene=',
			imageUrl: '/static/images/job.jpg'
		}
	},
	methods: {
		bootstrap() {
			if (!getToken()) {
				uni.reLaunch({ url: '/packageA/auth/login' })
				return
			}
			const s = store.getState()
			if (s.role === 'worker') {
				uni.reLaunch({ url: '/packageA/home/index' })
				return
			}
			this.booting = false
			this.$nextTick(() => {
				this.applyDeepLink()
			})
		},
		applyDeepLink() {
			const link = this.pendingDeepLink
			if (!link) return
			const home = this.$refs.enterpriseHome
			if (home && typeof home.handleDeepLink === 'function') {
				home.handleDeepLink(link)
				this.pendingDeepLink = null
				return
			}
			// 组件偶发未就绪，再试一次
			setTimeout(() => {
				const h = this.$refs.enterpriseHome
				if (h && typeof h.handleDeepLink === 'function' && this.pendingDeepLink) {
					h.handleDeepLink(this.pendingDeepLink)
					this.pendingDeepLink = null
				}
			}, 200)
		}
	}
}
</script>

<style lang="scss" scoped>
.chat-root {
	width: 100%;
	height: 100vh;
	overflow: hidden;
	background: $fe-bg;
}

.chat-boot {
	height: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 24rpx;
	background: $fe-surface;
}

.chat-boot__spinner {
	width: 56rpx;
	height: 56rpx;
	border: 4rpx solid $fe-gray-200;
	border-top-color: $fe-primary;
	border-radius: 50%;
	animation: chat-spin 0.8s linear infinite;
}

.chat-boot__text {
	font-size: 28rpx;
	color: $fe-gray-500;
}

@keyframes chat-spin {
	to {
		transform: rotate(360deg);
	}
}
</style>
