<template>
	<view class="page">
		<u-navbar title="押金" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='45'></u-navbar>
		<view class="norecord" v-if="userinfo.balance==0">
			<view class="card">
				<view class="tit">充值押金</view>
				<view class="tit">还车后<span style="color: #1E807A ;">随时可退</span></view>
				<view class="tit" style="font-size: 32rpx;color: #808080;margin-top: 32rpx;">
					押金金额
				</view>
				<view class="tit"
					style="margin-top: 18rpx;font-weight: 700;font-size: 92rpx;color: #3D3D3D;line-height: 72rpx;"><span
						style="font-size: 52rpx;">¥</span>{{areaInfo.deposit}}</view>
			</view>
			<view class="card1">
				<view class="cont">
					<view class="yuan"></view>
					<view class="txt">
						电单车归还后并完成支付,可在【个人中心-押 金】申请押金退还
					</view>
				</view>
				<view class="cont">
					<view class="yuan"></view>
					<view class="txt">
						申请押金退还后预计1~3个工作日退还至原支付 账户,平均3分钟到账
					</view>
				</view>
			</view>
			<view class="buy_btn" @click="sub4">
				立即充值
			</view>
		</view>
		<view class="record" v-if="userinfo.balance!=0">
			<view class="card" style="padding-top: 80rpx;">
				<view class="tit"
					style="margin-top: 18rpx;font-weight: 700;font-size: 92rpx;color: #3D3D3D;line-height: 72rpx;"><span
						style="font-size: 52rpx;">¥</span>{{userinfo.balance}}</view>
				<view class="btn " v-if="false">
					充值
				</view>
				<view class="btn" @click="show=true">
					提现
				</view>
			</view>
			<view class="card2" style="padding-bottom: 30rpx;">
				<view class="card_top">
					<view class="left">
						<image src="https://api.ccttiot.com/smartmeter/img/static/uf9o0xbkFdfs5wRT7aLD" mode=""></image>
						押金变动明细
					</view>
					<view class="right">
						共{{total*2-1}}条
					</view>
				</view>
				<view class="cardcont">
					<view v-for="(item,index) in orderList" :key="index"
						style="display: flex;flex-wrap: wrap;width: 100%;">
						<view class="cont_li" v-if="item.etRefund.amount&&item.etRefund.refundResult=='SUCCESS'"
							style="display: flex;flex-wrap: wrap;width: 100%;">
							<view class="cont_li_left">
								<view class="txt">
									押金提现
								</view>
								<view class="data">
									{{item.etRefund.createTime}}
								</view>
							</view>
							<view class="cont_li_right">
								<view class="cont_price">
									-{{item.etRefund.amount}}
								</view>
								<view class="cont_data">
									提现成功
								</view>
							</view>
						</view>
						<view class="cont_li">
							<view class="cont_li_left">
								<view class="txt">
									押金充值
								</view>
								<view class="data">
									{{item.payTime}}
								</view>
							</view>
							<view class="cont_li_right">
								<view class="cont_price">
									+{{item.totalFee}}
								</view>
								<view class="cont_data">
									充值成功
								</view>
							</view>
						</view>

					</view>


				</view>
			</view>
		</view>
		<u-mask :show="showBack" @click="showBack = false" :z-index='100' duration='0' />
		<view class="pops" v-if="showBack">
			<view class="tit" style="font-weight: 600;">
				充值成功
			</view>
			<view class="cont_box" style="text-align: center;color: #808080;justify-content: center;font-size: 36rpx;">
				押金充值成功是否返回继续骑行?
			</view>
			
			<view class="btn_box">
				<view class="btn1" @click="showBack = false">
					取消
				</view>
				<view class="btn2"@click="topage()">
					确定
				</view>
			</view>
			<!-- <view class="btn" @click="showtcs=false" style="margin-bottom: 0rpx;">
				点击查看
		
			</view> -->
			
		</view>
		<u-mask :show="showtxtip" @click="showtxtip = false" :z-index='100' duration='0' />
		<view class="pops" v-if="showtxtip">
			<view class="tit" style="font-weight: 600;">
				提现成功
			</view>
			<view class="cont_box" style="text-align: center;color: #808080;justify-content: center;font-size: 36rpx;">
				充值押金将在10分钟内按原路退还,是否返回?
			</view>
			
			<view class="btn_box">
				<view class="btn1" @click="showtxtip = false">
					取消
				</view>
				<view class="btn2"@click="topage()">
					确定
				</view>
			</view>
			<!-- <view class="btn" @click="showtcs=false" style="margin-bottom: 0rpx;">
				点击查看
		
			</view> -->
			
		</view>
		<u-mask :show="show" @click="show = false" :z-index='100' />
		<view class="tip_box" v-if="show">
			<view class="top">
				<view class="txt">
					押金提现后,下次使用电单车仍需要重
				</view>
				<view class="txt">
					新充值押金,您要继续提现吗?
				</view>
			</view>
			<view class="bot">
				<view class="bot_left" @click="show=false">
					取消
				</view>
				<view class="bot_right" @click="tixian()">
					继续提现
				</view>
			</view>
			
		</view>
		
		<u-mask :show="loadingmask" :z-index='100' duration='0' />
		<view class="pops" v-if="loadingmask" style="width: 500rpx;left:124rpx">
			<view class="tit" style="font-weight: 600;">
				加载中...
			</view>
			<!-- <u-loading size="50" mode="flower"></u-loading> -->
			<view class="loading_box">
				<image src="https://lxnapi.ccttiot.com/bike/img/static/uOQxwzp0Sj9l2WZ534zp" mode=""></image>
				<!-- <view class="loader">
					
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#F7FAFE",
				},
				show: false,
				bindDeviceNum: null,
				userinfo: {},
				orderList: [],
				total: 0,
				areaInfo: "",
				isback:false,
				showBack:false,
				loadingmask:false,
				ispaid:false,
				showtxtip:false

			}

		},
		onShow() {
			// this.$store.dispatch('fetchFeeRules', this.$u).then(() => {

			// 	// 执行其他操作...
			// });
			
			this.getarea()
		},
		onLoad(e) {
			console.log(e,'this.isback');
			if(e.isback){
				if(e.isback=='false'){
					this.isback=false
				}else{
						this.isback=true
				}
				
				
			}
			
			
		},
		watch: {
			userId(newValue, oldValue) {
				// 处理userId变化的逻辑
				console.log('userId 发生变化', newValue, oldValue);

			}
		},
		computed: {

			userId() {
				return this.$store.getters.userId;
			},
		},
		methods: {
			topage(){
				this.showBack=false
				uni.navigateBack({
					delta: 1 // delta值为1时表示返回的页面层数
				});
			},
			getlist() {
				// uni.showLoading({
				// 	title:'加载中...'
				// })
				this.$u.get("/appVerify/order/list?type=2&userId=" + this.userId).then((res) => {
					// uni.hideLoading()
					if (res.code == 200) {
						this.orderList = res.rows
						this.total = res.total
					} else {
						// uni.showToast({
						// 	title: res.msg,
						// 	icon: 'none',
						// 	duration: 2000
						// });
					}
				});
			},

			tixian() {
				this.show = false
				this.loadingmask=true
				this.$u.post("/appVerify/order/withdraw").then((res) => {

					if (res.code == 200) {
						setTimeout(() => {
							this.loadingmask=false
							this.getinfo()
							this.getlist()
							
							this.showtxtip=true
						}, 500)

					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			getarea() {
				let id = uni.getStorageSync('areaId');
				this.$u.get("/app/area/" + id).then((res) => {

					if (res.code == 200) {
						this.areaInfo = res.data
						this.getinfo()
					} else {
						// uni.showToast({
						// 	title: res.msg,
						// 	icon: 'none',
						// 	duration: 2000
						// });
					}
				});
			},
			getinfo() {
				// uni.showLoading({
				// 	title:'加载中'
				// })
				this.$u.get("/getAppInfo").then((res) => {
				
					if (res.code == 200) {
						// uni.hideLoading()
						this.$store.commit('SET_USERID', res.user.userId);
						this.userinfo = res.user
						console.log(res.user.balance,'es.user.balance',this.isback);
						if(res.user.balance==parseFloat(this.areaInfo.deposit)){
							console.log('进入判断了',this.isback);
							if(this.isback==true){
								console.log('进入判断了1');
								// this.showBack=true
							}
							
							
						}
						this.getlist()
					} else {
						setTimeout(()=>{
							this.getinfo()
							this.getlist()
						},500)
					}
				});
			},
			check(){
				
				this.$u.get("/getAppInfo").then((res) => {
				
					if (res.code == 200) {
						
						this.$store.commit('SET_USERID', res.user.userId);
						this.userinfo = res.user
						console.log(res.user.balance,'es.user.balance',this.isback);
						this.loadingmask=false
							this.getlist()
						if(res.user.balance==parseFloat(this.areaInfo.deposit)){
							console.log('进入判断了',this.isback);
							if(this.isback==true){
								
								console.log('进入判断了1');
								// this.showBack=true
								uni.navigateBack({
									delta: 1 // delta值为1时表示返回的页面层数
								});
							}
							
							
						}
					
					} else {
						
					}
				});
			},
			sub4() {
				if(this.ispaid==false){
					this.ispaid=true
					let id = uni.getStorageSync('areaId');
					let data = {
						userId: this.userId,
					
						// ruleId: this.freeInfo.ruleId,
						money: this.areaInfo.deposit,
						mark: "押金充值",
						type: '4',
						areaId: id
					
					
					}
					let that = this
					this.$u.post('/appVerify/pre/order', data).then((res) => {
						if (res.code === 200) {
							// this.freList=res.rows
							
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.packageVal,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success(res) {
									// 支付成功逻辑
									// uni.showLoading({
									// 	title: '加载中'
									// })
									that.ispaid=false
									that.loadingmask=true
									setTimeout(() => {
										
										that.check()
										
									}, 2000)
									setTimeout(() => {
										
										that.check()
										
									}, 4000)
					
								},
								fail(err) {
									that.ispaid=false
									// 支付失败逻辑
									uni.showToast({
										title: '支付失败',
										icon: 'none',
										duration: 2000
									});
								}
							});
						} else {
							that.ispaid=false
							uni.showToast({
								title: res.data,
								icon: 'none',
								duration: 2000
							});
						}
					})
								
				}
				
			
			},
		}
	}
</script>

<style lang="scss">
	page {
		// background: linear-gradient(180deg, #1E807A  0%, rgba(255, 255, 255, 0) 40%), #FFFFFF;
	}

	.page {
		width: 750rpx;
		.pops {
			padding: 46rpx 36rpx;
			position: fixed;
			top: 400rpx;
			left: 74rpx;
			width: 604rpx;
			// height: 606rpx;
			background: #fff;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			z-index: 110;
		
			.close {
				position: absolute;
				left: 266rpx;
				bottom: -100rpx;
		
				image {
					width: 80rpx;
					height: 80rpx;
				}
			}
			.loading_box{
				width: 100%;
				// height: 200rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				image{
					width: 336rpx;
					height: 154rpx;
				}
				// .loader {
				//   width: 120rpx;
				//   aspect-ratio: 1;
				//   border-radius: 50%;
				//   clip-path: inset(-360rpx); /* 4倍的比例调整 */
				//   box-shadow: -240rpx 60rpx, -240rpx 60rpx, -240rpx 60rpx; /* 4倍的比例调整 */
				//   transform: translateY(-60rpx); /* 4倍的比例调整 */
				//   animation: l19 1s infinite linear;
				// }
				
				// @keyframes l19 {
				//   16.67% {box-shadow:-240rpx 60rpx,-240rpx 60rpx,  76rpx 60rpx} /* 4倍的比例调整 */
				//   33.33% {box-shadow:-240rpx 60rpx,    0px 60rpx,  76rpx 60rpx} /* 4倍的比例调整 */
				//   40%,60%{box-shadow: -76rpx 60rpx,    0px 60rpx,  76rpx 60rpx} /* 4倍的比例调整 */
				//   66.67% {box-shadow: -76rpx 60rpx,    0px 60rpx, 240rpx 60rpx} /* 4倍的比例调整 */
				//   83.33% {box-shadow: -76rpx 60rpx, 240rpx 60rpx,240rpx 60rpx} /* 4倍的比例调整 */
				//   100%   {box-shadow: 240rpx 60rpx, 240rpx 60rpx,240rpx 60rpx} /* 4倍的比例调整 */
				// }
		
			}
			.btn_box {
				margin-top: 80rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
		
				.btn1 {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 216rpx;
					height: 90rpx;
					background: #989898;
					border-radius: 54rpx 54rpx 54rpx 54rpx;
		
					font-weight: 500;
					font-size: 40rpx;
					color: #FFFFFF;
				}
		
				.btn2 {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 268rpx;
					height: 90rpx;
					background: #1E807A ;
					border-radius: 54rpx 54rpx 54rpx 54rpx;
		
					font-weight: 500;
					font-size: 40rpx;
					color: #FFFFFF;
				}
			}
		
			.time {
				margin-top: 20rpx;
				text-align: center;
				font-weight: 500;
				font-size: 48rpx;
				color: #1E807A ;
			}
		
			.cont {
				height: 500rpx;
				overflow-x: hidden;
				overflow-y: auto;
			}
		
			.tit {
				// width: 604rpx;
				text-align: center;
				font-weight: 500;
				font-size: 36rpx;
				color: #3D3D3D;
				margin-bottom: 54rpx;
			}
		
			.cont_box {
				margin-top: 36rpx;
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				justify-content: space-between;
		
				.left {
					display: flex;
					flex-wrap: wrap;
					font-weight: 600;
					font-size: 36rpx;
					color: #3D3D3D;
		
					.km {
						font-weight: 400;
						font-size: 28rpx;
						color: #3D3D3D;
					}
		
					.speed {
						margin-top: 18rpx;
						width: 226rpx;
						height: 22rpx;
						background: #ccc;
						border-radius: 16rpx 16rpx 16rpx 16rpx;
		
						.speeds {
							// width: 90%;
							height: 100%;
							background: #77B8FD;
							border-radius: 16rpx 16rpx 16rpx 16rpx;
						}
					}
		
					.NO {
						font-weight: 400;
						font-size: 24rpx;
						color: #3D3D3D;
					}
				}
		
				.right {
					font-weight: 600;
					font-size: 36rpx;
					color: #3D3D3D;
		
					image {
						width: 244rpx;
						height: 196rpx;
					}
				}
			}
		
			.text {
				margin-top: 36rpx;
				display: flex;
				flex-wrap: wrap;
				// align-items: center;
		
				.yuan {
					margin-top: 10rpx;
					margin-right: 12rpx;
					width: 20rpx;
					height: 20rpx;
					background: #000;
					border-radius: 50%;
				}
		
				span {
					width: 90%;
					font-weight: 400;
					font-size: 28rpx;
					color: #3D3D3D;
				}
			}
		
			.btn {
				margin-left: 40rpx;
				margin-top: 50rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 470rpx;
				height: 90rpx;
				background: #1E807A ;
				border-radius: 54rpx 54rpx 54rpx 54rpx;
				font-weight: 500;
				font-size: 40rpx;
				color: #FFFFFF;
			}
		
			.act1 {
				background-color: #ccc;
			}
		}
		
		
		
		.card {
			display: flex;
			justify-content: center;
			flex-wrap: wrap;
			padding: 28rpx 0;
			margin: 30rpx auto;
			width: 680rpx;
			height: 406rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			.tit {
				width: 100%;
				text-align: center;
				font-weight: 500;
				font-size: 52rpx;
				color: #3D3D3D;
				line-height: 72rpx;
			}

			.btn {
				margin-top: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 586rpx;
				height: 68rpx;
				background: #1E807A ;
				border-radius: 54rpx 54rpx 54rpx 54rpx;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
			}
		}

		.buy_btn {
			position: fixed;
			bottom: 150rpx;
			left: 36rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 680rpx;
			height: 90rpx;
			background: #1E807A ;
			border-radius: 54rpx 54rpx 54rpx 54rpx;

			font-weight: 500;
			font-size: 40rpx;
			color: #FFFFFF;
		}

		.tip_box {
			position: fixed;
			left: 72rpx;
			top: 628rpx;
			width: 610rpx;
			height: 282rpx;
			background: #FFFFFF;
			border-radius: 30rpx 30rpx 30rpx 30rpx;
			z-index: 110;

			.top {
				padding: 52rpx 38rpx 42rpx 36rpx;

				.txt {
					width: 100%;
					text-align: center;
					font-weight: 500;
					font-size: 32rpx;
					color: #3D3D3D;
				}
			}

			.bot {
				border-top: 2rpx solid #D8D8D8;
				display: flex;
				flex-wrap: nowrap;
				height: 100%;

				.bot_left {
					width: 50%;
					height: 98rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-weight: 500;
					font-size: 36rpx;
					color: #3D3D3D;
				}

				.bot_right {
					width: 50%;
					height: 98rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					border-left: 2rpx solid #D8D8D8;
					font-weight: 500;
					font-size: 36rpx;
					color: #1E807A ;

				}

			}
		}

		.card2 {
			margin: 30rpx auto;
			// padding: 38rpx 40rpx;
			width: 680rpx;
			// height: 252rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			.card_top {
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-between;
				padding: 28rpx 35rpx 24rpx 35rpx;
				border-bottom: 2rpx solid #D8D8D8;

				.left {
					display: flex;
					flex-wrap: nowrap;
					align-items: center;
					font-weight: 400;
					font-size: 28rpx;
					color: #3D3D3D;

					image {
						margin-right: 18rpx;
						width: 36rpx;
						height: 36rpx;
					}
				}

				.right {
					font-weight: 400;
					font-size: 24rpx;
					color: #3D3D3D;
				}
			}

			.cardcont {
				display: flex;
				flex-wrap: wrap;
				padding: 0rpx 34rpx;

				.cont_li {
					margin-top: 28rpx;
					width: 100%;
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;
					border-bottom: 2rpx solid #D8D8D8;
					padding-bottom: 28rpx;

					.cont_li_left {
						width: 300rpx;
						display: flex;
						flex-wrap: wrap;

						.tit {
							width: 100%;
							font-weight: 400;
							font-size: 32rpx;
							color: #3D3D3D;
						}

						.data {
							margin-top: 8rpx;
							width: 100%;
							font-weight: 400;
							font-size: 24rpx;
							color: #979797;
						}
					}

					.cont_li_right {
						display: flex;
						flex-wrap: wrap;
						width: 100rpx;

						.cont_price {
							width: 100rpx;
							margin-left: auto;
							text-align: right;
							font-weight: 500;
							font-size: 40rpx;
							color: #3D3D3D;
						}

						.cont_data {
							width: 100rpx;
							text-align: right;
							font-weight: 400;
							font-size: 24rpx;
							color: #1E807A ;
						}
					}
				}
			}
		}

		.card1 {
			margin: 30rpx auto;
			padding: 38rpx 40rpx;
			width: 680rpx;
			// height: 252rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			.cont {
				display: flex;
				flex-wrap: nowrap;
				align-items: center;

				.yuan {
					margin-right: 26rpx;
					width: 20rpx;
					height: 20rpx;
					border-radius: 50%;
					background: #808080;
				}

				.txt {
					width: 90%;
					font-weight: 400;
					font-size: 28rpx;
					color: #808080;
				}
			}
		}
	}
</style>