<template>
	<view class="page" v-if="loading">
		<u-navbar title="订单详情" :border-bottom="false" :background="bgc" title-color='#2E4975' title-size='36'
			height='36'></u-navbar>
		<map class="map" id="map" ref="map" :scale="zoomSize" :latitude="latitude" :longitude="longitude"
			:show-location='true' :markers="markers" :polygons="polyline"> </map>
		<view class="info_card">
			<view class="info_tit">
				基本信息
			</view>
			<view class="lines"></view>
			<view class="cont">
				<view class="info_li">
					订单编号：<span>{{orderInfo.orderNo}}</span>
				</view>
				<view class="info_li">
					租赁用户：<span style="color:#4C97E7 ;">{{orderInfo.phonenumber}}</span>
				</view>
				<view class="info_li">
					<view class="half_infoli">
						租赁时长：<span>{{orderInfo.duration/60}}分钟</span>
					</view>
					<view class="half_infoli">
						行驶距离：<span>{{orderInfo.distance/1000}}Km</span>
					</view>
				</view>
				<view class="info_li">
					订单状态：<span>{{status()}}</span>
				</view>
			</view>
		</view>
		<view class="info_card" style="margin-top: 20rpx;">
			<view class="info_tit">
				设备信息
			</view>
			<view class="lines"></view>
			<view class="cont">

				<view class="info_li">
					<view class="half_infoli">
						车牌号：<span>{{orderInfo.device.vehicleNum}}</span>
					</view>
					<view class="half_infoli">
						SN：<span>{{orderInfo.device.sn}}</span>
					</view>
				</view>
				<view class="info_li">
					<!-- <view class="half_infoli">
						车辆编号：<span>{{orderInfo.device.areaName}}</span>
					</view> -->
					<view class="half_infoli">
						运营区域：<span>{{orderInfo.device.areaName}}</span>
					</view>
				</view>
			</view>
		</view>
	
		<view class="info_card" style="margin-top: 20rpx;">
			<view class="info_tit">
				 行程记录
			</view>
			<view class="lines"></view>
			<view class="cont">

				<view class="info_li">
					开锁时间：<span>{{orderInfo.unlock_time}}</span>
				</view>
				<view class="info_li">
					开锁地点：<span>{{orderInfo.unlock_time}}</span>
				</view>
				<!-- <view class="info_li">
					锁车时间：<span>120.56189，27.12379</span>
				</view>
				<view class="info_li">
					锁车地点：<span>120.56189，27.12379</span>
				</view> -->
				<!-- <view class="info_li">
					解除时间：<span>120.56189，27.12379</span>
				</view> -->
				<view class="info_li">
					关锁时间：<span>{{}}</span>
				</view>
				<view class="info_li">
					关锁地点：<span>120.56189，27.12379</span>
				</view>
			</view>
		</view>
		<view class="info_card" style="margin-top: 20rpx;">
			<view class="info_tit">
				费用明细
				<view class="money" >
					结算总费用：￥{{orderInfo.settlementFee}}
				</view>
			</view>
			<view class="lines"></view>
			<view class="cont">
		
				<view class="info_li">
					<view class="half_infoli">
						预约费用：<span>{{orderInfo.appointmentFee}}</span>
					</view>
					<view class="half_infoli">
						调度费用：<span>{{orderInfo.dispatchFee}}</span>
					</view>
				</view>
				<view class="info_li">
					<view class="half_infoli">
						管理费用：<span>{{orderInfo.manageFee}}</span>
					</view>
					<view class="half_infoli">
						骑行费用：<span>{{orderInfo.ridingFee}}</span>
					</view>
				</view>
				<view class="info_li">
					实收：<span>{{orderInfo.totalFee}}</span>
				</view>
				<view class="info_li">
					支付方式：<span>微信支付</span>
				</view>
				<view class="info_li" v-if="orderInfo.rule">
					计费模板：<span>{{orderInfo.rule.name}}</span>
				</view>
				<view class="info_li" v-if="orderInfo.payTime">
					支付时间：<span>{{orderInfo.payTime}}</span>
				</view>
				
			</view>
			<view class="lines"></view>
			<view class="cont" v-if="orderInfo.etRefund!=''">
					
			
				<view class="info_li">
					<view class="half_infoli">
						预约费退款：<span>{{orderInfo.etRefund.appointmentFee}}</span>
					</view>
					<view class="half_infoli">
						调度费退款：<span>{{orderInfo.etRefund.dispatchFee}}</span>
					</view>
				</view>
				<view class="info_li">
					<view class="half_infoli">
						骑行费退款：<span>{{orderInfo.etRefund.ridingFee}}</span>
					</view>
					<view class="half_infoli">
							管理费退款：<span>{{orderInfo.etRefund.manageFee}}</span>
					</view>
				
				</view>
				<view class="info_li">
					退款原因：<span>{{orderInfo.etRefund.reason}}</span>
				</view>
				<view class="info_li">
					退款时间：<span>{{orderInfo.etRefund.createTime}}</span>
				</view>
				
			</view>
		</view>
		<view class="bot">
			<view class="btn">
				退款
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#F7FAFE",
				},
				latitude: 39.916527,
				longitude: 116.397128,
				isMap: false,
				zoomSize: 15,
				markers: [],
				polyline: [],
				polygons: [],
				
				cardId: '001区域',
				orderId:'',
				orderInfo:{},
				loading:false,
				

			}
		},
		onLoad(e) {
			this.orderId=e.id
			this.getOrderDetail()
			this.getArea()
			this.getParking()
		},
		components:{
		
		},
		methods: {
			convertBoundaryToPolyline(boundary) {
				if (!boundary) return null;
			
				const points = JSON.parse(boundary).map(coord => ({
					latitude: coord[1],
					longitude: coord[0]
				}));
			
				const polyline = {
					points: points,
					fillColor: "#55888840", //填充颜色
					strokeColor: "#22FF00", //描边颜色
					strokeWidth: 2, //描边宽度
					zIndex: 1, //层级
				};
			
				return polyline;
			},
			convertBoundaryToPolylines(boundaries) {
				return boundaries.map(boundary => {
					if (!boundary) return null;
					let coords;
					try {
						coords = JSON.parse(boundary);
					} catch (error) {
						console.error("Error parsing boundary JSON:", error);
						return null;
					}
					if (!Array.isArray(coords)) {
						console.error("Parsed boundary is not an array:", coords);
						return null;
					}
					const points = coords.map(coord => ({
						latitude: coord[1],
						longitude: coord[0]
					}));
					return {
						points: points,
						fillColor: "#55888840", //填充颜色
						strokeColor: "#558888", //描边颜色
						strokeWidth: 2, //描边宽度
						zIndex: 1, //层级
					};
				}).filter(polyline => polyline !== null); // 过滤掉无效的折线数据
			},
			getParking() {
				// 发送请求获取数据
				this.$u.get('/app/parking/list').then((res) => {
					if (res.code === 200) {
						// 处理接口返回的数据
						const validBoundaries = res.rows.map(row => row.boundaryStr).filter(boundary =>
							typeof boundary === 'string' && boundary.trim() !== '');
						const polylines = this.convertBoundaryToPolylines(validBoundaries);
			
						// 将处理后的数据添加到 this.polyline 中
						this.polyline = this.polyline.concat(polylines);
						// console.log(this.polyline);
						res.rows.forEach(item => {
							this.markers.push({
								id: parseFloat(item.parkingId),
								latitude: parseFloat(item.latitude),
								longitude: parseFloat(item.longitude),
								// title: item.deviceName,
								width: 20,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u9yz0bKCWKyev0JYpTne',
								callout: {
									content: item.parkingName, // 修改为你想要显示的文字内容
									color: '#ffffff', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#000000', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						})
					}
				}).catch(error => {
					console.error("Error fetching parking data:", error);
				});
			},
			getArea() {
				// 发送请求获取数据
				this.$u.get('/app/area/list').then((res) => {
					if (res.code === 200) {
						// 处理接口返回的数据，将边界数据转换为地图组件需要的折线结构
						const polylines = res.rows
							.filter(area => area.boundaryStr) // 过滤掉boundary为空的数据
							.map(area => this.convertBoundaryToPolyline(area.boundaryStr));
						// 更新折线数据
						this.polyline = polylines;
						// console.log(this.polyline);
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				});
			},
			status(){
				if(this.orderInfo!=''){
					if(this.orderInfo.status==0){
						return '预约中'
					}else if(this.orderInfo.status==1){
						return '取消预约'
					}else if(this.orderInfo.status==2){
						return '开始骑行'
					}else if(this.orderInfo.status==3){
						return '骑行结束'
					}else if(this.orderInfo.status==4){
						return '订单完成'
					}
				}
				
			},
			getOrderDetail(){
				this.$u.get('/appVerify/order/'+this.orderId).then((res) => {
					if (res.code === 200) {
						// 处理接口返回的数据，将边界数据转换为地图组件需要的折线结构
					this.orderInfo=res.data
					this.loading=true
					this.latitude= parseFloat(this.orderInfo.device.latitude)
					this.longitude=  parseFloat(this.orderInfo.device.longitude )
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F3F3F3;
	}

	.page {
		padding-bottom: 200rpx;
		width: 750rpx;

		.map {
			width: 750rpx;
			height: 752rpx;
		}

		.info_card {
			background: #FFFFFF;

			.info_tit {
				display: flex;
				flex-wrap: nowrap;
				padding: 22rpx 28rpx;
				font-weight: 600;
				font-size: 32rpx;
				color: #3D3D3D;
				.money{
					margin-left: auto;
					font-weight: 500;
					font-size: 32rpx;
					color: #4C97E7;
				}
			}

			.lines {
				width: 750rpx;
				height: 2rpx;
				border: 2rpx solid #ccc;
			}

			.cont {
				padding: 26rpx 28rpx;

				.info_li {
					display: flex;
					flex-wrap: nowrap;
					font-weight: 400;
					font-size: 28rpx;
					color: #808080;

					span {
						color: #3D3D3D;
					}

					line-height: 48rpx;

					.half_infoli {
						width: 50%;
						font-weight: 400;
						font-size: 28rpx;
						color: #808080;

						span {
							color: #3D3D3D;
						}
					}
				}
			}
		}

		.bot {
			position: fixed;
			bottom: 0;
			width: 750rpx;
			height: 184rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08), 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 0rpx 0rpx 0rpx 0rpx;

			.btn {
				margin: 46rpx auto;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 680rpx;
				height: 90rpx;
				background: #4C97E7;
				border-radius: 54rpx 54rpx 54rpx 54rpx;
				font-weight: 500;
				font-size: 40rpx;
				color: #FFFFFF;
			}
		}
	}
</style>