<template>
	<view class="wrapper">
		<!-- 背景颜色充满屏 -->
		<view class="bg-color"></view>
		<!-- 补卡额订单列表-->
		<view class="search-time">
			<view class="search-time-box">
				<view class="show-info">
					<view class="show-text">
						<picker class="picker date" mode="date" :value="dataTime.startDate" @change="bindDateStart">
							<text class="date-text text-val">{{dataTime.startDate?dataTime.startDate:'开始时间'}}</text>
						</picker>
						<text class="line"></text>
						<picker class="picker date" mode="date" :value="dataTime.endDate" @change="bindDateEnd">
							<text class="date-text text-val">{{dataTime.endDate?dataTime.endDate:'结束时间'}}</text>
						</picker>
					</view>
					<u-icon name="calendar" :custom-style="{color:'#999999', size: '28rpx'}"></u-icon>
				</view>
			</view>
			<view class="time-btn" @click="search(1)">查询</view>
		</view>
		<view class="total-num" v-if="state.list.length > 0">
			<text>金额合计:{{state.aggregateAmount/100}}</text>
			<text class="total-space">通行次数: {{state.passTotal}}</text>
			<view class="btn btn-download" @click="doDownload">下载报表</view>
		</view>
		<view class="list-wrap" v-if="state.list.length >0">
			<view v-for="item in state.list" :key="item.listNo"
				:class="item.status === 3 ? ' card-info finished' : 'card-info'">
				<view class="info-wrap">
					<view class="info-left">
						<!-- <view class="info-left-text">
							<text class="label">单号:</text>
							<text class="val">{{item.listNo}}</text>
						</view> -->
						<view>
							<text class="label">卡号:</text>
							<text class="val">{{item.cardId}}</text>
						</view>
					</view>
					<view class="info-right">
						<view class="price-label">
							交易金额
						</view>
						<view class="price-val">
							<text>￥</text>
							<text class="price-val-text">{{item.fee/100}}</text>
						</view>
					</view>
				</view>
				<view class="card-text-wrap">
					<view class="text-box">
						<text class="name-text">{{item.enStationName}}</text>
						<text
							class="time-text">{{item.enTime.substring(item.enTime.length -8 ,item.enTime.length-3)}}</text>
						<text class="date-text">{{item.enTime.substring(0,10)}}</text>
					</view>
					<view class="arrow-wrap">
						<text class="arrow-text">{{item.vehiclePlate}}</text>
						<image class="d-img" :src="`${$imgUrl}order/arrowCard.png`"></image>
					</view>
					<view class="text-box">
						<text class="name-text">{{item.exStationName}}</text>
						<text
							class="time-text">{{item.exTime.substring(item.exTime.length -8 ,item.exTime.length-3)}}</text>
						<text class="date-text">{{item.exTime.substring(0,10)}}</text>
					</view>
				</view>
				<!-- <view class="btn-wrap">
					<view>
						<text v-if="item.status === 2" class="status-wrap">审核进度：<text
								class="status-text">审核中</text></text>
						<text v-if="item.status === 3" class="status-wrap">审核进度：<text>已完成</text></text>
					</view>
					<view class="btn-1 btn" v-if="item.status === 1" @click="toApply(item)">补卡额申请</view>
					<view class="btn-1 btn" v-if="item.status === 2" @click="toView(item)">查看进度</view>
					<view class="btn-1 btn" v-if="item.status === 3" @click="toEvaluate(item)">去评价</view>
				</view> -->
			</view>
			<view class="bottom-line" v-if="state.flags">我是有底线的~~~</view>
		</view>
		<view class="blank" v-else>
			<view class="text-center">暂无数据</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from "vue";
	import {
		request
	} from "@/utils/network/request.js";

	import {
		stringToJson
	} from "@/utils/network/encryption.js";
	import {
		orderDetail,
		passBills,
		passBillCount,
		downloadBills,
	} from "@/utils/network/api.js";
	import {
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		downloadFileURL
	} from '@/datas/fileURL.js'
	import {
		msg,
	} from "@/utils/utils";
	const dataTime = reactive({
		startDate: "开始时间",
		endDate: "结束时间"
	})

	const state = reactive({
		list: [],
		cardId: null,
		passTotal: 0,
		parkTotal: 0,
		aggregateAmount: 0,
		pageSize: 10, //每页数据量
		pageNo: 1, // 当前页
		flags: false,
		handleDate: "", //月结单选择时间
		vehicleId: ""
	})
	onLoad((option) => {
		state.cardId = option.cardId;
		state.handleDate = option.handleDate;
		state.vehicleId = option.vehicleId;
		console.log("月结单111", state.handleDate)
		if (state.handleDate != "undefined") {
			// "2024-04-25"
			let deadLine = getDaysInMonth(state.handleDate.substring(0, 4), state.handleDate.substring(4, 6))
			dataTime.startDate = state.handleDate.substring(0, 4) + "-" + state.handleDate.substring(4, 6) + "-" +
				"01"
			dataTime.endDate = state.handleDate.substring(0, 4) + "-" + state.handleDate.substring(4, 6) + "-" +
				deadLine

			console.log("月结单", dataTime.startDate, state.handleDate != "")
			search(1)
		}
	})

	function getDaysInMonth(year, month) {
		var date = new Date();
		date.setFullYear(year);
		date.setMonth(month);
		date.setDate(0);
		return date.getDate();
	}
	// 触底加载
	onReachBottom(() => {
		if (state.list.length < state.pageNo * 10) return state.flags = true
		console.log("触底了")
		state.pageNo++
		search(2)
	})
	const search = (val) => {
		// if (dataTime.startDate == "开始时间") {
		// 	msg('请选择开始时间');
		// 	return;
		// }
		// if (dataTime.endDate == "结束时间") {
		// 	msg('请选择结束时间');
		// 	return;
		// }
		if (val == 1) {
			state.pageNo = 1
		}
		if (state.pageNo == 1 && state.list.length > 0) {
			state.list = []
		}
		const options = {
			type: 2,
			data: {
				cardId: state.cardId,
				beforeDate: dataTime.startDate == "开始时间" ? "" : dataTime.startDate,
				afterDate: dataTime.endDate == "结束时间" ? "" : dataTime.endDate,
				pageNo: state.pageNo,
				pageSize: state.pageSize,
			},
			method: "POST",
			showLoading: true,
		};
		request(passBills, options).then((res) => {
			console.log(res);
			let result = stringToJson(res.bizContent)
			console.log(result);
			state.list = [...state.list, ...stringToJson(res.bizContent).data]
			console.log(result.data);
			getPpassBillCount()
		})

	}
	const getPpassBillCount = () => {
		const options = {
			type: 2,
			data: {
				cardId: state.cardId,
				starDate: dataTime.startDate == "开始时间" ? "" : dataTime.startDate,
				endDate: dataTime.endDate == "结束时间" ? "" : dataTime.endDate,
			},
			method: "POST",
			showLoading: true,
		};
		request(passBillCount, options).then((res) => {
			let result = stringToJson(res.bizContent)
			state.aggregateAmount = result.aggregateAmount ? result.aggregateAmount : 0
			state.passTotal = result.passTotal ? result.passTotal : 0
			state.parkTotal = result.parkTotal ? result.parkTotal : 0
			console.log(result);
		})
	}

	// 展示日历
	const bindDateStart = (e) => {
		console.log(e);
		dataTime.startDate = e.detail.value
	}
	// 展示日历
	const bindDateEnd = (e) => {
		console.log(e);
		dataTime.endDate = e.detail.value
		// show.value = true
	}

	// 补卡申请
	const toApply = (item) => {
		uni.navigateTo({
			// url: `/subpackage/orders/cardAmount?id=${item.id}`
			url: ""
		});
	}
	// 去评价
	const toEvaluate = (item) => {
		uni.navigateTo({
			// url: `/subpackage/orders/order-evaluate?id=${item.id}`
			url: ""
		});
	}
	// 查看进度
	const toView = (item) => {
		uni.navigateTo({
			// url: `/subpackage/orders/cardAmountDetail?id=${item.id}`
			url: ""
		});
	}

	//下载按钮事件
	const doDownload = () => {
		const options = {
			type: 2,
			data: {
				cardId: state.cardId,
				beforeDate: dataTime.startDate == "开始时间" ? "" : dataTime.startDate,
				afterDate: dataTime.endDate == "结束时间" ? "" : dataTime.endDate,
				vehiclePlate: state.vehicleId.split('_')[0],
				vehiclePlateColor: state.vehicleId.split('_')[1]
			},
			method: "POST",
			showLoading: true,
		};
		request(downloadBills, options).then((res) => {
			let result = stringToJson(res.bizContent)
			console.log("downloadFileURL", downloadFileURL + result.ossFilePath);
			download(downloadFileURL + result.ossFilePath)
		})
	}

	//下载文件
	const download = (attachLink) => {
		let that = this
		uni.downloadFile({
			url: attachLink, //下载地址，后端接口获取的链接
			success: (data) => {
				console.log(data.tempFilePath)
				console.log(JSON.stringify(data))
				if (data.statusCode === 200) {
					uni.saveFile({ //文件保存到本地
						tempFilePath: data.tempFilePath, //临时路径
						success: function(res) {
							console.log("下载成功" + res.savedFilePath)
							console.log(JSON.stringify(res))
							uni.showToast({
								icon: 'none',
								mask: true,
								title: '文件已保存!',
								duration: 3000,
							});
							uni.openDocument({
								//fileType: 'docx',
								showMenu: true, //关键点,可以转发到微信
								filePath: res.savedFilePath,
								success: function(res) {
									console.log('打开文档成功');
								}
							});
						}
					});
				}
			},
			fail: (err) => {
				console.log(err);
				uni.showToast({
					icon: 'none',
					mask: true,
					title: '失败请重新下载',
				});
			},
		});
	}
</script>

<style lang="scss" scoped>
	.u-btn--primary {
		background: #00B38B;
	}

	.blank {
		font-size: 30rpx;
		text-align: center;
	}

	.bg-color {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: #EEF7F7;
		z-index: -1;
	}

	.search-box {
		margin: 30rpx 30rpx 20rpx 30rpx;
		height: 72rpx;
		height: 81rpx;
		background: #FFFFFF;
		border: 1px solid #DCDCDC;
		border-radius: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
	}

	.picker {
		flex: 1;
	}

	.search-time-box {
		flex: 1;
	}

	.search-box .icon {
		width: 48rpx;
		height: 48rpx;
		margin: 0 20rpx;
	}

	.search-box .search {
		flex: 1;
		margin-right: 20rpx;
		height: 100%;
		padding: 0 10rpx;
		font-size: 28rpx;
		color: #00b38b;
	}

	.scroll-view {
		white-space: nowrap;
		position: sticky;
		top: 0;
		background: #ffffff;
		box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
	}

	.search-time {
		display: flex;
		margin: 20rpx 30rpx 48rpx 30rpx;

		.time-btn {
			width: 130rpx;
			height: 80rpx;
			background: #00B38B;
			border-radius: 40rpx;
			color: #FFFFFF;
			font-size: 32rpx;
			line-height: 80rpx;
			text-align: center;
			margin-left: 19rpx;
		}

		.show-info {
			height: 80rpx;
			padding: 0 31rpx;
			background: #FFFFFF;
			border: 1px solid #DCDCDC;
			border-radius: 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.show-text {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 100%;
				text-align: center;
			}

			.date-text {
				color: #999999;
				font-size: 28rpx;
			}

			.text-val {
				color: #333
			}

			.line {
				width: 25rpx;
				height: 1rpx;
				background: #999999;
				margin: 0 24rpx;
			}
		}
	}

	.total-num {
		font-size: 30rpx;
		color: #999999;
		display: flex;
		justify-content: space-evenly;
		align-items: center;

		// .total-space {
		// 	margin-left: 41rpx;
		// }
	}

	.btn-download {
		padding: 0 20rpx;
		border: 1px solid #00B38B;
		height: 60rpx;
		line-height: 60rpx;
		border-radius: 30rpx;
		font-size: 28rpx;
		color: #00B38B;
	}

	.list-wrap {
		margin: 30rpx;
		padding-bottom: 20rpx;
	}

	.card-info {
		background: #FFFFFF;
		box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
		border-radius: 20rpx;
		margin-bottom: 30rpx;
		padding: 30rpx 20rpx 10rpx 20rpx;

		.info-wrap {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #dcdcdc;
			padding-bottom: 30rpx;

			.info-left-text {
				margin-bottom: 15rpx;
			}

			.label {
				color: #999999;
				font-size: 26rpx;
				font-weight: 400;
			}

			.val {
				color: #333333;
				font-size: 26rpx;
				font-weight: 400;
			}

			.price-label {
				color: #999999;
				font-size: 24rpx;
				font-weight: 400;
				margin-bottom: 22rpx;
				text-align: center;
			}

			.price-val-text {
				font-size: 32rpx;
				font-weight: 700;
				color: #333333;
				margin-left: 9rpx;
			}

			.price-val {
				font-size: 26rpx;
				color: #333333;
			}

		}

		.card-text-wrap {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin: 38rpx 0 46rpx 0;

			.text-box {
				display: flex;
				flex-direction: column;
				align-items: center;

				.name-text {
					font-size: 30rpx;
					font-weight: 400;
					color: #333333;
				}

				.time-text {
					font-size: 30rpx;
					color: #333333;
					font-weight: 400;
					margin-top: 8rpx;
				}

				.date-text {
					font-size: 24rpx;
					color: #999999;
					font-weight: 400;
					margin-top: 8rpx;
				}
			}

			.arrow-wrap {
				display: flex;
				flex-direction: column;
				align-items: center;

				.d-img {
					width: 186rpx;
					height: 12rpx;
				}

				.arrow-text {
					color: #666666;
					font-size: 26rpx;
				}
			}
		}

		.btn-wrap {
			margin-top: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.status-wrap {
				color: #999999;
				font-size: 26rpx;
				margin-right: 23rpx;
			}

			.status-text {
				color: #00B38B;
			}

			.btn {
				background: #FFFFFF;
				border: 1px solid #00B38B;
				border-radius: 30rpx;
				line-height: 61rpx;
				text-align: center;
				color: #00B38B;
				font-size: 26rpx;
			}

			.btn-1 {
				width: 171rpx;
				height: 61rpx;
			}

			.btn-2 {
				width: 141rpx;
				height: 61rpx;
			}

			.btn-3 {
				width: 121rpx;
				height: 61rpx;
			}
		}
	}

	.finished .info-wrap,
	.finished .card-text-wrap {

		.price-val,
		.price-val-text,
		.val,
		.text-box .time-text,
		.text-box .name-text,
		.arrow-wrap .arrow-text {
			color: #999999;
		}
	}

	.bottom-line {
		text-align: center;
		margin: 30rpx 0;
	}
</style>