<template>
	<view class="page">
		<view class="search-box">
			<view>
				<u-input v-model="value" type="select" @click="show = true" style="background-color: #fff;"
					placeholder="-" :border="false" />
				<u-select v-model="show" :list="list1" @confirm="confirm($event)"></u-select>
			</view>

		</view>
		<view class="search-total">
			<view class="left">
				<view class="price-total"><u-icon name="rmb" style="font-size: 24rpx;" />{{yingPayAll}}</view>
				<view>（当前租金欠费金额)</view>
			</view>
			<div class="right" @click='toDetail'>
				<view>缴费记录 <u-icon name="arrow-right" size="28" /></view>
			</div>

		</view>
		<view style="text-align: center;color:#0EBB5A;padding: 30rpx 0;">{{etTimePeriodDesc(payNum,dayMsg)}}为付款日</view>
		<!-- 记录列表 -->
		<view class="record-list">
			<view class="record-list-box" v-for="(item,index) in recordList" :key="index">
				<u-checkbox-group class="list-group" style="width: 100%;">
					<view class="list">
						<u-checkbox v-model="recordList[index].gouxuan" shape="circle" :label-disabled="false"
							active-color="#0EBB5A" @change="xuanAll(item,$event)" />

						<view class="list-item">
							<view class="list-info">
								<view class="info-items">
									<view class="info-title">{{formatTime(payNum,item.tenementOrderDate)}}应缴租金：</view>
									<view class="price"><u-icon name="rmb"
											style="font-size: 24rpx;" />{{item.paymentAmount}}</view>
								</view>
							</view>
						</view>
					</view>

				</u-checkbox-group>
			</view>
		</view>
		<view style="height: 120rpx;"></view>
		<view class="page-footer">
			<view class="footer-buy">
				<u-checkbox-group>
					<u-checkbox shape="circle" @change="quanxuan" v-model="showAll"
						active-color="#0EBB5A">全选</u-checkbox>
				</u-checkbox-group>
				<view class="cart-add">
					合计：<view class="price"><u-icon name="rmb" style="font-size: 24rpx;" />{{payAll}}</view>
				</view>
				<u-button type="success" class="buttons" @click="joinShow" size="mini">去支付</u-button>
			</view>
		</view>
		<!-- <u-loading :show="true" size="60" style="position: absolute; top:80%;left: ;" color="red"></u-loading> -->
	</view>

</template>

<script>
	export default {
		data() {
			return {
				payNum: 1,
				dayMsg: '',
				show: false,
				payAll: 0,
				yingPayAll: 0,
				value: '',
				list1: [],
				checked: [],
				recordList: [],
				loadShow: false,
				showAll: false,
				ListIndex: 0,

			};
		},
		watch: {
			recordList: {
				deep: true,
				handler(newVal) {
					const allTrue = this.recordList.every(item => item.gouxuan)
					this.showAll = allTrue
				}
			}
		},
		onShow() {
			const isLogin = uni.getStorageSync('token');
			if (isLogin == '') {
				// 如果未登录，跳转到登录页面
				uni.navigateTo({
					url: '/pages/login/login'
				})
			} else {

				this.$http.sendRequest('/cerePlatformMerchant/queryByPage', 'POST', {
					pageSize: 10,
					pageNumber: 0,
					phone: uni.getStorageSync('user').phone
				}).then(res => {

					if (res.data.code == '20004' || res.data.code == '20003') {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					} else {

						this.$http.sendRequest('/cerePropertyOrder/queryByName', 'POST', {
							relatedMerchants: res.data.data.content[0].id
						}, 1).then(res => {
							if (res.data.data.length > 0) {
								const listNew = []
								res.data.data.map((item, index) => {

									const newObj = {
										label: item.shopName,
										value: index,
										list: []
									}
									newObj.list = item.cerePropertyOrderList

									listNew.push(newObj)

								})

								this.list1 = listNew

								this.value = res.data.data[0].shopName
								this.ListIndex = 0
								const redList = []
								this.payAll = 0
								this.yingPayAll = 0
								res.data.data[0].cerePropertyOrderList.map(item => {
									if (item.payState == 0) {
										this.yingPayAll += item.paymentAmount
										this.payNum = item.payType
										this.dayMsg = item.payDay
										const zujin = {
											...item,
											showItem: false,
											gouxuan: false,

										}
										redList.push(zujin)
									}
								})
								this.recordList = redList


							}
						})
					}
				})
			}

		},
		methods: {
			quanxuan(e) {
				this.payAll = 0
				const allTrue = this.recordList.every(item => item.gouxuan)
				if (allTrue) {
					this.showAll = !this.showAll
					this.recordList.forEach(item => {
						item.gouxuan = !allTrue;
					});
				} else {
					this.showAll = true
					this.recordList.forEach(item => {
						item.gouxuan = !allTrue;
						this.payAll += item.paymentAmount
					});
				}


			},
			xuanAll(item, e) {
				if (e.value) {
					this.payAll += item.paymentAmount
				} else {
					this.payAll -= item.paymentAmount
				}
			},
			recordXq(item) {
				const encodedItem = encodeURIComponent(JSON.stringify(item))
				uni.navigateTo({
					url: `/pages/accepting/accepting?item=${encodedItem}`,
				})
			},
			toDetail() {
				const msg = JSON.stringify(this.list1[this.ListIndex])
				uni.navigateTo({
					url: `/pagesA/rentPay/rentPayDetails?msg=${msg}`
				})
			},
			joinShow() {
				if (this.payAll == 0) {
					uni.showToast({
						icon: 'error',
						title: '请选择支付订单'
					})
				} else {

					let payArr = []
					this.recordList.map(item => {
						if (item.gouxuan) {
							payArr.push(item)
						}
					})
					const msg = JSON.stringify(payArr)
					uni.navigateTo({
						url: `/pagesA/rentPay/goPay?msg=${msg}`
					})
				}
			},
			confirm(e) {

				let arr = []
				this.value = e[0].label
				const redList = []
				let index = e[0].value
				this.ListIndex = e[0].value
				this.yingPayAll = 0
				this.payAll = 0
				this.list1[index].list.map(item => {
					this.yingPayAll += item.paymentAmount
					this.payNum = item.payType
					this.dayMsg = item.payDay
					const zujin = {
						...item,
						showItem: false,
						gouxuan: false,
					}
					redList.push(zujin)
				})
				this.recordList = redList

			},
			etTimePeriodDesc(num, day) {
				switch (num) {
					case 1:
						return '每日';
					case 2:
						return `每月${day}`;
					case 3:
						return `每季度${day}`;
					case 4:
						return `每年${day}`;
					default:
						return '';
				}
			},
			formatTime(type, time) {
				// 将传入的时间字符串转换为 Date 对象
				const date = new Date(time);

				const day = date.getDate();
				const month = date.getMonth() + 1;
				const quarter = Math.floor((month - 1) / 3) + 1;
				const year = date.getFullYear();
				switch (type) {
					case 1:
						return `${day}号`;
					case 2:
						return `${month}月${day}号`;
					case 3:
						return `${month}月${day}号`;
					case 4:
						return `${year}年`;
					default:
						return time;
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'rentPay.scss';
</style>