<template>
	<view class="page">
		<u-navbar title="我的订单" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='58'></u-navbar>
		<view class="listorder" @scrolltolower="onReachBottom" v-for="(item,index) in wateringList" :key="index">
			<view class="title">
				<view>订单号：{{item.orderNo}} &nbsp;&nbsp; <u-icon @click="fuzhi(item.orderNo)" name="file-text"
						size="38"></u-icon> </view>
				<view @click="btnnav(item.orderId)"><u-icon name="arrow-right"></u-icon></view>
			</view>
			<view class="bh">
				<text>柜机编号</text> <text>{{item.startCabinetSn}}</text>
			</view>
			<view class="bh">
				<text>电池编号</text> <text>{{item.deviceSn}}</text>
			</view>
			<view class="bh">
				<text>手机号码</text> <text>{{item.mobile}}</text>
			</view>
			<view class="bh">
				<text>订单状态</text>
				<text v-if="item.status == 1">租赁中</text>
				<text v-if="item.status == 2">已归还</text>
				<text v-if="item.status == 3">付款中</text>
				<text v-if="item.status == 4">已付款</text>
				<text v-if="item.status == 5">免费取消</text>
				<text v-if="item.status == 6">租转卖</text>
			</view>
			<view class="bh">
				<text>订单金额</text> <text>{{item.money == null ? '-' : item.money}}</text>
			</view>
			<view class="bh">
				<text>租借时间</text> <text>{{item.startRentTime}}</text>
			</view>
			<view class="bh">
				<text>租借时长</text> <text>{{item.remainingTime}}</text>
			</view>
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				pagenum: 1,
				wateringList: [],
				pagesize: 10, // 一页多少数据
				isLoading: false, // 是否正在加载数据  
				noMoreData: false, // 是否没有更多数据  
				total: 0,
				keyword: '',
				type: '',
				bgc: {
					background: '#25CE88'
				}
			}
		},
		onLoad() {
			this.getlist()
		},
		methods: {
			btnnav(orderId) {
				uni.navigateTo({
					url: '/pages/myorder/returned/index?orderId=' + orderId
				})
			},
			getlist() {
				this.$u.get('/app/order/rent/list?pageNum=' + this.pagenum + '&pageSize=' + this.pagesize).then(res => {
					if (res.code == 200) {
						this.total = res.total;
						if (res.rows.length > 0) {
							res.rows.forEach(order => {
								if(order.endRentTime == null){
									var targetDate = new Date(order.startRentTime);  
									var currentDate = new Date();  
									var diff = targetDate - currentDate;  
									// 确保 diff 是非负的，以便计算剩余时间  
									var absDiff = Math.abs(diff);  
									var hours = Math.floor(absDiff / (1000 * 60 * 60));   
									var minutes = Math.floor((absDiff % (1000 * 60 * 60)) / (1000 * 60));  
									let formattedMinutes = minutes < 10 ? '0' + minutes : minutes;  
									// 直接拼接字符串，不要插入 '-'  
									order.remainingTime = hours + '时' + formattedMinutes + '分钟';
								}else{
									var targetDate = new Date(order.endRentTime);
									var currentDate = new Date();  
									var diff = targetDate - currentDate;  
									var absDiff = Math.abs(diff);  
									var hours = Math.floor(absDiff / (1000 * 60 * 60));   
									var minutes = Math.floor((absDiff % (1000 * 60 * 60)) / (1000 * 60));  
									let formattedMinutes = minutes < 10 ? '0' + minutes : minutes;  
									order.remainingTime = hours + '时' + formattedMinutes + '分钟';
								}
							});
		              
							this.wateringList = this.wateringList.concat(res.rows);
							// console.log(this.wateringList);
							this.pagenum++;
						} else {
							// 没有更多数据  
							this.noMoreData = true;
						}
						this.isLoading = false;
					}
				}).catch(error => {
					// 处理错误情况  
					console.error('请求出错:', error);
				});
			},
			onReachBottom() {
				let sum = this.total / this.pagesize
				if (this.pagenum - 1 < sum) {
					this.getlist();
				} else {
					uni.showToast({
						title: '没有更多记录了',
						icon: 'none',
						duration: 1000
					});
				}
			},
			fuzhi(text) {
				uni.setClipboardData({
					data: text,
					success: function(res) {
						console.log('复制的信息：', text);
						uni.showToast({
							title: '复制成功',
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .u-title,
	/deep/ .uicon-nav-back {
		padding-bottom: 40rpx;
	}

	page {
		// background-color: ;
		background: linear-gradient(180deg, #F4F5F7 0%, rgba(255, 255, 255, 0) 100%);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		padding-bottom: 200rpx;
	}

	.page {
		width: 750rpx;
		padding-left: 34rpx;
		padding-right: 34rpx;
		box-sizing: border-box;

		// position: fixed;
		// top: 0;
		// left: 0;
		.listorder {
			margin-top: 32rpx;
			width: 100%;
			height: 100%;
			background-color: #fff;
			border-radius: 30rpx;
			padding: 32rpx;
			box-sizing: border-box;

			.title {
				display: flex;
				justify-content: space-between;
				font-weight: 400;
				font-size: 32rpx;
				color: #666666;
				border-bottom: 1px solid #ccc;
				padding-bottom: 20rpx;
				margin-bottom: 28rpx;
			}

			.bh {
				display: flex;
				justify-content: space-between;
				margin-top: 18rpx;
				font-size: 24rpx;
				color: #808080;
				line-height: 32rpx;
			}
		}
	}
</style>