<template>
	<view class="page">
		<!-- 自定义导航栏（可留空避免遮挡） -->
		<!-- <view class="nav-bar"></view> -->

		<!-- 右上角固定关闭/跳过按钮 -->
		<view class="skip-fixed" :style="{ top: 20 + 'px' }" @click="handleSkip">
			<text class="skip-text">关闭</text>
		</view>
		<!-- 视频区域 -->
		<view class="video-wrap" :style="{ height: videoHeight + 'px' }">
			<video
				:id="'briefingVideo'"
				:src="videoSrc"
				autoplay
				:controls="false"
				:enable-progress-gesture="false"
				:show-center-play-btn="false"
				:show-fullscreen-btn="false"
				:show-play-btn="false"
				:enable-play-gesture="false"
				object-fit="contain"
				@ended="handleEnded"
				@error="handleError"
				class="video"
			/>
		</view>
		<!-- 说明文案（可选） -->
		<view class="tips">请完整观看宣讲视频。如需跳过，可点击右上角“跳过”。</view>
	</view>
</template>

<script>
import { apiBaseUrl } from '@/common/config.js';
export default {
	data() {
		return {
			videoSrc: 'https://data.qicai321.com/minlong/latentsync/9791ad45-dd30-4f84-a1c6-0c07c4d08707_result.mp4',
			nextPath: '/pages/index/index',
			statusBarHeight: 0,
			navBarHeight: 44,
			videoHeight: 0,
			tenant_id: '', // 租户ID
		}
	},
	onLoad(options) {
		// 从参数获取视频地址与下一个页面路径，避免硬编码- this.navBarHeight
		this.videoSrc = (options && options.src) ? decodeURIComponent(options.src) : '';
		this.nextPath = (options && options.next) ? decodeURIComponent(options.next) : '/pages/index/index';
		const sysInfo = uni.getSystemInfoSync();
		this.statusBarHeight = sysInfo.statusBarHeight || 0;
		// 计算视频高度（去掉自定义导航）
		this.videoHeight = (sysInfo.windowHeight || 0) - this.statusBarHeight;
		// 获取本地存储的tenant_id
		this.getTenantId();
	},
	methods: {
		// 获取本地存储的tenant_id
		getTenantId() {
			const tenantId = uni.getStorageSync('tenant_id');
			if (tenantId) {
				this.tenant_id = tenantId;
				return tenantId;
			}
			return null;
		},
		handleSkip() {
			this.navigateToNext();
		},
		handleEnded() {
			this.navigateToNext();
		},
		handleError(e) {
			uni.showToast({ title: '视频加载失败', icon: 'none' });
		},
		/* navigateToNext() {
			console.log(uni.getStorageSync('configData'));
			// 若 nextPath 是 tabBar 页面，需要使用 switchTab；否则使用 navigateTo
			try {
				const tabBarPages = [
					'/pages/index/index',
					'/pages/my/my'
				];
				if (tabBarPages.includes(this.nextPath)) {
					uni.switchTab({ url: this.nextPath });
				} else {
					uni.navigateTo({ url: this.nextPath });
				}
			} catch (err) {
				// 兜底跳首页
				uni.switchTab({ url: '/pages/index/index' });
			}
		}, */
		navigateToNext() {
		try {
			// 获取配置数据
			const configStr = uni.getStorageSync('configData');
			let configData = null;
			
			if (configStr) {
				try {
					configData = JSON.parse(configStr);
					console.log('解析到的配置数据:', configData);
				} catch (parseError) {
					console.error('解析configData失败:', parseError);
				}
			}
			
			this.handleUserInfoAndNavigation(configData.require_resume_upload);
			
		} catch (error) {
			console.error('跳转处理失败:', error);
		}
	},
	// 处理用户信息获取和页面跳转逻辑
	handleUserInfoAndNavigation(require_resume_upload) {
				// 调用获取用户完整信息的接口
				const openid = JSON.parse(uni.getStorageSync('userInfo')).openid;
				uni.request({
					url: `${apiBaseUrl}/api/wechat/user/get_full_info?tenant_id=${this.tenant_id||JSON.parse(uni.getStorageSync('userInfo')).tenant_id ||1}&openid=${openid}`,
					method: 'GET',
					success: (infoRes) => {
						if (infoRes.statusCode === 200 && infoRes.data && infoRes.data.data && infoRes.data.data.profile) {
							const resumeUrl = infoRes.data.data.profile.resume_url || '';
							if (!resumeUrl && require_resume_upload) {
								// resume_url 为空，跳转到其他页面
								uni.navigateTo({
									url: '/pages/uploadResume/uploadResume', // 假设跳转到上传简历页面
									fail: (err) => {
										console.error('页面跳转失败:', err);
										uni.showToast({
											title: '页面跳转失败',
											icon: 'none'
										});
									}
								});
							} else {
								// resume_url 不为空，跳转到 Personal 页面
								uni.navigateTo({
									url: '/pages/Personal/Personal',
									fail: (err) => {
										console.error('页面跳转失败:', err);
										uni.showToast({
											title: '页面跳转失败',
											icon: 'none'
										});
									}
								});
							}
						} else {
							// 获取用户信息失败，直接跳转到 Personal 页面
							uni.navigateTo({
								url: '/pages/Personal/Personal',
								fail: (err) => {
									console.error('页面跳转失败:', err);
									uni.showToast({
										title: '页面跳转失败',
										icon: 'none'
									});
								}
							});
						}
					},
					fail: (err) => {
						console.error('获取用户信息失败:', err);
						uni.navigateTo({
							url: '/pages/Personal/Personal',
							fail: (err) => {
								console.error('页面跳转失败:', err);
								uni.showToast({
									title: '页面跳转失败',
									icon: 'none'
								});
							}
						});
					}
				});
			},

	}
}
</script>

<style>
.page {
	background: #000;
	min-height: 100vh;
}

.nav-bar {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: space-between;
	/* height: 44px; */
	padding: 0 16px;
	background: #000;
	color: #fff;
	box-sizing: border-box;
	z-index: 1;
}

.nav-title {
	font-size: 16px;
	font-weight: 600;
}

.skip-btn {
	font-size: 14px;
	color: #ffffff;
	opacity: 0.9;
}

.video-wrap {
	width: 100%;
	background: #000;
}

.video {
	width: 100%;
	height: 100%;
}

.tips {
	padding: 5px 16px;
    text-align: center;
	font-size: 12px;
	color: #bbb;
	background: #000;
}

/* 右上角固定关闭按钮样式 */
.skip-fixed {
	position: fixed;
	right: 12px;
	/* top 动态由内联样式结合状态栏高度控制 */
	background: rgba(0, 0, 0, 0.35);
	color: #fff;
	border-radius: 18px;
	padding: 6px 12px;
	border: 0.5px solid rgba(255, 255, 255, 0.35);
	backdrop-filter: blur(6px);
	-webkit-backdrop-filter: blur(6px);
    line-height: 12px;
	z-index: 999;
}

.skip-text {
	font-size: 12px;
	
}
</style>

