<?php
include 'header.php';

// Initialize variables
$current_coin = isset($_GET['coin']) ? strtolower($_GET['coin']) : 'btc';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
    $action = $_POST['action'];
    $amount = floatval($_POST['amount']);
    $coin = $_POST['coin'];
    $coin_field = $coin . '_bal';
    $coin_balance = $row[$coin_field];
    $coin_price = $coin_prices[$coin] ?? 1;
    $wallet_address = $row[$coin.'_add']; // Get wallet address for the coin
    
    if ($amount <= 0) {
        echo "<script>
            alert('Amount must be greater than 0');
            window.location.href = 'trading_wallets.php';
        </script>";
        exit();
    } else {
        if ($action == 'fund') {
            if ($amount > $balance) {
                echo "<script>
                    alert('Insufficient balance!');
                    window.location.href = 'trading_wallets.php';
                </script>";
                exit();
            } else {
                // Deduct exact USDT amount from balance
                $new_balance = $balance - $amount;
                // Calculate coin amount to add based on current rate
                $coin_to_add = $amount / $coin_price;
                $new_coin_balance = $coin_balance + $coin_to_add;
                
                $update_sql = "UPDATE users SET balance = '$new_balance', $coin_field = '$new_coin_balance' WHERE email = '$email'";
                if (mysqli_query($conn, $update_sql)) {
                    $txn_sql = "INSERT INTO transactions (user_email, amount, currency, type, status, created_at) 
                               VALUES ('$user', '$amount', '$coin', 'funding', 'completed', NOW())";
                    mysqli_query($conn, $txn_sql);
                    
                    // Send email notification
                    $subject = "Wallet Funding Confirmation - $sitename";
                    $body = "
                    <div style='background: #E4E9F0; padding: 20px; font-family: Arial, sans-serif;'>
                        <div style='max-width: 600px; margin: 0 auto; background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);'>
                            <div style='background-color: #1e293b; padding: 16px; display: flex; align-items: center;'>
                                <div style='display: flex; align-items: center;'>
                                    <center><img src='$site_url/user/image/logo.png' width='100px' style='margin-right: 8px;'></center>
                                </div>
                            </div>
                            <div style='padding: 32px; text-align: center;'>
                                <h1 style='font-size: 28px; color: #1e293b; margin-bottom: 24px; line-height: 1.2;'>
                                    Wallet Funding Successful
                                </h1>
                                <p style='font-size: 18px; color: #4b5563; margin-bottom: 32px;'>
                                    Dear $first_name, your wallet has been successfully funded.
                                </p>
                                
                                <div style='text-align: left; margin-bottom: 24px;'>
                                    <h3 style='color: #1e293b;'>Transaction Details:</h3>
                                    <p><strong>Amount:</strong> \$" . number_format($amount, 2) . "</p>
                                    <p><strong>Cryptocurrency:</strong> " . strtoupper($coin) . "</p>
                                    <p><strong>Credited Amount:</strong> " . number_format($coin_to_add, 8) . " " . strtoupper($coin) . "</p>
                                    <p><strong>Rate:</strong> 1 " . strtoupper($coin) . " = \$" . number_format($coin_price, 2) . "</p>
                                    <p><strong>Date:</strong> " . date('Y-m-d H:i:s') . "</p>
                                </div>
                                
                                <div style='margin-top: 32px; border-top: 1px solid #e5e7eb; padding-top: 24px; text-align: left;'>
                                    <p style='color: #4b5563; margin-bottom: 8px;'>Thanks,</p>
                                    <p style='color: #4b5563; font-weight: bold;'>Finance Team, $sitename</p>
                                </div>
                            </div>
                        </div>
                    </div>";
                    
                    sendMail($email, $subject, $body);
                    
                    // Send admin notification
                    $adminSubject = "New Wallet Funding - $first_name";
                    $adminBody = "
                    <div style='font-family: Arial, sans-serif;'>
                        <h2>New Wallet Funding</h2>
                        <p><strong>User:</strong> $first_name ($email)</p>
                        <p><strong>Amount:</strong> \$" . number_format($amount, 2) . "</p>
                        <p><strong>Cryptocurrency:</strong> " . strtoupper($coin) . "</p>
                        <p><strong>Credited Amount:</strong> " . number_format($coin_to_add, 8) . " " . strtoupper($coin) . "</p>
                        <p><strong>Date:</strong> " . date('Y-m-d H:i:s') . "</p>
                    </div>";
                    
                    if (!empty($admin_email)) {
                        sendMail($admin_email, $adminSubject, $adminBody);
                    }
                    
                    echo "<script>
                        alert('Funding successful');
                        window.location.href = 'trading_wallets.php';
                    </script>";
                    exit();
                }
            }
        } elseif ($action == 'withdraw') {
            // Check if wallet address is set
            if (empty($wallet_address)) {
                echo "<script>
                    alert('Please set your ".strtoupper($coin)." wallet address first');
                    window.location.href = 'account_edit.php?coin=$coin';
                </script>";
                exit();
            } else {
                // Calculate USDT value of current coin balance
                $usdt_value = $coin_balance * $coin_price;
                if ($amount > $usdt_value) {
                    echo "<script>
                        alert('Insufficient coin balance');
                        window.location.href = 'trading_wallets.php';
                    </script>";
                    exit();
                } else {
                    // Calculate coin amount to deduct based on current rate
                    $coin_to_deduct = $amount / $coin_price;
                    $new_coin_balance = $coin_balance - $coin_to_deduct;
                    // Add exact USDT amount to balance
                    $new_balance = $btc_usdt + $amount;
                    
                    $update_sql = "UPDATE users SET btc_bal = '$new_balance', $coin_field = '$new_coin_balance' WHERE email = '$user'";
                    if (mysqli_query($conn, $update_sql)) {
                        $txn_sql = "INSERT INTO transactions (user_email, amount, currency, type, status, wallet_address, created_at) 
                                   VALUES ('$user', '$amount', '$coin', 'withdrawal', 'pending', '$wallet_address', NOW())";
                        mysqli_query($conn, $txn_sql);
                        
                        // Send email notification
                        $subject = "Withdrawal Request Received - $sitename";
                        $body = "
                        <div style='background: #E4E9F0; padding: 20px; font-family: Arial, sans-serif;'>
                            <div style='max-width: 600px; margin: 0 auto; background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);'>
                                <div style='background-color: #1e293b; padding: 16px; display: flex; align-items: center;'>
                                    <div style='display: flex; align-items: center;'>
                                        <center><img src='$site_url/user/image/logo.png' width='100px' style='margin-right: 8px;'></center>
                                    </div>
                                </div>
                                <div style='padding: 32px; text-align: center;'>
                                    <h1 style='font-size: 28px; color: #1e293b; margin-bottom: 24px; line-height: 1.2;'>
                                        Withdrawal Request Received
                                    </h1>
                                    <p style='font-size: 18px; color: #4b5563; margin-bottom: 32px;'>
                                        Dear $first_name, your withdrawal request has been received and is being processed.
                                    </p>
                                    
                                    <div style='text-align: left; margin-bottom: 24px;'>
                                        <h3 style='color: #1e293b;'>Transaction Details:</h3>
                                        <p><strong>Amount:</strong> \$" . number_format($amount, 2) . "</p>
                                        <p><strong>Cryptocurrency:</strong> " . strtoupper($coin) . "</p>
                                        <p><strong>Amount to Receive:</strong> " . number_format($coin_to_deduct, 8) . " " . strtoupper($coin) . "</p>
                                        <p><strong>Wallet Address:</strong> $wallet_address</p>
                                        <p><strong>Status:</strong> Pending</p>
                                        <p><strong>Date:</strong> " . date('Y-m-d H:i:s') . "</p>
                                    </div>
                                    
                                    <p style='font-size: 18px; color: #4b5563; margin-bottom: 32px;'>
                                        Our team will process your request within 24-48 hours. You will receive a confirmation once completed.
                                    </p>
                                    
                                    <div style='margin-top: 32px; border-top: 1px solid #e5e7eb; padding-top: 24px; text-align: left;'>
                                        <p style='color: #4b5563; margin-bottom: 8px;'>Thanks,</p>
                                        <p style='color: #4b5563; font-weight: bold;'>Finance Team, $sitename</p>
                                    </div>
                                </div>
                            </div>
                        </div>";
                        
                        sendMail($email, $subject, $body);
                        
                        // Send admin notification
                        $adminSubject = "New Withdrawal Request - $first_name";
                        $adminBody = "
                        <div style='font-family: Arial, sans-serif;'>
                            <h2>New Withdrawal Request</h2>
                            <p><strong>User:</strong> $first_name ($email)</p>
                            <p><strong>Amount:</strong> \$" . number_format($amount, 2) . "</p>
                            <p><strong>Cryptocurrency:</strong> " . strtoupper($coin) . "</p>
                            <p><strong>Amount to Send:</strong> " . number_format($coin_to_deduct, 8) . " " . strtoupper($coin) . "</p>
                            <p><strong>Wallet Address:</strong> $wallet_address</p>
                            <p><strong>Date:</strong> " . date('Y-m-d H:i:s') . "</p>
                            <p>Please process this request in the admin panel.</p>
                        </div>";
                        
                        if (!empty($admin_email)) {
                            sendMail($admin_email, $adminSubject, $adminBody);
                        }
                        
                        echo "<script>
                            alert('Withdrawal request submitted. Please wait for processing!');
                            window.location.href = 'trading_wallets.php';
                        </script>";
                        exit();
                    }
                }
            }
        }
    }
}
?>

<div class="pt-28 md:pt-0 mt-0 md:mt-36 w-full md:w-4/5" id="general-content">
    <div class="w-full md:w-10/12 md:ml-64 2xl:ml-1/5" id="general-content-section">

        <div id="preloader" class="action-preloader">
            <div id="loading-bar-spinner" class="spinner">
                <div class="spinner-icon"></div>
            </div>
        </div>

        <!-- Popup for funding/withdrawal -->
        <div class="full-popup">
            <div class="flex justify-center mt-10">
                <div class="popup w-11/12 md:w-1/2 rounded-md bg-[#0e1726] text-[#ebedf2]">
                    <div class="w-full">
                        <div class="w-full flex justify-center">
                            <div class="w-full p-2 md:p-4">
                                <div class="flex justify-between items-center">
                                    <div id="segment-3">
                                        <h2 class="bg-transparent text-[#ebedf2] font-medium capitalize">
                                            <span id="wallet-action-heading"></span> <span class="text-green-600" id="wallet"><?php echo strtoupper($current_coin); ?></span> wallet
                                        </h2>
                                    </div>
                                    <div>
                                        <a role="button" id="close-popup" class="flex justify-start items-center text-xs text-red-600 hover:text-orange-600">
                                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                            </svg>
                                        </a>
                                    </div>
                                </div>
                                <hr class="w-full border-b border-dotted border-gray-600 border mb-4">

                                <div class="w-full p-6 md:p-10 flex justify-center" id="segment-2">
                                    <div class="w-full flex space-x-2 rounded-lg bg-[#131d2c] text-[#d3d6df] p-2 md:p-4 text-xs md:text-sm">
                                        <div class="text-orange-500">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                                <path stroke-linecap="round" stroke-linejoin="round" d="M20.618 5.984A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016zM12 9v2m0 4h.01" />
                                            </svg>
                                        </div>
                                        <div>
                                            <b class="font-medium">Disclaimer: </b> You are about to <span id="action-disclaimer">fund</span> your <b class="font-medium text-green-600" id="disclaimer-coin"><?php echo strtoupper($current_coin); ?></b> wallet. The amount would be <span id="action-direction">deducted from</span> your Account Balance.
                                        </div>
                                    </div>
                                </div>

                                <div class="flex justify-between items-center" id="segment-1">
                                    <div class="w-full">
                                        <div class="w-full flex justify-center">
                                            <div>
                                                <div class="text-xl md:text-2xl font-bold text-center">
                                                    <h1 id="funding-current-balance" data-bal="<?php echo $balance; ?>">
                                                        $<?php echo number_format($balance, 2); ?></h1>
                                                </div>
                                                <div class="text-xs md:text-sm text-center">
                                                    <h6 class="text-orange-600">Fiat Balance</h6>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="w-full flex justify-center">
                                            <div>
                                                <div class="text-xl md:text-2xl font-bold text-center">
                                                    <h1 id="coin-balance" data-bal="<?php echo ${$current_coin.'_usdt'}; ?>">
                                                        <?php echo number_format(${$current_coin.'_usdt'}, 8); ?>
                                                    </h1>
                                                </div>
                                                <div class="text-xs md:text-sm text-center">
                                                    <h6 class="text-green-600"><?php echo strtoupper($current_coin); ?> Balance in USD </h6>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="w-full">
                                        <div class="w-full my-6 md:my-10 flex justify-center space-x-2">
                                            <div>
                                                <div class="text-xl md:text-2xl font-bold text-center">
                                                    <h1 id="rate-symbol">1<?php echo strtoupper($current_coin); ?></h1>
                                                </div>
                                            </div>
                                            <div>
                                                <div class="text-xl md:text-2xl font-bold text-center text-orange-600">
                                                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 8h16M4 16h16"></path>
                                                    </svg>
                                                </div>
                                            </div>
                                            <div>
                                                <div class="text-xs md:text-sm text-center">
                                                    <h3 id="funding-rate"><?php echo number_format($coin_prices[$current_coin] ?? 0, 2); ?></h3>
                                                </div>
                                                <div class="text-xl md:text-2xl font-bold text-center">
                                                    <h1>USD</h1>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div id="reloadSection">
                                    <input type="hidden" id="reloadUrl" value="trading_wallets.php?coin=<?php echo $current_coin; ?>">
                                </div>

                                <div class="p-2 md:p-4">
                                    <form action="" method="POST" id="funding-form">
                                        <input type="hidden" id="action-type" name="action">
                                        <input type="hidden" name="coin" id="coin-type" value="<?php echo $current_coin; ?>">
                                        <div class="space-y-5">
                                            <div class="relative w-full">
                                                <span class="cred-hyip-theme1-input-icon h-8 w-8 font-semibold">
                                                    USD
                                                </span>
                                                <input name="amount" type="number" step='any' id="funding-amount" required class="cred-hyip-theme1-text-input" placeholder="Enter amount">
                                            </div>
                                            <div class="relative w-full">
                                                <span class="cred-hyip-theme1-input-icon h-8 w-8 font-semibold" id="symbol-change">
                                                    <?php echo strtoupper($current_coin); ?>
                                                </span>
                                                <input name="total" type="text" id="funding-total" class="cred-hyip-theme1-text-input" placeholder="" disabled>
                                            </div>
                                        </div>
                                        <div class="w-full my-5 px-5">
                                            <button type="submit" id="funding-submit" class="w-1/3 text-xs md:text-sm text-[#d1d5db] text-center px-5 py-2 bg-[#1b2e4b] hover:bg-gray-700 rounded-md">
                                                GO
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <style>
            .full-popup {
                position: fixed;
                top: 0;
                left: 0;
                width: 100vw;
                height: 100vh;
                z-index: 1000;
                background: rgb(19, 29, 44, 0.7);
                display: none;
            }
            .popup {
                min-height: 400px;
            }
            .parent-row {
                display: flex;
                justify-content: space-between;
            }
            .trade-section {
                position: relative;
                width: 35%;
            }
            .wallet-section {
                position: relative;
                width: 65%;
            }
            @media only screen and (max-width: 600px) {
                .parent-row {
                    display: block;
                }
                .trade-section, .wallet-section {
                    width: 100%;
                    display: block;
                    margin-top: 10px;
                }
            }
        </style>

        <div class="w-full py-5" id="content">
            <div class="w-full flex justify-center">
                <div class="w-11/12 md:w-12/12 rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">
                    <div class="parent-row">
                        <div class="wallet-section" id="content-left">
                            <div class="parent-row">
                                <div class="trade-section" id="price-block">
                                    <div class="w-full rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">
                                        <div class="w-full space-x-2 rounded-lg bg-[#131d2c] text-[#d3d6df] p-2 md:p-4 text-xs md:text-sm">
                                            <div class="flex lg:block justify-between items-center bg-[#152136] px-3 lg:px-6 py-2 lg:py-3 hover:text-white cursor-pointer rounded-md">
                                                <div class="hidden lg:block relative w-full">
                                                    <?php
                                                        if($current_coin = "btc"){
                                                    ?>
                                                    <div class="absolute flex justify-center items-center -top-7 -right-7 h-9 w-9 rounded-full bg-blue-600 text-white">
                                                        <img src="image/btc.svg" alt="" class="rounded-full">
                                                    </div>
                                                    <?php } ?>
                                                    
                                                   
                                                </div>
                                                <div>
                                                    <?php
                                                        if($current_coin = "btc"){
                                                    ?>
                                                    <div>
                                                        <h2 class="text-sm lg:text-base font-semibold"><?php echo number_format($btc_amount, 8); ?></h2>
                                                    </div>
                                                    <?php } ?>
                                                    
                                                   
                                                    
                                                    <div class="mt-2">
                                                        <h4 class="text-xs lg:text-sm font-medium"><?php echo strtoupper($current_coin); ?></h4>
                                                    </div>
                                                </div>
                                                <div class="lg:hidden opacity-50">
                                                    <?php
                                                        if($current_coin = "btc"){
                                                    ?>
                                                    <div class="absolute flex justify-center items-center -top-7 -right-7 h-9 w-9 rounded-full bg-blue-600 text-white">
                                                        <img src="image/btc.svg" alt="" class="rounded-full">
                                                    </div>
                                                    <?php } ?>
                                                    
                                                   
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="wallet-section">
                                    <div class="w-full rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">
                                        <div class="w-full flex justify-between space-x-2 rounded-lg bg-[#131d2c] text-[#d3d6df] p-2 md:p-4 text-xs md:text-sm">
                                            <div class="mt-3 mb-3">
                                                <a role="button" data-action="fund" class="wallet-trigger flex justify-start items-center space-x-1 text-xs md:text-sm text-[#d1d5db] text-center px-5 py-2 my-2 bg-[#1b2e4b] hover:bg-gray-700 rounded-md">
                                                    <span class="text-green-600">
                                                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 4h13M3 8h9m-9 4h6m4 0l4-4m0 0l4 4m-4-4v12"></path>
                                                        </svg>
                                                    </span>
                                                    <span>Fund</span>
                                                </a>
                                            </div>
                                            <div class="mt-3 mb-3">
                                                <a role="button" data-action="withdraw" class="wallet-trigger flex justify-start items-center space-x-1 text-xs md:text-sm text-[#d1d5db] text-center px-5 py-2 my-2 bg-[#1b2e4b] hover:bg-gray-700 rounded-md">
                                                    <span class="text-red-600">
                                                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 4h13M3 8h9m-9 4h9m5-4v12m0 0l-4-4m4 4l4-4"></path>
                                                        </svg>
                                                    </span>
                                                    <span>Withdraw</span>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="w-full rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">
                                <div class="w-full rounded-lg bg-[#131d2c] text-[#d3d6df] p-2 md:p-4 text-xs md:text-sm">
                                    <div id="market-orders" class="orders">
                                        <div class="w-full flex justify-between items-center mt-2 rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">
                                            <div>DATE</div>
                                            <div>TYPE</div>
                                            <div>AMOUNT</div>
                                            <div>ORDER</div>
                                        </div>
                                        <?php
                                        // Display recent transactions
                                        $txn_sql = "SELECT * FROM transactions WHERE user_email = '$user' AND currency = '$current_coin' ORDER BY created_at DESC LIMIT 5";
                                        $txn_result = mysqli_query($conn, $txn_sql);
                                        while($txn = mysqli_fetch_assoc($txn_result)) {
                                            echo '<div class="w-full flex justify-between items-center mt-2 rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">';
                                            echo '<div>' . date('d.m.Y<br>H:i:s', strtotime($txn['created_at'])) . '</div>';
                                            echo '<div class="flex">';
                                            echo $txn['type'] == 'funding' ? 
                                                '<span class="text-green-600"><svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18"></path></svg></span>' : 
                                                '<span class="text-red-600"><svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 14l-7 7m0 0l-7-7m7 7V3"></path></svg></span>';
                                            echo '<span>' . $txn['type'] . '</span>';
                                            echo '</div>';
                                            echo '<div>' . number_format($txn['amount'], 8) .  '</div>';
                                            echo '<div>' . $txn['type'] . '</div>';
                                            echo '</div>';
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="trade-section">
                            <div class="w-full rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4" id="content-right">
                                <div class="w-full space-x-2 rounded-lg bg-[#131d2c] text-[#d3d6df] p-2 md:p-4 text-xs md:text-sm">
                                    <h2 class="bg-transparent text-[#ebedf2] font-medium capitalize">
                                        Other Wallets
                                    </h2>
                                    <div class="mt-5"></div>

                                    <!-- BTC Wallet -->
                                    <div class="w-full flex justify-between items-center mt-2 rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">
                                        <div>
                                            <img src="image/btc.svg" alt="" width="30px" class="rounded-full">
                                        </div>
                                        <div>BTC</div>
                                        <div><?php echo number_format($btc_amount, 8); ?></div>
                                        <div class="inline-flex space-x-3 md:space-x-5">
                                            <a href="trading_wallets.php?coin=btc" class="wallet_link">
                                                <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1">
                                                    <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                                    <path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                                </svg>
                                            </a>
                                        </div>
                                    </div>

                                    <!-- ETH Wallet -->
                                    <!--<div class="w-full flex justify-between items-center mt-2 rounded-md bg-[#0e1726] text-[#d3d6df] p-2 md:p-4">-->
                                    <!--    <div>-->
                                    <!--        <img src="image/eth.svg" alt="" width="30px" class="rounded-full">-->
                                    <!--    </div>-->
                                    <!--    <div>ETH</div>-->
                                    <!--    <div><?php echo number_format($eth_amount, 8); ?></div>-->
                                    <!--    <div class="inline-flex space-x-3 md:space-x-5">-->
                                    <!--        <a href="trading_wallets.php?coin=eth" class="wallet_link">-->
                                    <!--            <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1">-->
                                    <!--                <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />-->
                                    <!--                <path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />-->
                                    <!--            </svg>-->
                                    <!--        </a>-->
                                    <!--    </div>-->
                                    <!--</div>-->

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


<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
$(document).ready(function() {
    // Handle wallet switching
    $('.wallet_link').on('click', function(e) {
        e.preventDefault();
        var coin = $(this).attr('href').split('=')[1];
        window.location.href = 'trading_wallets.php?coin=' + coin;
    });

    // Handle funding/withdrawal trigger
    $('.wallet-trigger').on('click', function() {
        var action = $(this).data('action');
        $('#action-type').val(action);
        $('#wallet-action-heading').html(action == 'fund' ? 'Fund ' : 'Withdraw From ');
        $('#action-disclaimer').text(action);
        $('#action-direction').text(action == 'fund' ? 'deducted from' : 'added to');
        
        $('.full-popup').show(200);
    });

    // Close popup
    $('#close-popup').on('click', function() {
        $('.full-popup').hide(200);
        $('#funding-total').val('');
        $('#funding-amount').val('');
    });

    // Calculate conversion in real-time
    $("#funding-amount").on('input', function() {
        var action = $('#action-type').val();
        var rate = parseFloat($('#funding-rate').text());
        var amount = parseFloat($(this).val()) || 0;
        var current_balance = parseFloat($('#funding-current-balance').data('bal'));
        var coin_balance = parseFloat($('#coin-balance').data('bal'));
        
        if (action == 'fund') {
            var total = amount / rate;
            $('#funding-total').val(total.toFixed(8));
            
            if (amount > current_balance) {
                $('#funding-current-balance').addClass('text-red-600').removeClass('text-green-600');
                $('#funding-submit').prop('disabled', true);
            } else {
                $('#funding-current-balance').removeClass('text-red-600').addClass('text-green-600');
                $('#funding-submit').prop('disabled', false);
            }
        } else {
            var total = amount * rate;
            $('#funding-total').val(total.toFixed(2));
            
            if (amount > coin_balance) {
                $('#coin-balance').addClass('text-red-600').removeClass('text-green-600');
                $('#funding-submit').prop('disabled', true);
            } else {
                $('#coin-balance').removeClass('text-red-600').addClass('text-green-600');
                $('#funding-submit').prop('disabled', false);
            }
        }
    });

    // Form submission
    $('#funding-form').on('submit', function(e) {
        e.preventDefault();
        $('#preloader').show();
        
        var formData = $(this).serialize();
        $.ajax({
            url: '',
            type: 'POST',
            data: formData,
            success: function(response) {
                $('#preloader').hide();
                $('.full-popup').hide(200);
            },
            error: function() {
                $('#preloader').hide();
                Swal.fire({
                    position: 'top-end',
                    icon: 'error',
                    title: 'Error',
                    text: 'An error occurred. Please try again.',
                    showConfirmButton: false,
                    timer: 3000,
                    background: "#0e1726",
                    color: "#b9bead",
                    toast: true
                });
            }
        });
    });
});
</script>

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