<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 class="skeleton-wrapper" v-if="isLoading">
			<view class="skeleton-order-item" v-for="i in 3" :key="i">
				<view class="skeleton-header">
					<view class="skeleton-text" style="width: 260rpx;"></view>
					<view class="skeleton-text" style="width: 140rpx;"></view>
				</view>
				<view class="skeleton-product">
					<view class="skeleton-img"></view>
					<view class="skeleton-product-details">
						<view class="skeleton-text" style="width: 90%;"></view>
						<view class="skeleton-text" style="width: 60%;"></view>
						<view class="skeleton-text" style="width: 120rpx;"></view>
					</view>
				</view>
				<view class="skeleton-total">
					<view class="skeleton-text" style="width: 160rpx;"></view>
				</view>
				<view class="skeleton-footer">
					<view class="skeleton-status"></view>
					<view class="skeleton-actions">
						<view class="skeleton-btn secondary"></view>
						<view class="skeleton-btn primary"></view>
					</view>
				</view>
			</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/orderList')
	let type = ref(0)
	let content = ref<any>({})
	onLoad((data : any) => {
		console.log(data);
		type.value = data.type
		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 = {
			type: type.value,
			token: token
		}
		const { code, data } = msg

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

	// 向子元素传递的值
	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">
	
	// ==================== 骨架屏 ====================
	@keyframes skeleton-loading {
		0% { background-position: 100% 50%; }
		100% { background-position: 0 50%; }
	}
	
	.skeleton-wrapper {
		padding: 20rpx;
		transition: opacity 0.3s ease;
	
		.skeleton,
		.skeleton-text,
		.skeleton-img,
		.skeleton-status,
		.skeleton-btn {
			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-img {
			width: 180rpx;
			height: 180rpx;
			border-radius: 16rpx;
			margin-right: 20rpx;
			flex-shrink: 0;
		}
	
		.skeleton-status {
			width: 100rpx;
			height: 40rpx;
			border-radius: 10rpx;
		}
	
		.skeleton-btn {
			height: 56rpx;
			border-radius: 40rpx;
	
			&.secondary {
				width: 140rpx;
			}
	
			&.primary {
				width: 140rpx;
			}
		}
	}
	
	.skeleton-order-item {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
	
		.skeleton-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
		}
	
		.skeleton-product {
			display: flex;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #e2e2e2;
	
			.skeleton-product-details {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
	
				.skeleton-text {
					margin-bottom: 16rpx;
	
					&:last-child {
						margin-bottom: 0;
					}
				}
			}
		}
	
		.skeleton-total {
			display: flex;
			justify-content: flex-end;
			padding: 20rpx 0;
		}
	
		.skeleton-footer {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;
	
			.skeleton-actions {
				display: flex;
				gap: 16rpx;
			}
		}
	}
	
	// ==================== 自定义导航栏 ====================
	.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; // 与左侧对称，保证标题严格居中
	}
	
	
</style>