<template>
	<view class="page">
		<nav-bar title="Transfer" 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=4')"></image>
		</nav-bar>
		<view style="width: 92%; margin: 10px auto; padding: 15px 10px;">
			<uni-forms :modelValue="transferModel" label-position="top" :rules="rules" ref="form">
				<uni-forms-item label="Other email" name="email" labelWidth="400rpx">
					<uni-combox :candidates="formattedItems" placeholder="Enter email" v-model="transferModel.email"></uni-combox>
				</uni-forms-item>
				<uni-forms-item label="Transfer amount" labelWidth="400rpx" name="amount">
					<view>
						<uni-easyinput type="number" v-model="transferModel.amount" placeholder="At least 1U">
							<template #right>
								<view style="float: left;  font-size: 12px;">USDT</view>
								<view style="margin:20rpx; float: right;">
								<span class="s" @click="max">Max</span></view>
							</template>
						</uni-easyinput>
					</view>
					<view style="font-size: 14px; color: #999; font-weight: 500; line-height: 50upx; margin-top: 10rpx;">Available balance：
					<span style="color: #1D61E7;">{{user.money }} USDT</span></view>
				</uni-forms-item>
				<uni-forms-item label="PIN code" name="pincode">
					<uni-easyinput type="password" :passwordIcon="true" v-model="transferModel.pincode" placeholder="Enter PIN code" />
				</uni-forms-item>
			</uni-forms>
		</view> 
		<view style="bottom: 0px; position: absolute; width:100vw; height: 150px;">
			<view class="cell_list">
				<view class="cell_left txt">Expected arrival</view>
				<view class="cell_right" style="font-size: 18px; font-weight: 600;">{{transferModel.amount}} USDT</view>
			</view>
			<view class="cell_list">
				<button type="primary" round style="width: 100%;" @click="submit">Submit</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	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;
			});
			this.$http.get('/api/user/profile?nickname='+this.userInfo.nickname+'&lang='+this.$i18n.locale).then(res => {
				if(res.code == 0){
					this.user = res.data;
					this.user.money = parseFloat(this.user.money).toFixed(4);
					this.setUserInfo(res.data);
				}
			});
			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);
			},
			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: 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;
	}
	.txt{
		font-size: 14px; color:#999; font-weight: 500;
	}
	.cell_right{
		font-size: 14px; color:#333; font-weight: 500;
	}
	.s{
		border-radius: 10px; border: 1px solid #1D61E7; padding: 2px 8px; color: #1D61E7; font-size: 28rpx; font-weight: 600;
	}
</style>