<template>
	<view class="page">
		<u-navbar title="账户明细" :border-bottom="false" :background="bgc" title-color='#000' title-size='36' height='45'
			back-icon-color='#000'></u-navbar>

		<view class="account_box">
			<view class="li_box">
				<view class="txt">
					账户总余额
				</view>
				<view class="num">
					{{info.balance}}元
				</view>
			</view>	
			<view class="li_box">
				<view class="txt">
					待结算金额
				</view>
				<view class="num">
					{{info.settlementAmount}}元
				</view>
			</view>
		
		</view>
		<view class="ipt_box">
			<view class="ipt">
				<u-input v-model="amount" :border="border" placeholder='请输入提现金额'
					placeholder-style="font-size: 32rpx;color: #A6A6A6;" @input="ckinpt()" />
			</view>

			<view class="txt" style="margin-left: auto;margin-right: 20rpx;">
				元
			</view>
		</view>
		<view class="" style="margin: 0 auto;
			margin-top: 38rpx;
			width: 676rpx;color: #ccc;" >
			可提现金额:{{info.withdrawableAmount}}元
		</view>
		<view class="info_card">
			<view class="info_li">
				<view class="text">提现金额:{{price}}元</view>
				<view class="txt">

				</view>
			</view>

			<view class="info_li">
				<view class="text">实际到账:{{realprice}}元</view>
				<!-- <view class="txt" @click="showtip=!showtip">
					查看明细 <u-icon name="arrow-down" v-show="showtip"></u-icon>  <u-icon name="arrow-up" v-show="showtip==false"></u-icon>
				</view> -->
			</view>
			<view class="info_li">
				<view class="text">提现手续费:{{Quantity}}元</view>
				<!-- <view class="txt">
					(单价{{eleprice}}元/度)
				</view> -->
			</view>
			<view class="tip" v-if="showtip">
				<view class="tip_txt">
					手续费:{{xsf}}
				</view>
			</view>

		</view>
		<view class="patBtn" @click="payBtn()">
			立即提现
		</view>
		<view class="licot">
			<view class="li_box" v-for="(item,index) in list" :key="index">
				<view class="box_top">
					<view class="top_left">
						{{status(item.busType)}}
					</view>
					<view class="top_right" v-if="item.type==1">
						+{{item.operatorDividend}}
					</view>
					<view class="top_right" v-if="item.type==2" style="color: #3D3D3D;">
						{{item.operatorDividend}}
					</view>
				</view>
				<view class="box_bot">
					<view class="bot_left">
						{{formatDate(item.createTime)}}
					</view>
					<view class="bot_right">
						余额{{item.operatorBalance}}
					</view>
				</view>
			</view>

		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				showtip: false,
				areaId: null,
				amount: '',
				list: [],
				pageNum: 1,
				pageSize: 25,
				info: {},
				price: 0,
				realprice: 0,
				Quantity: 0,
				border: false,
				isThrottled: false,
				moneyInfo:{},
				ownerInfo:{},
				imglist:[],
				token: '',
				upurl:''
			}
		},
		onLoad(e) {


		},
		onShow() {
			if (uni.getStorageSync('adminAreaid')) {
				this.areaId = uni.getStorageSync('adminAreaid')
				
				
			}
			
			this.getUserInfo()
		},
		onReachBottom() {
			// 触底时增加 pageNum 并调用 getlist 获取更多数据
			this.pageNum++;
			this.getlist();
		},
		methods: {
			getUserInfo(){
				this.$u.get(`/getAppInfo?`).then((res) => {
					if (res.code == 200) {
						this.userInfo=res.user
						this.getowner()
						this.getareaInfo()
					} else {
					
					}
				});
			},
			getowner(){
				this.$u.get('/appVerify/ownerType'	).then((res) => {
					if (res.code == 200) {
						this.ownerInfo=res.data
						console.log(this.ownerInfo,'ownerTypownerTypownerTypownerTyp');
						this.getlist()
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				});
			},
			ckinpt() {

				if (parseFloat(this.amount) > parseFloat(this.info.withdrawableAmount)) {

					uni.showToast({
						title: '提现金额不能大于可提现金额',
						icon: 'none',
						duration: 2000
					});
					this.amount = ''
				}
				const floatValue = parseFloat(this.amount);
				const decimalPart = this.amount.split('.')[1];
				if (decimalPart && decimalPart.length > 2) {
					// 如果小数点后三位之后存在值,则截取小数点后三位之前的部分
					uni.showToast({
						title: '金额只能精确到小数点后两位',
						icon: 'none',
						duration: 2000
					});
					this.amount = floatValue.toFixed(2).slice(0, -1);
				}

				// 调用 checkPrice() 方法
				this.checkPrice();
			},
			checkPrice() {
				if (this.amount != '') {
					this.price = parseFloat(this.amount);
				} else {
					this.price = 0.00
				}


				
			
				if(this.info.handlingChargeType==1){
					let abb = this.amount * (parseFloat(this.info.withdrawHandlingCharge) / 1000)
					this.Quantity = abb.toFixed(2)
				}else{
					this.Quantity = parseFloat(this.info.withdrawHandlingCharge).toFixed(2)
				}
				let aaa = this.amount - this.Quantity
				
				this.realprice = aaa.toFixed(2)
			},
			
			status(type) {
				if (type == 1) {
					return '骑行订单'
				} else if (type == 2) {
					return '押金订单'
				} else if (type == 3) {
					return '押金订单退款'
				} else if (type == 4) {
					return '骑行订单退款'
				} else if (type == 5) {
					return '提现'
				} else if (type == 6) {
					return '提现失败'
				} else if (type == 7) {
					return '车损收入'
				}else if (type == 8) {
					return '短信扣费'
				}

			},
			formatDate(value) {
				if (!value) return '';
				let date = new Date(value);
				let month = date.getMonth() + 1; // 月份从0开始计数,所以加1
				let day = date.getDate();
				let hours = date.getHours();
				let minutes = date.getMinutes();

				// 确保月份、日期、小时和分钟都是两位数
				month = month < 10 ? '0' + month : month;
				day = day < 10 ? '0' + day : day;
				hours = hours < 10 ? '0' + hours : hours;
				minutes = minutes < 10 ? '0' + minutes : minutes;

				return `${month}-${day} ${hours}:${minutes}`;
			},
			
			
			payBtn() {
				if(this.info.collectionCode==null){
					
					uni.showToast({
						title: '请上传收款二维码',
						icon: 'none',
						duration: 1000
					});
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages_admin/addcode?deptId='+this.info.deptId
						})
					},1100)
				}else{
					if (this.price != 0) {
						// 检查是否已经在等待三秒的时间内
						if (this.isThrottled) {
							uni.showToast({
								title: '请勿重复点击',
								icon: 'none',
								duration: 2000
							});
							return;
						}
						
						// 设置节流标志位
						this.isThrottled = true;
						
						if (parseFloat(this.price) > parseFloat(this.info.withdrawableAmount)) {
						
							uni.showToast({
								title: '提现金额不能大于可提现金额',
								icon: 'none',
								duration: 2000
							});
							
						}
						let data = {
							amount: this.price,
							
						}
						if(this.userInfo.role!=4){
							data.areaId= this.areaId
							
						}
						this.$u.post('/appVerify/admin/withdraw', data).then((res) => {
							if (res.code == 200) {
								this.amount = ''
								this.Quantity = 0
								this.realprice = 0
								this.price = 0
								this.list = []
								this.pageNum = 1
								this.getareaInfo()
								this.getlist()
								uni.showToast({
									title: '提现成功',
									icon: 'none',
									duration: 2000
								});
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								});
							}
						}).catch(error => {
							console.error("Error fetching area data:", error);
						}).finally(() => {
							// 三秒后解除节流
							setTimeout(() => {
								this.isThrottled = false;
							}, 3000);
						});
					} else {
						uni.showToast({
							title: '提现金额不能为0',
							icon: 'none',
							duration: 2000
						});
					}
				}
				

			},
			getareaInfo() {
				let data = {
					areaId: this.areaId
				}
				if(this.userInfo.role==4){
					
					this.$u.get('/appVerify/getDept', ).then((res) => {
						if (res.code == 200) {
							this.info = res.data
						}
					}).catch(error => {
						console.error("Error fetching area data:", error);
					});	
				}else{
					
					this.$u.get('/appVerify/getDept?', data).then((res) => {
						if (res.code == 200) {
							this.info = res.data
						}
					}).catch(error => {
						console.error("Error fetching area data:", error);
					});	
				}
				
				this.$u.get('index/statistics?', data).then((res) => {
					if (res.code == 200) {
						this.moneyInfo = res.data
						
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				});
			},
			getlist() {
				let data = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					ownerId: this.ownerInfo.ownerId,
					ownerType: this.ownerInfo.ownerType,
					
				};
				
				if( this.ownerInfo.ownerId==1&&this.userInfo.role!=4){
					data.areaId=this.areaId
				}
				if(this.userInfo.role==4){
					data.ownerType=2
				}
				this.$u.get('/appVerify/flowList?', data).then((res) => {
					if (res.code == 200) {
						// 确保合并新获取的数据
						this.list = this.list.concat(res.rows);
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				});
			}
		}
	}
</script>
<style lang="scss">
	page {
		background-color: #F7FAFE;
	}

	.page {

		// width: 750rpx;
		.licot {
			margin-top: 38rpx;
			display: flex;
			flex-wrap: wrap;
			width: 100%;

			.li_box {

				padding: 38rpx 28rpx;
				width: 100%;
				display: flex;
				flex-wrap: wrap;
				border-bottom: #ccc 2rpx solid;
				background: #FFFFFF;

				.box_top {
					width: 100%;
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;

					.top_left {
						font-weight: 400;
						font-size: 32rpx;
						color: #3D3D3D;
					}

					.top_right {
						font-weight: 400;
						font-size: 32rpx;
						color: #12D2AC;
					}
				}

				.box_bot {
					margin-top: 12rpx;
					width: 100%;
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;

					.bot_left {
						font-weight: 400;
						font-size: 28rpx;
						color: #909090;
					}

					.bot_right {
						font-weight: 400;
						font-size: 28rpx;
						color: #909090;
					}
				}
			}

		}

		.account_box {
			padding: 38rpx;
			margin: 0 auto;
			margin-top: 38rpx;
			width: 676rpx;
			height: 196rpx;
			background: #12D2AC;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			justify-content: space-between;
			.li_box{
				.txt {
					width: 100%;
					font-weight: 400;
					font-size: 32rpx;
					color: #FFFFFF;
				}
				
				.num {
					margin-top: 10rpx;
					width: 100%;
					font-weight: 400;
					font-size: 48rpx;
					color: #FFFFFF;
				}
			}
			
		}

		.patBtn {
			margin: 0 auto;
			margin-top: 38rpx;
			// position: fixed;
			// bottom: 100rpx;
			// left: 80rpx;
			// margin-top: 152rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 590rpx;
			height: 84rpx;
			background: #12D2AC;
			border-radius: 50rpx;
			font-weight: 400;
			font-size: 36rpx;
			color: #FFFFFF
		}

		.info_card {
			margin: 0 auto;
			margin-top: 38rpx;
			// margin-top: 46rpx;
			padding: 20rpx 28rpx 40rpx 36rpx;
			width: 676rpx;
			// height: 266rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
			display: flex;
			flex-wrap: wrap;

			.info_li {
				margin-top: 30rpx;
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.text {
					font-weight: 400;
					font-size: 28rpx;
					color: #383838;
				}

				.txt {
					font-weight: 400;
					font-size: 20rpx;
					color: #383838;
				}
			}

			.tip {
				padding-top: 15rpx;
				display: flex;
				align-items: center;
				width: 576rpx;
				height: 70rpx;
				background-image: url('https://api.ccttiot.com/smartmeter/img/static/uGcjoIs6xOPFWN6mJT8l');
				background-size: 100% 100%;
				background-repeat: no-repeat;

				.tip_txt {

					margin-left: 38rpx;
					font-weight: 400;
					font-size: 20rpx;
					color: #3D3D3D;
				}
			}
		}

		.ipt_box {
			margin: 0 auto;
			margin-top: 50rpx;
			width: 676rpx;
			height: 122rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin-top: 40rpx;
			display: flex;
			align-items: center;
			font-weight: 400;
			font-size: 32rpx;
			color: #A6A6A6;

			.ipt {
				margin-left: 20rpx;
				margin-right: 10rpx;

				/deep/.u-input__input {
					font-size: 32rpx !important;
					color: #A6A6A6 !important;
				}
			}

		}
	}
</style>