<template>
	<view class="container">
		<view class="infoBox">
			<view class="title">{{$t("transfer.收款地址")}}</view>
			<view class="inputBox">
				<view class="flex_item inpitTpl">
					<image src="/static/img/img21.png" style="width: 33rpx;height: 36rpx;margin-right: 25rpx;"></image>
			      <input type="text" :placeholder='$t("transfer.请输入或长按粘贴地址")' v-model="address" style="width: 100%;"/>
				</view>
			</view>
		</view>
		<view class="infoBox">
			<view class="title">{{$t("transfer.转账金额")}}</view>
			<view class="inputBox flex">
				<view class="flex_item inpitTpl">
					<image src="/static/img/img21.png" style="width: 33rpx;height: 36rpx;margin-right: 25rpx;"></image>
			      <input type="text" :placeholder='$t("transfer.请输入转账金额")' v-model="money"/>
				</view>
				<view class="all" @click="money = userInfo.NUSD * 1">{{$t("transfer.全部")}}</view>
			</view>
			<view class="tipTpl">{{$t("transfer.可转账余额")}}：{{userInfo.NUSD * 1}}NUSD</view>
		</view>
		<!-- <view class="infoBox">
			<view class="title">手续费</view>
			<view class="inputBox flex">
				 <input type="text" v-model="free"/>
				 <view class="tip">USTD</view>
			</view>
		</view> -->
		<view class="footBox">
			<!-- <view class="flex numBox">
				<view class="numName">实际到账</view>
				<view class="num">{{num}}</view>
			</view> -->
			<view class="btnBox">
				<view class="submitBtn" :class="{ submitNo: !payOn }" @click="payOn ? transfer() : ''">{{$t("property.转账")}}</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {getUserInfo,trade} from '@/api/login.js';
	export default {
		data () {
			return {
				address:"",
				money:"",
				free:"--",
				num:"--",
				userInfo:'',
				payOn:true
			}
		},
		onLoad () {
			this.getUserInfo()
		},
		onShow () {
			uni.setNavigationBarTitle({
				title: this.$t("property.转账")
			});
		},
		methods: {
			transfer(){
				let obj = this
				if(obj.address == ''){
					uni.showToast({
						title: this.$t("transfer.请输入收款地址"),
						icon: "none",
					});
					return;
				}
				if(obj.money == ''){
					uni.showToast({
						title: this.$t("transfer.请输入转账金额"),
						icon: "none",
					});
					return;
				}
				obj.payOn = false
				ethereum.request({
					method: 'eth_requestAccounts'
				}).then((account) => {
					const PKR_TRADE = 'PKR_TRADE' + (new Date()).getTime();
					ethereum.request({
						"method": "personal_sign",
						"params": [
							PKR_TRADE,
							account[0]
						]
					}).then((res) => {
						obj.trade(res,PKR_TRADE);
					});
				});
			},
			trade(sign,msg){
				let obj = this
				trade({
					sign,
					msg,
					address:obj.address,
					token:'NUSD',
					num:obj.money,
				}).then(function(res){
					if(res.code == 0){
						obj.payOn = true
						uni.showToast({
							title: obj.$t("transfer.参数错误"),
							icon: "error",
						});
					}else{
						uni.showToast({
							title:obj.$t("transfer.转账成功"),
							icon: "success",
						});
						obj.payOn = true
						setTimeout(function () {
							obj.$router.go(0)
						}, 1000);
					}
				}).catch(e => {
					obj.payOn = true
					console.log(e,11111);
				});
			},
			getUserInfo(){
				let obj = this
				getUserInfo({}).then(({data}) => {
					obj.userInfo = data
				}).catch(e => {
					console.log(e.msg);
				});
			},
		},
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		.container {
			width: 100%;
		}
	}
	.infoBox{
		color: #fff;
		padding: 58rpx 38rpx 0rpx 38rpx;
		.title{
			font-weight: bold;
			font-size: 32rpx;
			padding-bottom: 10rpx;
		}
		.inputBox{
			border-bottom: 1rpx solid #CBCBCA;
			padding: 30rpx 0rpx;
			.all{
				font-weight: 500;
				font-size: 28rpx;
				color: #ABB1CC;
				background: linear-gradient(258deg, #FFF0CF 0%, #CBA16B 30.0048828125%, #FCE9CF 67.67578125%, #C29963 100%);
				-webkit-background-clip: text;
				-webkit-text-fill-color: transparent;
			}
			.tip{
				font-weight: 500;
				font-size: 30rpx;
				color: #888785;
			}
		}
		.tipTpl{
			font-weight: 500;
			font-size: 24rpx;
			color: #888785;
			padding-top: 15rpx;
		}
	}
	.footBox{
		position: fixed;
		bottom: 0;
		width: 100%;
		background: #1A1A17;
		color: #fff;
		padding: 50rpx 42rpx;
		.numBox{
			.numName{
				font-weight: 500;
				font-size: 26rpx;
				color: #888785;
			}
		}
		.btnBox{
			.submitBtn{
				background: linear-gradient(258deg, #FFF0CF, #CBA16B, #FCE9CF, #C29963);
				border-radius: 10rpx;
				font-weight: bold;
				font-size: 32rpx;
				color: #31190B;
				text-align: center;
				padding: 20rpx 0rpx;
			}
			.submitNo {
				background: #999999 !important;
				color: #fff !important;
			}
		}
	}
</style>