<template>
	<view class="page">
		<u-navbar :title="title" :border-bottom="false" :background="bgc" title-color='#000' back-icon-color="#000" title-size='36' height='50'></u-navbar>
		<view class="box">
			<view class="top">
				<u-search placeholder="搜索" input-align="center" v-model="keyword" @custom="seach"></u-search>
			</view>
			<u-picker mode="time" v-model="show" :params="params" @confirm="confirm"></u-picker>
			<view class="date">
				<text @click="btnks(1)">{{selector}}</text> -- <text @click="btnks(2)">{{selectors}}</text>
			</view>

			<view class="list" @scrolltolower="onReachBottom" v-for="(item,index) in wateringList" :key="index">
				<view class="tops">
					<view class="" style="font-size: 32rpx;color: #3D3D3D;">{{item.orderNo}} 
					<u-icon  @click="fuzhi(item.orderNo)" name="file-text" size="38"></u-icon></view>
					<view class=""  @click="btnxq(item.orderId)"><u-icon name="arrow-right"></u-icon></view>
				</view>
				<view class="list_val">
					<view class="wz">
						租借店铺
					</view>
					<view class="gl" style="color: #1DBE7B;">
						{{item.startStoreName}}
					</view>
				</view>
				<view class="list_val">
					<view class="wz">
						租借时间
					</view>
					<view class="gl">
						{{item.startRentTime}}
					</view>
				</view>
				<view class="list_val">
					<view class="wz">
						手机号码
					</view>
					<view class="gl">
						{{item.mobile}}
					</view>
				</view>
				<view class="list_val">
					<view class="wz">
						订单时长
					</view>
					<view class="gl">
						{{item.remainingTime}}
					</view>
				</view>
				<view class="list_val">
					<view class="wz">
						代理信息
					</view>
					<view class="gl">
						{{item.userName}}（{{item.startAgentId}}）
					</view>
				</view>
				<view class="je">
					<view class="" style="font-size: 32rpx;color: #3D3D3D;">
						订单金额 <text
							style="font-size: 28rpx;color: #FF8157;display: inline-block;margin-left: 10rpx;">￥{{item.money}}</text>
					</view>
					<view class="" style="font-size: 28rpx;color: #FF8157;">
						<text v-if="item.status == '1'">租借中</text>
						<text v-if="item.status == '2'">待扣款</text>
						<text v-if="item.status == '4'">已完成</text>
						<text v-if="item.status == '6'">租转卖</text>
						<text v-if="item.status == '7'">弹出失败</text>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '',
				bgc: {
					background: '#25CE88'
				},
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				show: false,
				selector: '',
				selectors: '',
				num: '',
				status: '',

				pagenum: 1,
				wateringList: [],
				pagesize: 10, // 一页多少数据
				isLoading: false, // 是否正在加载数据  
				noMoreData: false, // 是否没有更多数据  
				total: 0,
				keyword: '',
				showflag: false
			}
		},
		onLoad(option) {
			let today = new Date()
			this.selector = this.formatDate(today)
			this.selectors = this.formatDate(today)
			this.title = option.title
			this.status = option.status
			this.getlist()
		},
		methods: {
			getlist() {
				this.$u.get('/agent/order/rent/list?pageNum=' + this.pagenum + '&pageSize=' + this.pagesize + '&keyword=' +
					this.keyword + '&status=' + this.status + '&startDate=' + this.selector + '&endDate=' + this.selectors).then(res => {
					if (res.code == 200) {
						this.total = res.total
						if (this.total > 0) {
							this.showflag = false
						} else {
							this.showflag = true
						}
						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
									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 + '分钟'
									if (order.priceStandard.feeMode == 2) {
										if (minutes >= order.priceStandard.feeFreeTime) {
											order.money = (hours + 1) * order.priceStandard.feePrice
										} else {
											order.money = hours * order.priceStandard.feePrice
										}
									} else if (order.priceStandard.feeMode == 1) {
										if (minutes >= order.priceStandard.feeFreeTime) {
											order.money = (hours * 2 + 1) * order.priceStandard.feePrice
											if (minutes >= 30) {
												order.money = (hours * 2 + 2) * order.priceStandard.feePrice
											}
										} else {
											order.money = hours * 2 * order.priceStandard.feePrice
										}
									}
								} 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 + '分钟'
									if (order.priceStandard.feeMode == 2) {
										if (minutes >= order.priceStandard.feeFreeTime) {
											order.money = (hours + 1) * order.priceStandard.feePrice
										} else {
											order.money = hours * order.priceStandard.feePrice
										}
									} else if (order.priceStandard.feeMode == 1) {
										if (minutes >= order.priceStandard.feeFreeTime) {
											order.money = (hours * 2 + 1) * order.priceStandard.feePrice
											if (minutes >= 30) {
												order.money = (hours * 2 + 2) * order.priceStandard.feePrice
											}
										} else {
											order.money = hours * 2 * order.priceStandard.feePrice
										}
									}
								}
							})
							this.wateringList = this.wateringList.concat(res.rows)
							this.pagenum++
						} else {
							// 没有更多数据  
							this.noMoreData = true;
						}
						this.isLoading = false;
					}
				})
			},
			onReachBottom() {
				let sum = this.total / this.pagesize
				if (this.pagenum - 1 < sum) {
					this.getlist(); // 上拉加载更多  
				} else {
					uni.showToast({
						title: '没有更多记录了',
						icon: 'none',
						duration: 1000
					});
				}
			},
			formatDate(date) {
				let year = date.getFullYear()
				let month = String(date.getMonth() + 1).padStart(2, '0')
				let day = String(date.getDate()).padStart(2, '0')
				return `${year}-${month}-${day}`
			},
			btnks(num) {
				this.show = true
				if (num == 1) {
					this.num = 1
				} else {
					this.num = 2
				}
			},
			confirm(e) {
				if (this.num == 1) {
					this.selector = e.year + '-' + e.month + '-' + e.day
					this.pagenum = 1
					this.wateringList = []
					this.getlist()
				} else {
					this.selectors = e.year + '-' + e.month + '-' + e.day
					this.pagenum = 1
					this.wateringList = []
					this.getlist()
				}
			},
			btnxq(orderId) {
				uni.navigateTo({
					url: '/page_user/order/xiangqing?orderId=' + orderId
				})
			},
			fuzhi(text) {
				uni.setClipboardData({
					data: text,
					success: function(res) {
						console.log('复制的信息：', text);
						uni.showToast({
							title: '复制成功',
						});
					}
				});
			}, 
			seach(){
				this.pagenum = 1
				this.wateringList = []
				this.getlist()
			}
		}
	}
</script>

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

	page {}

	.page {
		width: 750rpx;

		.box {
			width: 750rpx;
			height: 100%;
			padding-bottom: 200rpx;
			background: #F4F5F7;
			border-radius: 0rpx 0rpx 0rpx 0rpx;

			.list {
				width: 680rpx;
				height: 500rpx;
				background: #FFFFFF;
				border-radius: 38rpx 38rpx 38rpx 38rpx;
				padding: 32rpx 42rpx;
				box-sizing: border-box;
				margin: auto;
				margin-bottom: 30rpx;

				.je {
					border-top: 1px solid #D8D8D8;
					display: flex;
					justify-content: space-between;
					padding-top: 20rpx;
					margin-top: 20rpx;
					box-sizing: border-box;
				}

				.list_val {
					display: flex;
					justify-content: space-between;
					margin-top: 28rpx;

					.wz {
						font-size: 24rpx;
						color: #808080;
					}

					.gl {
						font-size: 24rpx;
						color: #3D3D3D;
					}
				}

				.tops {
					display: flex;
					justify-content: space-between;
					width: 100%;
					border-bottom: 1px solid #D8D8D8;
					padding-bottom: 20rpx;
				}
			}

			.top {
				padding: 28rpx 32rpx;
				box-sizing: border-box;
				background-color: #fff;

				/deep/ .u-content {
					border: 1px solid #ccc;
					border-radius: 50rpx 0 0 50rpx !important;
				}

				/deep/ .u-action {
					border-radius: 0 50rpx 50rpx 0 !important;
					width: 112rpx;
					height: 65rpx;
					line-height: 65rpx;
					border: 2rpx solid #ccc;
					margin-left: 0;
					color: #3D3D3D;
					background-color: #f2f2f2;
				}
			}

			.date {
				padding-left: 80rpx;
				padding-right: 80rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;
				background-color: #fff;
				padding-bottom: 40rpx;
				margin-bottom: 26rpx;

				text {
					padding: 6rpx 18rpx;
					box-sizing: border-box;
					background: #eee;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
					font-size: 24rpx;
					color: #808080;
				}
			}
		}
	}
</style>