<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true"
		:title="$t('putforward.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=3')"></image>
			</template>
		</uni-nav-bar>
		<view style="width: 94%; margin: 20rpx auto; padding: 20rpx 10rpx;">
			<uni-forms :modelValue="putmodel" label-position="top" :rules="rules" ref="form">
				<uni-forms-item name="address">
					<view style="padding-bottom: 20rpx;">
						<view style="float: left; font-size: 32rpx; color: #333; font-weight: 400;">{{$t('putforward.text7')}}</view>
						<view style="float: right; font-size: 28rpx; color: #333; font-weight: 400;" @click="onPageJump('/pages/wallet/switchnetwork')">
							<image style="width: 36rpx; height: 36rpx; float: left;" v-if="network=='TRC-20'" src="/static/images/s1.png" mode="cover"></image>
							<span style="margin-left: 20rpx; float: left;" v-if="network=='TRC-20'">Tron(TRC20)</span>
							<image style="width: 36rpx; height: 36rpx; float: left;" v-if="network=='BEP-20'" src="/static/images/s2.png" mode="cover"></image>
							<span style="margin-left: 20rpx; float: left;" v-if="network=='BEP-20'">BSC-BNB(BEP20)</span>
							<uni-icons size="20" type="down" style=" margin-left: 20rpx;"></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: 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;">{{$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: 0rpx; position: absolute; width:100vw; height: 360rpx;">
			<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: 36rpx; 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;
				}
				this.totalAmount = parseFloat(newVal == '' ? 0 : newVal) - parseFloat(this.costs);
				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) ;
			},
			goto(url, type) {
			    if (type == 2) {
			        return uni.switchTab({ url: url })
			    }
			    if (type == 1) {
			        return uni.navigateBack({ delta: url });
			    }
			    uni.navigateTo({
			        url: url
			    })
			},
			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: 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>