<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">
			<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 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: 20rpx;"></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="280rpx">
					<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: 24rpx; 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: 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;">{{$t('recharge.text3')}} {{init.recharge_minimum}} USDT</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: 20rpx 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>
</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:""
				},
				
				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"
				});
			},
			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) {
				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: 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;
	}
</style>