<template>
	<view class="contentAll">
		<view class="titleOne">
			<text @click="invoiceRecord">开票记录</text>
			<text class="line">|</text>
			<text @click="$util.navTo(`/subpackage/orders/invoiceApply/invoice-header-list?manage=1`,true)">管理抬头</text>
		</view>
		<view class="twoOne" v-if="state.type==1">
			<view class="item last">
				<text>开票状态：</text>
				<uni-data-select v-model="state.businessTypeValOne" :localdata="state.businessRangeOne"
					@change="changeBusinessOne"></uni-data-select>
			</view>
			<button size="mini" style="color: #ffffff;
				background-color: rgb(118, 200, 77);
				border-color: rgb(118, 200, 77);
				font-size: 26rpx;
				flex-shrink: 0;margin-left: 20rpx;" @click="searchOne()">查询</button>
		</view>
		<view class="two" v-if="state.type==2">
			<view class="input">
				ETC卡号：<input placeholder="请输入ETC卡号" v-model="state.etcCardNo" />
			</view>
			<view class="three">
				<view class="item last">
					<text>开票状态：</text>
					<uni-data-select v-model="state.businessTypeVal" :localdata="state.businessRange"
						@change="changeBusiness"></uni-data-select>
				</view>
				<button size="mini" style="color: #ffffff;
					background-color: rgb(118, 200, 77);
					border-color: rgb(118, 200, 77);
					font-size: 26rpx;
					flex-shrink: 0;margin-left: 20rpx;" @click="searchTwo">查询</button>
			</view>

		</view>
		<view>
			<view class="cardbox">
				<view class="card" v-for="(item,i) in state.data" :key="i">
					<view>
						<checkbox-group @change="checkboxChange(item)">
							<checkbox :value="item.orderNo" :checked="state.checked" style="transform:scale(0.7)" />
						</checkbox-group>
					</view>
					<view class="content">
						<view class="title">订单号：{{item.orderNo}}</view>
						<view class="title">订单时间：{{item.orderTime}}</view>
						<view class="title" v-if="item.productType">订单类型：{{item.productType}}</view>
						<view class="title">车牌号：{{item.vehiclePlate}}</view>
						<view class="title">订单金额：￥{{item.invoiceAmount}}</view>
						<view class="title">开票状态：{{item.status==1?'已开票':"可开票"}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
	<view class="bottom-line" v-if="state.flags">我是有底线的~~~</view>
	<view class="apply-wrap">
		<view>
			<checkbox-group @change="checkboxAll">
				<checkbox value="ALL" style="transform:scale(0.7)" :checked="state.checkedAll" />全选
			</checkbox-group>

		</view>
		<view class="apply" @click="apply">去申请</view>
	</view>

</template>

<script lang="ts" setup>
	import {
		navTo, msg
	} from "@/utils/utils";
	import { reactive, ref } from "vue";
	import { onLoad, onReachBottom} from "@dcloudio/uni-app";
	import { billQueryProductOrder, getUserMsg, passageFlowingWaterOrderPageQuery } from "@/utils/network/api.js";
	import {requestNew } from "@/utils/network/request.js";
	import {
		getItem
	} from "@/utils/storage";
	const state = reactive({
		type: 1,  //1 ETC产品订单  2通行流水服务费订单
		userMobile: "",
		data: [],
		businessTypeValOne: 9,
		businessRangeOne: [{
			'value': 9,
			'text': '可开票'
		}, {
			'value': 1,
			'text': '已开票'
		}],
		businessTypeVal: 9,
		businessRange: [{
			'value': 9,
			'text': '可开票'
		}, {
			'value': 1,
			'text': '已开票'
		}, {
			'value': 2,
			'text': '已开纸质票'
		}],
		etcCardNo: "",
		pageSize: 10,  //每页数据量
		pageNo: 1,	// 当前页
		ordersArr: [],
		checked: false,
		checkedAll: false,
		flags: false,
		customerIdNum: ""
	})
	onLoad((option) => {
		state.type = option.type
		getUserinfo()
	})
	const value = ref('')
	const getUserinfo = () => {
		const options = {
			type: 2,
			data: {
				openId: getItem('openId'),
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(getUserMsg, options).then((res) => {
			const data = res;
			console.log(data, "用户信息");
			state.userMobile = data.customerTel
			state.customerIdNum = data.customerIdNum
			if (state.type == 1) {
				etcOrder(1)
			}
		});
	}
	const etcOrder = (val) => {
		if (val == 1) {
			state.pageNo = 1
		}
		if (state.pageNo == 1 && state.data.length > 0) {
			state.data = []
		}
		const options = {
			type: 2,
			data: {
				userMobile: state.userMobile,
				page: state.pageNo,
				pageSize: state.pageSize,
				status: state.businessTypeValOne
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(billQueryProductOrder, options).then((res) => {
			const data = res;
			state.data = data.orders
			console.log(data, "查询ETC产品订单");
		});
	}
	const trafficFlow = (val) => {
		if (val == 1) {
			state.pageNo = 1
		}
		if (state.pageNo == 1 && state.data.length > 0) {
			state.data = []
		}
		const options = {
			type: 2,
			data: {
				etcCardNo: state.etcCardNo,
				userMobile: state.userMobile,
				pageNumber: state.pageNo,
				page: state.pageSize,
				status: state.businessTypeVal
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(passageFlowingWaterOrderPageQuery, options).then((res) => {
			const data = res;
			state.data = data.orders
			console.log(data, "通行");
		});
	}
	const changeBusiness = (e) => {
		state.businessTypeVal = e
		console.log(e)
	}
	const changeBusinessOne = (e) => {
		state.businessTypeValOne = e
		console.log(e)
	}
	const apply = () => {
		const params = encodeURIComponent(JSON.stringify(state.ordersArr))
		if (state.ordersArr.length >= 1) {
			navTo(`/subpackage/orders/invoiceApply/invoice-step1?type=${state.type}&&ordersArr=${params}&&userMobile=${state.userMobile}&&customerIdNum=${state.customerIdNum}`)
		} else {
			msg("请选择订单开票")
		}
	}
	const searchOne = () => {
		state.pageNo = 1
		etcOrder(2)
	}
	const searchTwo = () => {
		state.pageNo = 1
		trafficFlow(1)
	}
	const invoiceRecord = () => {
		navTo(`/subpackage/orders/invoiceApply/invoiceList?userMobile=${state.userMobile}`)
	}
	const checkboxChange = (item) => {

		var items = state.data,
			values = item.orderNo;
		for (var i = 0, lenI = items.length; i < lenI; ++i) {
			console.log("values", values, items[i].orderNo)
			if (values.includes(items[i].orderNo)) {
				items[i]['checked'] = !items[i]['checked']
			}
		}
		state.ordersArr = []
		for (var k = 0; k < state.data.length; k++) {
			if (state.data[k]['checked']) {
				if (state.data[k]['checked'] == true) {
					state.ordersArr.push(state.data[k])
				}
			}
		}
		if (state.ordersArr.length == state.data.length) {
			console.log("进来了")
			state.checkedAll = true
		} else {
			state.checkedAll = false
		}
		console.log("e", state.ordersArr)
	}
	const checkboxAll = (e) => {
		if (e.detail.value.toString() == 'ALL') {
			for (var i = 0; i < state.data.length; i++) {
				state.ordersArr.push(state.data[i])
				state.checkedAll = true
				state.checked = true
			}
		} else {
			state.ordersArr = []
			state.checked = false
		}
		console.log("全部", e.detail.value)
	}
	// 触底加载
	onReachBottom(() => {
		if (state.data.length < state.pageNo * 10) return state.flags = true
		console.log("触底了")
		state.pageNo++
		if (state.type == 1) {
			etcOrder(2)
		} else {
			trafficFlow(2)
		}
	})
</script>

<style scoped lang="scss">
	.contentAll {
		font-size: 32rpx;
		background: #eef7f7;
		width: 100%;
		min-height: 100vh;
	}

	.titleOne {
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		background-color: white;
		padding: 20rpx;
	}

	.line {
		color: #ccc;
	}

	.cardbox {
		width: 95%;
		margin: 0 auto;
		margin-top: 20rpx;

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

			.content {
				margin-left: 30rpx;

				.title {
					font-size: 30rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #333333;
					margin-bottom: 15rpx;
				}

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

			.icon-active {
				width: 43rpx;
				height: 43rpx;
				background: #00B38B;
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.icon-no-active {
				width: 44rpx;
				height: 44rpx;
				background: #FFFFFF;
				border: 1px solid #00B38B;
				border-radius: 50%;
			}

			.edit {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;


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

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

	.apply {
		width: 150rpx;
		height: 70rpx;
		background: linear-gradient(-90deg, #43A1E0 0%, #13E7C1 100%);
		border-radius: 40rpx;
		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 70rpx;
		text-align: center;

	}

	.apply-wrap {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: white;
		font-family: Noto Sans S Chinese;
		font-weight: 400;
		display: flex;
		align-items: center;
		height: 100rpx;
		padding: 0 20rpx;
		justify-content: space-between;
		box-sizing: border-box;
	}

	::deep.last .uni-stat__select {
		width: 360rpx !important;
	}

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

	.twoOne {
		background-color: white;
		margin-top: 10rpx;
		padding: 20rpx;
		display: flex;
		align-items: center;
	}

	.three {
		display: flex;
		align-items: center;
	}

	.two {
		background-color: white;
		margin-top: 10rpx;
		padding: 20rpx;

	}

	.input {
		display: flex;
		align-items: center;
		padding-left: 10prx;
	}

	.input>input {
		width: 45%;
		margin-top: 10rpx;
		font-size: 30rpx;
		border: 1rpx solid #ccc;
		padding: 0 10rpx;
		height: 28rpx;
		line-height: 28rpx;
	}
</style>