<template>
	<view class="fe-page fe-page--tab mine-page">
		<scroll-view scroll-y class="page-scroll" :show-scrollbar="false">
			<view class="page-body">
				<view v-if="registered">
					<EnterpriseHubPanel ref="hubPanel" show-logout @logout="logout" />
				</view>
				<view v-else class="guest-panel">
					<view class="guest-card">
						<view class="guest-card__avatar">游</view>
						<text class="guest-card__name">游客</text>
						<text class="guest-card__mobile">{{ mobileMask || '未绑定企业' }}</text>
						<view class="guest-card__badge">未认证</view>
						<text class="guest-card__tip">完成企业认证后，可使用全部企业能力</text>
						<view class="fe-btn fe-btn--primary fe-btn--lg fe-btn--full guest-card__cta" @tap="goRegister">
							去企业认证
						</view>
					</view>

					<view class="guest-logout-panel">
						<view class="fe-btn fe-btn--secondary fe-btn--lg fe-btn--full guest-logout" @tap="logout">
							退出账号
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<FeBottomNav current="mine" role="enterprise" />
	</view>
</template>

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

export default {
	components: { FeBottomNav, EnterpriseHubPanel },
	data() {
		return {
			registered: false,
			mobileMask: ''
		}
	},
	onShow() {
		if (!this.ensureEnterpriseAccess()) return
		this.refreshGuestState()
		if (!this.registered) return
		this.$nextTick(() => {
			const panel = this.$refs.hubPanel
			if (panel) {
				if (typeof panel.syncStore === 'function') panel.syncStore()
				if (typeof panel.refreshPasswordStatus === 'function') panel.refreshPasswordStatus()
			}
		})
	},
	methods: {
		ensureEnterpriseAccess() {
			if (!getToken()) {
				uni.reLaunch({ url: '/packageA/auth/login' })
				return false
			}
			const s = store.getState()
			if (s.role === 'worker') {
				uni.reLaunch({ url: '/packageA/home/index' })
				return false
			}
			return true
		},
		async refreshGuestState() {
			try {
				const info = await store.syncProfile({ showError: false })
				this.registered = !!(info && info.registered)
			} catch (e) {
				this.registered = store.isEnterpriseRegistered()
			}
			const s = store.getState()
			this.mobileMask = (s.user && s.user.mobile) || ''
		},
		goRegister() {
			uni.navigateTo({ url: '/packageA/enterprise/register' })
		},
		logout() {
			store.logout()
			uni.reLaunch({ url: '/packageA/auth/login' })
		}
	}
}
</script>

<style lang="scss" scoped>
.mine-page {
	display: flex;
	flex-direction: column;
	height: 100vh;
	overflow: hidden;
	background: #f3f5f9;
}

.page-scroll {
	flex: 1;
	height: 0;
}

.page-body {
	padding: 20rpx 24rpx calc(32rpx + 120rpx + #{$fe-h-safe-bottom});
}

.guest-panel {
	display: flex;
	flex-direction: column;
	gap: 20rpx;
}

.guest-card {
	background: #ffffff;
	border-radius: 20rpx;
	padding: 48rpx 40rpx 40rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	text-align: center;
	box-shadow: 0 8rpx 24rpx rgba(15, 23, 42, 0.06);
	border: 1rpx solid rgba(226, 232, 240, 0.9);
	box-sizing: border-box;
}

.guest-card__avatar {
	width: 120rpx;
	height: 120rpx;
	border-radius: 50%;
	background: $fe-primary-bg;
	color: $fe-primary;
	font-size: 44rpx;
	font-weight: 700;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-bottom: 20rpx;
}

.guest-card__name {
	font-size: 36rpx;
	font-weight: 700;
	color: $fe-foreground;
}

.guest-card__mobile {
	margin-top: 8rpx;
	font-size: 26rpx;
	color: $fe-muted;
}

.guest-card__badge {
	margin-top: 16rpx;
	padding: 6rpx 20rpx;
	border-radius: 9999rpx;
	background: $fe-warning-light;
	color: $fe-warning;
	font-size: 22rpx;
	font-weight: 600;
}

.guest-card__tip {
	margin-top: 28rpx;
	font-size: 26rpx;
	color: $fe-muted;
	line-height: 1.5;
}

.guest-card__cta {
	margin-top: 36rpx;
}

.guest-logout-panel {
	background: #ffffff;
	border-radius: 20rpx;
	padding: 16rpx 20rpx;
	box-shadow: 0 8rpx 24rpx rgba(15, 23, 42, 0.06);
	border: 1rpx solid rgba(226, 232, 240, 0.9);
	box-sizing: border-box;
}

.guest-logout {
	margin-top: 0;
}
</style>
