<template>
	<view class="content">
		<navigation>
			Recharge
			<view class="bread" slot="bread">
				<image src="../../../static/promo/nav_icon_more@2x.png" mode=""></image>
			</view>
		</navigation>
		<view class="rech-body">
			<view class="pro-body-p1">Balance:<span data-v-642729fd="" class="category-sigh">{{currencySymbol}}</span><span
					class="s1">{{currentBalance}}</span></view>
			<view class="input-item flex">
				<view class="login-sigh login-sigh2  flex">
					<view class="imgcon">
						<image src="../../../static/login/list_icon_card@2x.png" mode=""></image>
					</view>
				</view>
				<input type="text" value="" placeholder="Enter or Select Recharge Amount"
					placeholder-style="color: #BFC2CCFF;font-size:14px;" v-model="tranAmt" />
			</view>

			<view class="rech-input-con">
		<!-- 		<view class="input-item flex">
					<input type="text" value="" placeholder="Name"
						placeholder-style="color: #BFC2CCFF;font-size:14px;" />
				</view> -->
				<view class="input-item flex" v-show="pd==0">
					<view class="login-sigh flex">
						<span class="quhao">+{{areacode}}</span>
					</view>
					<input type="text" value="" placeholder="mobile number"
						placeholder-style="color: #BFC2CCFF;font-size:14px;" v-model="mobile" />
				</view>
			<!-- 	<view class="input-item flex">
					<input type="text" value="" placeholder="email"
						placeholder-style="color: #BFC2CCFF;font-size:14px;" />
				</view> -->
			</view>


		</view>

		<view class="rech-mount flex" v-show="pd==0">
			<view class="mount" v-for="(item,index) in recharge_amount" :key="item" @click="getMonVal(item)">
				{{currencySymbol}} {{item}}
			</view>
		</view>

		<view class="rech-capy">
			<view class="payment">
				payment
			</view>
			<view class="paymentType">
				<u-radio-group  @change="radioGroupChange" class="payTypenName">
					<u-radio @change="radioChange(item)" v-for="(item, index) in paymentType" :key="index"
						:name="item.typeName" :disabled="item.disabled" active-color="#954DE3FF">
						{{item.typeName}}
					</u-radio>
				</u-radio-group>

			</view>
		</view>
		<view class="rech-btn-con" v-show="pd==0">
			<view class="rech-btn" @click="PayTo()">
				Confirm
			</view>
		</view>
		<view class="offline-text" v-show="pd==1">
			You can follow the steps to pay: 
		  <view class="">
		  		1. Go to your M-Pesa menu or App on your phone.
		  </view>
			 <view class="">
			 	2. Go to Lipa na M-Pena then select "Pay Bill" option. 
			 </view> 
			 <view class="">
			 	3. Enter business number: {{mobile}} <button type="default" class="offline-button" @click="copyTextMethod()">Copy</button> Account number: Enter your  ID : {{userId}}
			 </view>
			<view class="">
						5. Enter the amount 
			</view>
	          <view class="">
	          	6. Enter your M-Pesa PIN. 
	          </view>
			 <view class="">
			 			7. Confirm that all details are correct and submit.
			 </view>
	
		</view>
	</view>

</template>

<script>
	import api from '@/utils/api'
	import utils from '@/utils'
	import cont from "@/components/navigation/navigation.vue"
	export default {
		data() {
			return {
				copyText: '',
				pd:0,
				mobile:'',
				areacode: '',
				tranAmt: '',
				rechangeInfo: [],
				recharge_amount: [],
				paymentType: [],
				payType: '',
				currencySymbol:'',
				currentBalance:'',
				userId:'',
			}
		},

		onLoad() {
			this.copyText=uni.getStorageSync('loginMobile');
            this.mobile=uni.getStorageSync('loginMobile');
			this.currentBalance=uni.getStorageSync('currentBalance');
			this.areacode = uni.getStorageSync('siteInfo').areaCode;
            this.currencySymbol=uni.getStorageSync('siteInfo').currencySymbol;
			this.userId=uni.getStorageSync('MyInfo').userId;
			const post = api.rechargeConfig({
				"ticket": uni.getStorageSync('logInfo').data,
			})
			post.then(res => {
				uni.setStorage({
					key: 'rechargeInfo',
					data: res.data,
					success: function() {
						console.log('success')
					}
				});
				this.rechangeInfo = res.data;
				this.recharge_amount = this.rechangeInfo.recharge_amount.split(',');
				console.log(res.data, 9990);
				})
				.catch(e => {
					uni.showToast({
						title: e,
						icon: 'none',
						duration: 2500
					})
				})
			
			const post2 = api.getPayType({
				"ticket": uni.getStorageSync('logInfo').data,
			})
			post2.then(res => {
			this.paymentType = res.data;
			console.log(res.data);
				})
				.catch(e => {
					uni.showToast({
						title: e,
						icon: 'none',
						duration: 2500
					})
				})
			

		},
		methods: {
			copyTextMethod() {
				this.$copyText(this.copyText).then(res => {
					uni.showToast({
						title: 'Copy succeeded',
						icon: 'success'
					})
				})
			},
			PayTo(){
				uni.showLoading({
					title:'loading'
				})
				if(this.payType==''){
					uni.showToast({
						title: 'Please choose the payment method',
						 icon: 'none',
						duration: 1500
					})
					return;
				}
				if(this.mobile==''){
					uni.showToast({
						title: 'Please input mobile phone number',
						 icon: 'none',
						duration: 1500
					})
					return;
				}
				var a= /^[0-9]{4,14}$/;
				if(!a.test(this.mobile)){
				uni.showToast({
					title: 'Wrong format of mobile phone number',
					 icon: 'none',
					duration: 1500
				})
				return;
				}
				if(this.tranAmt==''){
					uni.showToast({
						title: 'Please input recharge amount',
						 icon: 'none',
						duration: 1500
					})
					return;
				}
				if(this.tranAmt<100){
					uni.showToast({
						title: 'Recharge amount cannot be less than 100',
						 icon: 'none',
						duration: 1500
					})
					return;
				}
				
				const post2 = api.recharge({
					"ticket": uni.getStorageSync('logInfo').data,
					"payType":this.payType,
					"tranAmt":this.tranAmt,
					"mobile":this.mobile,
				})
				post2.then(res => {
			if(res.success==true){
							uni.showToast({
								title: 'Recharge success',
								 icon: 'success',
								duration: 1500
							})
							setTimeout(function(){
								location.reload()
							},1000)
						}else{
							uni.showToast({
								title: 'Payment failed',
								 icon: 'none',
								duration: 1500
							})
                        }
							})
				            .catch(e => {
					uni.showToast({
						title: e,
						icon: 'none',
						duration: 2500
					})
				})
					},
			
			radioChange(e) {
				if(e.payType=='pay.offline'){
					this.pd=1;
					this.payType=e.payType
				}else{
					this.pd=0;
					this.payType=e.payType
				}
				
				console.log(e)
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				console.log(this.payType,8888888888888)
			
			},
			getMonVal(m) {
				this.tranAmt = m
			}

	},
	}
</script>

<style>
	@import './index.css';
</style>
