<template>
	<view class="launch-root">
		<app-top-push-notice />
		<view class="launch-brand">
			<image
				class="launch-logo"
				:src="logoSrc"
				mode="aspectFit"
				:lazy-load="false"
				@error="onLogoError"
			/>
			<view class="launch-spinner" aria-hidden="true"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				done: false,
				navScheduled: false,
				splashStartedAt: 0,
				/** 最短白屏展示时间，避免接口太快时页面被立刻关掉、本地图还没画出来 */
				splashMinMs: 650,
				logoSrc: '/static/newlogo.png'
			}
		},
		onLoad() {
			this.splashStartedAt = Date.now()
			// this.ensureDefaultLanguage()
			this.preloadLogo()
			this.bootstrap()
		},
		methods: {
			/** 本地未保存语言时，默认写入英文（与 common/i18n 的 storage key `language` 一致） */
			ensureDefaultLanguage() {
				try {
					const lang = uni.getStorageSync('language')
					if (lang === '' || lang === null || lang === undefined) {
						uni.setStorageSync('language', 'en')
						if (this.$i18n && typeof this.$i18n.setLanguage === 'function') {
							this.$i18n.setLanguage('en')
						}
					}
				} catch (e) {}
			},
			preloadLogo() {
				try {
					uni.getImageInfo({
						src: this.logoSrc,
						fail: () => {
							console.warn('[launch] logo 路径无效或文件缺失:', this.logoSrc)
						}
					})
				} catch (e) {}
			},
			onLogoError() {
				console.warn('[launch] newlogo 加载失败，请确认 static/newlogo.png 已放入项目并重新编译')
			},
			relaunch(url) {
				if (this.done || this.navScheduled) return
				this.navScheduled = true
				const go = () => {
					if (this.done) return
					this.done = true
					uni.reLaunch({ url })
				}
				const elapsed = Date.now() - this.splashStartedAt
				const wait = Math.max(0, this.splashMinMs - elapsed)
				setTimeout(go, wait)
			},
			// 跳转到登录页
			goLogin() {
				this.relaunch('/pages/login/index')
			},
			// 跳转到首页
			goHome() {
				this.relaunch('/pages/index/index')
			},
			bootstrap() {
				const timeoutMs = 15000
				const timer = setTimeout(() => {
					if (!this.done) {
						console.warn('[launch] 校验超时，进入登录页')
						this.goLogin()
					}
				}, timeoutMs)

				const token = uni.getStorageSync('token')
				if (!token) {
					clearTimeout(timer)
					this.goLogin()
					return
				}

				this.$http
					.get('/getInfo')
					.then((res) => {
						clearTimeout(timer)
						if (this.done) return
						if (res && res.code == 200 && res.user) {
							try {
								uni.setStorageSync('user', res.user)
							} catch (e) {}
							// 与登录页 getInfo 判断一致（enablePin 可能为非布尔）
							if (res.user.enablePin == true) {
								this.relaunch('/pages/login/index')
							} else {
								this.goHome()
							}
						} else {
							this.goLogin()
						}
					})
					.catch(() => {
						clearTimeout(timer)
						if (!this.done) {
							this.goLogin()
						}
					})
			}
		}
	}
</script>

<style scoped>
	.launch-root {
		min-height: 100vh;
		width: 100%;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
	}

	.launch-brand {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.launch-logo {
		width: 280rpx;
		height: 280rpx;
		margin-bottom: 48rpx;
	}

	.launch-spinner {
		width: 48rpx;
		height: 48rpx;
		border: 4rpx solid #e8e8e8;
		border-top-color: #c8c8c8;
		border-radius: 50%;
		animation: launch-spin 0.75s linear infinite;
	}

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