<template>
	<view class="content">
		<view class="yue">
			<view class="font">
				当前余额
			</view>
			<view class="money">
				{{ nowmoney | getMoneyStyle }}
			</view>
		</view>
		<view class="row-box">
			<view class="title">充值金额</view>
			<view class="row">
				<text class="tit">￥</text>
				<input class="input" type="number" v-model="money" placeholder="请输入充值金额" placeholder-class="placeholder" />
			</view>
			<view class="xian"></view>
			<view class="moneyBtn-box">
				<view class="moneyBtn" v-for="(item,index) in moneyList" :class="{'current' : choose == index}" :key="index" @click="changemoney(item,index)">
					{{ item }}元
				</view>
			</view>
		</view>

		<view class="list">
			<radio-group @change="tabRadio">
				<!-- #ifndef APP-PLUS -->
				<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="#FF4343" :checked="type == 'weixin'" /></view>
					</view>
				</label>
				<!-- #endif -->
				<!-- <label>
					<view class="box">
						<view class="icon iconfont"><image class="yongjing" src="http://xmpt.liuniu946.com/img/yongjing.png"></image></view>
						<view class="title-box">
							<view class="title"><text>佣金充值</text></view>
							<view class="node">可用佣金￥{{ yongMoney }}</view>
							
						</view>
						<view class="right"><radio value="yongjing" color=" #5dbc7c" :checked="type == 'yongjing'" /></view>
					</view>
				</label> -->
				
			</radio-group>
		</view>
		<button class="add-btn up" :class="{ 'active-bg': payLoding }" @click="!payLoding ? confirm() : ''">确认充值</button>
	</view>
</template>

<script>
	
import { getMoneyStyle } from '@/utils/rocessor.js';
import { userBalance,extractBank } from '@/api/wallet.js'
// #ifdef H5
import { rechargeWechat } from '@/api/wallet.js';
// #endif
// #ifdef MP
import { rechargeRoutine } from '@/api/wallet.js';
// #endif
import { mapState } from 'vuex';
// #ifdef H5

import weixinObj from '@/plugin/jweixin-module/index.js';
// #endif
export default {
	filters: {
		getMoneyStyle
	},
	data() {
		return {
			// #ifndef APP-PLUS
			typeText: 0,
			type: 'weixin',
			// #endif
			// #ifdef APP-PLUS
			typeText: 1,
			type: 'yongjing',
			// #endif
			money: '', //充值金额
			payLoding: false, //是否加载中
			moneyList: [300,200,150,100,50],
			nowmoney: '',
			choose: -1,
			yongMoney:'',
		};
	},
	onLoad(options) {
		const obj = this;
		if(options.type == '1'){
			obj.type = 'yongjing',
			obj.typeText = 1
		}
	},
	onShow() {
		userBalance({}).then(({ data }) => {
			console.log(data)
			this.nowmoney = data.now_money;
		});
		extractBank({}).then(({ data }) => {
			this.yongMoney = (data.commissionCount*1).toFixed(2);//累积佣金
		});
	},
	computed: {
		// #ifdef H5
		...mapState(['weichatObj'])
		// #endif
	},
	methods: {
		// 跳转
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		// 切换选中对象
		tabRadio(e) {
			console.log(e)
			if ( e.detail.value == 'weixin' ) {
				this.typeText = 0
			} 
			else {
				this.typeText = 1
			}
			this.type = e;
			console.log(this.typeText);
		},
		// 提交
		confirm() {
			let obj = this;
			obj.payLoding = true;
			if( obj.money == ''){
				obj.$api.msg('请输入充值金额');
				obj.payLoding = false;
				return;
			}
			if( obj.type == 'yongjin' && obj.money > parseInt(obj.yongMoney) ) {
				obj.$api.msg('佣金不足');
				obj.payLoding = false;
				return;
			}
			// #ifdef H5
			// rechargeWechat({ price: this.money, from: this.type })
			rechargeWechat({ price: this.money, from: 'weixin',
			 type: this.typeText})
				.then(e => {
					console.log(e)
					if ( obj.type == 'weixin' ){
						let da = e.data.data;
						console.log(weixinObj,"weixin")
						weixinObj.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;
					}else {
						uni.navigateTo({
							url: '/pages/money/rechargeSuccess'
						})
						obj.payLoding = false;
						console.log(e, '佣金');
					}
				})
				.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);
		},
		changemoney(item,index){
			this.choose = index;
			this.money = item
		}
	}
};
</script>

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

.add-btn {
	&.modified {
		color: $base-color;
	}
	&.up {
		background: linear-gradient(-35deg, #FF4343, #FF4343);
		color: #fff;
	}
	display: flex;
	align-items: center;
	justify-content: center;
	width: 604rpx;
	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 {
	margin-top: 30rpx;
	padding: 20rpx 0rpx 20rpx 30rpx;
	background: #fff;
	.title {
		font-size: $font-base + 2rpx;
		color: $font-color-dark;
	}
	.row {
		display: flex;
		align-items: center;
		position: relative;
		height: 80rpx;
		.tit {
			flex-shrink: 0;
			width: 40rpx;
			font-size: 30rpx;
			color: $font-color-dark;
		}
		.input {
			flex: 1;
			font-size: 30rpx;
			color: $font-color-dark;
		}
		.iconlocation {
			font-size: 36rpx;
			color: $font-color-light;
		}

		.buttom {
			color: $font-color;
			font-size: $font-base;
		}
	}
	.xian {
		width: 700rpx;
		height: 1rpx;
		background: #E6E6E6;
		margin-bottom: 16rpx;
	}
	.moneyBtn-box {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		.moneyBtn {
			margin-right: 30rpx;
			width: 210rpx;
			height: 70rpx;
			background: #F0F0F0;
			border-radius: 4px;
			margin-top: 30rpx;
			text-align: center;
			line-height: 70rpx;
		}
	}
}
.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;
		}
		.yongjing {
			width: 48rpx;
			height: 48rpx;
			
		}
		.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;
}
.yue {
	display: flex;
	justify-content: space-between;
	padding: 30rpx 48rpx 30rpx 26rpx;
	background: #FFFFFF;
	.font{
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
	}
	.money{
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #EF041F;
	}
}
.current {
	background: linear-gradient(-35deg, #F8DD4F, #FBEB77) !important;
}
</style>
