<template>
	<view class="wrap">
		<view>
			<view class="item last">
				<text>状态：</text>
				<uni-data-select v-model="state.status" :localdata="state.statusData"
					@change="statusChange"></uni-data-select>
			</view>
			<view class="item last">
				<text>订单类型：</text>
				<uni-data-select v-model="state.orderType" :localdata="state.orderTypeData"
					@change="orderTypeChange"></uni-data-select>
				<button size="mini" style="color: #ffffff;
						backgroundColor: rgb(118, 200, 77);
						borderColor: rgb(118, 200, 77);
						font-size: 26rpx;
						flex-shrink: 0;margin-left: 20rpx;" @click="search">查询</button>
			</view>
		</view>
	</view>

	<view class="listbox" v-if="state.data.length>0">
		<view class="card-no-active" v-for="(item,index) in state.data">
			<view class="content">
				<view class="row">
					订单类型：<text>{{item.invoiceTypeName}}</text>
				</view>
				<view class="row">
					车牌号：<text>{{item.remarks}}</text>
				</view>
				<view class="row">
					发票抬头：<text>{{item.buyerName}}</text>
				</view>
				<view class="row">
					状态：<text>{{item.status==1?'正常':"红冲"}}</text>
				</view>
				<view class="row">
					商品价：<text>￥{{item.goodsAmount}}</text>
				</view>
				<view class="row">
					开票日期：<text>{{item.invoiceOpenTime}}</text>
				</view>
			</view>
			<view class="btns">
				<button @click="down(item.fileUrl)">下载</button>
			</view>
		</view>
		<!-- <view class="bottom-line" v-if="state.flags">我是有底线的~~~</view> -->
	</view>
	<view v-else class="listbox" style="text-align: center;font-size: 32rpx;">
		暂无数据
	</view>
</template>

<script setup lang="ts">
	import {
		reactive
	} from "vue";
	import { onLoad, onReachBottom } from "@dcloudio/uni-app";
	import { billQueryBill } from "@/utils/network/api.js";
	import { requestNew } from "@/utils/network/request.js";
	const state = reactive({
		userMobile: "",
		pageNumber: 1,
		pageSize: 10,
		status: 1,
		statusData: [{
			'value': 1,
			'text': '正常'
		}, {
			'value': -1,
			'text': '红冲'
		}],
		orderType: 1,
		orderTypeData: [{
			'value': 1,
			'text': 'ETC服务'
		}, {
			'value': 2,
			'text': '通行服务费'
		}],
		data: [],
		flags: false,
	})
	onLoad((options) => {
		state.userMobile = options.userMobile
		query(1)
	})
	const search = () => {
		query(1)
	}
	const query = (val) => {
		if (val == 1) {
			state.pageNumber = 1
		}
		console.log("state.data",state.data)
		if (state.pageNumber == 1 && state.data.length > 0) {
			state.data = []
		}
		const options = {
			type: 2,
			data: {
				userMobile: state.userMobile,
				pageNumber: state.pageNumber,
				pageSize: state.pageSize,
				status: state.status,
				orderType: state.orderType,
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(billQueryBill, options).then((res) => {
			const data = res;
			// 有数据才赋值
			if(Boolean(data.invoices)){
				state.data = data.invoices
			}
			console.log(data, "通行");
		});
	}
	const statusChange = (e) => {
		state.status = e
	}
	const orderTypeChange = (e) => {
		state.orderType = e
	}
	// 触底加载
	onReachBottom(() => {
		if (state.data.length < state.pageNumber * 10) return state.flags = true
		console.log("触底了")
		state.pageNumber++
		query(2)
	})
	const down = (fileUrl) => {
		uni.downloadFile({
			url: fileUrl, //仅为示例，并非真实的资源
			success: (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('打开文档成功');
								}
							});
						}
					});
				}
			}
		});

	}
</script>


<style>
	page {
		width: 100%;
		height: 100%;
	}
</style>

<style lang="scss" scoped>
	.listbox {
		height: 100%;
		background-color: #EEF7F7;
		padding: 30rpx;
		border-top: 1rpx solid #DFDFDF;
		margin-top: 200rpx;

		.card-no-active {
			margin-bottom: 30rpx;
			height: 438rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;

			.head {
				display: flex;
				padding: 30rpx;
				align-items: center;
				border-bottom: 1px solid #DCDCDC;

				image {
					width: 48rpx;
					height: 48rpx;
				}

				text {
					font-size: 30rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #333333;
				}
			}

			.content {
				margin: 30rpx;
				margin-bottom: 0;
				border-bottom: 1rpx solid #DCDCDC;

				.row {
					font-size: 26rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #999999;
					margin-bottom: 20rpx;

					text {
						font-size: 26rpx;
						font-family: Noto Sans S Chinese;
						font-weight: 400;
						color: #333333;
					}
				}
			}

			.btns {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				padding: 0 30rpx;

				button {
					margin: 0;
					display: inline-block;
					height: 61rpx;
					background: #FFFFFF;
					border: 1px solid #00B38B;
					border-radius: 30rpx;
					font-size: 26rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #00B38B;
					line-height: 61rpx;
				}
			}
		}


		.card-active {
			height: 375rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;

			.head {
				display: flex;
				padding: 30rpx;
				align-items: center;
				border-bottom: 1px solid #DCDCDC;

				image {
					width: 48rpx;
					height: 48rpx;
				}

				text {
					font-size: 30rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #333333;
				}

				.status {
					position: absolute;
					right: 35px;
					font-size: 26rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #777777;
				}
			}

			.content {
				margin: 30rpx;

				.row {
					font-size: 26rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #999999;
					margin-bottom: 20rpx;

					text {
						font-size: 26rpx;
						font-family: Noto Sans S Chinese;
						font-weight: 400;
						color: #333333;
					}
				}
			}

		}
	}

	.item {
		display: flex;
		font-size: 30rpx;
		margin-top: 10rpx;
		align-items: center;
	}

	.wrap {
		padding: 10rpx 20rpx;
		box-sizing: border-box;
		// display: flex;
		// justify-content: space-evenly;
		position: fixed;
		left: 0;
		top: 0;
		background-color: white;
		width: 100%;
	}

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