<template>
	<view class="main">
		<view class="pr" style="z-index:2">
			<view class="plr15">
				<view class="bdr4 bg-white" style="margin-top: 38rpx;box-shadow: 0 0 50rpx 0 rgba(0, 0, 0, 0.1);">
					<view class="" style="height: 350rpx;">
						<view class="text-df text-bold" style="padding-top: 32rpx; margin-left: 28rpx;">
							兑换金币
						</view>
						<view class="flex flex-wrap align-center justify-around" style="margin-top: 15rpx;margin-left: 28rpx;margin-right: 28rpx">
							<view @click="replaceMoney(item)" :class="num==item.index?'text-lg flex align-center justify-center margin-top moneyActivate':'text-lg flex align-center justify-center margin-top money'"
							 v-for="(item,index) in moneyList" :data-index="index" :key="index" style="width: 174rpx;height: 78rpx;border-radius: 16rpx;">
								{{item.price}}元
							</view>
							<input type="number" placeholder="自定义" class="margin-top padding-lr-sm" style="width: 174rpx;height: 78rpx;border-radius: 16rpx;border: 2rpx solid #C0C0C0;"
							 value="" v-model="inputmoney" />
						</view>
					</view>
					<view class="pr" style="padding: 40rpx 40rpx;background-color: #fff;border-radius: 16rpx; border-top: 2rpx solid #efefef; ">
						<view class="tl f14  flex justify-between align-center">
							<view class="m-gray-letter mt5 f12">支付金额</view>
							<view class="m-gray-big f15 fb pr10">￥{{paymentMoney}}</view>
						</view>
						<view class="tl f14 flex justify-between align-center margin-top-sm">
							<view class="m-gray-letter mt5 f12">到账金额</view>
							<view class="m-gray-big f15 fb pr10">￥{{accountMoney}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="margin-lr margin-top">
			<view class="text-df" style="color: #999999;font-weight: 400;">
				温馨提示
			</view>
			<view class="text-sm" style="color: #999999;font-weight: 400;margin-top: 15rpx;">
				温馨提示信息后台编辑温馨提示信息后台编辑温馨提示信息后台编辑温馨提示信息后台编辑温馨提示信息后台编辑温馨提示信息后台编辑温馨提示信息后。
			</view>
		</view>
		<view class="margin-lr" style="margin-top: 210rpx;">
			<view class="text-sm margin-bottom" style="color: #999999;text-align: center;">
				点击充值按钮即表示已阅读并同意 <text style="color: #0B73B9;">《充值规则》</text>
			</view>
			<u-button size="default" :ripple="true" @click="recharge" :custom-style="customStyle">充值</u-button>
		</view>
		<view class="cu-tabbar-height"></view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			this.paymentMoney = this.moneyList[0].price
			this.accountMoney = this.moneyList[0].price
		},
		data() {
			return {
				paymentMoney: "",
				accountMoney: "",
				inputmoney: '',
				num: 0,
				moneyList: [{
						index: 0,
						price: 0.01
					},
					{
						index: 1,
						price: 100
					},
					{
						index: 2,
						price: 200
					},
					{
						index: 3,
						price: 500
					},
					{
						index: 4,
						price: 1000
					},
				],
				customStyle: {
					backgroundColor: '#0B73B9',
					color: '#FFFFFF'
				},
			};
		},
		watch: {
			inputmoney: function(newtext, jiu) {
				if (typeof newtext === 'string') {
					if (newtext.trim().length !== 0) {
						this.debounce(this.changeStr, 1500);
					} else {}
				}
			}
		},
		methods: {
			replaceMoney(item) {
				this.paymentMoney = item.price
				this.accountMoney = item.price
				this.num = item.index
			},
			debounce(fn, wait) {
				if (this.fun !== null) {
					clearTimeout(this.fun)
				}
				this.fun = setTimeout(fn, wait)
			},
			changeStr() {
				this.paymentMoney = this.inputmoney
				this.accountMoney = this.inputmoney
				if (this.inputmoney == "") {
					this.paymentMoney = 0
					this.accountMoney = 0
				}
			},
			recharge: async function() {
				let res = await this.$request.post("/api/v1/order/topup", {
					amount: this.paymentMoney * 100
				})
				if (res.status == 0) {
					uni.requestPayment({
						timeStamp: res.data.timeStamp,
						nonceStr: res.data.nonceStr,
						package: res.data.package,
						signType: res.data.signType,
						paySign: res.data.paySign,
						success: (res) => {
							if (res.errMsg == "requestPayment:ok") {
								uni.showToast({
									title: "充值成功",
									duration: 1500
								})
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 1500)
							}
						},
						fail: (err) => {
							console.log(err)
						},
						complete: (res) => {
							console.log(res)
						}
					})
				}
			}
		},
		onShow() {

		}
	};
</script>
<style lang="scss">
	@import url("../index/mine.css");

	button {
		border-radius: 15rpx;
	}

	.money {
		border: 2rpx solid #C0C0C0;
	}

	.moneyActivate {
		border: 2rpx solid #0B73B9;
		color: #0B73B9;
		background-color: rgba(11, 115, 185, .1);
	}

	.header-content {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: -1;
		overflow: hidden;
		box-sizing: border-box;
	}

	.header-box {
		box-shadow: 0px 8rpx 34rpx -6rpx rgba(248, 151, 164, 0.13);
	}

	.header-img {
		width: 36rpx;
		height: 36rpx;
	}

	.tip {
		position: absolute;
		top: 0;
		right: 20%;
		bottom: 0;
		background-color: red;
		width: 12rpx;
		height: 12rpx;
		border-radius: 50%;
	}

	.msg {
		top: -3rpx;
		right: -9rpx;
	}

	swiper {
		height: 320rpx;
	}

	.swiper-wrapper {
		padding: 0 10rpx;
		box-sizing: border-box;
	}

	.swiper-item {
		width: 100%;
		height: 300rpx;
		box-shadow: 0 26rpx 40rpx 0 rgba(255, 255, 255, 0.31);
		background: url("http://file.supermm.me/miniProgram/images/personal/bg_doc.png") no-repeat bottom center;
	}

	.dots {
		position: absolute;
		left: 0;
		right: 0;
		bottom: -40rpx;
		display: flex;
		justify-content: center;
	}

	.dots .dot {
		margin: 0 8rpx;
		width: 14rpx;
		height: 14rpx;
		background: #E4E4E4;
		border-radius: 8rpx;
		transition: all .6s;
	}

	.dots .dot.active {
		width: 24rpx;
		background: rgb(153, 153, 153);
	}

	.game-wrap {
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		box-sizing: border-box;
	}

	.game-item {
		height: 100%;
		background: url("https://file.supermm.me/h5/wechat-h5/integral/personeBg001.jpg") no-repeat;
		background-size: 100% 100%;
		background-position: center center;
		border-radius: 100rpx;
	}
</style>
