<template>
	<view>
		<view style="width: 100%;height: 384upx;position: relative;">
			<image src="../../static/images/img/image_bg.png" style="width: 100%;height: 100%;"></image>
			<view class="box">
				<view class="margin-top padding-top">保证金（元）</view>
				<view class="margin-top-lg text-bold" style="font-size: 78upx;">{{cashDeposit?cashDeposit:0}}</view>
				<view class="mingxi" @click="bindmingxi()">保证金明细</view>
				<view class="flex justify-center padding-lr" style="margin-top: 100upx;">
					<view class="btn1" @click="Tuiprice()">退保证金</view>
					<view class="btn2" @click="submit()">缴纳保证金</view>
				</view>
			</view>
		</view>
		<view class="padding" style="margin-top: 180upx;">
			<view style="color: #333333;font-size: 38upx;" class="text-bold">保证金规则</view>
			<view style="font-size: 32upx;margin-top: 58upx;" class="text-bold">保证金退款</view>
			<view style="color: #666666;margin-top: 29upx;font-size: 28upx;">
				48小时内无订单记录即可申请退保证金，保证金统一退款至账户余额，实时到账。
			</view>
			<view style="font-size: 32upx;margin-top: 58upx;" class="text-bold">保证金缴纳</view>
			<view style="color: #666666;margin-top: 29upx;font-size: 28upx;">
				所有店铺发布商品需缴纳保证金{{money}}元，没有缴纳者将无法接订单，建议您及时缴纳。
			</view>
		</view>

		<!-- 支付方式 -->
		<!-- <u-popup v-model="showpay" mode="bottom" :closeable="closeable">
			<view class="popup_pay">
				<view style="background-color: #fff;">
					<view style="padding: 0 20upx;margin-top: 60rpx;margin-bottom: 20rpx;">
						<view
							style="display: flex;height: 100upx;align-items: center;padding: 20upx 0;justify-content: center;"
							v-for="(item,index) in openLists" :key='index'>
							<image :src="item.image" style="width: 55upx;height: 55upx;border-radius: 50upx;">
							</image>
							<view style="font-size: 30upx;margin-left: 20upx;width: 70%;">
								{{item.text}}
							</view>
							<radio-group name="openWay" style="margin-left: 45upx;" @tap='selectWay(item)'>
								<label class="tui-radio">
									<radio color="#1777FF" :checked="openWay === item.id ? true : false" />
								</label>
							</radio-group>
						</view>
					</view>
				</view>
				<view class="pay_btn" @click="pay()">确认支付</view>
			</view>
		</u-popup> -->
		<!-- 支付方式 -->
		<u-popup v-model="showpay" 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" 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 openLists"
							: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.text}}</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="pay()">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				money: 0,
				showpay: false,
				openLists: [],
				openWay: 1,
				closeable: true,
				renzheng: '',
				cashDeposit: '',
				earnestMoney: '',
				openId: ''
			}
		},
		onLoad() {
			this.cashDeposit = uni.getStorageSync('cashDeposit')
			this.openId = uni.getStorageSync('openId')
			this.renzheng = uni.getStorageSync("renzheng")
			// #ifdef APP
			this.openLists = [{
					image: '../../static/images/img/icon_weixin.png',
					text: '微信',
					id: 1
				}, {
					image: '../../static/images/img/zhifubao.png',
					text: '支付宝',
					id: 2
				}],
				this.openWay = 1;
			// #endif

			// #ifdef MP-WEIXIN
			this.openLists = [{
					image: '../../static/images/img/icon_weixin.png',
					text: '微信',
					id: 1
				}],
				this.openWay = 1;
			// #endif

			// #ifdef H5
			this.openLists = [{
				image: '../../static/images/img/zhifubao.png',
				text: '支付宝',
				id: 2
			}]
			this.openWay = 2;
			// #endif


		},
		onShow() {
			this.Getmoney()
			this.getshanghuinfo()
		},
		methods: {
			selectWay: function(item) {
				this.openWay = item.id;
			},
			//保证金明细
			bindmingxi() {
				uni.navigateTo({
					url: '/my/other/moneylist'
				})
			},
			getshanghuinfo() {
				let data = {
					shopId: uni.getStorageSync('shopId')
				}
				this.$Request.get("/app/shop/selectShopMessage", data).then(res => {
					if (res.code == 0) {
						this.cashDeposit = res.data.cashDeposit
						uni.setStorageSync('cashDeposit', res.data.cashDeposit)
					}
				});
			},
			// 保证金
			Getmoney() {
				this.$Request.get("/app/common/type/270").then(res => {
					console.log(res)
					if (res.code == 0) {
						this.money = res.data.value
					}
				});
			},
			//缴纳保证金
			submit() {
				// if (this.isCashDeposit == 3) {
				// 	uni.showToast({
				// 		title: '系统审核中，请耐心等待',
				// 		icon: 'none',
				// 		duration: 1000
				// 	})
				// } else if (this.isCashDeposit == 1) {
				// 	this.showpay = true
				// }
				this.showpay = true
				// #ifdef MP-WEIXIN
				this.wxlogin()
				// #endif

			},
			//退保证金
			Tuiprice() {
				if (this.cashDeposit == 0) {
					uni.showToast({
						title: '保证金余额0',
						icon: 'none',
						duration: 1000
					})
				} else {
					this.$Request.getT('/app/shop/shopRefundCashDeposit', {
						shopId: uni.getStorageSync('shopId')
					}).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '申请提交成功，保证金已退还至您的钱包账户',
								icon: 'none',
								duration: 1000
							})
							uni.switchTab({
								url: '/pages/my/index'
							})
						}else{
							this.$queue.showToast(res.msg);
						}
					})
				}

			},
			// 关闭底部弹出层
			closePopup() {
				this.shows = false
			},
			wxlogin() {
				let that = this
				wx.login({
					success(res) {
						if (res.code) {
							console.log(res)
							//发起网络请求
							let data = {
								code: res.code
							}
							that.$Request.getA("/app/Login/wxShopLogin", data).then(res => {
								if (res.code == 0) {
									that.$queue.setData("openId", res.data.open_id);
								}
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			pay() {
				let that = this;

				if (!this.earnestMoney) {
					that.$queue.showToast('请输入支付金额');
					return;
				}
				uni.showLoading({
					title: '支付中'
				});
				let userId = uni.getStorageSync('userId')
				let openId = uni.getStorageSync('openId')
				that.showpay = false
				if (that.openWay == 1) { //微信支付
					// #ifdef MP-WEIXIN
					let data = {
						money: that.earnestMoney,
						openId: openId,
						type: 3
					}
					that.$Request.post('/shop/shopmoney/shopCashDeposit', data).then(res => {
						console.log(res)
						uni.hideLoading();
						if (res.code == 0) {
							that.earnestMoney = ''
							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) {
									console.log(res)
									uni.showToast({
										title: '支付成功',
										icon: 'nones'
									});
									// this.$queue.showToast('支付成功');
									that.getshanghuinfo()
									// uni.switchTab({
									// 	url: '/pages/my/index'
									// })
								},
								fail: function(err) {
									that.$queue.showToast('支付失败');
								}
							});
						}
					});
					// #endif


					// #ifdef H5
					let data = {
						money: that.money,
						type: 2
					}
					that.$Request.post('/shop/shopmoney/shopCashDeposit', data).then(res => {
						uni.hideLoading();
						that.showpay = false
						that.callPay(res);
					});
					// #endif

					// #ifdef APP
					let data = {
						money: that.money,
						type: 1
					}
					that.$Request.post('/shop/shopmoney/shopCashDeposit', data).then(res => {
						uni.hideLoading()
						console.log(res)
						that.showpay = false
						if (res.code == 0) {
							that.isCheckPay(res.code, 'wxpay', JSON.stringify(res.data));
						}
					});
					// #endif


				} else if (that.openWay == 2) { //支付宝支付
					// #ifdef H5
					let data = {
						money: that.money,
						type: 5
					}
					that.$Request.post('/shop/shopmoney/shopCashDeposit', data).then(
						res => {
							uni.hideLoading()
							that.showpay = false
							const div = document.createElement('div')
							div.innerHTML = res.data //此处form就是后台返回接收到的数据
							document.body.appendChild(div)
							document.forms[0].submit()
						});
					// #endif

					// #ifdef APP-PLUS
					let data = {
						money: that.money,
						type: 4
					}
					that.$Request.post('/shop/shopmoney/shopCashDeposit', data).then(
						res => {
							uni.hideLoading()
							that.showpay = false
							that.setPayment('alipay', res.data);
						});
					// #endif

				}
			},
			callPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
					}
				} else {
					this.onBridgeReady(response);
				}
			},
			onBridgeReady: function(response) {
				let that = this;
				if (!response.package) {
					return;
				}
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.appid, //公众号名称，由商户传入
						"timeStamp": response.timestamp, //时间戳，自1970年以来的秒数
						"nonceStr": response.noncestr, //随机串
						"package": response.package,
						"signType": response.signType, //微信签名方式：
						"paySign": response.sign //微信签名
					},
					function(res) {
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							uni.showLoading({
								title: '支付成功'
							});
							uni.hideLoading();

							uni.navigateTo({
								url: '/pages/my/index'
							})
						} else {
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			isCheckPay(code, name, order) {
				if (code == 0) {
					console.log('999999999999')
					this.setPayment(name, order);
				} else {
					uni.hideLoading();
					uni.showToast({
						title: '支付信息有误'
					});
				}
			},
			setPayment(name, order) {
				console.log(777777777, name, order)
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						uni.hideLoading();
						uni.showLoading({
							title: '支付成功'
						});
						uni.switchTab({
							url: '/pages/my/index'
						})
					},
					fail: function(err) {
						uni.hideLoading();
					},
					complete() {
						uni.hideLoading();
					}
				});
			}
		}
	}
</script>

<style>
	page {
		background: #FFFfff;
	}

	.box {
		width: 690upx;
		height: 430upx;
		background: #FFFDFC;
		box-shadow: 0upx 15upx 43upx 0upx rgba(232, 215, 199, 0.29);
		border-radius: 24upx;
		margin: 0 auto;
		position: absolute;
		top: 80upx;
		left: 0;
		right: 0;
		text-align: center;
	}

	.btn1 {
		/* width: 305upx; */
		width: 100%;
		height: 88upx;
		background: #ffffff;
		border: 2upx solid #CCCCCC;
		border-radius: 8upx;
		line-height: 88upx;
		/* color: #FFFFFF; */
		font-size: 28upx;
		margin-right: 20upx;
	}

	.btn2 {
		/* width: 305upx; */
		width: 100%;
		height: 88upx;
		background: #FCD202;
		border-radius: 8upx;
		line-height: 88upx;
		/* color: #FFFFFF; */
		font-size: 28upx;
	}

	.popup_pay {
		width: 100%;
		position: relative;
		padding-bottom: 45rpx;
		/* height: 160px; */
		/* #ifndef MP-WEIXIN */
		/* height: 130px; */
		/* #endif */

	}

	.pay_btn {
		width: 90%;
		margin: 0 auto;
		text-align: center;
		background: #2FB57A;
		height: 80rpx;
		border-radius: 16rpx;
		color: #ffffff;
		line-height: 80rpx;

	}

	/* 支付金额弹框 */
	.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: #2FB57A;
		color: white;
		border-radius: 46rpx;
		text-align: center;
		line-height: 80rpx;
		margin-top: 30rpx;
		letter-spacing: 2rpx;
	}

	.mingxi {
		position: relative;
		top: 10px;
	}
</style>
