<template>
	<view class="main">
		<view class="flex">
			<view class="topview">
				<image src="../../../../static/tongyonh/search.png" mode="aspectFit" class="searchimg"></image>
				<u-input @input="getSearchContract()" placeholderStyle="fontSize:12px;color:#D0D0D2" :placeholder="teee" v-model="params.searchName" :focus="true" :type="type" :border="border" class="custom-style"  />
			</view>
			  <text class="text" @click="back">  {{i18n.Cancel}}</text>
		</view>
	<u-empty  font-size="24" :text="aotext" mode="list" v-if="valueNu"></u-empty>
	
	<view class="main_con">
	<scroll-view  :scroll-y="true" style="max-height: 1080rpx;"
						@scrolltolower="scrollToPage" lower-threshold="50">
	<view class="item flex" v-for="item,index in conInfo" :key="index">
		<view class="left flex2">
			<image :src="item.contractIconUrlAbs" mode="aspectFit" class="img"></image>
		    <view class="textcon">
		    	<view class="text1">
		    		{{item.contractName}}
		    	</view>
				<view class="text2">
						{{item.contractAddress}}
				</view>
		    </view>
		</view> 
		<image src="../../../../static/tongyonh/IconGroup.png" mode="aspectFit" class="img" @click="addOther(item,index)" v-if="trueIndex[index]==null" ></image>
	    <image src="../../../../static/tongyonh/backj.png" mode="aspectFit" class="img2" v-if="index==trueIndex[index]" @click="removeOther(item,index)"></image>
	</view>
		</scroll-view>
	</view>
		
	<view class="info_con green"  :class="toTop?'goTop':''">
				<image src="../../../../static/tongyonh/copy_24px_2.png" mode="aspectFit" class="copyImg"></image>
				{{i18n.Addedsuccessfully}}
				</view>
	</view>
</template>

<script>
	export default {
		data() {
			// 
			// No related tokens found
			return {
				teee:this.$t('index').tela,
				coinType:'',
				imgindex:[],
				walletInfo:{},
				coinAddress:'',
				toTop:false,
				tmoney:0,
				trueIndex:[],
				aotext: this.$t('index').thetokenname,
				type: 'text',
				valueNu:true,
				show: false,
				border: false,
				conInfo:[],
			params:{
				pageNum:1,
				pageSize:10,
				orderByColumn:'',
				searchName:'',
			  }
			}
		},
		onPullDownRefresh() {
			this.conInfo=[];
			this.params.pageNum =1;
			this.getSearchContract();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 500);
		},
		computed: {
			i18n() {
				return this.$t('index')
			
			},
		},
		onLoad() {
			// 添加代币修改
			if(uni.getStorageSync('imgindex').length>0){
				this.imgindex=uni.getStorageSync('imgindex')
			}
			if(uni.getStorageSync('trueIndex').length>0){
				this.trueIndex=uni.getStorageSync('trueIndex')
			}
			// 添加代币修改
		this.coinAddress=uni.getStorageSync('wallet').address
		this.coinType=uni.getStorageSync('wallet').type
		this.walletInfo=uni.getStorageSync('walletInfo')
		},
		methods: {
			scrollToPage() {
					this.params.pageNum  = this.params.pageNum + 1;
					this.getSearchContract();
			},
		// 添加代币到钱包
		
		addOther(item,index){
			console.log(index,454545)
			console.log(item,444)
				this.imgindex.push(index)
				// 添加代币修改
				for(var i=0;i<this.imgindex.length;i++){
					if(this.imgindex[i]==index){
						this.trueIndex[index]=index;
						uni.setStorageSync('trueIndex',this.trueIndex)
					}
				}
				// 添加代币修改
				this.toTop=true
				uni.showLoading({
					title: 'loading',
					mask: true
				})
				setTimeout(()=>{
					uni.hideLoading()
					this.toTop=false
				},500)
				uni.setStorageSync('imgindex',this.imgindex)
			// }	
			console.log(this.coinType,444444)
			if(this.coinType=='ETH'){
				const walleti=uni.getStorageSync('walleti')
				const walletInfo=uni.getStorageSync('walletInfo')
				console.log(walleti)
				for(var i=0;i<walletInfo[this.coinType].length;i++){
					if(walletInfo[this.coinType][i].address==walleti){
							 var that=this
								 this.$EthUtil.getTokenBalance(this.coinAddress
								 ,item.contractAddress,function(balance){
									 console.log(balance,123132)
									that.tmoney=balance
								 }); 
							var b={
									name:item.contractName,
									xname:item.chainName,
									icon:item.contractIconUrlAbs,
									contractAddress:item.contractAddress,
									balance:this.tmoney,
								};
								 
							walletInfo[this.coinType][i].coinList.push(b)
							console.log(walletInfo[this.coinType][i],22222)				
							uni.setStorageSync('walletInfo',walletInfo)
							uni.setStorageSync('wallet',walletInfo[this.coinType][i])
					}
				}
				
			}
			
		
		
		
		},
		// 删除eth代币
		removeOther(item,index){
			console.log(index,466666545)
				// 添加代币修改
				for(var i=0;i<this.imgindex.length;i++){
					if(this.imgindex[i]==index){
						console.log(this.imgindex[i])
						console.log(index)
						console.log(this.trueIndex)
						this.trueIndex[index]=null;
						console.log(this.trueIndex)
						uni.setStorageSync('trueIndex',this.trueIndex)
						this.imgindex.splice(i, 1)
						uni.setStorageSync('imgindex',this.imgindex)
						 this.$forceUpdate()
					}
				}	
			if(this.coinType=='ETH'){
				const walleti=uni.getStorageSync('walleti')
				const walletInfo=uni.getStorageSync('walletInfo')
				console.log(walleti)
				for(var i=0;i<walletInfo[this.coinType].length;i++){
					if(walletInfo[this.coinType][i].address==walleti){
							 var that=this
							 for(var j=0;j<walletInfo[that.coinType][i].coinList.length;j++){
								 if(item.contractAddress==walletInfo[that.coinType][i].coinList[j].contractAddress){
									 walletInfo[this.coinType][i].coinList.splice(j, 1);
									 console.log(item,7777777)
									 
									 uni.setStorageSync('walletInfo',walletInfo)
									 uni.setStorageSync('wallet',walletInfo[this.coinType][i])
									 // uni.redirectTo({
									 // 		url:'/pages/menu/wallet/search/index'
									 // })
									 }
							
							
							 }
						
								
						
					}
				}
				
			}
		},
			async getSearchContract(){
		
			const response = await this.$api.getSearchContract(this.params)
			console.log(response.data.rows.length,7777)
			if(response.data.rows.length==0){
				console.log(454545454)
				this.valueNu=true
				this.conInfo=response.data.rows
				this.aotext=this.$t('index').tokensfound
			}else if(response.data.rows.length>0){
				this.valueNu=false
					this.conInfo=response.data.rows
			}
			if(this.params.searchName==''){
				console.log(this.params.searchName,777777)
			   this.valueNu=true
			   	this.params.pageNum  =  1;
			   this.aotext=this.$t('index').thetokenname
				 this.conInfo=[]
				 return;
			}
			console.log(response.data.rows,787878)
			},
		back(){
			uni.navigateBack({
				
			})
		}
		}
	}
</script>
<style>
	@import './index.css';
</style>