<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true"
		:title="$t('transfer.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=4')"></image>
			</template>
		</uni-nav-bar>
		<view style="width: 94%; margin: 20rpx auto; padding: 30rpx 10rpx;">
			<uni-forms :modelValue="transferModel" label-position="top" :rules="rules" ref="form">
				<uni-forms-item :label="$t('transfer.text7')" name="email" labelWidth="400rpx">
					<uni-combox :candidates="formattedItems" :placeholder="$t('transfer.text8')" v-model="transferModel.email"></uni-combox>
				</uni-forms-item>
				<uni-forms-item :label="$t('transfer.text9')" labelWidth="400rpx" name="amount">
					<view>
						<uni-easyinput type="number" v-model="transferModel.amount" :placeholder="$t('transfer.text10')">
							<template #right>
								<view style="float: left;  font-size: 24rpx;">USDT</view>
								<view style="margin:20rpx; float: right;">
								<span class="s" @click="max">Max</span></view>
							</template>
						</uni-easyinput>
					</view>
					<view style="font-size: 28rpx; color: #999; font-weight: 500; line-height: 50upx; margin-top: 10rpx;">{{$t('transfer.text11')}}：
					<span style="color: #1D61E7;">{{user.money }} USDT</span></view>
				</uni-forms-item>
				<uni-forms-item :label="$t('transfer.text3')" name="pincode">
					<uni-easyinput type="password" :passwordIcon="true" v-model="transferModel.pincode" :placeholder="$t('transfer.text5')" />
				</uni-forms-item>
			</uni-forms>
		</view> 
		<view style="bottom: 0rpx; position: absolute; width:100vw; height: 300rpx;">
			<view class="cell_list">
				<view class="cell_left txt">{{$t('transfer.text12')}}</view>
				<view class="cell_right" style="font-size: 36rpx; font-weight: 600;">{{transferModel.amount}} USDT</view>
			</view>
			<view class="cell_list">
				<button type="primary" round style="width: 100%;" @click="submit">{{$t('buttonSubmit')}}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import { getUserInfo } from '@/config/utils';
	export default {
		data() {
			return {
				productNameStr: '',
				network:'TRC-20',
				transferModel:{
					email:"",
					amount: '',
					pincode:"",
				},
				init:{},
				user:{},
				rules: {
					email: {
						rules:[{required: true,errorMessage: this.$t('transfer.text1')}],
					},
					amount:{
						rules:[
							{required: true,errorMessage: this.$t('transfer.text2')}],
					},
					pincode:{
						rules:[ {required: true,errorMessage: this.$t('transfer.text5')},
								{minLength: 6, maxLength: 6, errorMessage: this.$t('transfer.text6'), }
						],
					}
				},
				candidates:[]
			};
		},
		computed: {
			...mapState(['userInfo']),
			formattedItems() {
			    return this.candidates.map(item => item.username);
			}
		},
		watch: {
		    'transferModel.email'(newVal, oldVal) {
				let data = {
					kw: this.transferModel.email,
					lang: this.$i18n.locale,
				};
				this.$http.get('/api/user/getuserlist', data).then(res => {
					if(res.code == 0){
						this.candidates = res.data;
					}
				});
		    }
		},
		//第一次加载
		onLoad(e) {
			this.$http.get('/api/common/init?lang='+this.$i18n.locale).then(res => {
				this.init = res.data;
			});
			getUserInfo(this.$i18n.locale).then(res => {
				this.user =  res;
			});
			
			let data = {
				kw: this.transferModel.email,
				lang: this.$i18n.locale,
			};
			this.$http.get('/api/user/getuserlist', data).then(res => {
				if(res.code == 0){
					this.candidates = res.data;
				}
			});
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			...mapMutations(['setUserInfo']),
			max(){
				this.transferModel.amount = parseFloat(this.user.money == '' ? 0 : this.user.money);
			},
			goto(url, type) {
			    if (type == 2) {
			        return uni.switchTab({ url: url })
			    }
			    if (type == 1) {
			        return uni.navigateBack({ delta: url });
			    }
			    uni.navigateTo({
			        url: url
			    })
			},
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			onTokenJump(url) {
				this.judgeLogin(() => {
					uni.navigateTo({
						url: url
					});
				});
			},
			submit(form){
				
				if (parseFloat(this.transferModel.amount) > parseFloat(this.user.money)){
					uni.showToast({
						icon: 'error',
						title:this.$t('putforward.text4')
					});
					return;
				}
				
				this.$refs.form.validate().then(res=>{
					var data = {
						username: this.transferModel.email,
						amount: this.transferModel.amount,
						trade_password: this.transferModel.pincode,
						lang: this.$i18n.locale
					};
					this.$http.get('/api/user/user_transfer', data).then(res => {
						if(res.code == 0){
							uni.showToast({
								title: res.msg
							});
							this.transferModel={
								email:"",
								amount: '',
								pincode:"",
							};
							
						}
					});
				}).catch(err =>{console.log(err)})
			}
		},
		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//页面下来刷新
		onPullDownRefresh() {},
		//页面上拉触底
		onReachBottom() {},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';
	body{background-color: #fff;}
	.page{min-height: 100vh;position: relative;}
	.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;
	}
	.txt{
		font-size: 28rpx; color:#999; font-weight: 500;
	}
	.cell_right{
		font-size: 28rpx; color:#333; font-weight: 500;
	}
	.s{
		border-radius: 20rpx; border: 2rpx solid #1D61E7; padding: 4rpx 16rpx; color: #1D61E7; font-size: 28rpx; font-weight: 600;
	}
</style>