<template>
	<view class="page">
		<u-navbar title="收入对账" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='45'></u-navbar>
		<view class="" style="background: #FFFFFF;width: 750rpx;padding-bottom: 36rpx;">
			<view class="top_box">
				<view class="left_text">
					时间范围
				</view>
				<view class="timebox">
					<view class="left_time" @click="time1=true">
						{{firsTime}}
					</view>
					-
					<view class="right_time" @click="time2=true">
						{{lasTime}}
					</view>
				</view>
			</view>
			<view class="top_box" v-if="typeindex==2" style="margin-top: 20rpx;">
				<view class="left_text">
					车辆编号
				</view>
				<view class="sear_ipt">
					<input type="text" v-model="searchKeyword" placeholder="请输入内容" class="input"
						placeholder-style="color:#C7CDD3" @input="search()">
				</view>
			</view>
			<view class="data_box">
				<view class="data_cont " :class="typeindex==1?'act1':''" @click="changeType(1)">
					按日期
				</view>
				<view class="data_cont" :class="typeindex==2?'act1':''" @click="changeType(2)">
					按车辆
				</view>

			</view>
		</view>


		<view class="cont_box" v-if="typeindex==1">
			<view class="cont_info">
				<view class="info_li" style="color:#4C97E7 ;">
					已支付:{{infoList.totalPayFee}}
				</view>
				<view class="info_li" style="color:#4C97E7 ;">
					已退款:¥{{infoList.totalRefundFee}}
				</view>


			</view>
			<view class="cont_info">
				<view class="info_li" style="color:#4C97E7 ;">
					手续费:¥{{infoList.totalServiceFee}}
				</view>
				<view class="info_li" style="color:#4C97E7 ;">
					收入:¥{{infoList.totalIncome}}
				</view>


			</view>
		</view>
		<view class="info_card" style="margin-top: 20rpx;" v-for="(item,index) in infoList.reconciliationList"
			:key="index" v-if="typeindex==1">
			<view class="info_tit">
				{{item.day}}
			</view>
			<view class="lines"></view>
			<view class="cont">


				<view class="info_li">
					<view class="half_infoli">
						支付金额:<span> {{item.payFee}}</span>
					</view>
					<view class="half_infoli">
						已退款:<span>¥ {{item.refundFee}}</span>
					</view>
				</view>
				<view class="info_li">
					<view class="half_infoli">
						手续费:<span>¥{{item.serviceFee}}</span>
					</view>
					<view class="half_infoli">
						收入:<span style="color:#4C97E7 ;">¥{{item.income}}</span>
					</view>
				</view>
			</view>
		</view>
		<view class="info_card" style="margin-top: 20rpx;" v-for="(item,index) in infoList.reconciliationList"
			:key="index" v-if="typeindex==2">
			<view class="info_tit">
				{{item.sn}}
			</view>
			<view class="lines"></view>
			<view class="cont">


				<view class="info_li">
					<view class="half_infoli">
						支付金额:<span> ¥{{item.payFee}}</span>
					</view>
					<view class="half_infoli">
						已退款:<span>¥ {{item.refundFee}}</span>
					</view>
				</view>
				<view class="info_li">
					<view class="half_infoli">
						手续费:<span>¥{{item.serviceFee}}</span>
					</view>
					<view class="half_infoli">
						收入:<span style="color:#4C97E7 ;">¥{{item.income}}</span>
					</view>
				</view>
			</view>
		</view>
		<u-picker mode="time" v-model="time1" :params="params" @confirm="confirm1" :default-time="firsTime"></u-picker>
		<u-picker mode="time" v-model="time2" :params="params" @confirm="confirm2" :default-time="lasTime"></u-picker>
	</view>
</template>

<script>
	let timerId;
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				time1: false,
				time2: false,
				lasTime: '',
				firsTime: '',
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				typeindex: 1,
				infoList: [],
				searchKeyword: '',
				areaId:null

			}
		},
		onLoad() {

			let today = new Date();
			// 获取七天前的日期
			let sevenDaysAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
			// 格式化日期为 yyyy-MM-dd
			this.firsTime = this.formatDate(sevenDaysAgo);
			this.lasTime = this.formatDate(today);
			
			if(uni.getStorageSync('adminAreaid')){
				this.areaId = uni.getStorageSync('adminAreaid')
				this.operatingData()
			}
		},
		onReady() {
			// this.firsTime = this.formatDate(sevenDaysAgo);
			// this.lasTime = this.formatDate(today);
		},
		methods: {

			search() {
				clearTimeout(timerId);
				timerId = setTimeout(() => {
				this.operatingData()
				}, 500);
			},
			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}`;
			},
			operatingData() {
				let data = {
					timeStart: this.firsTime,
					timeEnd: this.lasTime,
					type: this.typeindex,
					sn: this.searchKeyword,
					areaId:this.areaId
				}
				this.$u.get('/appVerify/reconciliation', data).then((res) => {
					if (res.code === 200) {
						// 处理接口返回的数据,将边界数据转换为地图组件需要的折线结构
						this.infoList = res.data
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				});
			},
			confirm1(e) {
				this.firsTime = e.year + '-' + e.month + '-' + e.day;
				// 计算时间间隔
				let timeDiff = this.calculateTimeDifference(this.firsTime, this.lasTime);
				if (timeDiff > 60) {
					// 提示用户
					// 示例:uMessage.warning('时间区间不能超过60天');
					// 或者根据您的需求采取其他措施
					uni.showToast({
						title: '选择时间区间需要小于60天',
						icon: 'none',
						duration: 2000
					});

				}else{
					this.operatingData();
				}

				// 更新 firisttime
				
				// 进行其他操作
				// this.operatingData();
			},
			calculateTimeDifference(startTime, endTime) {
				 let start = new Date(startTime).getTime();
				    let end = new Date(endTime).getTime();
				    let diff = Math.abs(end - start);
				    return Math.ceil(diff / (1000 * 60 * 60 * 24));
			},
			confirm2(e) {
				// 计算时间间隔
				this.lasTime = e.year + '-' + e.month + '-' + e.day;
				let timeDiff = this.calculateTimeDifference(this.firsTime, this.lasTime);
				if (timeDiff > 60) {
					uni.showToast({
						title: '选择时间区间需要小于60天',
						icon: 'none',
						duration: 2000
					});
					// 提示用户
					// 示例:uMessage.warning('时间区间不能超过60天');
					// 或者根据您的需求采取其他措施
					return; // 中断操作
				}else{
					this.operatingData();
				}

				// 更新 lasttime
				
				// 进行其他操作
				// 
			},
			changeType(num) {
				this.typeindex = num
				if (this.typeindex == 1) {
					this.operatingData()
				} else {
					
					this.infoList = []
					
				}

			}
		}
	}
</script>

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

	.page {
		width: 750rpx;

		.lines {
			width: 748rpx;
			height: 22rpx;
			background: #F6F6F6;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
		}

		.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 {
						display: flex;
						flex-wrap: nowrap;
						width: 50%;
						font-weight: 400;
						font-size: 28rpx;
						color: #808080;

						span {
							color: #3D3D3D;
						}

						.input {
							width: 30%;
						}
					}
				}
			}
		}

		.cont_box {
			padding: 14rpx 30rpx;
			width: 750rpx;
			background: #E2F2FF;
			border-radius: 0rpx 0rpx 0rpx 0rpx;

			.cont_info {
				margin-top: 8rpx;
				display: flex;
				flex-wrap: nowrap;

				.info_li {
					width: 50%;
					font-weight: 400;
					font-size: 28rpx;
					color: #3D3D3D;
				}
			}
		}

		.data_box {
			width: 672rpx;
			margin: 36rpx auto;
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			// justify-content: space-between;

			.data_cont {
				margin-right: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 150rpx;
				height: 60rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 4rpx 22rpx 0rpx rgba(0, 0, 0, 0.07);
				border-radius: 10rpx 10rpx 10rpx 10rpx;
				font-weight: 400;
				font-size: 32rpx;
				color: #3D3D3D;
				border: 2rpx solid #fff;
			}

			.act1 {
				background: #E2F2FF;
				box-shadow: 0rpx 4rpx 22rpx 0rpx rgba(0, 0, 0, 0.07);
				color: #4C97E7;
				border: 2rpx solid #4C97E7;
			}
		}

		.top_box {

			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			padding: 28rpx 30rpx;
			// padding: 20rpx 0;

			margin: 0 auto;
			width: 672rpx;
			height: 100rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 22rpx 0rpx rgba(0, 0, 0, 0.07);
			border-radius: 10rpx 10rpx 10rpx 10rpx;

			.left_text {
				width: 25%;
				font-weight: 400;
				font-size: 32rpx;
				color: #3D3D3D;
			}

			.timebox {
				width: 75%;
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				margin-left: 34rpx;

				.left_time {
					text-align: center;
					margin-right: 6rpx;
					height: 50rpx;
					width: 45%;
					border: 2rpx solid #ccc;
					border-radius: 12rpx;
					font-weight: 400;
					font-size: 32rpx;
					color: #979797;
				}

				.right_time {
					text-align: center;
					margin-left: 6rpx;
					height: 50rpx;
					width: 45%;
					border: 2rpx solid #ccc;
					border-radius: 12rpx;
					font-weight: 400;
					font-size: 32rpx;
					color: #979797;
				}
			}
		}
	}
</style>