<template>
	<view>
		<view class="block">
			<view class="content">
				<view class="my">
					我的账户余额： {{user.gold}} 个 ( 1:100)
				</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 class="font14">{{amount * 100}}分</view>
						</view>
					</view>
					<view class="num">
						<view class="text font13">
							自定义充值金额(兑:{{inputAmount * 100}}分)
						</view>
						<view class="input">
							<input type="number" v-model="inputAmount" />
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				选择支付方式
			</view>
			<view class="content">
				<view class="pay-list">
					<view class="row" @tap="paytype='wxpay'">
						<view class="left">
							<image mode="widthFix" style="border-radius: 50%;" src="../../../static/img/unionpay.png">
							</image>
						</view>
						<view class="center">
							银联支付
						</view>
						<view class="right">
							<radio :checked="paytype=='wxpay'" color="#0083f5" />
						</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 {
		addOrder,
		orderToPay,
		unionPay
	} from "../../../Utils/Api.js";
	// #ifdef H5
	var jweixin = require('jweixin-module');
	// #endif
	export default {
		data() {
			return {
				inputAmount: '', //金额
				amountList: [100, 200, 300, 500, 800, 1000], //预设3个可选快捷金额
				paytype: 'wxpay', //支付类型
				user: "",
				imagewxUrl: app.globalData.imageWxImg,
				imgadres: 'wxpay.png'
			};
		},
		onLoad() {
			this.user = app.globalData.userInfo
		},
		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;
				}
				uni.showLoading({
					title: '正在提交订单...'
				})
				let goods = {
					"identificationCode": app.globalData.identificationCode,
					"highChildOrderList": [{
						"goodsType": 2,
						"goodsId": app.globalData.userInfo.id,
						"saleCount": 1,
						"goodsPrice": this.inputAmount
					}]
				}
				addOrder(goods).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000') {
						//模板模拟支付，实际应用请调起微信/支付宝
						this.unionPay(res.return_data.id);
					} else {
						uni.showToast({
							title: res.return_msg
						});
					}
				})
			},
			//银联获取信息
			unionPay(item) {
				let goods = {
					"orderId": item
				}
				unionPay(goods).then(res => {
					if (res.return_code == '000000') {
						this.uniontopay(res.return_data.prepayid);
					} else {
						uni.showToast({
							title: res.return_msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			//银联支付
			uniontopay(item) {
				let that = this;
				upsdk.pluginReady(function() {
					upsdk.pay({
						tn: item,
						success: function(res) {
							uni.showToast({
								title: '支付成功'
							})
							uni.navigateBack({})
						},
						fail: function(err) {
							uni.showToast({
								title: err.msg,
								icon: 'none',
								duration: 2000
							})
							// 支付失败, err.msg 是失败原因描述, 比如TN号不合法, 或者用户取消了交易 等等。
						}
					});
				});
			}
		},

	}
</script>

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

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

		.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;
						// line-height: 120upx;
						display: inline-grid;
						justify-content: center;
						align-items: center;
						text-align: center;
						box-shadow: 0upx 0upx 4upx #666666;
						font-size: 36upx;
						color: #089bf5;
						background-color: #f1f1f1;
						&.on {
							background-color: #089bf5;
							color: #ffffff;
						}
					}
				
					.heTitle {
						width: 100%;
						font-size: 15px;
					}
				}

				.num {
					margin-top: 20upx;
					display: flex;
					justify-content: flex-end;
					align-items: center;
				
					.text {
						padding-right: 10upx;
						font-size: 40upx;
					}
				
					.input {
						width: 28.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: 20upx;
		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>
