<template>
	<view class="content">
		<view class="acont_part1">
			<view class="part1_bg">
				<image src="../../../../static/rider/image-bg.png"></image>
			</view>
			<view class="part1_box">
				<view class="text-right margin-top-sm margin-right-sm" @click="goDet">查看明细</view>
				<view class="balance">
					<view class="balance_name">保证金余额(元)</view>
					<view class="balance_price">{{mayMoney?mayMoney:0}}</view>
				</view>
				<view class="part1_btn">
					<view class="btn_left" @click="tuiBtn()">退保证金</view>
					<view class="btn_right" @click="openPay()">缴纳保证金</view>
				</view>
			</view>
		</view>
		<view class="acont_part2">
			<view class="acount_type">保证金规则</view>
			<view class="acount_box" v-for="(item,index) in list" :key="item.id">
				<view class="name">{{item.name}}</view>
				<view class="tit">{{item.tit}}</view>
			</view>

		</view>
		<!-- 支付方式 -->
		<u-popup v-model="shows" mode="center" width="640rpx" height="640rpx" border-radius="14" :closeable="true"
			close-icon="close-circle" close-icon-size="50" close-icon-color="#CCCCCC" @close="closePopup()">
			<view style="width: 100%;height: 300upx;background: #FFFFFF;">
				<view class="receipt_code">
					<view class="code_title">请输入支付金额</view>
					<u-input v-model="earnestMoney" type="text" placeholder="请输入支付金额" :border="true" />
					<view class="margin-tb padding-lr radius bg-white" style="height: 220upx;">
						<view class="flex align-center justify-between padding-tb-sm" v-for="(item,index) in payList"
							:key='index'>
							<image :src="item.image" style="width: 80upx;height: 80upx;border-radius: 50upx;"></image>
							<view class="flex-sub text-xl text-bold margin-left">{{item.name}}</view>
							<radio-group name="openWay" style="margin-left: 20upx;" @change='selectWay(item)'>
								<label class="tui-radio">
									<radio class="red" :checked="openWay === item.id ? true : false" />
								</label>
							</radio-group>
						</view>
					</view>
					<view class="sure" @click="jiaoBtn">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shows: false,
				baozhengjin: 0,
				disabled: true,
				moneys: '',
				list: [{
					id: 1,
					name: '保证金退款',
					tit: '48小时内无订单记录即可申请退保证金，保证金统一退款至账户余额，实时到账。'
				}, {
					id: 2,
					name: '保证金缴纳',
					tit: '所有跑腿专人直送订单需缴纳保证金，没有缴纳者将无法接订单，建议您及时缴纳。'
				}],
				mayMoney: 0,
				openWay: 1,
				payList: [],
				earnestMoney: ''
			}
		},
		onLoad() {
			//获取保证金
			this.$Request.getT('/app/common/type/273').then(res => {
				if (res.code === 0) {
					this.baozhengjin = res.data.value;

				}
			});

			// #ifdef MP-WEIXIN
			this.payList = [{
				id: 1,
				image: '../../../../static/my/weixin.png',
				name: '微信'
			}]
			this.openWay = 1
			// #endif
			// #ifdef H5
			this.payList = [{
				id: 2,
				image: '../../../../static/my/zhifubao.png',
				name: '支付宝'
			}]
			this.openWay = 2
			// #endif
			// #ifdef APP-PLUS
			this.payList = [{
					id: 1,
					image: '../../../../static/my/weixin.png',
					name: '微信'
				},
				{
					id: 2,
					image: '../../../../static/my/zhifubao.png',
					name: '支付宝'
				}
			]
			// #endif

		},
		onShow() {
			this.taskData()

		},
		methods: {
			openPay() {
				this.$Request.getT("/app/wxPayErrRider/selectCertificationFlag").then(res => {
					if (res.code == 0) {
						this.shows = true
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},
			// 跳转保证金明细
			goDet() {
				uni.navigateTo({
					url: '/pages/riderMy/myAccount/AcontMoney/Acontlist'
				})
			},
			selectWay: function(item) {
				this.openWay = item.id;
			},
			zhifu() {
				this.shows = true
			},
			// 关闭底部弹出层
			closePopup() {
				this.shows = false
			},

			// 获取任务数据
			taskData() {
				let that = this;
				let token = this.$queue.getData('token');
				let userId = this.$queue.getData('userId');
				if (token) {
					//this.$queue.showLoading("加载中...");
					//可以提现金额查询预估收入查询
					this.$Request.getT("/app/userinfo/findUserInfoById").then(res => {
						uni.hideLoading();
						if (res.code === 0 && res.data) {
							that.mayMoney = res.data.cashDeposit;
							if (!res.data.cashDeposit) {
								// this.earnestMoney=this.baozhengjin;
								// this.disabled=true;
							} else {
								// this.disabled=false;
							}

						} else if (res.code === -102) {
							this.$queue.showToast(res.msg);
							this.$queue.logout();
						} else {
							that.mayMoney = '0';
						}
					});
				}
			},

			// 缴纳
			jiaoBtn() {
				let that = this;
				if (!that.earnestMoney) {
					that.$queue.showToast('请输入支付金额');
					return;
				}
				uni.showLoading({
					title: '支付中'
				});
				// 微信支付
				if (that.openWay == 1) {
					// #ifdef APP-PLUS
					uni.hideLoading();
					// 微信APP支付 根据订单id获取支付信息
					that.$Request.postT('/app/wxPayErrRider/wxPayCashDeposit?money=' + that.earnestMoney + '&type=1').then(
						ret => {
							// console.log(JSON.stringify(ret), '支付信息')
							if (ret.code == 0) {
								console.log(JSON.stringify(ret), '支付信息')
								that.isCheckPay(ret.code, 'wxpay', JSON.stringify(ret.data));

							} else {
								that.shows = false
								that.earnestMoney = ''
								that.$queue.showToast(res.msg);
							}

						});

					// #endif

					// #ifdef H5
					let ua = navigator.userAgent.toLowerCase();
					console.log(ua.indexOf('micromessenger'))
					if (ua.indexOf('micromessenger') !== -1) {
						let openId = that.$queue.getData('openid') ? that.$queue.getData('openid') : '';
						that.$Request.postT('/app/wxPayErrRider/wxPayCashDeposit?money=' + that.earnestMoney + '&type=2')
							.then(
								res => {
									if (res.code == 0) {
										that.callPay(res);
									} else {
										that.shows = false
										that.earnestMoney = ''
										that.$queue.showToast(res.msg);
									}

								});
					}
					// #endif

					// #ifdef MP-WEIXIN
					that.$Request.postT('/app/wxPayErrRider/wxPayCashDeposit?money=' + that.earnestMoney + '&type=3').then(
						res => {
							console.log('res', res)
							if (res.code == 0) {
								uni.requestPayment({
									provider: 'wxpay',
									timeStamp: res.data.timestamp,
									nonceStr: res.data.noncestr,
									package: res.data.package,
									signType: res.data.signType,
									paySign: res.data.sign,
									success: function(res) {
										that.shows = false
										that.earnestMoney = ''
										uni.hideLoading();


										that.taskData()
										that.$queue.showToast('支付成功');
										setTimeout(function() {
											uni.hideLoading();
										}, 1000);
									},
									fail: function(err) {
										that.shows = false
										that.earnestMoney = ''
										console.log('fail:' + JSON.stringify(err));
										uni.hideLoading();
										that.$queue.showToast('支付失败');
									}
								});
							} else {
								that.shows = false
								that.earnestMoney = ''
								that.$queue.showToast(res.msg);
							}
						});
					// #endif
				} else if (that.openWay == 2) { //支付宝支付
					uni.hideLoading();
					// #ifdef H5
					let userId = this.$queue.getData('userId');
					that.$Request.postT('/app/wxPayErrRider/wxPayCashDeposit?money=' + that.earnestMoney + '&type=5')
						.then(res => {
							if (res.code == 0) {
								const div = document.createElement('div')
								div.innerHTML = res.data //此处form就是后台返回接收到的数据
								document.body.appendChild(div)
								document.forms[0].submit()
								that.earnestMoney = ''
								that.shows = false
							} else {
								uni.hideLoading();
								that.$queue.showToast(res.msg);
							}
						});
					// #endif
					// #ifdef APP-PLUS
					that.$Request.postT('/app/wxPayErrRider/wxPayCashDeposit?money=' + that.earnestMoney + '&type=4').then(
						ret => {
							uni.hideLoading();
							// console.log(JSON.stringify(ret), '支付信息')
							if (ret.code == 0) {
								that.setPayment('alipay', ret.data);
								that.shows = false
								// that.isCheckPay(ret.code, 'alipay', ret.data);
							} else {
								that.shows = false
								that.earnestMoney = ''
								that.$queue.showToast(res.msg);
							}
						});

					// #endif
				}
			},
			callPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', that.onBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', that.onBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', that.onBridgeReady(response));
					}
				} else {
					that.onBridgeReady(response);
				}
			},
			onBridgeReady: function(response) {
				let that = this;
				console.log(!response.package, '2222222')
				// if (!response.package) {
				// 	return;
				// }
				that.earnestMoney = ''
				that.shows = false
				console.log(response, '1111111111')
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.data.appid, //公众号名称，由商户传入
						"timeStamp": response.data.timestamp, //时间戳，自1970年以来的秒数
						"nonceStr": response.data.noncestr, //随机串
						"package": response.data.package,
						"signType": response.data.signType, //微信签名方式：
						"paySign": response.data.sign //微信签名
					},
					function(res) {
						console.log(res, '/*-/*-/*-')
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							that.taskData()
							uni.showLoading({
								title: '支付成功'
							});
							setTimeout(function() {
								uni.hideLoading();
							}, 1000);
						} else {
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			isCheckPay(status, name, order) {
				console.log(status, name, order, '111111')
				this.earnestMoney = ''
				this.shows = false
				if (status == 0) {
					this.setPayment(name, order);
				} else {
					uni.hideLoading();
					uni.showToast({
						title: '支付信息有误',
						icon: 'none'
					});
				}
			},
			setPayment(name, order) {
				let that = this
				console.log(name, '*-*-*', order)
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						console.log(res)
						that.taskData()
						uni.hideLoading();
						that.earnestMoney = ''
						that.shows = false
						uni.showLoading({
							title: '支付成功'
						});
					},
					fail: function(err) {
						console.log(err)
						uni.hideLoading();
					},
					complete() {
						uni.hideLoading();
					}
				});
			},
			// 退保证金
			tuiBtn() {
				let that = this
				if (that.mayMoney == 0) {
					uni.showToast({
						icon: 'none',
						title: '暂无保证金'
					})
					return
				}
				uni.showModal({
					title: '提示',
					content: '确认退还保证金吗？如果存在未完成订单和未处理完成的投诉无法退还，需要先处理完毕后再申请',
					success: function(res) {
						if (res.confirm) {
							that.$Request.getT('/app/cash/cashDepositMoney').then(res => {
								console.log(res)
								if (res.code == 0) {
									uni.showToast({
										title: '申请成功'
									})
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
								setTimeout(function() {
									that.taskData()
								}, 1000)
							});
						}
					},
				})

			}
		}
	}
</script>

<style>
	/* 支付金额弹框 */
	.receipt_code {
		width: 90%;
		margin: 0 auto;
	}

	.code_title {
		width: 100%;
		line-height: 100rpx;
		font-size: 31rpx;
		font-weight: bold;
		text-align: center;
		letter-spacing: 2rpx;
		margin-top: 21rpx;
		margin-bottom: 25rpx;
	}

	.u-input--border {
		border: 1px solid #F2F2F2 !important;
		background: #F2F2F2 !important;
		color: #999999 !important;
		font-weight: 500 !important;
		letter-spacing: 2rpx !important;
	}

	.u-input__input {
		font-size: 30rpx;
		font-weight: bold;
		flex: 1;
		color: #999999 !important;
		min-height: 85rpx !important;
		margin-top: 7rpx;
	}

	.sure {
		width: 100%;
		height: 80rpx;
		background: #FF7F00;
		color: white;
		border-radius: 46rpx;
		text-align: center;
		line-height: 80rpx;
		margin-top: 30rpx;
		letter-spacing: 2rpx;
	}


	.content {
		width: 100%;
		position: relative;
	}

	.acont_part1 {
		width: 100%;

	}

	.part1_bg image {
		width: 100%;
		height: 730rpx;
	}

	.part1_box {
		width: 90%;
		height: 400rpx;
		background: #ffffff;
		position: absolute;
		top: 70rpx;
		left: 37rpx;
		border-radius: 25rpx;
	}

	.balance {
		width: 90%;
		margin: 0 auto;
		text-align: center;
		/* margin-top: 50rpx; */
		line-height: 55rpx;
	}

	.balance_name {
		font-size: 26rpx;
		letter-spacing: 2rpx;
	}

	.balance_price {
		margin-top: 10rpx;
		font-weight: bold;
		font-size: 60rpx;
	}

	.part1_btn {
		width: 90%;
		margin: 0 auto;
		margin-top: 80rpx;
		display: flex;
	}

	.btn_left {
		flex: 1;
		border: 1rpx solid #CCCCCC;
		text-align: center;
		line-height: 80rpx;
		color: black;
		font-size: 24rpx;
		border-radius: 12rpx;
		margin-right: 10rpx;
	}

	.btn_right {
		flex: 1;
		border: 1rpx solid #CCCCCC;
		text-align: center;
		line-height: 80rpx;
		background: #FF7F00;
		color: white;
		font-size: 24rpx;
		border-radius: 12rpx;
		margin-left: 10rpx;
	}

	.acont_part2 {
		width: 90%;
		position: absolute;
		bottom: -100rpx;
		left: 37rpx;
	}

	.acount_type {
		font-size: 32rpx;
		font-weight: bold;
		letter-spacing: 2rpx;
		line-height: 75rpx;
	}

	.name {
		font-size: 26rpx;
		font-weight: bold;
		letter-spacing: 2rpx;
		line-height: 70rpx;
	}

	.tit {
		color: #666666;
		font-size: 25rpx;
	}
</style>
