<template>
	<view class="content">
		<view class="row-box">
			<view class="money-box">
				<view class="money">{{ now_money }}</view>
				<view class="money-text">我的糖果数量</view>
			</view>
			<view class="chong">充值记录</view>
		</view>
		<view class="main-box">
			<view class="main-top flex">
				<view @click="change(item)" class="main-item" v-for="(item, index) in numList" :class="{check: money === item}" :key="index">
					<view class="item-top">
						<view class="bg"><image src="../../static/icon/candy.png" mode=""></image></view>
						<view class="item-name" :class="{checks: money === item}">{{ item }}糖果</view>
					</view>
					<view class="item-price" :class="{checks: money === item}">
						￥
						<text>{{ item }}元</text>
					</view>
				</view>
			</view>
			<view class="list">
				<radio-group @change="tabRadio">
					<label>
						<view class="box">
							<view class="icon iconfont iconzhifubao"></view>
							<view class="title-box">
								<view class="title"><text>支付宝充值</text></view>
							</view>
							<view class="right"><radio value="alipay" color="#EB001C" :checked="type == 'alipay'" /></view>
						</view>
					</label>
					<label>
						<view class="box">
							<view class="icon iconfont iconweixin1"></view>
							<view class="title-box">
								<view class="title"><text>微信充值</text></view>
							</view>
							<view class="right"><radio value="weixin" color=" #EB001C" :checked="type == 'weixin'" /></view>
						</view>
					</label>
				</radio-group>
			</view>
			<button class="add-btn up" :class="{ 'active-bg': payLoding }" @click="!payLoding ? confirm() : ''">确认充值</button>
		</view>
	</view>
</template>

<script>
import { getMoneyStyle } from '@/utils/rocessor.js';
// #ifdef H5
import { rechargeWechat } from '@/api/wallet.js';
// #endif
// #ifdef MP
import { rechargeRoutine } from '@/api/wallet.js';
// #endif
import { mapState } from 'vuex';
export default {
	filters: {
		getMoneyStyle
	},
	data() {
		return {
			type: 'weixin',
			numList: ['10', '20', '50', '80', '100', '200'],
			money: '', //充值金额
			payLoding: false, //是否加载中
			myMoney: 0, //我的糖果数
			money: '10' //充值糖果数
		};
	},
	onLoad(options) {},
	computed: {
		// #ifdef H5
		...mapState(['weichatObj']),
		// #endif
		//我的糖果数
		now_money() {
			return this.$store.state.user.userInfo.now_money
		}
	},
	methods: {
		// 跳转
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		change(item){
			this.money = item
		},
		// 切换选中对象
		tabRadio(e) {
			this.type = e;
		},
		// 提交
		confirm() {
			let obj = this;
			obj.payLoding = true;
			// #ifdef H5
			rechargeWechat({ price: this.money, from: this.type })
				.then(e => {
					let da = e.data.data;
					obj.weichatObj.chooseWXPay({
						timestamp: da.timestamp,
						nonceStr: da.nonceStr,
						package: da.package,
						signType: da.signType,
						paySign: da.paySign,
						success: function(res) {
							uni.showToast({
								title: '充值成功',
								duration: 2000,
								position: 'top'
							});
						}
					});
					obj.payLoding = false;
				})
				.catch(e => {
					obj.payLoding = false;
					console.log(e);
				});
			// #endif
			// #ifdef MP
			rechargeRoutine({ price: this.money })
				.then(e => {
					let da = e.data;
					wx.requestPayment({
						timeStamp: da.timestamp,
						nonceStr: da.nonceStr,
						package: da.package,
						signType: da.signType,
						paySign: da.paySign,
						success: function(res) {
							uni.redirectTo({
								url: '/pages/money/paySuccess'
							});
						}
					});

					obj.payLoding = false;
				})
				.catch(e => {
					obj.payLoding = false;
					console.log(e);
				});
			// #endif
		},
		//获取订单列表
		loadData(source) {
			console.log(source);
			//这里是将订单挂载到tab列表下
			let index = this.tabCurrentIndex;
			let navItem = this.navList[index];
			let state = navItem.state;
			if (source === 'tabChange' && navItem.loaded === true) {
				//tab切换只有第一次需要加载数据
				return;
			}
			if (navItem.loadingType === 'loading') {
				//防止重复加载
				return;
			}
			navItem.loadingType = 'loading';
			setTimeout(() => {
				let orderList = [];
				orderList.forEach(item => {
					navItem.orderList.push(item);
				});
				//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
				this.$set(navItem, 'loaded', true);
				//判断是否还有数据， 有改为 more， 没有改为noMore
				navItem.loadingType = 'more';
			}, 600);
		}
	}
};
</script>

<style lang="scss">
page {
	height: 100%;
	background: #ffffff;
}

.add-btn {
	&.modified {
		color: $base-color;
	}
	&.up {
		background-color: $base-color;
		color: #fff;
	}
	display: flex;
	align-items: center;
	justify-content: center;
	width: 690rpx;
	height: 80rpx;
	margin: 0 auto;
	margin-top: 30rpx;
	font-size: $font-lg;
	border-radius: 10rpx;
	// box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
}
.row-box {
	width: 750rpx;
	height: 354rpx;
	background: linear-gradient(0deg, #2e58ff, #32c6ff);
	.money-box {
		padding-top: 80rpx;
		margin: 0 auto;
		text-align: center;
		position: relative;
		.money {
			font-size: 74rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #ffffff;
		}
		.money-text {
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #ffffff;
		}
	}
	.chong {
		position: absolute;
		top: 36rpx;
		right: 0;
		width: 128rpx;
		height: 45rpx;
		line-height: 45rpx;
		background: #2e58ff;
		border-radius: 23rpx 0px 0px 23rpx;
		font-size: 25rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #ffffff;
		text-align: center;
	}
}
.main-box {
	margin-top: -100rpx;
	width: 750rpx;
	background: #ffffff;
	border-radius: 50rpx;
	padding-top: 46rpx;
	.main-top {
		flex-wrap: wrap;
		justify-content: space-around;
		align-items: center;
		.check {
			background: #FFF6F6 !important;
			border: 1px solid #D7272B;
		}
		.main-item {
			text-align: center;
			margin-top: 30rpx;
			width: 220rpx;
			background: #ffffff;
			box-shadow: 0px 2rpx 20rpx 0px rgba(215, 39, 43, 0.12);
			border-radius: 20rpx;
			padding: 46rpx 0 52rpx;
			.item-top {
				display: flex;
				justify-content: center;
				.bg {
					width: 42rpx;
					height: 42rpx;
					image {
						width: 42rpx;
						height: 42rpx;
					}
				}
				.item-name {
					padding-left: 6rpx;
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #333333;
				}
			}
			.checks{
				color: #D7272B !important;
			}
			.item-price {
				padding-top: 14rpx;
				font-size: 20rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #999999;
				text {
					font-size: 28rpx;
				}
			}
		}
	}
}
.list {
	padding-left: 30rpx;
	margin-top: 30rpx;
	background-color: #ffffff;
	.box {
		display: flex;
		align-items: center;
		width: 100%;
		height: 120rpx;
		border-bottom: 1px solid $border-color-light;
		.icon {
			font-size: 48rpx;
			padding-right: 20rpx;
		}
		.iconweixin1 {
			color: #18bf16;
		}
		.iconzhifubao {
			color: #08aaec;
		}
		.title-box {
			flex-grow: 1;
			text-align: left;
			.title {
				font-size: $font-base + 2rpx;
				color: $font-color-base;
			}
			.node {
				font-size: $font-sm;
				color: $font-color-light;
			}
		}
	}
}
/deep/ .uni-radio-input {
	width: 45rpx;
	height: 45rpx;
}

.active-bg {
	background-color: $color-gray !important;
}
</style>
