<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 class="nav-placeholder" :style="{ height: (statusBarHeight + 44) + 'px' }"></view>
		<template v-if="isLoading">
			<!-- 骨架屏 -->
			<view class="skeleton-wrapper">
				<!-- 订单状态 -->
				<view class="order-status skeleton-status">
					<view class="skeleton-text-lg"></view>
					<view class="skeleton-text-sm"></view>
				</view>
		
				<!-- 收货信息 -->
				<view class="shipping-info skeleton-shipping">
					<view class="skeleton-icon"></view>
					<view class="skeleton-shipping-content">
						<view class="skeleton-name-phone">
							<view class="skeleton-text" style="width: 120rpx;"></view>
							<view class="skeleton-text" style="width: 200rpx;"></view>
						</view>
						<view class="skeleton-text" style="width: 80%;"></view>
					</view>
					<view class="skeleton-icon-sm"></view>
				</view>
		
				<!-- 商品列表 -->
				<view class="goods-list skeleton-goods-list">
					<view class="goods-item skeleton-goods-item" v-for="i in 2" :key="i">
						<view class="skeleton-img"></view>
						<view class="skeleton-goods-info">
							<view class="skeleton-text" style="width: 90%;"></view>
							<view class="skeleton-text" style="width: 50%;"></view>
							<view class="skeleton-price-quantity">
								<view class="skeleton-text" style="width: 120rpx;"></view>
								<view class="skeleton-text" style="width: 60rpx;"></view>
							</view>
						</view>
					</view>
					<view class="info-item skeleton-total">
						<view class="skeleton-text" style="width: 140rpx;"></view>
						<view class="skeleton-text" style="width: 160rpx;"></view>
					</view>
				</view>
		
				<!-- 订单信息 -->
				<view class="order-info skeleton-order-info">
					<view class="section-title skeleton-title"></view>
					<view class="info-item skeleton-info-item" v-for="i in 4" :key="i">
						<view class="skeleton-text" style="width: 140rpx;"></view>
						<view class="skeleton-text" style="width: 200rpx;"></view>
					</view>
				</view>
		
				<!-- 底部占位 -->
				<view class="skeleton-bottom-space"></view>
		
				<!-- 底部按钮 -->
				<view class="bottom-buttons skeleton-bottom">
					<view class="skeleton-btn"></view>
				</view>
			</view>
		</template>
		
		<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/orderDetail')
	let orderId = ref(0)
	let content = ref<any>({})
	onLoad((data : any) => {
		orderId.value=data.orderId
		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 = {
			orderId: orderId.value,
			token: token
		}
		const { code, data } = msg

		if (code == 'Back') {
			Service.GoPageBack()
		}
		if (code === 'Ready') {
			SendMsg(content.value)
			isLoading.value=false
		}
		if (code === 'addAddress') {
			Service.GoPage('/pages/userFunc/addAddress');
		}
		
		
		
		
		
	};

	// 向子元素传递的值
	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; // 与左侧对称，保证标题严格居中
	}
	
	// ==================== 骨架屏 ====================
	@keyframes skeleton-loading {
		0% { background-position: 100% 50%; }
		100% { background-position: 0 50%; }
	}
	
	.skeleton-wrapper {
		transition: opacity 0.3s ease;
	
		.skeleton,
		.skeleton-text,
		.skeleton-text-lg,
		.skeleton-text-sm,
		.skeleton-icon,
		.skeleton-icon-sm,
		.skeleton-img,
		.skeleton-btn,
		.skeleton-title {
			background: linear-gradient(90deg, #f2f2f2 25%, #e6e6e6 50%, #f2f2f2 75%);
			background-size: 200% 100%;
			animation: skeleton-loading 1.5s infinite;
			border-radius: 8rpx;
		}
	
		.skeleton-text {
			height: 28rpx;
		}
	
		.skeleton-text-lg {
			height: 44rpx;
			width: 160rpx;
			margin-bottom: 16rpx;
		}
	
		.skeleton-text-sm {
			height: 24rpx;
			width: 240rpx;
		}
	
		.skeleton-icon {
			width: 30rpx;
			height: 30rpx;
			border-radius: 50%;
		}
	
		.skeleton-icon-sm {
			width: 20rpx;
			height: 20rpx;
			border-radius: 50%;
		}
	
		.skeleton-img {
			width: 160rpx;
			height: 160rpx;
			border-radius: 10rpx;
			margin-right: 20rpx;
			flex-shrink: 0;
		}
	
		.skeleton-btn {
			width: 100%;
			height: 70rpx;
			border-radius: 35rpx;
		}
	}
	
	.skeleton-status {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #fff;
		padding: 30rpx;
	}
	
	.skeleton-shipping {
		display: flex;
		align-items: center;
		background-color: #fff;
		margin-top: 20rpx;
		padding: 30rpx;
	
		.skeleton-shipping-content {
			flex: 1;
			margin-right: 20rpx;
	
			.skeleton-name-phone {
				display: flex;
				justify-content: space-between;
				margin-bottom: 16rpx;
			}
		}
	}
	
	.skeleton-goods-list {
		background-color: #fff;
		margin-top: 20rpx;
		padding: 0 30rpx;
	
		.skeleton-goods-item {
			display: flex;
			padding: 30rpx 0;
			border-bottom: 1rpx solid #f5f5f5;
	
			&:last-child {
				border-bottom: none;
			}
	
			.skeleton-goods-info {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
	
				.skeleton-text {
					margin-bottom: 16rpx;
	
					&:last-child {
						margin-bottom: 0;
					}
				}
	
				.skeleton-price-quantity {
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}
		}
	
		.skeleton-total {
			display: flex;
			justify-content: space-between;
			padding: 20rpx 0;
		}
	}
	
	.skeleton-order-info {
		background-color: #fff;
		margin-top: 20rpx;
		padding: 30rpx;
	
		.skeleton-title {
			width: 140rpx;
			height: 32rpx;
			margin-bottom: 25rpx;
		}
	
		.skeleton-info-item {
			display: flex;
			justify-content: space-between;
			padding: 18rpx 0;
		}
	}
	
	.skeleton-bottom-space {
		width: 100%;
		height: 100rpx;
	}
	
	.skeleton-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 110rpx;
		padding: 20rpx;
		background-color: #fff;
		border-top: 1rpx solid #eee;
		display: flex;
		align-items: center;
	}
	
	
</style>