<template>
	<view>
		<!-- 自定义导航栏 -->
		<view v-if="isLoading" class="custom-nav" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="nav-content">
				<view class="nav-left" @click="Service.GoPageBack()">
					<up-icon name="arrow-left" size="20" color="#000000"></up-icon>
				</view>
				<view class="nav-center">
					<text class="nav-title">商品详情</text>
				</view>
				<view class="nav-right"></view>
			</view>
		</view>
		<view v-if="isLoading" class="nav-placeholder" :style="{ height: (statusBarHeight + 44) + 'px' }"></view>

		<!-- 骨架屏区域 - 数据加载时显示 -->
		<view v-if="isLoading" class="skeleton-detail-container">
			<!-- 轮播图骨架 -->
			<view class="skeleton-swiper">
				<view class="skeleton-item skeleton-shimmer"></view>
				<view class="skeleton-indicator">
					<view class="skeleton-item"></view>
				</view>
			</view>

			<!-- 商品信息骨架 -->
			<view class="skeleton-goods-info">
				<view class="skeleton-price-row">
					<view class="skeleton-price skeleton-item skeleton-shimmer"></view>
					<view class="skeleton-old-price skeleton-item skeleton-shimmer"></view>
				</view>
				<view class="skeleton-tag skeleton-item skeleton-shimmer"></view>
				<view class="skeleton-name skeleton-item skeleton-shimmer"></view>
				<view class="skeleton-name skeleton-item skeleton-shimmer short"></view>
			</view>

			<!-- 标签栏骨架 -->
			<view class="skeleton-tabs">
				<view class="skeleton-tab skeleton-item skeleton-shimmer"></view>
				<view class="skeleton-tab skeleton-item skeleton-shimmer"></view>
			</view>

			<!-- 商品介绍骨架 -->
			<view class="skeleton-intro">
				<view class="skeleton-intro-title skeleton-item skeleton-shimmer"></view>
				<view class="skeleton-intro-content">
					<view class="skeleton-intro-img skeleton-item skeleton-shimmer"></view>
					<view class="skeleton-intro-img skeleton-item skeleton-shimmer"></view>
				</view>
			</view>

			<!-- 评价区域骨架 -->
			<view class="skeleton-comment">
				<view class="skeleton-comment-title">
					<view class="skeleton-item skeleton-shimmer"></view>
				</view>
				<view v-for="i in 2" :key="i" class="skeleton-comment-item">
					<view class="skeleton-comment-avatar skeleton-item skeleton-shimmer"></view>
					<view class="skeleton-comment-content">
						<view class="skeleton-comment-header">
							<view class="skeleton-item skeleton-shimmer"></view>
							<view class="skeleton-item skeleton-shimmer"></view>
						</view>
						<view class="skeleton-comment-text skeleton-item skeleton-shimmer"></view>
						<view class="skeleton-comment-text skeleton-item skeleton-shimmer short"></view>
					</view>
				</view>
			</view>

			<!-- 底部占位 -->
			<view class="skeleton-bottom-placeholder"></view>
		</view>
		<web-view v-show="!isLoading" :src="h5url" @message="handleWebMessage"></web-view>
	</view>
</template>

<script setup lang="ts">
	import { onShow, onLoad } from "@dcloudio/uni-app";
	import { ref } from "vue";
	import { Service } from "@/Service/Service";

	let isLoading = ref(true)
	let statusBarHeight = ref(uni.getWindowInfo().statusBarHeight || 20)

	const pages = getCurrentPages()
	const vw = ref(null)

	let h5url = ref()
	let url = ref('http://192.168.0.192:5173/#')
	let address = ref('/pages/userFunc/goodsDetail')
	let goodsId = ref('')
	let content = ref<any>({})
	onLoad((data : any) => {
		goodsId.value = data.goodsId
		h5url.value = url.value + address.value

	});

	onShow(() => {

	});

	// 接收H5 postMessage消息
	const handleWebMessage = (e : any) => {
		const msg = e.detail.data[0]
		console.log('收到H5消息', msg)
		let token = Service.GetUserToken()
		content.value = {
			goodsId: goodsId.value,
			token: token
		}
		const { code, data } = msg

		if (code == 'Back') {
			Service.GoPageBack()
		}
		if (code === 'Ready') {
			isLoading.value = false
			SendMsg(content.value)
		}
		if (code === 'orderDetail') {
			Service.GoPage('/pages/webView/orderDetail?orderId=' + data)
		}
		

		if (code === 'shop') {
			setTimeout(() => {
				Service.GoPageTab('/pages/index/shop')
			}, 1000)
		}

	};

	// 向子元素传递的值
	function SendMsg(data : any) {
		vw.value = pages[pages.length - 1].$getAppWebview().children()[0]
		const jsonStr = JSON.stringify({ code: 'init', data: data })
		vw.value.evalJS(`GetTwEvent('${jsonStr}')`)
	}
</script>

<style lang="scss">
	// ==================== 自定义导航栏 ====================
	.custom-nav {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 999;
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #E5E5E5; // 加上底部细线
	}

	.nav-content {
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 8rpx; // 原生左右边距较小
	}

	.nav-left {
		width: 44px;
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.nav-title {
		font-size: 34rpx; // 接近原生 17px
		
		color: #000000;
	}

	.nav-center {
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.nav-right {
		width: 44px; // 与左侧对称，保证标题严格居中
	}

	// ==================== 骨架屏样式 ====================

	// 骨架屏容器
	.skeleton-detail-container {
		padding-bottom: 200rpx;
		background-color: #f6f6f6;
		min-height: 100vh;
	}

	// 骨架屏基础元素样式 - 渐变灰色占位块
	.skeleton-item {
		// 使用柔和的渐变背景，从浅灰到稍深的灰色
		background: linear-gradient(135deg,
				#f8f8f8 0%,
				#f0f0f0 50%,
				#e8e8e8 100%);
		border-radius: 8rpx;
	}

	// ==================== 闪光动画效果 ====================
	// 使用CSS动画实现骨架屏的闪光效果
	.skeleton-shimmer {
		position: relative;
		overflow: hidden;

		// 创建闪光层
		&::after {
			content: '';
			position: absolute;
			top: 0;
			left: -100%; // 从左侧开始
			width: 100%;
			height: 100%;
			// 使用多层渐变创造更丰富的闪光效果
			background: linear-gradient(90deg,
					transparent 0%,
					rgba(255, 255, 255, 0.3) 20%, // 淡入
					rgba(255, 255, 255, 0.6) 40%, // 最亮
					rgba(255, 255, 255, 0.4) 60%, // 稍暗
					rgba(255, 255, 255, 0.2) 80%, // 淡出
					transparent 100%);
			animation: skeleton-shimmer 2s infinite; // 循环播放动画，稍慢一点更自然
		}

		// 添加一个额外的渐变层，创造更深的视觉层次
		&::before {
			content: '';
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background: linear-gradient(180deg,
					rgba(255, 255, 255, 0.1) 0%,
					transparent 50%,
					rgba(0, 0, 0, 0.03) 100%);
			pointer-events: none; // 不影响点击
		}
	}

	// 闪光动画关键帧
	@keyframes skeleton-shimmer {
		0% {
			transform: translateX(-100%); // 起始位置
		}

		100% {
			transform: translateX(100%); // 结束位置
		}
	}

	// ==================== 轮播图骨架 ====================
	.skeleton-swiper {
		width: 100%;
		height: 600rpx;
		background-color: #fff;
		position: relative;

		.skeleton-item {
			width: 100%;
			height: 100%;
			border-radius: 0;
		}
	}

	.skeleton-indicator {
		position: absolute;
		bottom: 30rpx;
		right: 30rpx;

		.skeleton-item {
			width: 80rpx;
			height: 40rpx;
			border-radius: 40rpx;
		}
	}

	// ==================== 商品信息骨架 ====================
	.skeleton-goods-info {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}

	.skeleton-price-row {
		display: flex;
		align-items: baseline;
		margin-bottom: 20rpx;
	}

	.skeleton-price {
		width: 160rpx;
		height: 40rpx;
		margin-right: 20rpx;
	}

	.skeleton-old-price {
		width: 100rpx;
		height: 28rpx;
	}

	.skeleton-tag {
		width: 300rpx;
		height: 36rpx;
		margin-bottom: 20rpx;
	}

	.skeleton-name {
		width: 100%;
		height: 32rpx;
		margin-bottom: 12rpx;

		&.short {
			width: 70%;
		}
	}

	// ==================== 标签栏骨架 ====================
	.skeleton-tabs {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
		display: flex;
		gap: 60rpx;
		justify-content: center;
	}

	.skeleton-tab {
		width: 120rpx;
		height: 36rpx;
	}

	// ==================== 商品介绍骨架 ====================
	.skeleton-intro {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}

	.skeleton-intro-title {
		width: 160rpx;
		height: 36rpx;
		margin-bottom: 30rpx;
	}

	.skeleton-intro-content {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}

	.skeleton-intro-img {
		width: 100%;
		height: 400rpx;
	}

	// ==================== 评价区域骨架 ====================
	.skeleton-comment {
		background-color: #fff;
		padding: 20rpx;
	}

	.skeleton-comment-title {
		margin-bottom: 30rpx;

		.skeleton-item {
			width: 200rpx;
			height: 36rpx;
		}
	}

	.skeleton-comment-item {
		display: flex;
		padding: 20rpx 0;
		border-top: 1rpx solid #e2e2e2;
	}

	.skeleton-comment-avatar {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		flex-shrink: 0;
		margin-right: 16rpx;
	}

	.skeleton-comment-content {
		flex: 1;
	}

	.skeleton-comment-header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;

		.skeleton-item:first-child {
			width: 120rpx;
			height: 28rpx;
		}

		.skeleton-item:last-child {
			width: 140rpx;
			height: 24rpx;
		}
	}

	.skeleton-comment-text {
		width: 100%;
		height: 28rpx;
		margin-bottom: 12rpx;

		&.short {
			width: 60%;
		}
	}

	// ==================== 底部占位 ====================
	.skeleton-bottom-placeholder {
		width: 100%;
		height: 200rpx;
	}
</style>