<template>
	<view>
		<u-navbar title="充值">
		</u-navbar>
		<view class="recharge">
			<view class="recharge-text">
				<p>账单余额（元）</p>
				<h1>{{detail.balance}}</h1>
			</view>
			<view 
			 @click="gotoUrl('pages/user/rechargeList')"
			class="recharge-btn">
				账单
			</view>
		</view>
		<view class="charge">
			<view class="chargeTit">
				选择充值金额
			</view>
			<view class="chargeMain">
				<template v-for="(item,i) in list">
				
				<view  :key="i" :class="{
					'active':chargingMarketingId==item.id
				}"  @click="chargingMarketingId=item.id,selectItem=item"
				class="chargeMain-item "><p>{{item.rechargeAmount}}元</p> <span v-if="item.giftAmount">赠{{item.giftAmount}}元</span></view>
				
				
				</template>
			</view>
		</view>
		<view style=" padding-bottom: 120px; ">
			<view class="charge">
			<view class="chargeRadio">
				<view class="u-flex">
					<u-icon name="weixinzhifu" custom-prefix="custom-icon" color="#22ac38" size="80"></u-icon>
					<view class="chargeRadio-text">
						<h4>微信支付</h4>
						<p>推荐微信支付</p>
					</view>
				</view>
				<u-radio-group>
					<u-radio></u-radio>
				</u-radio-group>
			</view>
		</view>
			
		</view>
		
		<view class="foot-btn">
			<view class="foot-pirce">
				<u-icon name="tikuan" custom-prefix="custom-icon" color="#FF9502" size="48"></u-icon>
				<span>支付 ￥{{selectItem.rechargeAmount}}</span>
			</view>
			<u-button type="primary" @click="submit" :custom-style="customStyle" shape="square">确定充值</u-button>
		</view>
	</view>
</template>

<script>
	import * as Pay from '@/apis/weixin.js'
	import {
		wxPayJs
	} from '@/utils/wxpay'
	import * as API from '@/apis/index.js'
	
	export default {
		data() {
			return {
				isReady:false,
				customStyle: {
					background: '#1677ff'
				},
				detail:{},
				chargingMarketingId:'',
				selectItem:{},
				list:[],
			}
		},
		onReady() {
			this.init();
		},
		methods: {
			submit(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				Pay.wxpay(this.chargingMarketingId).then((response) => {
					if(!response.result){		
						uni.showToast({
							title:  response.message
						})
						return
					}
					var data = response.data
					uni.hideLoading()
					console.log("Pay+"+new Date().getTime())		
					wxPayJs(data);
						
				}).catch(error => {
					this.$refs.common.showLoading(false, error);
						
				})
			},
			init1(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
			
				API.personalCenter().then((res) => {
					this.detail = res.data
						this.isReady=true;
					uni.hideLoading()
					
				}).catch(error => {
					uni.showToast({
						title: error
					})
				})
			},
			init(){
				
				
				
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				var data = {
					
				};
				
				
				API.marketingData(data).then((res) => {
						
					this.list = res.data.chargingMarketingList
					if(this.list.length>0){
						this.selectItem=this.list[0];
						this.chargingMarketingId=this.list[0].id
					}
					uni.hideLoading()
					this.init1()
				}).catch(error => {
					uni.showToast({
						
						title: error
					})
				})
			}
		},onShow(){
			if(this.isReady){
				this.init1()
			}
		}
	}
</script>
<style>
	page{
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	.recharge{
		padding: 15px;
		background-color: #1677FF;
		margin-bottom: 10px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.recharge-text{
			color:#fff;
		}
		.recharge-btn{
			width: 60px;
			height: 30px;
			text-align: center;
			line-height: 30px;
			background-color: #589EFF;
			color:#fff;
			border-radius: 15px;
		}
	}
	.charge{
		padding: 15px;
		background-color: #fff;
		margin-bottom: 10px;
	}
	.chargeRadio{
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		.chargeRadio-text{
			margin-left: 5px;
			h4{
				font-weight: normal;
				font-size: 15px;
			}
			p{
				font-size: 12px;
				color:#999;
				margin-top: 5px;
			}
		}
	}
	.chargeTit{
		border-bottom: 1px solid #f7f7f7;
		padding-bottom: 10px;
		font-size: 15px;
	}
	.chargeMain{
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin-top: 20px;
		.chargeMain-item{
			width: 48%;
			text-align: center;
			padding: 25px 10px;
			border: 2px solid #cacaca;
			margin-bottom: 10px;
			border-radius: 10px;
			color:#333;
			position: relative;
			p{
				font-size: 20px;
			}
			span{
				background-color: #ff8d00;
				color:#fff;
				padding: 2px 10px;
				border-radius:0 10px 0 10px;
				position: absolute;
				font-size: 12px;
				right: -2px;
				top: -2px;
			}
		}
		.active{
			border-color:#ff8d00;
			color:#ff8d00;
		}
		.fault{
			background-color:#e1e1e1;
			color:#666;
			border: 1px solid #ccc;
		}
		.occupy{
			color:#FF4F3F;
			border: 1px solid #FF4F3F;
		}
	}
	.foot-btn{
		padding: 10px;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #fff;
		.foot-pirce{
			display: flex;
			align-items: center;
			span{
				margin-left: 5px;
				font-size: 16px;
			}
			padding-bottom: 10px;
			border-bottom: 1px solid #f7f7f7;
			margin-bottom: 10px;
		}
	}
</style>
