<template>
	<view>
		<view class="block">
			<view class="content">
				<view class="my" v-if="type== 1">
					当前活动金余额 : <view class="font22 fcoreb5 margle">¥ {{balance}} </view>
				</view>
				<view class="my" v-if="type== 2">
					当前活动余额 : <view class="font22 fcoreb5 margle">¥ {{balance}} </view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				充值金额
			</view>
			<view class="content">
				<view class="amount">
					<view class="list">
						<view class="box" v-for="(amount,index) in amountList" :key="index" @tap="select(amount)"
							:class="{'on':amount == inputAmount}">
							<view class="heTitle">{{amount}}元</view>
						</view>
					</view>
					<view class="num">
						<view class="text font15">
							输入充值金额
						</view>
						<view class="input">
							<input v-model="inputAmount" />
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="pay">
			<button class="btn" @tap="doDeposit">立即充值</button>
			<view class="tis">
				点击立即充值，即代表您同意<view class="terms">
					《条款协议》
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	let app = getApp()
	import {
		rebateCreate,
		recharge
	} from "../../../Utils/Api.js";
	// #ifdef H5
	var jweixin = require('jweixin-module');
	// #endif
	export default {
		data() {
			return {
				balance: '', //余额
				inputAmount: '', //金额
				type: '', //类型
				activityNo: '', //活动编号
				amountList: [100, 200, 300, 500, 800, 1000], //预设3个可选快捷金额
				accountNumber: '', //账户编号
			};
		},
		onLoad(options) {
			this.accountNumber = options.merNo;
			this.balance = options.price;
			this.type = options.type;
			this.activityNo = options.activityNo;
			if (this.type == 1) {
				this.amountList = [100, 200, 300, 500, 800, 1000]
			}
			if (this.type == 2) {
				this.amountList = [10, 50, 100, 150, 200, 300]
			}
		},
		methods: {
			select(amount) {
				this.inputAmount = amount;
			},
			doDeposit() {
				if (parseFloat(this.inputAmount).toString() == "NaN") {
					uni.showToast({
						title: '请输入正确金额',
						icon: 'none'
					});
					return;
				}
				if (this.inputAmount <= 0) {
					uni.showToast({
						title: '请输入大于0的金额',
						icon: 'none'
					});
					return;
				}
				if (parseFloat(this.inputAmount).toFixed(2) != parseFloat(this.inputAmount)) {
					uni.showToast({
						title: '最多只能输入两位小数哦~',
						icon: 'none'
					});
					return;
				}
				
				let _this = this;
				uni.showModal({
				    title: '提示',
				    content: `确定充值${this.inputAmount}元吗?`,
				    success: function(res) {
				        if (res.confirm) {
							_this.createrOrder();
				        } else if (res.cancel) {
				
				            // console.log('用户点击取消');
				        }
				    }
				});
				
			},
			createrOrder(){
				uni.showLoading({
					title: '正在提交订单...'
				})
				if (this.type == 1) {
					let goods = {
						"accountNumber": this.accountNumber,
						"orderType": "1", // 订单类型  1:充值
						"payType": "WECHAT", // WECHAT:微信 ，ALIPAY:支付宝，UQRCODEPAY：银联
						"tradePrice": this.inputAmount,
						"openId": app.globalData.openId
					}
				
					rebateCreate(goods).then(res => {
						uni.hideLoading();
						if (res.return_code == '000000') {
							this.payRequest(res.return_data.payParam);
						} else {
							uni.showToast({
								title: res.return_msg,
								duration: 2000,
								icon: 'none'
							});
						}
					})
				}
				
				if (this.type == 2) {
					let data = {
						"activityNo": this.activityNo,
						"amount": this.inputAmount,
					}
					recharge(data).then(res => {
						uni.hideLoading();
						if (res.return_code == '000000') {
							uni.showToast({
								title: '充值成功',
								icon: 'none',
								duration: 2000,
								success() {
									setTimeout(() => {
										uni.navigateBack()
									}, 2000);
								}
							})
						} else {
							uni.showToast({
								title: res.return_msg,
								duration: 2000,
								icon: 'none'
							});
						}
					})
				}
			},
			//H5支付
			payRequest: function(self) {
				uni.showLoading({
					title: '支付中...'
				})
				jweixin.config({
					// debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: self.appId, // 必填，公众号的唯一标识
					timestamp: self.timeStamp, // 必填，生成签名的时间戳
					nonceStr: self.nonceStr, // 必填，生成签名的随机串
					signature: self.sign, // 必填，签名，见附录1
					jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				uni.hideLoading();
				jweixin.ready(function() {
					jweixin.checkJsApi({
						jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
						success: function(res) {},
						fail: function(res) {}
					});
					jweixin.chooseWXPay({
						appId: self.appId,
						timestamp: self
							.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
						nonceStr: self.nonceStr, // 支付签名随机串，不长于 32 位
						package: self.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
						signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
						paySign: self.sign, // 支付签名
						success: function(res) {
							// 支付成功后的回调函数
							uni.showToast({
								title: '支付成功'
							})
							uni.navigateBack({})
						},
						cancel: function(r) {},
						fail: function(res) {}
					});
				});

				jweixin.error(function(res) {
					uni.showToast({
						icon: 'none',
						title: '支付失败了',
						duration: 4000
					});
				});
			},
		}

	}
</script>

<style lang="scss">
	.block {
		width: 94%;
		padding: 20upx 3%;

		.title {
			width: 100%;
			font-size: 30upx;
		}

		.content {
			.my {
				width: 100%;
				height: 120upx;
				display: flex;
				align-items: center;
				font-size: 30upx;
				border-bottom: solid 1upx #eee;
			}

			.amount {
				width: 100%;


				.list {
					width: 94%;
					margin-left: 3%;
					display: flow-root;
					justify-content: space-between;
					align-items: center;
					padding: 20upx 0;

					.box {
						width: 30%;
						margin-left: 1%;
						margin-right: 2%;
						margin-top: 20upx;
						float: left;
						height: 120upx;
						display: inline-grid;
						justify-content: center;
						align-items: center;
						text-align: center;
						color: #089bf5;
						background-color: #f1f1f1;

						&.on {
							background-color: #089bf5;
							color: #ffffff;
						}
					}

					.heTitle {
						width: 100%;
						font-size: 18px;
					}
				}

				.num {
					margin-top: 30upx;
					display: flex;
					justify-content: flex-start;
					align-items: center;

					.text {
						padding-right: 10upx;
					}

					.input {
						width: 40.2vw;
						border-bottom: solid 2upx #999;

						justify-content: flex-end;
						align-items: center;

						input {
							margin: 0 20upx;
							height: 60upx;
							font-size: 40upx;
							color: #0083f5;
							justify-content: flex-end;
							align-items: center;
						}
					}
				}
			}

			.pay-list {
				width: 100%;
				border-bottom: solid 1upx #eee;

				.row {
					width: 100%;
					height: 120upx;
					display: flex;
					align-items: center;

					.left {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						align-items: center;

						image {
							width: 80upx;
							height: 80upx;
						}
					}

					.center {
						width: 100%;
						font-size: 30upx;
					}

					.right {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						justify-content: flex-end;
					}
				}
			}
		}
	}

	.pay {
		margin-top: 30upx;
		width: 100%;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;

		.btn {
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			background-color: #0083f5;
			box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.2);
		}

		.tis {
			margin-top: 10upx;
			width: 100%;
			font-size: 24upx;
			display: flex;
			justify-content: center;
			align-items: baseline;
			color: #999;

			.terms {
				color: #5a9ef7;
			}
		}
	}
</style>