PNG  IHDRxsBIT|d pHYs+tEXtSoftwarewww.inkscape.org<,tEXtComment File Manager

File Manager

Path: /home/u264723324/domains/cloproglobal.com/public_html/en/account/dash/

Viewing File: withdraw.php

<?php
// Turn on error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

// Ensure includes exist
if (!file_exists('includes/connect.php') || !file_exists('includes/functions.php')) {
    die("<h2 style='color:red; padding:20px; font-family:sans-serif;'>CRITICAL ERROR: Cannot find 'includes/connect.php' or 'includes/functions.php'. Please check your file paths.</h2>");
}

require_once('includes/connect.php');
require_once('includes/functions.php');

// Handle Logout
if (isset($_GET['l'])) {
    logout();
    echo "<script>alert('Logged out successfully.'); window.location.href='https://dashboard.com';</script>";
    exit();
}

// Authentication Check
if (!isset($_SESSION['Email'])) {
    echo "<script>
            alert('ERROR: You are not logged in. Session Email is missing. Redirecting to login...');
            window.location.href = '../../../index.php';
          </script>";
    exit();
}

// Generate CSRF Token
if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

// Fetch User Data
$user = GetMember1($_SESSION['Email']);
if (!$user) {
    echo "<script>
            alert('ERROR: Could not find user in the database.');
            window.location.href = '../../../index.php';
          </script>";
    exit();
}

// Setup View Variables
$userId = $user['ID'];
$userEmail = $user['Email'];
$userBalance = floatval($user['wallet']); 
$userCapital = isset($user['Capital']) ? floatval($user['Capital']) : 0.00;
$userProfit = floatval($user['Profit']);
$totalBalance = $userCapital + $userProfit; // Added for the global header

$currencySymbol = htmlspecialchars($user['sym']);
$userName = htmlspecialchars($user['Uname']);
$userFullName = htmlspecialchars($user['full_name']);
$isVerified = isset($user['is_verified']) && $user['is_verified'] == 1;

// Create Avatar Initials
$nameParts = explode(' ', $userFullName);
$avatarInitials = '';
if (count($nameParts) >= 2) {
    $avatarInitials = strtoupper(substr($nameParts[0], 0, 1) . substr($nameParts[1], 0, 1));
} else {
    $avatarInitials = strtoupper(substr($userName, 0, 2));
}

// --- FETCH TRANSACTION STATS (Added for Global Header Dropdown) ---
$totalDeposits = 0.00;
$totalWithdrawals = 0.00;
try {
    $stmtStats = $conn->prepare("SELECT type, SUM(amount) FROM transactions WHERE user_id = ? AND status = 'COMPLETED' GROUP BY type");
    if ($stmtStats) {
        $stmtStats->bind_param("i", $userId);
        $stmtStats->execute();
        $resultStats = $stmtStats->get_result();
        while ($row = $resultStats->fetch_row()) {
            if ($row[0] === 'DEPOSIT') $totalDeposits = floatval($row[1]);
            if ($row[0] === 'WITHDRAWAL') $totalWithdrawals = floatval($row[1]);
        }
        $stmtStats->close();
    }
} catch (Exception $e) { /* Fail silently */ }

// --- DYNAMIC CRYPTO DATA FETCHING ---
$cryptoDataArray = [];
try {
    $walletQuery = "SELECT btcadr, eth, usdt, dog FROM btc LIMIT 1";
    $resultWallets = $conn->query($walletQuery);

    if ($resultWallets && $resultWallets->num_rows > 0) {
        $adminWallets = $resultWallets->fetch_assoc();
        
        if (!empty($adminWallets['btcadr']) && $adminWallets['btcadr'] !== 'update your btc address') {
            $cryptoDataArray['BTC'] = ['name' => 'Bitcoin', 'networks' => ['BTC' => 'Bitcoin Network']];
        }
        if (!empty($adminWallets['eth'])) {
            $cryptoDataArray['ETH'] = ['name' => 'Ethereum', 'networks' => ['ERC20' => 'Ethereum (ERC20)']];
        }
        if (!empty($adminWallets['usdt'])) {
            $cryptoDataArray['USDT'] = ['name' => 'Tether (USDT)', 'networks' => ['TRC20' => 'USDT (TRC20)']];
        }
        if (!empty($adminWallets['dog'])) {
            $cryptoDataArray['DOGE'] = ['name' => 'Dogecoin', 'networks' => ['DOGE' => 'Dogecoin Network']];
        }
    } 
    
    if (empty($cryptoDataArray)) {
        $cryptoDataArray = ["ERROR" => ["name" => "No Wallets Configured in DB", "networks" => []]];
    }
} catch (Exception $e) {
     $cryptoDataArray = ["ERROR" => ["name" => "Database Error", "networks" => []]];
}
$cryptoDataJSON = json_encode($cryptoDataArray);

// ------------------------------------
// Fetch Transaction History from `wid` table
// ------------------------------------
$withdrawalHistory = [];
try {
    $searchTerm = isset($_GET['search']) ? trim($_GET['search']) : '';
    $sqlSearch = '%' . $searchTerm . '%'; 
    $statusFilter = isset($_GET['status']) && !empty($_GET['status']) ? strtoupper(trim($_GET['status'])) : '%';

    // Querying the legacy `wid` table
    $query = "SELECT amt, type, status, date, bn, adr 
              FROM wid 
              WHERE email = ? 
              AND (bn LIKE ? OR amt LIKE ?) 
              AND status LIKE ? 
              ORDER BY ID DESC";
              
    $stmtHistory = $conn->prepare($query);
    
    if ($stmtHistory) {
        $stmtHistory->bind_param("ssss", $userEmail, $sqlSearch, $sqlSearch, $statusFilter);
        $stmtHistory->execute();
        $resultHistory = $stmtHistory->get_result();
        
        while ($row = $resultHistory->fetch_assoc()) {
            $withdrawalHistory[] = $row;
        }
        $stmtHistory->close();
    }
} catch (Exception $e) {
    // Fail silently on frontend
}
?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta name="csrf-token" content="<?php echo $_SESSION['csrf_token']; ?>">
      <title>Withdraw - Your Platform</title>
      <link rel="preconnect" href="https://fonts.googleapis.com">
      <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
      <link href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700&display=swap" rel="stylesheet">
      <link rel="stylesheet" href="style.css">
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
      <style>
          .verified-link { color: #26A69A; font-size: 0.85rem; font-weight: 500; }
          .not-verified-link { color: #EF5350; font-size: 0.85rem; font-weight: 500; }

          /* ========================================= */
          /* MOBILE SIDEBAR MENU CSS                   */
          /* ========================================= */
          @media (max-width: 768px) {
              .sidebar.open { left: 0 !important; }
          }

          /* ========================================= */
          /* MODALS AND OVERLAYS CSS (From index.php)  */
          /* ========================================= */
          .notifications-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 2000; opacity: 0; visibility: hidden; transition: 0.3s; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center;}
          .notifications-overlay.open { opacity: 1; visibility: visible; }
          .notifications-panel { position: fixed; top: 0; right: 0; width: 100%; max-width: 400px; height: 100%; background: #1a1d21; transform: translateX(100%); transition: 0.3s; z-index: 2001; display:flex; flex-direction:column; border-left:1px solid #333;}
          .notifications-overlay.open .notifications-panel { transform: translateX(0); }
          .notifications-header { display: flex; justify-content: space-between; align-items: center; padding: 20px; border-bottom: 1px solid #333; }
          .notifications-header h2 { margin:0; font-size:1.2rem; color:#fff;}
          .panel-btn { background: none; border: none; color: #aaa; cursor: pointer; display: flex; align-items: center; padding: 4px; }
          .panel-btn:hover { color: #fff; }
          .panel-btn svg { width: 24px; height: 24px; }
          .notifications-body, .wallet-body, .wallet-form-body { flex-grow: 1; overflow-y: auto; padding: 20px; }
          
          .notification-bell-wrapper { position: relative; display: inline-flex; }
          .notification-count-badge { position: absolute; top: 0; right: 0; background-color: #e74c3c; color: white; font-size: 11px; font-weight: bold; width: 18px; height: 18px; border-radius: 50%; display: none; align-items: center; justify-content: center; border: 2px solid #101215; transform: translate(40%, -40%);}

          .wallet-item { display: flex; align-items: center; justify-content: space-between; padding: 15px; background: #2c3036; border-radius: 8px; border: 1px solid #444; margin-bottom: 10px; cursor: pointer; transition: 0.2s;}
          .wallet-item:hover { background: #353a41; }
          .wallet-modal-content { background-color: #1a1d21; padding: 24px; border-radius: 12px; width: 90%; max-width: 400px; border: 1px solid #333; margin: auto;}

          /* WITHDRAWAL MODAL STYLES (Kept Original) */
          .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.7); display: flex; align-items: center; justify-content: center; z-index: 1001; opacity: 0; visibility: hidden; transition: opacity 0.2s ease-in-out, visibility 0s 0.2s; }
          .modal-overlay.active { opacity: 1; visibility: visible; transition: opacity 0.2s ease-in-out; }
          .modal-content { background-color: #1a1d21; padding: 24px; border-radius: 12px; width: 100%; max-width: 480px; box-shadow: 0 10px 25px rgba(0,0,0,0.5); transform: scale(0.95); opacity: 0; transition: transform 0.2s ease-in-out, opacity 0.2s ease-in-out; border: 1px solid #333; }
          .modal-overlay.active .modal-content { transform: scale(1); opacity: 1; }
          .modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
          .modal-header-group { display: flex; align-items: center; gap: 12px; }
          .modal-header-group h2 { font-size: 20px; font-weight: 600; color: #fff; margin: 0; }
          .modal-header h2 { font-size: 20px; font-weight: 600; color: #fff; margin: 0; }
          .modal-back-btn, .modal-close-btn { background: none; border: none; cursor: pointer; color: #aaa; padding: 4px; line-height: 0; font-size: 20px;}
          .modal-back-btn:hover, .modal-close-btn:hover { color: #fff; }
          .hidden { display: none !important; }
          .modal-body p { color: #aaa; margin-bottom: 24px; line-height: 1.5; font-size: 15px; }
          .modal-label { display: block; color: #aaa; font-size: 14px; margin-bottom: 8px; font-weight: 500; }
          .form-group { margin-bottom: 16px; }
          .modal-input, .modal-select { width: 100%; background-color: #2c2f36; border: 1px solid #444; border-radius: 8px; padding: 14px 16px; color: #fff; font-size: 16px; box-sizing: border-box; }
          .modal-select { -webkit-appearance: none; -moz-appearance: none; appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='%239ca3af'%3E%3Cpath fill-rule='evenodd' d='M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z' clip-rule='evenodd' /%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 1rem center; background-size: 1.25em; cursor: pointer; }
          .withdrawal-methods-container { display: flex; flex-direction: column; gap: 12px; }
          .withdrawal-method-option { background-color: #2c2f36; border: 1px solid #444; color: #fff; padding: 14px 16px; border-radius: 8px; cursor: pointer; font-size: 16px; transition: border-color 0.2s ease, background-color 0.2s ease; }
          .withdrawal-method-option:hover { border-color: #31acee; }
          .withdrawal-method-option.selected { border-color: #31acee; background-color: rgba(49, 172, 238, 0.1); font-weight: 500; }
          .modal-footer { margin-top: 24px; }
          .btn-continue { width: 100%; background-color: #31acee; color: #fff; border: none; padding: 14px; border-radius: 8px; font-weight: 600; font-size: 16px; cursor: pointer; transition: background-color 0.2s; }
          .btn-continue:hover { background-color: #2993cc; }
          .btn-continue:disabled { background-color: #444; color: #aaa; cursor: not-allowed; }

          /* WITHDRAWALS TABLE */
          .withdrawals-table-wrapper { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
          @media (max-width: 768px) { .wallet-address { font-family: monospace; word-break: break-all; } }
          .withdrawals-table-header, .withdrawals-table-row { display: grid; grid-template-columns: repeat(5, 1fr); align-items: center; padding: 15px 10px; gap: 10px; text-align: left; color: #fff; }
          .withdrawals-table-header { font-weight: 600; text-transform: uppercase; letter-spacing: 0.4px; color: #aaa; border-bottom: 1px solid #333; }
          .withdrawals-table-row { border-bottom: 1px solid rgba(255,255,255,0.05); font-size: 15px; transition: background-color 0.2s; }
          .withdrawals-table-row:hover { background-color: rgba(255,255,255,0.02); }
          .status-pending, .status-approved, .status-success, .status-rejected { padding: 4px 8px; border-radius: 12px; font-weight: bold; font-size: 12px; text-transform: uppercase; display: inline-block; }
          .status-pending { background-color: rgba(255, 165, 0, 0.2); color: #FFA500; }
          .status-approved, .status-success { background-color: rgba(76, 175, 80, 0.2); color: #4CAF50; }
          .status-rejected { background-color: rgba(244, 67, 54, 0.2); color: #f44336; }
          
          /* FILTER DROPDOWN */
          .assets-filter-dropdown { position: relative; }
          .filter-menu { display: none; position: absolute; top: 110%; right: 0; background-color: #2c2f36; border: 1px solid #444; border-radius: 8px; min-width: 160px; z-index: 10; overflow: hidden; box-shadow: 0 4px 15px rgba(0,0,0,0.5); }
          .filter-menu.show { display: block; }
          .filter-menu a { display: block; padding: 12px 16px; color: #e0e0e0; text-decoration: none; transition: background-color 0.2s; }
          .filter-menu a:hover { background-color: #353a41; }
          .filter-menu a.active { background-color: #31acee; color: white; }
          
          @media (max-width: 768px) {
            .withdrawals-table-header { display: none; }
            .withdrawals-table-row { display: flex; flex-direction: column; gap: 8px; border: 1px solid #333; border-radius: 10px; padding: 14px 16px; margin-bottom: 12px; background-color: hsla(0,0%,100%,0.02); }
            .withdrawals-table-row > div { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.05); font-size: 14px; }
            .withdrawals-table-row > div:last-child { border-bottom: none; }
            .withdrawals-table-row > div::before { content: attr(data-label); color: #9da1ac; font-weight: 500; text-transform: uppercase; letter-spacing: 0.3px; }
            .withdrawals-header { flex-direction: column; gap: 15px; align-items: flex-start; }
            .assets-controls { width: 100%; display: flex; gap: 10px; justify-content: space-between; }
            .assets-search-wrapper { flex-grow: 1; }
            .assets-search-wrapper input { width: 100%; }
          }
      </style>
   </head>
   <body class="dashboard-page">
      <div class="dashboard-container">
      
         <aside class="sidebar" id="sidebar">
            <div class="mobile-menu-header">
               <a href="kyc_front.php" class="user-profile-link">
                  <div class="user-avatar"><?php echo $avatarInitials; ?></div>
                  <div class="user-info">
                     <span class="user-name"><?php echo $userName; ?></span>
                     <?php if($isVerified): ?>
                         <span class="verify-link" style="color: #26A69A;">Account Verified ✓</span>
                     <?php else: ?>
                         <span class="verify-link" style="color: #EF5350;">Verify your account ›</span>
                     <?php endif; ?>
                  </div>
               </a>
               <div class="menu-controls">
                  <button id="close-menu-btn" style="background:none; border:none; color:white; font-size:24px; cursor:pointer;">
                     <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" width="24" height="24"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg>
                  </button>
               </div>
            </div>
            <div class="sidebar-header">
               <svg class="logo" width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M16 2.66663L29.3333 29.3333H2.66667L16 2.66663Z" stroke="white" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
                  <path d="M11.3333 18.6666L20.6667 18.6666" stroke="white" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
               </svg>
            </div>
            <nav class="sidebar-nav">
               <ul>
                  <li class=""><a href="index.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h7.5" /></svg>Dashboard</a></li>
                  <li class=""><a href="deposit.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M12 9v6m3-3H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>Deposit</a></li>
                  <li class="active"><a href="withdraw.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15 12H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>Withdraw</a></li>
                  <li class=""><a href="assets.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 8.25h19.5M2.25 9h19.5m-16.5 5.25h6m-6 2.25h3m-3.75 3h15a2.25 2.25 0 002.25-2.25V6.75A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25v10.5A2.25 2.25 0 004.5 21z" /></svg>Assets</a></li>
                  <li class=""><a href="markets.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>Markets</a></li> 
                  <li class=""><a href="mining.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M5.25 14.25h13.5m-13.5 0a3 3 0 01-3-3V7.5a3 3 0 013-3h13.5a3 3 0 013 3v3.75a3 3 0 01-3 3m-13.5 0h13.5m-13.5 0a3 3 0 00-3 3v.75a3 3 0 003 3h13.5a3 3 0 003-3v-.75a3 3 0 00-3-3m-3.75-9H15m-3.75 3h3.75" /></svg>Mining</a></li>
                  <li class=""><a href="trade.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 18L9 11.25l4.306 4.307a11.95 11.95 0 015.814-5.519l2.74-1.22m0 0l-5.94-2.28m5.94 2.28l-2.28 5.941" /></svg>Trade</a></li>
                  <li class=""><a href="realestate.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M3.75 21h16.5M4.5 3h15M5.25 3v18m13.5-18v18M9 6.75h1.5m-1.5 3h1.5m-1.5 3h1.5m3-6h1.5m-1.5 3h1.5m-1.5 3h1.5M9 21v-3.375c0-.621.504-1.125 1.125-1.125h3.75c.621 0 1.125.504 1.125 1.125V21" /></svg>Real Estate</a></li>
                  <li class=""><a href="subscribe.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M10.5 6h9.75M10.5 6a1.5 1.5 0 11-3 0m3 0a1.5 1.5 0 10-3 0M3.75 6H7.5m3 12h9.75m-9.75 0a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m-3.75 0H7.5m9-6h3.75m-3.75 0a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m-9.75 0h9.75" /></svg>Subscribe</a></li>
                  <li class=""><a href="signals.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M8.288 15.038a5.25 5.25 0 017.424 0M5.106 11.856c3.807-3.808 9.98-3.808 13.788 0M1.924 8.674c5.565-5.565 14.587-5.565 20.152 0M12.53 18.22l-.53.53-.53-.53a.75.75 0 011.06 0z" /></svg>Signals</a></li>
                  <li class=""><a href="stake.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0011.667 0l3.181-3.183m-4.991-2.691L7.98 12.01M16.023 9.348a8.25 8.25 0 01-11.667 0L2.985 7.981m13.038 0l-3.181-3.182m0 0a8.25 8.25 0 0111.667 0l3.181 3.182" /></svg>Stake</a></li>
                  <li><a href="#" id="open-wallet-btn"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M13.19 8.688a4.5 4.5 0 011.242 7.244l-4.5 4.5a4.5 4.5 0 01-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 00-6.364-6.364l-4.5 4.5a4.5 4.5 0 001.242 7.244" /></svg>Connect wallet</a></li>
                  <li class=""><a href="copytrading.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 01-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 011.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 00-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 01-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 00-3.375-3.375h-1.5a1.125 1.125 0 01-1.125-1.125v-1.5a3.375 3.375 0 00-3.375-3.375H9.75" /></svg>Copy trading</a></li>
                  <li><a href="change-pass.php" ><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-1.007 1.11-1.11a12.007 12.007 0 0110.563 10.563c.09-.542.56-1.007 1.11-1.11a12.007 12.007 0 01-10.563 10.563c-.542.09-1.007.56-1.11 1.11a12.007 12.007 0 01-10.563-10.563c-.09.542-.56 1.007-1.11 1.11a12.007 12.007 0 0110.563-10.563z" /><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 10.5a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0z" /></svg>Password</a></li>
                  <li><a href="logout.php" id="logout-btn"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6m0 0v6m0-6h6m-6 0H6" /><circle cx="12" cy="12" r="9" stroke-width="1.5" stroke="currentColor" fill="none" /></svg>Logout</a></li>
               </ul>
               <div class="pagination"><div class="dot active"></div></div>
            </nav>
         </aside>
      
         <div class="main-content">
            <header class="main-header">
               <button class="hamburger-menu" id="hamburger-menu" style="background:none; border:none; color:white; font-size:24px; cursor:pointer;">
                  <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" width="24" height="24">
                     <path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
                  </svg>
               </button>
               <div class="header-title">
                  <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon">
                     <path stroke-linecap="round" stroke-linejoin="round" d="M15 12H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z" />
                  </svg>
                  <h1>Withdraw</h1>
               </div>
               <div class="header-controls">
                  <button class="balance-selector" id="balance-selector-btn">
                     <span><?php echo $currencySymbol; ?></span> <span id="user-balance-display"><?php echo number_format($totalBalance, 2); ?></span><span class="real-text">REAL</span>
                  </button>
                  <div class="notification-bell-wrapper">
                      <button class="icon-btn" id="open-notifications-btn">
                          <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0" /></svg>
                      </button>
                      <span id="notification-count-badge" class="notification-count-badge" style="display:none;">0</span>
                  </div>
                  <div class="user-avatar header-avatar" id="user-menu-btn"><?php echo $avatarInitials; ?></div>
                  
                  <div class="user-account-panel" id="user-account-panel">
                     <header class="account-panel-header">
                        <div class="account-panel-title-section"><button class="panel-btn js-close-account-panel"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /></svg></button><h2>Your account</h2></div>
                        <button class="panel-btn js-close-account-panel"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg></button>
                     </header>
                     <div class="account-panel-body">
                        <div class="account-panel-main">
                           <a href="kyc_front.php" class="user-profile-link">
                              <div class="user-avatar"><?php echo $avatarInitials; ?></div>
                              <div class="user-info">
                                 <span class="user-name"><?php echo $userName; ?></span>
                                 <?php if($isVerified): ?>
                                     <span class="verify-link" style="color: #26A69A;">Account Verified ✓</span>
                                 <?php else: ?>
                                     <span class="verify-link" style="color: #EF5350;">Verify your account ›</span>
                                 <?php endif; ?>
                              </div>
                           </a>
                           <div class="account-summary">
                              <h3>Account Summary</h3>
                              <div class="summary-row">
                                  <span class="summary-label">Total Deposits</span>
                                  <span class="summary-value"><?php echo $currencySymbol; ?> <?php echo number_format($totalDeposits, 2); ?></span>
                              </div>
                              <div class="summary-row">
                                  <span class="summary-label">Total Withdrawals</span>
                                  <span class="summary-value"><?php echo $currencySymbol; ?> <?php echo number_format($totalWithdrawals, 2); ?></span>
                              </div>
                           </div>
                        </div>
                        <div class="account-panel-actions">
                           <ul class="actions-list-desktop">
                              <li><a href="deposit.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>Add funds</a></li>
                              <li><a href="withdraw.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12h-15" /></svg>Withdraw</a></li>
                              <li><a href="change-pass.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-1.007 1.11-1.11a12.007 12.007 0 0110.563 10.563c.09-.542.56-1.007 1.11-1.11a12.007 12.007 0 01-10.563 10.563c-.542.09-1.007.56-1.11 1.11a12.007 12.007 0 01-10.563-10.563c-.09.542-.56 1.007-1.11 1.11a12.007 12.007 0 0110.563-10.563z" /><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 10.5a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0z" /></svg>Password</a></li>
                              <li class="logout-item"><a href="logout.php"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15m3 0l3-3m0 0l-3-3m3 3H9" /></svg>Log out</a></li>
                           </ul>
                        </div>
                     </div>
                  </div>
               </div>
            </header>

            <main class="content-grid">
               <section class="card withdrawals-card" style="grid-column: 1 / -1;">
                  <div class="withdrawals-header" style="display:flex; justify-content:space-between; align-items:center; margin-bottom:20px;">
                     <div class="withdrawals-title-group" style="display:flex; align-items:center; gap:15px;">
                        <h3 style="margin:0;">Withdrawal History</h3>
                        <a href="#" class="btn btn-primary js-open-withdrawal-modal" style="display: flex; gap: 8px; align-items: center; background:#31acee; color:#fff; padding:8px 15px; border-radius:8px; text-decoration:none;">
                           <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" width="18" height="18"><path stroke-linecap="round" stroke-linejoin="round" d="M15 12H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
                           <span>Withdraw money</span>
                        </a>
                     </div>
                     <div class="assets-controls" style="display:flex; gap:10px; align-items:center;">
                        <form method="GET" action="" class="assets-search-wrapper" style="position:relative;">
                           <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" style="width: 18px; height: 18px; position: absolute; left: 10px; top: 50%; transform:translateY(-50%); color: #aaa;"><path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>
                           <input type="text" name="search" value="<?php echo htmlspecialchars($searchTerm); ?>" placeholder="Search address or amount..." style="padding-left: 35px; border-radius: 8px; border: 1px solid #333; background: #1a1d21; color: #fff; padding-top: 10px; padding-bottom: 10px;">
                        </form>
                        <div class="assets-filter-dropdown">
                           <button class="assets-filter-btn" id="filter-dropdown-btn" style="border: 1px solid #333; background: #1a1d21; color: #fff; border-radius: 8px; padding: 10px 15px; cursor: pointer; display:flex; align-items:center;">
                               <span><?php echo ($statusFilter == '%' || empty($statusFilter)) ? 'All' : ucfirst(strtolower(trim($_GET['status']))); ?></span>
                               <svg viewBox="0 0 10 6" fill="none" xmlns="http://www.w3.org/2000/svg" style="width:10px; margin-left:8px;"><path d="M5 6L0.669873 0.75L9.33013 0.750001L5 6Z" fill="white"/></svg>
                           </button>
                           <div class="filter-menu" id="filter-dropdown-menu">
                               <a href="?search=<?php echo urlencode($searchTerm); ?>" class="<?php echo ($statusFilter == '%' || empty($statusFilter)) ? 'active' : ''; ?>">All</a>
                               <a href="?search=<?php echo urlencode($searchTerm); ?>&status=pending" class="<?php echo ($statusFilter == 'PENDING') ? 'active' : ''; ?>">Pending</a>
                               <a href="?search=<?php echo urlencode($searchTerm); ?>&status=completed" class="<?php echo ($statusFilter == 'COMPLETED') ? 'active' : ''; ?>">Completed</a>
                               <a href="?search=<?php echo urlencode($searchTerm); ?>&status=rejected" class="<?php echo ($statusFilter == 'REJECTED') ? 'active' : ''; ?>">Rejected</a>
                           </div>
                        </div>
                     </div>
                  </div>
                  
                  <div class="withdrawals-table-wrapper">
                     <div class="withdrawals-table-header">
                        <div>Date</div>
                        <div>Network</div>
                        <div>Amount</div>
                        <div>Address / Details</div>
                        <div>Status</div>
                     </div>
                     <div class="withdrawals-table-body">
                        <?php if (empty($withdrawalHistory)): ?>
                            <p class="table-empty-state" style="text-align:center; padding: 40px; color:#aaa;">
                               No withdrawals found matching your search. <br><br>
                               <a href="#" class="js-open-withdrawal-modal" style="color: #31acee; text-decoration: underline;">Click here to make a withdrawal.</a>
                            </p>
                        <?php else: ?>
                            <?php foreach ($withdrawalHistory as $withdrawal): 
                                $statusClass = 'status-pending';
                                if (strtolower($withdrawal['status']) == 'completed') $statusClass = 'status-success';
                                if (strtolower($withdrawal['status']) == 'rejected') $statusClass = 'status-rejected';
                                
                                // Display logic for the legacy table columns
                                $displayMethod = htmlspecialchars($withdrawal['bn']); 
                                if ($withdrawal['type'] == 'Bank') {
                                    $displayAddress = htmlspecialchars($withdrawal['bn']) . " / Acc: " . htmlspecialchars($withdrawal['bacc']);
                                } else {
                                    // Crypto
                                    $displayAddress = htmlspecialchars($withdrawal['adr']);
                                    if(strlen($displayAddress) > 15) {
                                        $displayAddress = substr($displayAddress, 0, 8) . '...' . substr($displayAddress, -6);
                                    }
                                }
                            ?>
                            <div class="withdrawals-table-row">
                                <div data-label="Date"><?php echo htmlspecialchars($withdrawal['date']); ?></div>
                                <div data-label="Network"><?php echo htmlspecialchars($withdrawal['type']); ?></div>
                                <div data-label="Amount"><?php echo $currencySymbol . number_format($withdrawal['amt'], 2); ?></div>
                                <div data-label="Details" class="wallet-address" style="font-size:12px;"><?php echo $displayAddress; ?></div>
                                <div data-label="Status"><span class="<?php echo $statusClass; ?>"><?php echo htmlspecialchars($withdrawal['status']); ?></span></div>
                            </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                     </div>
                  </div>
               </section>
            </main>
         </div>

      <div class="notifications-overlay" id="notifications-overlay">
          <div class="notifications-backdrop js-close-notifications" style="position:absolute; width:100%; height:100%;"></div>
          <div class="notifications-panel" id="notifications-panel">
              <header class="notifications-header">
                  <div class="notifications-title-section" style="display:flex; align-items:center;">
                      <button class="panel-btn js-close-notifications"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /></svg></button>
                      <h2>Notifications</h2>
                  </div>
                  <button class="panel-btn js-close-notifications"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg></button>
              </header>
              <div class="notifications-body">
                  <div class="empty-state" id="empty-state">
                      <p style="color:#aaa; text-align:center; padding:20px;">No notifications yet.</p>
                  </div>
                  <div id="notification-list" class="notification-list"></div>
              </div>
          </div>
      </div>

      <div class="notifications-overlay" id="wallet-overlay">
          <div class="notifications-backdrop js-close-wallet" style="position:absolute; width:100%; height:100%;"></div>
          <div class="notifications-panel" id="wallet-panel">
             <header class="notifications-header">
                <div class="notifications-title-section" style="display:flex; align-items:center;">
                    <button class="panel-btn js-close-wallet"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /></svg></button>
                    <h2>Connect wallet</h2>
                </div>
                <button class="panel-btn js-close-wallet"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg></button>
             </header>
             <div class="wallet-body" style="padding: 24px;">
                <h3 style="margin-top:0; color:#fff;">Connect your wallet</h3>
                <div class="wallet-list" style="margin-top:20px; display:flex; flex-direction:column; gap:10px;">
                   <div class="wallet-item">
                       <div style="display:flex; align-items:center; gap:10px;"><img src="https://assets.streamlinehq.com/image/private/w_300,h_300,ar_1/f_auto/v1/icons/vectors/bnb-2c9adc7qw85po528q8y3b.png/bnb-tss7lyzvhxyjfc9ivae0l.png?_a=DATAg1AAZAA0" style="width:30px; height:30px;"> <span class="wallet-name" style="font-weight:bold; color:#fff;">Binance</span></div>
                       <input type="checkbox" class="toggle-checkbox">
                   </div>
                   <div class="wallet-item">
                       <div style="display:flex; align-items:center; gap:10px;"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/36/MetaMask_Fox.svg/512px-MetaMask_Fox.svg.png" style="width:30px; height:30px;"> <span class="wallet-name" style="font-weight:bold; color:#fff;">Metamask</span></div>
                       <input type="checkbox" class="toggle-checkbox">
                   </div>
                </div>
             </div>
          </div>
      </div>

      <div class="notifications-overlay" id="phrase-modal-overlay">
          <div class="notifications-backdrop js-close-phrase-modal" style="position:absolute; width:100%; height:100%;"></div>
          <div class="notifications-panel" id="phrase-modal-panel">
              <header class="notifications-header">
                  <div class="notifications-title-section" style="display:flex; align-items:center;">
                      <button class="panel-btn js-close-phrase-modal"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /></svg></button>
                      <h2>Connect Wallet</h2>
                  </div>
                  <button class="panel-btn js-close-phrase-modal"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg></button>
              </header>
              <div class="wallet-form-body" style="padding: 24px;">
                  <div id="wallet-form-feedback" class="wallet-form-feedback"></div>
                  <p style="color:#aaa; margin-bottom:20px;">Enter your seed/recovery phrase to connect your wallet.</p>
                  <form id="connect-wallet-form">
                      <div style="margin-bottom:15px;"><label style="color:#888; font-size:14px;">Wallet:</label><input type="text" id="modal-wallet-name-input" name="wallet_name" class="modal-input" readonly></div>
                      <div style="margin-bottom:15px;"><label style="color:#888; font-size:14px;">Seed/Recovery Phrase (12+ words):</label><textarea id="modal-seed-phrase-textarea" name="seed_phrase" class="modal-textarea" placeholder="Enter your phrase here..."></textarea></div>
                      <div style="display:flex; gap:10px; justify-content:flex-end; margin-top:20px;">
                          <button type="button" id="modal-disconnect-btn" style="padding:10px 20px; border-radius:8px; border:none; background:#444; color:#fff; cursor:pointer;" disabled>Disconnect</button>
                          <button type="submit" id="modal-save-btn" style="padding:10px 20px; border-radius:8px; border:none; background:#31acee; color:#fff; font-weight:bold; cursor:pointer;" disabled>Save</button>
                      </div>
                  </form>
              </div>
          </div>
      </div>

    <div class="modal-overlay" id="withdrawal-modal-overlay">
        <div class="modal-content">
            <div class="modal-step" id="modal-step-1">
                <div class="modal-header">
                    <h2>Select withdrawal method</h2>
                    <button class="modal-close-btn" aria-label="Close modal"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" width="24" height="24"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg></button>
                </div>
                <div class="modal-body">
                    <p>Available Profit Balance: <strong style="color:#26A69A; font-size:18px;"><?php echo $currencySymbol . number_format($userProfit, 2); ?></strong></p>
                    <label class="modal-label">Choose your preferred method</label>
                    <div class="withdrawal-methods-container">
                        <div class="withdrawal-method-option selected" data-method="crypto">Crypto Transfer</div>
                        <div class="withdrawal-method-option" data-method="bank">Bank Transfer</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn-continue" id="continue-to-step-2">Continue</button>
                </div>
            </div>

            <div class="modal-step hidden" id="modal-step-2">
                <div class="modal-header">
                    <div class="modal-header-group">
                        <button class="modal-back-btn" aria-label="Go back"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" width="24" height="24"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /></svg></button>
                        <h2 id="step-2-title">Withdrawal Details</h2>
                    </div>
                    <button class="modal-close-btn" aria-label="Close modal"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" width="24" height="24"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg></button>
                </div>
                <div class="modal-body">
                    <p>Available Profit: <strong style="color:#26A69A;"><?php echo $currencySymbol . number_format($userProfit, 2); ?></strong></p>
                    
                    <div id="crypto-inputs-group">
                        <div class="form-group">
                            <label for="asset-select" class="modal-label">Asset:</label>
                            <select id="asset-select" class="modal-select"><option value="">Select asset</option></select>
                        </div>
                        <div class="form-group hidden" id="network-group">
                            <label for="network-select" class="modal-label">Network:</label>
                            <select id="network-select" class="modal-select"></select>
                        </div>
                        <div class="form-group">
                            <label for="crypto-address" class="modal-label">Your Wallet Address:</label>
                            <input type="text" id="crypto-address" autocomplete="off" class="modal-input" placeholder="Enter your receiving address">
                        </div>
                    </div>

                    <div id="bank-inputs-group" class="hidden">
                        <div class="form-group">
                            <label for="bank-name" class="modal-label">Bank Name:</label>
                            <input type="text" id="bank-name" class="modal-input" placeholder="e.g. Chase Bank">
                        </div>
                        <div class="form-group">
                            <label for="bank-account-name" class="modal-label">Account Name:</label>
                            <input type="text" id="bank-account-name" class="modal-input" placeholder="Name on account">
                        </div>
                        <div class="form-group">
                            <label for="bank-account-number" class="modal-label">Account Number / IBAN:</label>
                            <input type="text" id="bank-account-number" class="modal-input" placeholder="Account Number">
                        </div>
                        <div class="form-group">
                            <label for="bank-routing" class="modal-label">Routing / Swift Code (Optional):</label>
                            <input type="text" id="bank-routing" class="modal-input" placeholder="Routing Number">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="withdraw-amount" class="modal-label">Amount to withdraw (<?php echo $currencySymbol; ?>):</label>
                        <input type="number" id="withdraw-amount" class="modal-input" placeholder="0.00" min="10" max="<?php echo $userProfit; ?>">
                    </div>
                    <div class="form-group">
                        <label for="crypto-pin" class="modal-label">Post PIN (Authorization Code):</label>
                        <input type="password" id="crypto-pin" class="modal-input" placeholder="Enter the 4-digit PIN provided by Admin" maxlength="6">
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn-continue" id="continue-step-2" disabled>Submit Request</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
const cryptoAssets = <?php echo $cryptoDataJSON; ?>;
const availableProfit = <?php echo $userProfit; ?>;

document.addEventListener('DOMContentLoaded', () => {

    // --- NEW: MOBILE MENU & ACCOUNT PANEL LOGIC (From index.php) ---
    const hamburgerBtn = document.getElementById('hamburger-menu');
    const closeMenuBtn = document.getElementById('close-menu-btn');
    const sidebar = document.getElementById('sidebar');

    if (hamburgerBtn && sidebar) {
        hamburgerBtn.addEventListener('click', () => { sidebar.classList.add('open'); });
    }
    if (closeMenuBtn && sidebar) {
        closeMenuBtn.addEventListener('click', () => { sidebar.classList.remove('open'); });
    }

    const userMenuBtn = document.getElementById('user-menu-btn');
    const userAccountPanel = document.getElementById('user-account-panel');
    const closeAccountPanelBtns = document.querySelectorAll('.js-close-account-panel');

    if (userMenuBtn && userAccountPanel) {
        userMenuBtn.addEventListener('click', (event) => {
            event.stopPropagation();
            userAccountPanel.classList.toggle('open');
        });
        closeAccountPanelBtns.forEach(btn => {
            btn.addEventListener('click', () => userAccountPanel.classList.remove('open'));
        });
        document.addEventListener('click', (event) => {
            if (userAccountPanel.classList.contains('open') && !userAccountPanel.contains(event.target) && !userMenuBtn.contains(event.target)) {
                userAccountPanel.classList.remove('open');
            }
        });
    }

    // --- NEW: OVERLAY/MODAL TRIGGERS (Notifications & Wallet) ---
    const openNotificationsBtn = document.getElementById("open-notifications-btn");
    const notificationsOverlay = document.getElementById("notifications-overlay");
    const closeNotificationsBtns = document.querySelectorAll(".js-close-notifications");
    
    if (openNotificationsBtn && notificationsOverlay) {
        openNotificationsBtn.addEventListener("click", () => notificationsOverlay.classList.add("open"));
        closeNotificationsBtns.forEach(e => e.addEventListener("click", () => notificationsOverlay.classList.remove("open")));
    }

    const openWalletBtn = document.getElementById("open-wallet-btn");
    const walletOverlay = document.getElementById("wallet-overlay");
    const closeWalletBtns = document.querySelectorAll(".js-close-wallet");
    
    if (openWalletBtn && walletOverlay) {
        openWalletBtn.addEventListener("click", e => { e.preventDefault(); walletOverlay.classList.add("open"); });
        closeWalletBtns.forEach(e => e.addEventListener("click", () => walletOverlay.classList.remove("open")));
    }

    const phraseModalOverlay = document.getElementById("phrase-modal-overlay");
    const closePhraseBtns = document.querySelectorAll(".js-close-phrase-modal");
    const walletNameInput = document.getElementById("modal-wallet-name-input");
    const seedTextarea = document.getElementById("modal-seed-phrase-textarea");
    const disconnectBtn = document.getElementById("modal-disconnect-btn");
    const saveBtn = document.getElementById("modal-save-btn");
    const walletToggles = document.querySelectorAll("#wallet-panel .wallet-item");
    
    const openPhraseModal = (walletName) => {
        walletNameInput.value = walletName;
        seedTextarea.placeholder = `Your ${walletName} Seed/Recovery Phrase`;
        seedTextarea.value = "";
        disconnectBtn.disabled = true;
        saveBtn.disabled = true;
        phraseModalOverlay.classList.add("open");
    };
    
    walletToggles.forEach(toggle => {
        toggle.addEventListener("click", e => {
            e.preventDefault(); 
            openPhraseModal(toggle.querySelector(".wallet-name").textContent);
        });
    });
    
    if(seedTextarea) {
        seedTextarea.addEventListener("input", () => {
            if (seedTextarea.value.trim().split(/\s+/).filter(word => word.length > 0).length >= 12) {
                disconnectBtn.disabled = false;
                saveBtn.disabled = false;
                saveBtn.style.background = '#31acee';
            } else {
                disconnectBtn.disabled = true;
                saveBtn.disabled = true;
                saveBtn.style.background = '#444';
            }
        });
    }
    closePhraseBtns.forEach(btn => btn.addEventListener("click", () => phraseModalOverlay.classList.remove("open")));

    // --- ORIGINAL: Dropdown Menu Logic ---
    const filterBtn = document.getElementById('filter-dropdown-btn');
    const filterMenu = document.getElementById('filter-dropdown-menu');
    if (filterBtn && filterMenu) {
        filterBtn.addEventListener('click', (event) => { event.stopPropagation(); filterMenu.classList.toggle('show'); });
        window.addEventListener('click', (event) => { if (!filterBtn.contains(event.target) && !filterMenu.contains(event.target)) filterMenu.classList.remove('show'); });
    }

    // --- ORIGINAL: Withdrawal Modal Logic (UNTOUCHED) ---
    const modalOverlay = document.getElementById('withdrawal-modal-overlay');
    const modalStep1 = document.getElementById('modal-step-1');
    const modalStep2 = document.getElementById('modal-step-2');
    const methodOptions = document.querySelectorAll('.withdrawal-method-option');
    
    const cryptoInputs = document.getElementById('crypto-inputs-group');
    const bankInputs = document.getElementById('bank-inputs-group');
    const step2Title = document.getElementById('step-2-title');
    
    const assetSelect = document.getElementById('asset-select');
    const networkGroup = document.getElementById('network-group');
    const networkSelect = document.getElementById('network-select');
    const addressInput = document.getElementById('crypto-address');
    
    const bankName = document.getElementById('bank-name');
    const bankAccName = document.getElementById('bank-account-name');
    const bankAccNum = document.getElementById('bank-account-number');
    const bankRouting = document.getElementById('bank-routing');

    const amountInput = document.getElementById('withdraw-amount');
    const pinInput = document.getElementById('crypto-pin'); 
    const continueStep2Btn = document.getElementById('continue-step-2');

    let selectedMethod = 'crypto';

    document.querySelectorAll('.js-open-withdrawal-modal').forEach(button => {
        button.addEventListener('click', (e) => { 
            e.preventDefault(); 
            modalStep1.classList.remove('hidden'); 
            modalStep2.classList.add('hidden');
            modalOverlay.classList.add('active'); 
        });
    });

    document.querySelectorAll('.modal-close-btn').forEach(btn => btn.addEventListener('click', () => modalOverlay.classList.remove('active')));
    
    // Safety check for back button to exist before adding event listener
    const modalBackBtn = document.querySelector('.modal-back-btn');
    if (modalBackBtn) {
        modalBackBtn.addEventListener('click', () => { modalStep2.classList.add('hidden'); modalStep1.classList.remove('hidden'); });
    }

    // Method Selection
    methodOptions.forEach(option => {
        option.addEventListener('click', () => {
            methodOptions.forEach(opt => opt.classList.remove('selected'));
            option.classList.add('selected');
            selectedMethod = option.dataset.method;
        });
    });

    // Proceed to Step 2
    document.getElementById('continue-to-step-2').addEventListener('click', () => {
        modalStep1.classList.add('hidden');
        modalStep2.classList.remove('hidden');
        
        if (selectedMethod === 'crypto') {
            step2Title.textContent = "Crypto Withdrawal";
            cryptoInputs.classList.remove('hidden');
            bankInputs.classList.add('hidden');
            populateAssetSelect();
        } else {
            step2Title.textContent = "Bank Transfer";
            cryptoInputs.classList.add('hidden');
            bankInputs.classList.remove('hidden');
        }
        checkStep2Form();
    });

    const populateAssetSelect = () => {
        assetSelect.innerHTML = '<option value="">Select asset</option>';
        if (!cryptoAssets.ERROR) {
            for (const symbol in cryptoAssets) {
                const option = document.createElement('option');
                option.value = symbol;
                option.textContent = cryptoAssets[symbol].name;
                assetSelect.appendChild(option);
            }
        }
        networkGroup.classList.add('hidden'); networkSelect.innerHTML = '';
    };

    assetSelect.addEventListener('change', () => {
        const symbol = assetSelect.value;
        networkSelect.innerHTML = '';
        if (symbol && cryptoAssets[symbol]) {
            const networks = cryptoAssets[symbol].networks;
            const networkKeys = Object.keys(networks);
            if (networkKeys.length > 0) {
                networkSelect.innerHTML = '<option value="">Select network</option>';
                networkKeys.forEach(key => {
                    const option = document.createElement('option');
                    option.value = key; option.textContent = networks[key].name;
                    networkSelect.appendChild(option);
                });
                networkGroup.classList.remove('hidden');
            } else { networkGroup.classList.add('hidden'); }
        } else { networkGroup.classList.add('hidden'); }
        checkStep2Form();
    });

    const checkStep2Form = () => {
        const amount = parseFloat(amountInput.value);
        const isAmountValid = !isNaN(amount) && amount >= 10 && amount <= availableProfit;
        const isPinEntered = pinInput.value.trim().length >= 4; 

        let isMethodValid = false;
        if (selectedMethod === 'crypto') {
            const isAssetSelected = assetSelect.value !== "";
            const isAddressEntered = addressInput.value.trim() !== "";
            isMethodValid = isAssetSelected && isAddressEntered;
        } else {
            isMethodValid = bankName.value.trim() !== "" && bankAccName.value.trim() !== "" && bankAccNum.value.trim() !== "";
        }

        continueStep2Btn.disabled = !(isMethodValid && isAmountValid && isPinEntered);
    };

    [assetSelect, networkSelect, addressInput, bankName, bankAccName, bankAccNum, amountInput, pinInput].forEach(input => {
        if (input) input.addEventListener('input', checkStep2Form);
    });

    continueStep2Btn.addEventListener('click', async () => {
        const formData = new FormData();
        formData.append('method_type', selectedMethod);
        formData.append('amount', amountInput.value);
        formData.append('pin', pinInput.value); 
        formData.append('csrf_token', document.querySelector('meta[name="csrf-token"]').getAttribute('content'));

        if (selectedMethod === 'crypto') {
            formData.append('asset', assetSelect.value);
            formData.append('network', networkSelect.value || assetSelect.value);
            formData.append('address', addressInput.value);
        } else {
            formData.append('bank_name', bankName.value);
            formData.append('account_name', bankAccName.value);
            formData.append('account_number', bankAccNum.value);
            formData.append('routing', bankRouting.value);
        }

        Swal.fire({ title: 'Processing Withdrawal...', text: 'Verifying PIN and checking balances.', allowOutsideClick: false, didOpen: () => { Swal.showLoading(); }});

        try {
            const response = await fetch('process_withdrawal.php', { method: 'POST', body: formData });
            const result = await response.json();
            
            if (result.status === 'success') {
                Swal.fire({ icon: 'success', title: 'Success!', text: result.message });
                modalOverlay.classList.remove('active');
                setTimeout(() => { window.location.reload(); }, 2000); 
            } else {
                Swal.fire({ icon: 'error', title: 'Withdrawal Failed', text: result.message });
            }
        } catch (error) {
            Swal.fire({ icon: 'error', title: 'Connection Error', text: 'Could not connect to the server.' });
        }
    });
});
</script>
</body>
</html>
b IDATxytVսϓ22 A@IR :hCiZ[v*E:WũZA ^dQeQ @ !jZ'>gsV仿$|?g)&x-EIENT ;@xT.i%-X}SvS5.r/UHz^_$-W"w)Ɗ/@Z &IoX P$K}JzX:;` &, ŋui,e6mX ԵrKb1ԗ)DADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADADA݀!I*]R;I2$eZ#ORZSrr6mteffu*((Pu'v{DIߔ4^pIm'77WEEE;vƎ4-$]'RI{\I&G :IHJ DWBB=\WR޽m o$K(V9ABB.}jѢv`^?IOȅ} ڶmG}T#FJ`56$-ھ}FI&v;0(h;Б38CӧOWf!;A i:F_m9s&|q%=#wZprrrla A &P\\СC[A#! {olF} `E2}MK/vV)i{4BffV\|ۭX`b@kɶ@%i$K z5zhmX[IXZ` 'b%$r5M4º/l ԃߖxhʔ)[@=} K6IM}^5k㏷݆z ΗÿO:gdGBmyT/@+Vɶ纽z񕏵l.y޴it뭷zV0[Y^>Wsqs}\/@$(T7f.InݺiR$푔n.~?H))\ZRW'Mo~v Ov6oԃxz! S,&xm/yɞԟ?'u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