<template>
	<view class="page">
		<nav-bar :title="$t('recharge.title')" bgColor="#fff">
			 <image slot="right" style="width: 40rpx; height: 40rpx; margin-right: 20px;" src="/static/images/r2.png" mode="cover" @click="onTokenJump('/pages/wallet/fundrecords?id=2')"></image>
		</nav-bar>
		<view class="rechargeTitle">{{$t('recharge.text1')}}</view>
		<view class="rechargeNet" @click="onPageJump('/pages/wallet/switchnetwork')">{{rechargeModel.name}}
		<uni-icons size="20" type="down" style="margin-left: 10px;"></uni-icons></view>
		<view v-if="show==1" style="width: 80vw; margin: 40rpx auto 20rpx auto;">
			<uni-forms :modelValue="rechargeModel" label-position="left">
				<uni-forms-item :label="$t('recharge.text2')" labelWidth="250rpx">
					<view>
						<uni-easyinput type="number" v-model="rechargeModel.amount" :placeholder="$t('putforward.text10', {par: init.recharge_minimum})">
							<template #right>
								<view style="float: left;  font-size: 12px; margin-right: 20rpx;">USDT</view>
							</template>
						</uni-easyinput>
					</view>
				</uni-forms-item>
				<button type="primary" round style="width: 100%;" @click="submit">{{$t('recharge.title')}}</button>
			</uni-forms>
		</view>
		<view v-if="show==2">
			<view style="text-align: center; margin-top: 10px;">
				<view v-if="show=2" style="text-align: center; width: 300rpx; 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: 16px; color: #999; text-align: center;">{{$t('recharge.text3')}} {{init.recharge_minimum}} USDT</view>
			<view style="width: 92%; margin: 10px auto; background-color: #F8F8F8; padding: 15px 10px;">
				<view style="width: 70%; float: left;">
					<view style="color: #999; font-size: 14px;">{{$t('recharge.text4')}}</view>
					<view style="width: 100%; font-size: 16px; color: #333; font-weight: 600; margin-top: 10px; white-space: pre-wrap; word-break: break-all;">{{qrcode}}</view>
				</view>
				<view>
					<button type="primary" @click="copy(qrcode)" style="background-color: #1D61E7; margin-top: 25px; padding: 0px 20px; line-height: 35px; border-radius: 10px; color: #fff; float: right;">{{$t('copy')}}</button>
				</view>
				<view style="clear: both;"></view>
			</view>
		</view>
		<view style="width: 92%; margin: 10px auto;">
			<view class="cell_list">
				<view style="color: #333; font-size: 18px; font-weight: 600;">{{$t('recharge.text5')}}</view>
			</view>
			<view style="color: #999; line-height: 30px; font-size: 16px; font-weight: 400; padding: 5px 10px;">
				1. {{$t('recharge.text6')}}<br>
				2. {{$t('recharge.text7')}}<br>
				3. {{$t('recharge.text8')}}<br>
				4. {{$t('recharge.text9')}}
			</view>
		</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:"/static/images/r1.png"
				},
				
				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 != typeof(undefined) && e.neetwork != undefined){
				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"
				});
			},
			onTokenJump(url) {
				this.judgeLogin(() => {
					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.qrcode = res.data.order.address;
						this.icon = this.rechargeModel.network == 'TRC-20' ? '/static/images/s1.png' : '/static/images/s2.png'
					}else{
						uni.showToast({
							title: res.msg
						})
					}
				});
			},
			copy(val = ''){
				const textArea = document.createElement('textarea');
				textArea.value = val;
				document.body.appendChild(textArea);
				textArea.select();
				try {
					document.execCommand('copy');
					uni.showToast({
						title: this.$t('copySuccess')
					});
				} catch (err) {
					console.error('Could not copy text: ', err);
				}
				document.body.removeChild(textArea);
			},
		},
		//页面隐藏
		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: 16px;color: #999999;line-height: 23px; text-align: center; margin-top: 30px;
	}
	.rechargeNet{
		font-weight: 600;font-size: 20px;color: #333;line-height: 23px; text-align: center; margin-top: 20px;
	}
</style>