<template>
	<view class="page">
		<nav-bar :title="$t('putforward.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=3')"></image>
		</nav-bar>
		<view style="width: 94%; margin: 10px auto; padding: 20rpx 10rpx;">
			<uni-forms :modelValue="putmodel" label-position="top" :rules="rules" ref="form">
				<uni-forms-item name="address">
					<view style="padding-bottom: 10px;">
						<view style="float: left; font-size: 16px; color: #333; font-weight: 400;">{{$t('putforward.text7')}}</view>
						<view style="float: right; font-size: 14px; color: #333; font-weight: 400;" @click="onPageJump('/pages/wallet/switchnetwork')">
							<image style="width: 18px; height: 18px; float: left;" v-if="network=='TRC-20'" src="/static/images/s1.png" mode="cover"></image>
							<span style="margin-left: 10px; float: left;" v-if="network=='TRC-20'">Tron(TRC20)</span>
							<image style="width: 18px; height: 18px; float: left;" v-if="network=='BEP-20'" src="/static/images/s2.png" mode="cover"></image>
							<span style="margin-left: 10px; float: left;" v-if="network=='BEP-20'">BSC-BNB(BEP20)</span>
							<uni-icons size="20" type="down" style=" margin-left: 10px;"></uni-icons>
						</view>
						<view style="clear: both;"></view>
					</view>
					<view>
						<uni-easyinput type="text" v-model="putmodel.address" :placeholder="$t('putforward.text8')">
							<template #right>
								<picker :range="formattedItems" mode="selector" @change="onPickerChange">
									<image src="/static/images/5.png" style="float: left; width: 40rpx; height: 40rpx; margin-right: 20rpx;"></image>
								</picker>
							</template>
						</uni-easyinput>
					</view>
				</uni-forms-item>
				<uni-forms-item :label="$t('putforward.text9')" labelWidth="400rpx" name="amount">
					<view>
						<uni-easyinput type="number" v-model="putmodel.amount" :placeholder="$t('putforward.text10', {par: withdrawlminimum})">
							<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;">{{$t('putforward.text11')}}：
					<span style="color: #1D61E7;">{{parseFloat(user.money).toFixed(2)}} USDT</span></view>
				</uni-forms-item>
				<uni-forms-item label="PIN code" name="pincode">
					<uni-easyinput type="password" :passwordIcon="true" v-model="putmodel.pincode" :placeholder="$t('putforward.text12')" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<view style="bottom: 0px; position: absolute; width:100vw; height: 180px;">
			<view class="cell_list">
				<view class="cell_left txt">{{$t('putforward.text13')}}</view>
				<view class="cell_right " v-if="showCosts">{{costs}} USDT</view>
				<view class="cell_right " v-else>0 USDT</view>
			</view>
			<view class="cell_list">
				<view class="cell_left txt">{{$t('putforward.text14')}}</view>
				<view class="cell_right" style="font-size: 18px; font-weight: 600;">{{totalAmount}} 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 { getUserInfo } from '@/config/utils';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				network:'TRC-20',
				showCosts: false,
				costs: 0.00,
				totalAmount: 0.00,
				withdrawlminimum: 1,
				putmodel: {
				    address: '',
					addressId: '',
				    amount: '',
				    pincode: '',
				},
				init:{},
				user:{},
				addressList: [],
				rules: {
					address: {
						rules:[{required: true,errorMessage: this.$t('putforward.text1')}],
					},
					amount:{
						rules:[
							{required: true,errorMessage: this.$t('putforward.text2')},
							// {validateFunction:function(rule, value, data, callback){
							// 	if (parseFloat(value) < parseFloat(_this.init.withdrawl_minimum[_this.network])) {
							// 		callback(Error(_this.$t('putforward.text3', {par: _this.init.withdrawl_minimum[_this.network]})))
							// 	}
							// 	return;
							// }}
							],
					},
					pincode:{
						rules:[ {required: true,errorMessage: this.$t('putforward.text5')},
								{minLength: 6, maxLength: 6, errorMessage: this.$t('putforward.text6'), }
						],
					}
				},
			};
		},
		computed: {
			...mapState(['userInfo']),
			formattedItems() {
			    return this.addressList.map(item => `${item.title} (${item.network})`);
			}
		},
		watch: {
		    'putmodel.amount'(newVal, oldVal) {
				if(newVal != ''){
					this.showCosts = true;
				}else{
					this.showCosts = false;
				}
				console.log(parseFloat(newVal == '' ? 0 : newVal), parseFloat(this.costs));
				this.totalAmount = parseFloat(newVal == '' ? 0 : newVal) - parseFloat(this.costs);
				console.log(this.totalAmount);
				if(this.totalAmount < 0){
					this.totalAmount = 0;
				}
		    }
		},
		//第一次加载
		onLoad(e) {
			uni.showLoading({
				title: this.$t('loading')
			});
			this.$http.get('/api/common/init?lang='+this.$i18n.locale).then(res => {
				this.init = res.data;
				if(e.network != typeof(undefined) && e.network != undefined){
					if(e.network != this.neetwork){
						this.network=e.network;
					}
				};
				this.costs = this.init.withdrawl_fee[this.network];
				this.withdrawlminimum = this.init.withdrawl_minimum[this.network];
				//this.totalAmount = parseFloat(this.putmodel.amount == '' ? 0 : this.putmodel.amount) + parseFloat(this.costs);
				let data = {
					lang: this.$i18n.locale,
					network: this.network,
					page: 1,
					limit: 100
				};
				this.$http.post('/api/address/list', data).then(res => {
					if(res.code == 0){
						this.addressList = res.data.data;
					}
					uni.hideLoading();
				}).catch(err => {
					uni.hideLoading();
				}); 
			});
			getUserInfo(this.$i18n.locale).then(res => {
				this.user =  res;
			});
			this.user.money = parseFloat(this.user.money).toFixed(4);
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			...mapMutations(['setUserInfo']),
			max(){
				this.putmodel.amount = parseFloat(this.user.money) ;
			},
			onPickerChange(e) {
				let address = this.addressList[e.detail.value];
				this.putmodel.address = address.address;
				this.putmodel.addressId = address.id;
			},
			onPageJump(url) {
				uni.navigateTo({
					url: url.indexOf('switchnetwork') > 0 ? url + "?network="+this.network+"&page=putforward" : url+"?page=putforward"
				});
			},
			onTokenJump(url) {
				this.judgeLogin(() => {
					uni.navigateTo({
						url: url
					});
				});
			},
			submit(form){
				if (parseFloat(this.putmodel.amount) < parseFloat(this.withdrawlminimum)) {
					uni.showToast({
						icon: 'error',
						title:this.$t('putforward.text3', {par: this.withdrawlminimum})
					});
					return;
				}
				if (parseFloat(this.putmodel.amount) > parseFloat(this.user.money)){
					uni.showToast({
						icon: 'error',
						title:this.$t('putforward.text4')
					});
					return;
				}
				if (parseFloat(this.putmodel.amount) <= this.costs){
					uni.showToast({
						icon: 'error',
						title:this.$t('putforward.text4')
					});
					return;
				}
				this.$refs.form.validate().then(res=>{
					var data = {
						amount: this.putmodel.amount,
						address_id: this.putmodel.addressId,
						trade_password: this.putmodel.pincode,
						lang: this.$i18n.locale
					};
					this.$http.post('/api/withdrawl/create', data).then(res => {
						if(res.code == 0){
							uni.showToast({
								title: res.msg
							});
							this.putmodel= {
								address: '',
								addressId: '',
								amount: '',
								pincode: '',
							}
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/wallet/ExtractResults?id='+res.data.id
								});
							}, 1000);
						}
					});
				}).catch(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: 96vh;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>