<template>
	<view class="">
		<web-view ref="webviewRef"  v-if="isshow" :src="url" @message="handleMessage" ></web-view>

		<!-- ✅ 新增：一个绝对定位的遮罩层，用于在刷新时覆盖 web-view -->
		<view v-else class="reloading-mask">
			<up-loading-icon text="正在获取订单状态..." v-if="orderOver" textSize="16"></up-loading-icon>
			
			<up-loading-icon text="订单已完成" v-else textSize="16"></up-loading-icon>
		</view>
	</view>
</template>

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

	let orderId = ref<string>('')
	let url = ref<string>('')
	let isshow = ref<boolean>(false)

	let orderInfo = ref<any>({
		isFood: 0
	})
	
	let riderOrder = ref<any>({
		status:0
	})
	
	let orderOver = ref<true>(false)
	let addInfo = ref<any>({
		latitude:0,
		longitude:0,
		name:'',
		address:''
	})

	onLoad((data) => {
		if (data.orderId) {
			orderId.value = data.orderId
			getData()
			getlog()
		} else {
			Service.Msg('为获取到订单ID')
		}
	});

	// 初始化url
	const getUrl = () => {
		isshow.value = false
		url.value = 'https://cndh.hnzchl.cn/?orderId=' + orderId.value + '&isFood=' + riderOrder.value.status
		isshow.value = true
	}


	const getData = () => {
		CNRiderOrderService.GetUnitOrderInfo(orderId.value).then(res => {
			if (res.code==0) {
				orderInfo.value = res.data.orderInfo
				riderOrder.value = res.data.riderOrder
				getUrl()
				
				
				
			}else{
				Service.Msg(res.mgs)
			}
		})
	}
	
	
	const getlog = () =>{
		
		CNRiderOrderService.GetLoginOrderInfo(orderId.value).then(res => {
				
			if (res.code==0) {
			
			if(res.data.orderRider.status==0){
				// 取餐
				addInfo.value = {
					latitude:res.data.storeLocation.lat,
					longitude:res.data.storeLocation.lon,
					name:res.data.storeLocation.storeName,
					address:res.data.storeLocation.address
				}
			}else{
				// 送餐
				addInfo.value = {
					latitude:res.data.userAddress.lat,
					longitude:res.data.userAddress.lon,
					name:res.data.userAddress.realName,
					address:res.data.userAddress.address
				}
				
			}
				
				
				
			}else{
				Service.Msg(res.mgs)
			}
		})
	}

	// 点击完成送餐取餐调用
	const handleMessage = (data) => {
		
		let preat = data.detail.data[0]
		if(preat.action =='message'){
		
			if (riderOrder.value.status == 0) {
				// 去商家、取餐
				pickFood(1)
			} else {
				// 去用户，送餐
				pickFood(2)
			}
		
		}else if(preat.action =='logData'){
			CNRiderOrderService.UpdateRiderLocation(preat.data[0],preat.data[1]).then(res=>{
				if(res.code==0){
					
				}
			})
		}else if(preat.action =='Navigation'){
			uni.openLocation({
			    latitude: parseFloat(addInfo.value.latitude),
			    longitude: parseFloat(addInfo.value.longitude),
			    name: addInfo.value.name,
			    address: addInfo.value.address,
			    success: function (res) {
			        console.log('打开系统位置地图成功')
			    },
			    fail: function (error) {
			        console.log(error,'xxx')
			    }
			})
		}
		
		return
	};
	
	
	// 取餐
	const pickFood = ( type:number) => {
		CNRiderOrderService.UpdateRiderOrderTake(orderId.value, type).then(res => {
			if (res.data) {
				Service.Msg(type==1?'取餐成功':'订单完成')
				if(type==2){
					setTimeout(()=>{Service.GoPageTab('/pages/index/index')},500)
				}
			getlog()
				getData()
			} else {
				Service.Msg(res.msg)
			}
		})
	};
	
</script>

<style lang="scss" scoped>
	.reloading-mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #f7f7f7; // 使用页面底色
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 10;
	}
</style>