<template>
	<view class="title">
		<view>
			<text :class="state.reimburseStatus=='0'?'status':'status1'" @click="getlist(0)">待补缴</text>
			<text :class="state.reimburseStatus=='1'?'status2':'status3'" @click="getlist(1)">已补缴</text>
		</view>
	</view>
	<view class="content">
		<view class="list" v-for="(item,index) in state.list" @click="state.reimburseStatus=='1'?details(item.id):''">
			<checkbox-group @change="checkboxGroupChange(item)">
				<label class="uni-list-cell uni-list-cell-pd" style="display: flex;">
					<checkbox :checked="item.checked" style="transform:scale(0.8);top: -12rpx;position: relative;" v-if="state.reimburseStatus=='0'"/>
					<view class="right-box">
						<view class="right-box-top">
							<view>
								<image :src="`${$imgUrl}arrears.png`" mode=""></image>
								<text>{{item.vehiclePlate}}</text>
							</view>
							<text class="money">¥{{item.reimburseFee/100}}</text>
						</view>
						<view class="right-box-bot">
							<view class="name">补缴原因：</view>
							<view class="details">{{item.reason}}</view>
						</view>
					</view>
				</label>
			</checkbox-group>
		</view>
	</view>
	<view class="pay_wrap" v-if="state.reimburseStatus=='0'">
		<checkbox-group  @change="checkboxChangeAll">
			<label class="uni-list-cell uni-list-cell-pd">
				<checkbox value="ALL" :checked="state.checkedAll" style="transform:scale(0.8)"/>合计 ￥{{allPrice/100}}
			</label>
		</checkbox-group>
		<view class="pay" @click="pay">支付</view>
	</view>
</template>

<script lang="ts" setup>
	import {
		ref,
		reactive
	} from "vue"
	import {
		onLoad,
		onPullDownRefresh
	} from "@dcloudio/uni-app";
	import {requestNew} from "@/utils/network/request.js";
	import {reimbursePage,reimburseUserPay} from "@/utils/network/api.js";
	import {navTo} from '@/utils/utils';
	const allPrice = ref(0)
	const state = reactive({
		reimburseStatus:"0",//0未补缴 1已补缴 2已关闭
		pageNo:1,
		pageSize:10,
		list:[],
		suppleNoList:[],//支付合集
	    checkedAll:false
	})
	onLoad(() => {
		getlist(state.reimburseStatus)
	})
	const checkboxGroupChange = (e) => {
		console.log(e,state.list);
		for(var k=0;k<state.list.length;k++){
			if (state.list[k]['orderId']==e.orderId) {
				state.list[k]['checked'] = !state.list[k]['checked']
			}
		}
		
		// 过滤数据
		let arr = []
		allPrice.value = 0
		
		state.list.forEach((val, index) => {
			if (val.checked) {
				arr.push(val.reimburseFee)
			}
		})
		console.log("arr",arr)
		// 计算价格
		arr.forEach(item => {
			allPrice.value += item
		})
		// 全选
	    if(arr.length==state.list.length){
			state.checkedAll=true
		}else{
			state.checkedAll=false
		}
		console.log(arr, "支付集合",allPrice.value);
	}
	const checkboxChangeAll = (e) => {
		console.log(e);
		if (e.detail.value.toString() == 'ALL') {
			for (var i = 0; i < state.list.length; i++) {
				state.checkedAll = true
				state.list[i].checked = true
				allPrice.value+=state.list[i].reimburseFee
			}
		} else {
			for (var i = 0; i < state.list.length; i++) {
				state.list[i].checked = false
			}
			allPrice.value=0
		}
		console.log(state.suppleNoList, "支付集合");
	}
	// 获取列表数据
	const getlist = (reimburseStatus) => {
		state.reimburseStatus=reimburseStatus
		const options = {
			type: 2,
			data: {
				reimburseStatus:reimburseStatus,//补缴单状态
				pageNo:state.pageNo,
				pageSize:state.pageSize
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(reimbursePage, options).then((res) => {
			let data=res.result
			for(var i=0;i<data.length;i++){
				data[i]['vehiclePlate']=data[i]['vehicleId'].split("_")[0]
				data[i]['checked']=false
			}
			state.list =data
			console.log("state.list",state.list);
		});
	}
	const pay=()=>{
		console.log(allPrice.value, "支付金额");
		// 选中得orderid
		let orderIds=[]
		for (var i = 0; i < state.list.length; i++) {
			if(state.list[i].checked){
				orderIds.push(state.list[i].orderId)
			}
		}
		const options = {
			type: 2,
			data: {
				orderIds:orderIds,//补缴申请单id
				reimburseFee:allPrice.value,//交易金额
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(reimburseUserPay, options).then((res) => {
			console.log("res",res);
			
		});
	}
	const details=(id)=>{
		navTo(`/subpackage/after-sale/arrears/arrears-details?id=${id}`)
	}
</script>

<style lang="scss">
	.title{
		width: 100%;
		height: 140rpx;
		background-color: white;
		position: fixed;
		left: 0;
		top: 0;
		display: flex;
		justify-content: center;
		line-height: 140rpx;
		.status{
			background: #CCB375;
			border-radius: 34rpx 0rpx 0rpx 34rpx;
			border: 1px solid #CCB375;
			color: white;
			width: 200rpx;
			height: 68rpx;
			font-size: 28rpx;
			line-height: 68rpx;
			display: inline-block;
			text-align: center;
		}
		.status1{
			border-radius: 34rpx 0rpx 0rpx 34rpx;
			border: 1px solid #CCB375;
			color: #CCB375;
			width: 200rpx;
			height: 68rpx;
			font-size: 28rpx;
			line-height: 68rpx;
			display: inline-block;
			text-align: center;
		}
		.status2{
			background:#CCB375;
			border-radius: 0rpx 34rpx 34rpx 0rpx;
			border: 1px solid #CCB375;
			font-size: 28rpx;
			color: white;
			width: 200rpx;
			height: 68rpx;
			font-size: 28rpx;
			line-height: 68rpx;
			display: inline-block;
			text-align: center;
		}
		.status3{
			border-radius: 0rpx 34rpx 34rpx 0rpx;
			border: 1px solid #CCB375;
			font-size: 28rpx;
			color: #CCB375;
			width: 200rpx;
			height: 68rpx;
			font-size: 28rpx;
			line-height: 68rpx;
			display: inline-block;
			text-align: center;
		}
	}
	.pay_wrap{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 128rpx;
		background: #FFFFFF;
		border-radius: 30rpx 30rpx 0rpx 0rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx;
		box-sizing: border-box;
		.pay{
			width: 210rpx;
			height: 80rpx;
			background: linear-gradient(90deg, #01253B 0%, #004576 100%);
			border-radius: 40rpx;
			font-size: 32rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 80rpx;
		}
	}
	.content{
		width: 90%;
		margin: 0 auto;
		margin-top: 170rpx;
		.list{
			background: #FFFFFF;
			border-radius: 12rpx;
			border: 1px solid #FFFFFF;
			padding: 20rpx;
			box-sizing: border-box;
			margin-bottom: 30rpx;
			.right-box{
				margin-left: 10rpx;
				width: 100%;
				.right-box-top{
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 2rpx solid #DCDCDC;
					padding-bottom: 18rpx;
					view{
						display: flex;
						align-items: center;
						image{
							width: 50rpx;
							height: 50rpx;
							margin-right: 10rpx;
						}
					}
					
					.money{
						color: #CE1717;
					}
				}
				.right-box-bot{
					font-size: 26rpx;
					color: #6C6C6C;
					display: flex;
					padding-top: 20rpx;
					.name{
						flex: 1;
					}
					.details{
						flex:3;
					}
				}
				
			}
		}
	}
</style>