<template>
    <div>
        <div class="title">{{ $t('me.AddAddressWhitelist') }}</div>
        <div class="input-box">
            <van-field v-model="message" :placeholder="$t('me.PleaseProvideTheCompleteAddressOfYourWallet')" />
        </div>
        <div class="admin-btn">
            <van-button class="add" color="#4765DD" @click="changeFun">{{ $t('me.AddToTheWhitelist') }}</van-button>
            <!-- <van-button class="delete" color="#4765DD" @click="changeFun(2)">{{ $t('me.RemoveTheWhitelist') }}</van-button> -->
        </div>
    </div>
</template>

<script setup>
import { useWalletStore } from "@/stores/modules/walletStore";
import { showToast,showLoadingToast } from 'vant';
import Web3 from "web3";
import feesabiData from "@/utils/feesabi.json";
const walletStore = useWalletStore();
const web3 = new Web3(walletStore.rpcUrl);

const message = ref('');
const changeFun = async () => {
    if (!message.value) {
        showToast($t('me.PleaseEnterTheAddress'));
	    return;
	}
    showLoadingToast({
        message: $t('me.Adding'),
        forbidClick: true,
        duration: 0,
    });
    const contractAddress = '0x1b7fE9d431a8357dBf7BB1621df9e7Bd2973eBB2'; //wgt
    const contract = new web3.eth.Contract(feesabiData, contractAddress);
    const senderAccount = web3.eth.accounts.privateKeyToAccount(walletStore.privateKey);//onlyOwner 的私钥
    const ownerAddress = senderAccount.address;
    const targetAddress = message.value;//目标地址

    try {
        const contractOwner = await contract.methods.owner().call();
        console.log('合约 owner:', contractOwner);
        console.log('你当前使用的地址:', ownerAddress);
        console.log('需要加入白名单的地址', targetAddress)
        const nonce = await web3.eth.getTransactionCount(ownerAddress, 'pending');
        const gasPrice = await web3.eth.getGasPrice();
        const gasLimit = await contract.methods.addToWhitelist(targetAddress, true).estimateGas({ from: ownerAddress });

        const tx = {
            from: ownerAddress,
            to: contractAddress,
            nonce: nonce,
            gas: gasLimit,
            gasPrice,
            data: contract.methods.addToWhitelist(targetAddress, true).encodeABI()
        };

        const signedTx = await web3.eth.accounts.signTransaction(tx, walletStore.privateKey);
        web3.eth.sendSignedTransaction(signedTx.rawTransaction)
            .on('transactionHash', (hash) => {
                console.log(`交易已提交，等待确认...`);
            })
            .on('receipt', (receipt) => {
                showToast($t('me.AddedSuccessfully'));
                console.log(`✅ 交易成功!`);
                
            })
            .on('error', (error) => {
                showToast($t('me.AdditionFailed'));
                console.error(`❌ 交易失败: ${error.message}`);
            });

    } catch (error) {
        showToast($t('me.AdditionFailed'));
        console.error('交易失败:', error);
    }
}
</script>

<style lang="less" scoped>
.title{
    margin: 20px 0 16px;
    text-align: center;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 15px;
    color: #000000;
}
.input-box{
    background: #F2F2F2;
    border-radius: 8px;
    border: 1px solid #D8D8D8;
    margin: 0 30px;
    height: 64px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 15px;
    color: #8D8D8D;
    :deep(.van-cell){
        background: initial !important;
        height: 64px;
        box-sizing: border-box;
        line-height: 64px;
        padding: 0 16px !important;
    }
}
.admin-btn{
    margin: 32px 30px 0;
    display: flex;
    .add,.delete{
        width: calc(95% / 2);
        margin: 0 auto;
        height: 32px;
        line-height: 32px;
        border-radius: 17px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 15px;
        color: #FFFFFF;
        border: none !important;
    }
    // .add{
    //     background: #869EFF;
    // }
}
</style>
