<template>
	<view class="">
		<navigation :showBack="true" :bgnum="false">
			<text class="big_title">
				Transaction password
			</text>
		</navigation>
		<view class="main">
			<view class="big_title" v-if="borNum">
				Set transaction password
			</view>
			<view class="big_title" v-if="borNum2">
				Please enter again
			</view>
			<view class="title">
				Secure transaction passwords, <br>please do not disclose
			</view>
			<u-message-input @finish="finish" inactive-color="#E0E5F2" active-color="#5B53FF" :width="94" :maxlength="6"
				mode="box" :focus="fes" :dot-fill="true" v-if="borNum"></u-message-input>
			<u-message-input @finish="finish2" :inactive-color="info?'#F16063':'#E0E5F2'" active-color="#5B53FF"
				:width="94" :maxlength="6" mode="box" :focus="fes" :dot-fill="true" v-if="borNum2" @change="back">
			</u-message-input>

			<view class="infoText" v-if="info">
				Password discrepansies
			</view>

		</view>



	</view>
	</view>



</template>

<script>
	import cont from "@/components/navigation/navigation.vue"
	export default {
		data() {
			return {
				password: '',
				password2: '',
				msg: '',
				msgLength: 0,
				fes: false,
				borNum: true,
				borNum2: false,
				info: false,
				word: {},
				walletInfo: {},
				btcBalance: 0,
				ethBbalance: 0,
				trxBbalance: 0,
			}
		},
		watch: {

		},

		methods: {
	     // 首次创建三条主链钱包 首次执行这里
			setWalletInfo(e) {
				this.word = uni.getStorageSync('word');
				this.walletInfo = this.$Token.initialWallet(this.word, e)
				let that=this
				this.$api.getBtcBalance().then((res) => {
					var b = res.data.data[0]
					for (var k in b) {
						that.btcBalance = b[k]
					}
				})
				this.walletInfo.BTC[0].balance = this.btcBalance
				this.walletInfo.BTC[0].coinList = [{
					name: "BTC",
					name2: "BTC",
					xname: 'Bitcoin',
					icon: require('@/static/tongyonh/bye.png'),
					balance: this.btcBalance
				}];
				
				// this.$EthUtil.getBalance(eth.address,function(res){
				// 	eth.balance = res
				// 	this.ethBbalance = res;
				// });
				console.log(this.walletInfo.ETH[0].address,2222222)
				this.$EthUtil.getBalance(this.walletInfo.ETH[0].address,function(res){	
					console.log(res,787878)
						 that.walletInfo.ETH[0].balance = res
						console.log(res,4444455555)
						 that.walletInfo.ETH[0].coinList = [
							{
							name: "ETH",
							name2: "ETH",
							xname: 'Ethereum',
							icon: require('@/static/tongyonh/Frame3299.png'),
							balance: res
						},
						{
							name:"USDT",
							name2:"ERC20-USDT",
							xname:'Tether USD',	
							contractAddress:'0xdac17f958d2ee523a2206206994597c13d831ec7',
							icon:require('@/static/tongyonh/img500.png'),
							balance: 0
						}
						]
					
					
				});
				
				this.$TronUtil.getTronBalance(this.walletInfo.TRON[0].address).then((res) => {
					that.walletInfo.TRON[0].balance = res
					that.walletInfo.TRON[0].coinList = [
						{
						name: "TRX",
						name2: "TRX",
						xname: 'TRON',
						icon: require('@/static/tongyonh/tron1.png'),
						balance: res
					},
					{
						name:"USDT",
						name2:"TRC20-USDT",
						xname:'Tether USD',	
						contractAddress:'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',			
						icon:require('@/static/tongyonh/tether_usd.png')
					}
					]
				
				
				}).catch(err => {
					console.log(err)
				})
				setTimeout(()=>{
					uni.setStorageSync('ethi', 0);
					uni.setStorageSync('itype', true);
					uni.setStorageSync('walletInfo', this.walletInfo);
				},1500)
	
			},

			// 删除密码触发
			back(e) {
				if (e.length < 6) {
					this.info = false;
				}
			},
			// 比较
			// 第二次创建钱包 第二次才执行 
			finish2(e) {
				var a = uni.getStorageSync('createWalletName')
				if (a == 'BTC' && a != '' && a != undefined) {
					this.walletInfo = uni.getStorageSync('walletInfo');
					var mnemonic = uni.getStorageSync('word');
					console.log(mnemonic, 7777777777)
					let btc = this.$Token.generateBtc(mnemonic);

					btc.mnemonic = mnemonic;
					btc.password = e;

          
					this.$api.getBtcBalance().then((res) => {
						var b = res.data.data[0]
						for (var k in b) {
							this.btcBalance = b[k]
						}
					})
					btc.coinList = [{
						name: "BTC",
						name2: "BTC",
						xname: 'Bitcoin',
						icon: require('@/static/tongyonh/bye.png'),
						balance: this.btcBalance
					}];
					btc.balance = this.btcBalance
					console.log(this.$store.state.btci, 555566565)
					this.$store.commit('addBtci', 1)
					uni.setStorageSync('wallet', btc)
					uni.setStorageSync('walleti', btc.privateKey)

					this.walletInfo.BTC[this.$store.state.btci] = btc;
					// 增加钱包位置
					console.log(this.$store.state.btci, 555566565)
					console.log(this.walletInfo, 44444)
					uni.setStorageSync('walletInfo', this.walletInfo);
					if (this.password != e) {
						this.info = true;
						return;
					} else {
						uni.showToast({
							title: 'successfully',
							icon: 'success',
							duration: 1500,
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '../../wallet/index'
							})
						}, 1500)
						this.info = false;
					}
					return;
				}

				if (a == 'ETH' && a != '' && a != undefined) {

					this.walletInfo = uni.getStorageSync('walletInfo');

					var mnemonic = uni.getStorageSync('word');
					let eth = this.$Token.generateEth(mnemonic);

					eth.mnemonic = mnemonic;
					eth.password = e;

					this.$EthUtil.getBalance(eth.address,function(res){
						eth.balance = res
						this.ethBbalance = res;
					});
					
					
					eth.coinList = [{
						name: "ETH",
						name2: "ETH",
						xname: 'Ethereum',
						icon: require('@/static/tongyonh/Frame3299.png'),
						balance: this.ethBbalance
					}];
					this.$store.commit('addEthi', 1)
                    
					uni.setStorageSync('walleti', eth.privateKey)
					this.walletInfo.ETH[this.$store.state.ethi] = eth;
                      
					// 增加钱包位置
					console.log(this.$store.state.ethi, 555566565)
					console.log(this.walletInfo, 44444)
					setTimeout(()=>{
							uni.setStorageSync('walletInfo', this.walletInfo);
							// 存一下当前钱包的索引方便后续做
							var e=uni.getStorageSync(ethi)
							e+=1
							uni.setStorageSync('ethi', e);
							uni.setStorageSync('itype', true);
					},1000)
				
					setTimeout(()=>{
						uni.setStorageSync('wallet', eth)
					},1000)
					if (this.password != e) {
						this.info = true;
						return;
					} else {
						uni.showToast({
							title: 'successfully',
							icon: 'success',
							duration: 1500,
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '../../wallet/index'
							})
						}, 1500)
						this.info = false;
					}
					return;
				}

				if (a == 'TRX' && a != '' && a != undefined) {
					console.log(123123)
					this.walletInfo = uni.getStorageSync('walletInfo');
					var mnemonic = uni.getStorageSync('word');
					let tron = this.$Token.generateTron(mnemonic);
					tron.mnemonic = mnemonic;
					tron.password = e;

					this.$TronUtil.getTronBalance(tron.address).then((res) => {
						tron.balance = res
						this.trxBbalance = res
						
					}).catch(err => {
						console.log(err)
					})
					tron.coinList = [{
						name: "TRX",
						name2: "TRX",
						xname: 'TRON',
						icon: require('@/static/tongyonh/tron1.png'),
						balance: this.trxBbalance
					}];
					console.log(this.$store.state.troni, 555566565)
					// 增加钱包位置
					this.$store.commit('addTroni', 1)
					setTimeout(()=>{
						uni.setStorageSync('wallet', tron)
					},1000)
					uni.setStorageSync('walleti', tron.privateKey)
					this.walletInfo.TRON[this.$store.state.troni] = tron;
					setTimeout(()=>{
							uni.setStorageSync('walletInfo', this.walletInfo);
						
					},1000)
					if (this.password != e) {
						this.info = true;
						return;
					} else {
						uni.showToast({
							title: 'Set successfully',
							icon: 'success',
							duration: 1500,
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '../../wallet/index'
							})
						}, 1500)
						this.info = false;
					}
					return;
				}




				if (this.password != e) {
					this.info = true;
					return;
				} else {
					uni.showToast({
						title: 'Set successfully',
						icon: 'success',
						duration: 1500,
					})
					this.setWalletInfo(e);
					setTimeout(() => {
						// uni.navigateTo({
						// 	url:'../reSuccessful'
						// })	
						uni.navigateTo({
							url: '../../wallet/index'
						})
					}, 1500)
					this.info = false;
				}




			},
			// 第一次存密码
			finish(e) {
				this.password = e
				var that = this;
				that.borNum = false;
				that.borNum2 = false;
				// uni.setStorage({
				// 	key: 'transPaw',
				// 	data: e,
				// 	success: function() {
				// 		
				// 	},

				// });	
				setTimeout(() => {
					that.borNum2 = true;
				}, 50)

			},
		},
		onLoad() {

		}
	};
</script>
<style scoped>

</style>

<style>
	@import './index.css';
</style>
