<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true"
		:title="$t('recharge.title')" backgroundColor="#fff" :statusBar="true">
			<template v-slot:right>
			    <image style="width: 40rpx; height: 40rpx;" src="/static/images/r2.png" mode="cover" @click="onTokenJump('/pages/wallet/fundrecords?id=2')"></image>
			</template>
		</uni-nav-bar>
		<view v-if="show==2" style="background-color: #fefefe; width: 90%; margin: 40rpx auto; border: 1px solid #1D61E7; border-radius: 40rpx; padding: 34rpx 60rpx; text-align: center;">
			<span style="font-weight: 600; line-height: 50rpx; display: block; padding-left: 34%;">
				<uni-countdown :show-day="false" color="#1D61E7" :font-size="24" :showHour="false" :minute="time" :second="0" style="margin: auto;" />
			</span>
			<span style="color: #1D61E7; font-size: 28rpx; font-weight: 400;">Available balance</span>
		</view>
		<view v-if="show==1" style="width: 90vw; margin: 40rpx auto 20rpx auto;">
			<uni-forms :modelValue="rechargeModel" label-position="top">
				<uni-forms-item :label="$t('recharge.text12')" labelWidth="280rpx">
					<view>
						<uni-easyinput type="number" style="height: 80rpx;" v-model="rechargeModel.amount" :placeholder="$t('putforward.text10', {par: init.recharge_minimum})">
							<template #right>
								<view style="float: left;  font-size: 24rpx; margin-right: 20rpx;">USDT</view>
							</template>
						</uni-easyinput>
					</view>
				</uni-forms-item>
				<uni-forms-item :label="$t('recharge.text13')" labelWidth="280rpx">
					<view>
						<uni-row @click.native="onPageJump('/pages/wallet/switchnetwork')" style="border: 1px solid #eee; height: 80rpx; line-height: 80rpx; border-radius: 10rpx;">
							<uni-col :span="22" style="padding-top: 20rpx; padding-left: 20rpx;">
								<image v-if="rechargeModel.network === 'TRC-20'" style="width: 40rpx; height: 40rpx; float: left;" src="/static/images/s1.png" mode="cover"></image>
								<image v-if="rechargeModel.network === 'BEP-20'" style="width: 40rpx; height: 40rpx; float: left;" src="/static/images/s2.png" mode="cover"></image>
								<view style="font-weight: 600; line-height: 40rpx; font-size: 28rpx; color: #333333; float:left; margin-left: 20rpx;">{{rechargeModel.name}}</view>
							</uni-col>
							<uni-col :span="2">
								<uni-icons type="right" size="20"></uni-icons>
							</uni-col>
						</uni-row>
					</view>
				</uni-forms-item>
				
			</uni-forms>
		</view>
		<view v-if="show==2">
			<view style="text-align: center; margin-top: 20rpx;">
				<view v-if="show=2" style="text-align: center; width: 260rpx; margin: auto;">
				    <tki-qrcode ref="qrcode" :val="qrcode" :size="size" :unit="unit" :onval="onval" :load-make="load_make" :show-loading="show_loading" :lv="lv" :background="background" :foreground="foreground" :pdground="pdground" :icon='icon' :iconsize='iconsize' />
				</view>
			</view>
			<view style="font-size: 32rpx; color: #999; text-align: center; margin: 20rpx;">{{$t('recharge.text3')}} 
			<span style="color: #1D61E7; font-weight: 600; margin-left: 20rpx;">{{rechargeModel.amount}} USDT</span></view>
			<view style="width: 92%; margin: 20rpx auto; background-color: #F8F8F8; padding: 30rpx 20rpx;">
				<view style="width: 70%; float: left;">
					<view style="color: #999; font-size: 28rpx;">{{$t('recharge.text4')}}</view>
					<view style="width: 100%; font-size: 32rpx; color: #333; font-weight: 600; margin-top: 20rpx; white-space: pre-wrap; word-break: break-all;">{{qrcode}}</view>
				</view>
				<view>
					<button type="primary" @click="copy(qrcode)" style="background-color: #1D61E7; margin-top: 50rpx; padding: 0rpx 40rpx; line-height: 70rpx; border-radius: 20rpx; color: #fff; float: right;">{{$t('copy')}}</button>
				</view>
				<view style="clear: both;"></view>
			</view>
		</view>
		<view style="width: 92%; margin: auto;">
			<view class="cell_list">
				<view style="color: #333; font-size: 36rpx; font-weight: 600;">{{$t('recharge.text5')}}</view>
			</view>
			<view style="color: #999; line-height: 60rpx; font-size: 32rpx; font-weight: 400; padding: 10rpx 20rpx;">
				1. {{$t('recharge.text6')}}<br>
				2. {{$t('recharge.text7')}}<br>
				3. {{$t('recharge.text8')}}<br>
				4. {{$t('recharge.text9')}}
			</view>
		</view>
		<view style="padding-bottom: 20rpx;">
			<button type="primary" v-if="show==1" round style="width: 90%; margin: 20rpx auto;" @click="submit">{{$t('recharge.title')}}</button>
			<button type="primary" v-if="show==2" round style="width: 90%; margin: 20rpx auto;" @click="onPageJump('/page/wallet/index')">{{$t('btnClose')}}</button>
		</view>
		
	</view>
</template>

<script>
	import tkiQrcode from "tki-qrcode"
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {
			tkiQrcode
		},
		data() {
			return {
				show:1,
				rechargeModel:{
					name:"Tron(TRC20)",
					network: 'TRC-20',
					amount: '',
					address:"",
					images:"",
					time:"0"
				},
				
				init: {},
				qrcode: '', //二维码的内容链接
				size: 260, //二维码的大小
				unit: 'upx', //大小单位 !!! rpx单位有误
				load_make: true, //组件加载完成后自动生成二维码
				show_loading: false, //是否添加loading
				onval: true, //val值变化时自动重新生成二维码
				background: '#ffffff', //背景色
				foreground: '#000',//点色
				pdground: '#000', //角标色
				icon: '', //二维码图标
				iconsize: 30, //二维码图标大小
				lv: 3, //二维码容错级别
				loadingText: '加载中', //loading内容
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
			this.$http.get('/api/common/init?lang='+this.$i18n.locale).then(res => {
				this.init = res.data;
			});
			if(e.network){
				if(e.network != this.rechargeModel.neetwork){
					this.rechargeModel.network=e.network;
					if(this.rechargeModel.network=='TRC-20'){
						this.rechargeModel.name = "Tron(TRC20)";
						this.rechargeModel.network = "TRC-20";
					}else{
						this.rechargeModel.name="BSC-BNB(BEP20)";
						this.rechargeModel.network = "BEP-20";
					}
				}
			}
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			onPageJump(url) {
				uni.navigateTo({
					url: url.indexOf('switchnetwork') > 0 ? url + "?page=recharge&network="+this.network : url+"?page=recharge"
				});
			},
			goto(url, type) {
			    if (type == 2) {
			        return uni.switchTab({ url: url })
			    }
			    if (type == 1) {
			        return uni.navigateBack({ delta: url });
			    }
			    uni.navigateTo({
			        url: url
			    })
			},
			onTokenJump(url) {
				if(this.userInfo.token === typeof(undefined) || this.userInfo.token === undefined){
					uni.showModal({
						title: this.$t('gotoLogin.title'),
						content: this.$t('gotoLogin.content'),
						confirmText: this.$t('gotoLogin.login'),
						cancelText: this.$t('gotoLogin.cancel'),
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: "/pages/mine/login"
								});
							}
						}
					});
				}else{
					uni.navigateTo({
						url: url
					});
				}
			},
			submit(){
				if(this.rechargeModel.amount == ''){
					uni.showToast({
						icon: 'error',
						title: this.$t('recharge.text10')
					});
					return;
				}
				if (parseFloat(this.rechargeModel.amount) < parseFloat(this.init.recharge_minimum)) {
					uni.showToast({
						icon: 'error',
						title: this.$t('recharge.text11') + this.init.recharge_minimum
					});
					return;
				}
				let data = {
					amount: this.rechargeModel.amount,
					network: this.rechargeModel.network,
					lang: this.$i18n.locale
				}
				this.$http.post('/api/recharge/create', data).then(res => {
					if(res.code == 0){
						this.show = 2;
						this.time = 15;
						this.qrcode = res.data.order.address;
						this.icon = this.rechargeModel.network == 'TRC-20' ? '/static/images/s1.png' : '/static/images/s2.png'
					}else{
						if(this.$i18n.locale == 'zh'){
							console.log(res.msg)
							if(res.msg.indexOf('limit of uncompleted orders') > 0){
								uni.showToast({
									title: '订单已达上限，请完成该订单或者15分钟后再试'
								})
							}else{
								uni.showToast({
									title: res.msg
								})
							}
						}else{
							uni.showToast({
								title: res.msg
							})
						}
					}
				});
			},
			copy(val = ''){
				// #ifdef APP-PLUS
				uni.setClipboardData({
				    data: val, // e是你要保存的内容
				    success: function () {
						uni.showToast({
							title:this.$t('copySuccess'),
							icon:'none'
						})
				    }
				})
				// #endif
				// #ifdef H5
				this.$copyText(val).then(res => {
					uni.showToast({title: this.$t('copySuccess'), icon: 'none'});
				});
				// #endif
			},
		},
		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//页面下来刷新
		onPullDownRefresh() {},
		//页面上拉触底
		onReachBottom() {},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';
	body{background-color: #fff;}
	.rechargeTitle{
		font-weight: 400;font-size: 32rpx;color: #999999;line-height: 46rpx; text-align: center; margin-top: 60rpx;
	}
	.rechargeNet{
		font-weight: 600;font-size: 40rpx;color: #333;line-height: 46rpx; text-align: center; margin-top: 40rpx;
	}
	::v-deep .uni-input-input{ height: 80rpx; line-height: 80rpx;}
	::v-deep .uni-input-wrapper{height: 80rpx;line-height: 80rpx;}
	::v-deep .uni-easyinput__content{height: 80rpx;line-height: 80rpx;}
	::v-deep .uni-easyinput__content-input{height: 80rpx;line-height: 80rpx;}
</style>