<template>
	<view class="page">
		<u-navbar title="选择订单" :border-bottom="false" :background="bgc" title-color="#000" title-size="36"
			height="45"></u-navbar>

		<view class="order-list">
			<!-- 租车订单 -->
			<view class="order-item" v-for="(item,index) in list" :key="index" @click="btnitem(item)">
				<view class="order-header">
					<view class="order-time">{{item.orderNo}}</view>
					<view class="order-more">
						<u-icon name="more" size="24" color="#999"></u-icon>
					</view>
				</view>
				<view class="order-content">
					<view class="order-price">{{item.rideFee == null ? '0.00' : item.rideFee}}元</view>
				</view>
				<view class="order-detail">
					<view class="detail-item">车辆编号：<text style="color: #333;">{{item.deviceSn == null ? '--' : item.deviceSn}}</text> </view>
					<view class="detail-item">车牌号：<text style="color: #333;">{{item.deviceVehicleNum == null ? '--' : item.deviceVehicleNum}}</text></view>
					<view class="detail-item">下单时间：<text style="color: #333;">{{item.orderStartTime}}</text></view>
					<!-- 骑行时长使用计算方法 -->
					<view class="detail-item">骑行时长：<text style="color: #333;">{{ calculateRideDuration(item.orderStartTime) }}</text></view>
					<view class="detail-item">行驶距离：<text style="color: #333;">{{item.orderDistance == null ? '0.0' : (item.orderDistance / 1000).toFixed(1)}}Km</text></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				list: [],
				orderobj: {} // 注意：原代码中orderobj未初始化，建议添加默认值避免报错
			};
		},
		onLoad() {
			 this.getlist()
		},
		onShow() {
			
		},
		methods: {
			getlist(){
				this.$u.get(`/app/orderDevice/mineUsingList`).then(res => {
					if (res.code == 200) {
						this.list = res.data;
					}
				})
			},
			// 点击切换订单
            btnitem(item){
				this.$u.put(`/app/orderDevice/setHighestSort?id=${item.id}`).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '切换订单成功',
							icon: 'success',
							duration: 2000
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			// 计算骑行时长（核心方法）
			calculateRideDuration(startTime) {
				if (!startTime) return "0时1分"; // 兼容无开始时间的情况
				
				// 转换开始时间为时间戳（处理ISO格式字符串）
				const start = new Date(startTime).getTime();
				const now = new Date().getTime(); // 当前时间戳
				const diffMs = now - start; // 时间差（毫秒）
				
				// 转换为分钟，不足1分钟按1分钟算
				const totalMinutes = Math.max(Math.ceil(diffMs / (1000 * 60)), 1);
				
				// 计算小时和分钟
				const hours = Math.floor(totalMinutes / 60);
				const minutes = totalMinutes % 60;
				
				return `${hours}时${minutes}分`;
			}
		},
	};
</script>

<style lang="scss">
	/* 样式不变 */
	page {
		background-color: #f5f7fa;
		font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
	}

	.order-list {
		padding: 20rpx;
	}

	.order-item {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 10rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
	}

	.order-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 28rpx;
	}

	.order-time {
		font-size: 28rpx;
		color: #999;
	}

	.order-more {
		padding: 4rpx;
	}

	.order-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.order-price {
		font-size: 36rpx;
		color: #e53e3e;
		font-weight: 600;
	}

	.order-detail {
		font-size: 28rpx;
		color: #999;
		border-top: 1px dashed #999;
		padding-top: 10rpx;
		box-sizing: border-box;
	}

	.detail-item {
		margin-bottom: 8rpx;
	}

	.detail-item:last-child {
		margin-bottom: 0;
	}
</style>