<template>
	<view class="center plr-25">

		<view class="flex-column br-12 fs-28 mt-30" style="background: white;">
			<text class="fs-32 fw-700 mt-30" style="text-align: left;padding-left: 30rpx;">物业费</text>
			<text class="fs-28 mt-30" style="text-align: left;padding-left: 30rpx;">房屋</text>
			<view class="flex-row-between mar-30 br-4 pad-25" style="background: #F2F2F2;">
				<text>{{userInfo.room}}</text>
			</view>
			<text class="fs-28 mt-30" style="text-align: left;padding-left: 30rpx;">房屋面积</text>
			<view class="flex-row-between mar-30 br-4 pad-25" style="background: #F2F2F2;">
				<text>{{userInfo.sqm}}m²</text>
			</view>
			<text class="fs-28 mt-30" style="text-align: left;padding-left: 30rpx;">缴费时长</text>
			<view class="flex-center-center mar-30 br-4" style="background: #F2F2F2;">
				<dateTimeQuantum @onConfirmStartTime="onConfirmStartTime" @onConfirmEndTime="onConfirmEndTime">
				</dateTimeQuantum>
			</view>
			<text class="fs-28 mt-30" style="text-align: left;padding-left: 30rpx;">缴费金额</text>
			<view class="flex-row-between mar-30 br-4 pad-25" style="background: #F2F2F2;">
				<text>￥{{money}}</text>
			</view>
		</view>

		<view class="fullscreen-btn" style="margin: 0;margin-top: 60rpx;" @click="onSubmit()">
			立即缴费
		</view>
		<u-picker :show="show" :columns="columns" closeOnClickOverlay="true" @cancel="close" @close="close"
			@confirm="confirm"></u-picker>
	</view>
</template>

<script>
	import dateTimeQuantum from "@/components/dateTimeQuantum"
	export default {
		data() {
			return {
				show: false,
				columns: [
					['1个月', '3个月', '6个月', '9个月', '12个月']
				],
				monthsNumber: '',
				userInfo: '',
				money: 0,
				startTime: '',
				endTime: '',
				dayNumber: 0,
			};
		},
		onLoad(options) {
			this.type = options.type
			this.getUserInfo()

		},
		components: {
			dateTimeQuantum
		},
		methods: {
			// 计算日期差值
			calculateDateDifference(startDate, endDate) {
				// 将日期字符串转为 Date 对象
				let start = new Date(startDate);
				let end = new Date(endDate);

				// 计算时间差（毫秒）
				let timeDifference = end - start;

				// 将毫秒差值转换为天数
				let daysDifference = timeDifference / (1000 * 3600 * 24);
				return daysDifference;
			},
			onConfirmStartTime(e) {
				this.startTime = e
				console.log(e)
			},
			onConfirmEndTime(e) {
				this.endTime = e
				console.log(e)
				let dayNumber = this.calculateDateDifference(this.startTime, this.endTime)
				this.money = parseFloat(dayNumber) * parseFloat(this.$wskj.isNull(this.userInfo.dayMoney) ? 0.01 : this
					.userInfo.dayMoney);
				this.money = this.money.toFixed(2);
				this.dayNumber = dayNumber;
				console.log(this.money)
			},
			open() {
				this.show = true
			},
			close() {
				this.show = false
			},
			confirm(e) {
				this.show = false
				this.monthsNumber = e.value[0]
				this.money = parseFloat(this.monthsNumber.replace('个月', '')) * parseFloat(this.userInfo.monthMoney);
				this.money = this.money.toFixed(2)
				console.log(this.money)
			},
			onSubmit() {
				if (this.$wskj.isNull(this.startTime)) {
					this.$wskj.toast('请选择开始时间')
					return
				}
				if (this.$wskj.isNull(this.endTime)) {
					this.$wskj.toast('请选择结束时间')
					return
				}
				this.onCjdd()
			},
			getUserInfo() {
				const params = {
					url: '/api/login/userInfo',
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					this.userInfo = res.data
				})
			},
			onCjdd() {
				const params = {
					url: '/api/liveBill/add',
					loadingTip: '',
					data: {
						money: this.money,
						type: 3, //类型（1电费 2水费 3物业费 4车位管理费 5车位出租费 6储藏室出租 7其他费用）
						duration: this.dayNumber, //缴费时长（日）
						startTime: this.startTime,
						endTime: this.endTime
					}
				}
				this.$request(params).then(res => {
					this.pay(res.data.billId)
				})
			},
			pay(orderId) {
				const params = {
					url: '/api/payRecord/pay',
					loadingTip: '',
					data: {
						orderId: orderId,
						amount: this.money,
						payBizCode: "PRESTORE",
					}
				}
				this.$request(params).then(res => {
					uni.requestPayment({
						provider: 'wxpay',
						appid: res.data.data.appId, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						nonceStr: res.data.data.nonceStr, // 随机字符串
						package: res.data.data.package, // 固定值
						timeStamp: res.data.data.timeStamp, // 时间戳（单位：秒）
						paySign: res.data.data.paySign, // 签名，这里用的 MD5/RSA 签名
						signType: 'MD5',
						success: (res) => {

						},
						fail: (err) => {
							console.log(err);
						}
					});
				})
			},
		}
	};
</script>

<style>
	.center {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #F2F2F2;
	}

	.grid-container {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around; // 可以根据需求调整元素的水平排列方式，这里是均匀分布
		align-items: center; // 垂直方向居中对齐
	}

	.grid-item {
		width: 32%;
		height: 80rpx; // 宫格高度
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background: #F2F2F2;
		border-radius: 12rpx;
		margin-top: 10rpx;
	}
</style>