<?php
include('inc/session.php'); // Ensure $conn and $email are defined here

$error_message = '';
$success_message = '';

if (isset($_POST['submit'])) {
    // Retrieve form data
    $amount_str = $_POST['amount'] ?? '';
    $currency = $_POST['currency'] ?? '';
    $network = $_POST['network'] ?? null; // Optional, so allow null
    $date = date('Y-m-d H:i:s');
    $status = 'Pending'; // Default status for a new withdrawal request

    // --- Validation ---
    $errors = [];

    if (empty($email)) { // Check if $email is set from session.php
        $errors[] = "User session not found. Please log in again.";
    }

    if (!is_numeric($amount_str) || floatval($amount_str) <= 0) {
        $errors[] = "Please enter a valid positive amount.";
    } else {
        $amount_float = floatval($amount_str); // Store the validated float
    }

    if (empty($currency)) {
        $errors[] = "Please select a currency.";
    }
    // You might want more specific validation for currency values if needed

    if (empty($errors)) {
        if (!$conn || $conn->connect_error) {
            error_log("Database connection error before withdrawal prepare: " . ($conn ? $conn->connect_error : "Connection object null"));
            $error_message = "Database connection error. Please try again later.";
        } else {
            // Prepare SQL and bind parameters for the 'withdrawal' table
            // Ensure your 'withdrawal' table has these columns: email, amount, currency, network, status, created_at
            $sql = "INSERT INTO withdrawal (email, amount, currency, network, status, created_at) VALUES (?, ?, ?, ?, ?, ?)";
            $stmt = $conn->prepare($sql);

            if ($stmt === false) {
                $db_error = $conn->error;
                error_log("Withdrawal Prepare failed: " . $db_error);
                $error_message = "An error occurred while preparing your withdrawal request. Details: " . htmlspecialchars($db_error);
            } else {
                // Bind parameters:
                // s = string, d = double (for float)
                // Assuming 'amount' column in DB is DECIMAL/FLOAT, use 'd' for $amount_float.
                // If 'amount' is VARCHAR, use 's' for $amount_str.
                // For now, let's assume amount is numeric in DB
                $stmt->bind_param("sdssss", $email, $amount_float, $currency, $network, $status, $date);

                if ($stmt->execute()) {
                    $success_message = "Withdrawal request submitted successfully. It is now pending approval.";
                    // Optionally, clear form fields or redirect
                    // $_POST = array(); // Clear POST to prevent resubmission on refresh
                } else {
                    $exec_error = $stmt->error;
                    error_log("Withdrawal Execute failed: " . $exec_error);
                    $error_message = "Error submitting your withdrawal request. Details: " . htmlspecialchars($exec_error);
                }
                $stmt->close();
            }
        }
    } else {
        $error_message = implode("<br>", array_map('htmlspecialchars', $errors));
    }
}
// No $conn->close() here if it's needed by the history table below. It will be closed at the end of the script.

// $full_name should be defined in inc/session.php for the sidebar display
$full_name = $full_name ?? 'User'; // Default if not set
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Withdrawal Request</title> <!-- Page-specific title -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionic/1.3.2/css/ionic.min.css">
    <link rel="shortcut icon" href="logo1.png" /> <!-- Assuming logo1.png is your favicon -->
   <style>
    /* style.css */

    /* --- Basic Reset & Defaults --- */
    * { margin: 0; padding: 0; box-sizing: border-box; }
    html { scroll-behavior: smooth; height: 100%; }
    body {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background-color: #f4f7fc; color: #333; line-height: 1.6;
        overflow-x: hidden; /* Prevent horizontal scroll on body */
        min-height: 100%; position: relative;
    }
    a { text-decoration: none; color: inherit; }
    ul { list-style: none; }

    /* --- CSS Only Toggle Setup --- */
    .sidebar-toggle-checkbox {
        position: absolute; opacity: 0; pointer-events: none; width: 0; height: 0;
    }

    /* --- Main Layout --- */
    .dashboard-container { display: flex; min-height: 100vh; position: relative; }

    /* --- Sidebar --- */
    .sidebar {
        width: 250px; background-color: #2c3e50; color: #ecf0f1;
        display: flex; flex-direction: column; transition: width 0.3s ease, left 0.3s ease;
        position: sticky; /* Stick to viewport top */
        top: 0; height: 100vh; z-index: 1000;
        flex-shrink: 0; overflow: visible; /* Allow tooltips to overflow */
    }
    .sidebar-header { padding: 15px; text-align: center; border-bottom: 1px solid #34495e; flex-shrink: 0; }
    .sidebar-header .logo { max-width: 60px; height: auto; /*padding: 5px;*/ border-radius: 4px; display: inline-block; transition: max-width 0.3s ease; vertical-align: middle; margin-bottom:10px;}
    .sidebar-header h2 { font-size: 1.2em; font-weight: 600; white-space: nowrap; overflow: hidden; transition: opacity 0.1s ease 0.1s; opacity: 1; color: #ecf0f1;}
    .sidebar-nav { flex-grow: 1; overflow-y: auto; overflow-x: hidden; } /* Allows internal nav scrolling if needed */
    .sidebar-nav ul { padding-top: 10px; }
    .sidebar-nav li { margin-bottom: 2px; position: relative; }
    /* Make anchor links relative for tooltip positioning */
    .sidebar-nav a { display: flex; align-items: center; padding: 12px 20px; color: #bdc3c7; transition: background-color 0.3s ease, color 0.3s ease, padding 0.3s ease; white-space: nowrap; overflow: hidden; position: relative; }
    .sidebar-nav a i:first-child { margin-right: 15px; font-size: 1.3em; width: 20px; text-align: center; flex-shrink: 0; transition: margin-right 0.3s ease; }
    .sidebar-nav a span { flex-grow: 1; overflow: hidden; text-overflow: ellipsis; opacity: 1; transition: opacity 0.2s ease 0.1s, visibility 0s linear 0s; visibility: visible; } /* Added visibility transition */
    .sidebar-nav a:hover { background-color: #34495e; color: #ffffff; }
    /* Active state styles */
    .sidebar-nav a.active,
    .sidebar-nav li.active > a { /* Target direct child 'a' when li has active */
        background-color: #34495e; color: #ffffff; font-weight: 500;
    }

    /* --- Submenu Styling --- */
    .has-submenu > a .submenu-indicator { position: absolute; right: 15px; top: 50%; transform: translateY(-50%) rotate(0deg); transition: transform 0.3s ease; font-size: 0.8em; color: #bdc3c7; }
    .submenu { list-style: none; padding-left: 0; margin: 0; background-color: rgba(0, 0, 0, 0.1); max-height: 0; overflow: hidden; transition: max-height 0.3s ease-out; }
    .submenu li { margin-bottom: 0; }
    .submenu a.sub-link { padding-left: 40px; font-size: 0.9em; color: #a8b0b4; background-color: transparent; }
    .submenu a.sub-link i { margin-right: 10px !important; font-size: 1.1em !important; }
    .submenu a.sub-link:hover, .submenu a.sub-link.active { color: #ffffff; background-color: rgba(0, 0, 0, 0.2); }

    /* Desktop Submenu Hover (Only when NOT collapsed and NOT forced mobile open) */
    #sidebar-toggle:not(:checked) ~ .dashboard-container .sidebar:not(.mobile-forced-open) .has-submenu:hover > a .submenu-indicator {
        transform: translateY(-50%) rotate(180deg);
    }
    #sidebar-toggle:not(:checked) ~ .dashboard-container .sidebar:not(.mobile-forced-open) .has-submenu:hover > .submenu {
        max-height: 500px;
        transition: max-height 0.4s ease-in;
    }
    /* JS/Mobile driven open state */
    .sidebar .has-submenu.open > a .submenu-indicator {
        transform: translateY(-50%) rotate(180deg);
    }
     .sidebar .has-submenu.open > .submenu {
         transition: max-height 0.3s ease-out;
     }


    .sidebar-nav li.logout { margin-top: 10px; border-top: 1px solid #34495e; padding-top: 10px; }

    /* --- Sidebar Toggler (Label Button) --- */
    .sidebar-toggler-wrapper { padding: 10px 0; border-top: 1px solid #34495e; flex-shrink: 0; }
    .sidebar-toggle-btn { background-color: transparent; border: none; color: #bdc3c7; padding: 10px 20px; cursor: pointer; width: 100%; text-align: center; font-size: 1.5em; line-height: 1; transition: color 0.3s ease; display: block; }
    .sidebar-toggle-btn:hover { color: #ffffff; }
    .sidebar-toggle-btn .icon-expand { display: none; }
    .sidebar-toggle-btn .icon-collapse { display: inline-block; }

    /* --- Sidebar Collapsed State (Desktop - via Checkbox) --- */
    #sidebar-toggle:checked ~ .dashboard-container .sidebar { width: 70px; overflow: visible; /* Allow tooltips */ }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-header h2 { opacity: 0; visibility: hidden; transition: opacity 0.1s ease, visibility 0s linear 0.1s; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-header .logo { max-width: 40px; margin-bottom: 0; }
    /* Hide span immediately when collapsing */
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a span { opacity: 0; width: 0; pointer-events: none; visibility: hidden; transition: opacity 0.1s ease, visibility 0s linear 0.1s, width 0s linear 0.1s; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a i:first-child { margin-right: 0; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a { justify-content: center; padding: 12px 0; overflow: visible; /* Allow tooltips */ } /* Center icon */
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .has-submenu > a .submenu-indicator { display: none; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .submenu { display: none !important; max-height: 0; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-toggler-wrapper .sidebar-toggle-btn .icon-expand { display: inline-block; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-toggler-wrapper .sidebar-toggle-btn .icon-collapse { display: none; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .has-submenu:hover > .submenu { max-height: 0; } /* Prevent submenu hover open */


    /* --- Tooltip for Collapsed Sidebar --- */
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a::after {
        content: attr(data-tooltip); /* Get text from data-tooltip attribute */
        position: absolute;
        left: calc(100% + 10px); /* Position to the right of the sidebar */
        top: 50%;
        transform: translateY(-50%);
        background-color: #2c3e50; /* Match sidebar color or choose another */
        color: #ecf0f1;
        padding: 5px 10px;
        border-radius: 4px;
        font-size: 0.85em;
        white-space: nowrap;
        z-index: 1010; /* Ensure it's above other elements */
        opacity: 0;
        visibility: hidden;
        pointer-events: none; /* Prevent tooltip from blocking hover on item below */
        transition: opacity 0.2s ease 0.1s, visibility 0s linear 0.3s; /* Delay appearance slightly */
    }
    /* Show tooltip on hover */
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a:hover::after {
        opacity: 1;
        visibility: visible;
        transition: opacity 0.2s ease 0.1s, visibility 0s linear 0.1s; /* Faster appearance */
    }
    /* Hide tooltip if mobile overlay is forced open (shouldn't happen with collapsed, but good practice) */
     #sidebar-toggle:checked ~ .dashboard-container .sidebar.mobile-forced-open .sidebar-nav a:hover::after {
        opacity: 0;
        visibility: hidden;
     }


    /* --- Main Content Area --- */
    .main-content {
        flex-grow: 1;
        display: flex; flex-direction: column;
        position: relative;
        height: 100vh;
        overflow-y: auto; /* Main scroll container */
        background-color: #f4f7fc;
    }

    /* --- Header --- */
    .main-header {
        background-color: #02101c; padding: 15px 30px;
        display: flex; justify-content: space-between; align-items: center;
        border-bottom: 1px solid #34495e; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        position: sticky; /* Stick to the top of main-content */
        top: 0;
        z-index: 900; flex-shrink: 0;
    }
    .header-left { display: flex; align-items: center; }
    .header-left h1 { font-size: 1.5em; margin-left: 15px; color: #fff; font-weight: 600; } /* Header title color white */
    .header-right { display: flex; align-items: center; }
    .admin-info { display: flex; align-items: center; color: #ecf0f1; } /* Admin info color light */
    .admin-info span { margin-right: 10px; }
    .admin-info .avatar { width: 40px; height: 40px; border-radius: 50%; background-color: #ccc; object-fit: cover; }
    .menu-toggle { background: none; border: none; font-size: 1.8em; cursor: pointer; color: #fff; display: none; padding: 5px; line-height: 1; } /* Menu toggle color white */

    /* --- Page Content --- */
    .page-content {
        padding: 30px;
        flex-grow: 1; /* Allows content below header to fill remaining space */
    }
    h2 { font-size: 1.8em; margin-bottom: 20px; color: #fff; font-weight: 600; } /* Page h2 color white */

    /* --- Tables, Status, Buttons, Forms, Pagination, Placeholders --- */
    .table-container { overflow-x: auto; margin-top: 20px; background-color: #ffffff; box-shadow: 0 2px 5px rgba(0,0,0,0.05); border-radius: 8px; }
    table { width: 100%; border-collapse: collapse; }
    th, td { padding: 12px 15px; text-align: left; border-bottom: 1px solid #e0e0e0; white-space: nowrap; }
    thead th { background-color: #f8f9fa; color: #495057; font-weight: 600; font-size: 0.9em; text-transform: uppercase; position: sticky; top: 0; z-index: 1; } /* Sticky within table-container */
    .table-container tr:last-child td { border-bottom: none; }
    tbody tr:hover { background-color: #f1f3f5; }
    .status { padding: 3px 8px; border-radius: 12px; font-size: 0.8em; font-weight: bold; text-transform: capitalize; display: inline-block; }
    .status.completed, .status.approved { background-color: #d4edda; color: #155724; } /* Added approved */
    .status.pending { background-color: #fff3cd; color: #856404; }
    .status.failed, .status.rejected { background-color: #f8d7da; color: #721c24; } /* Added rejected */

    .btn { padding: 8px 15px; border: 1px solid #ccc; background-color: #f8f9fa; color: #333; border-radius: 4px; cursor: pointer; transition: background-color 0.2s ease, border-color 0.2s ease; font-size: 0.9em; margin-right: 5px; white-space: nowrap; }
    .btn:last-child { margin-right: 0; }
    .btn:hover { background-color: #e2e6ea; border-color: #adb5bd; }
    .btn-primary { background-color: #007bff; border-color: #007bff; color: white; }
    .btn-primary:hover { background-color: #0056b3; border-color: #0056b3; }
    .btn-success { background-color: #28a745; border-color: #28a745; color: white; }
    .btn-success:hover { background-color: #1e7e34; border-color: #1c7430; }
    .btn-warning { background-color: #ffc107; border-color: #ffc107; color: #212529; }
    .btn-warning:hover { background-color: #e0a800; border-color: #d39e00; }
    .btn-danger { background-color: #dc3545; border-color: #dc3545; color: white; }
    .btn-danger:hover { background-color: #c82333; border-color: #bd2130; }
    .btn-sm { padding: 5px 10px; font-size: 0.8em; }
    .controls { margin-bottom: 20px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
    input[type="text"], input[type="number"], select, textarea { /* Added textarea and number */
        padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 0.9em;
        min-width: 150px; flex-grow: 1; max-width: 300px;
        background-color: #fff; color: #333; /* Ensure contrast */
    }
    .controls input[type="text"] { max-width: 400px; }
    .controls select { max-width: 200px; flex-grow: 0; }
    .controls button { flex-grow: 0; }
    input[type="text"]:focus, input[type="number"]:focus, select:focus, textarea:focus {
        outline: none; border-color: #007bff; box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
    }
    .pagination { margin-top: 20px; padding: 10px 15px; background-color: #ffffff; border-radius: 0 0 8px 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); display: flex; justify-content: flex-end; align-items: center; }
    .pagination span { margin-right: 15px; font-size: 0.9em; color: #6c757d; }
    .pagination button { margin-left: 5px; }


    /* --- Styling for Withdraw Request Form --- */
    #withdraw-request .withdrawal-form {
        background-color: #02101c; /* Match page background */
        padding: 25px 30px;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.09);
        margin-top: 15px; /* Space from h2 */
        border: 1px solid #34495e; /* Subtle border */
    }
    #withdraw-request .form-group {
        margin-bottom: 20px;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
    }
    #withdraw-request .form-group label {
        display: block;
        margin-bottom: 5px;
        font-weight: 600;
        color: #bdc3c7; /* Lighter label color for dark bg */
        min-width: 180px; 
        padding-right: 15px;
        flex-basis: 180px;
        flex-shrink: 0;
    }
    #withdraw-request .form-group input[type="text"],
    #withdraw-request .form-group input[type="number"],
    #withdraw-request .form-group select,
    #withdraw-request .form-group textarea {
        padding: 10px 12px;
        border: 1px solid #34495e; /* Darker border for inputs */
        border-radius: 4px;
        font-size: 0.9em;
        font-family: inherit;
        flex-grow: 1;
        max-width: 450px; /* Consistent max width */
        transition: border-color 0.2s ease, box-shadow 0.2s ease;
        background-color: #1e2a38; /* Darker input background */
        color: #ecf0f1; /* Light text in inputs */
    }
    /* Specific for textarea */
    #withdraw-request .form-group textarea {
        min-height: 100px;
        resize: vertical;
        width: 100%; /* Important within flex */
    }
    /* Focus styles */
    #withdraw-request .form-group input[type="text"]:focus,
    #withdraw-request .form-group input[type="number"]:focus,
    #withdraw-request .form-group select:focus,
    #withdraw-request .form-group textarea:focus {
        outline: none;
        border-color: #007bff;
        box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
    }
    /* Placeholder text color for dark inputs */
    #withdraw-request .form-group input::placeholder,
    #withdraw-request .form-group textarea::placeholder {
        color: #7f8c8d;
    }


    /* --- Styling for Withdraw Request Form Actions (Refined) --- */
    #withdraw-request .form-actions {
        margin-top: 25px;
        padding-top: 20px;
        border-top: 1px solid #34495e; /* Darker separator line */
        padding-left: 195px; 
        display: flex; 
        gap: 10px; 
        align-items: center; 
    }

    #withdraw-request .form-actions .btn {
        padding: 8px 15px;  /* Standard button padding */
        font-size: 0.9em; 
        margin-right: 0; 
        flex-grow: 0; 
        flex-shrink: 0; 
        white-space: nowrap; 
    }
    /* Adjust button colors for dark theme if needed, but generic .btn styles might be okay */
    #withdraw-request .form-actions .btn-success {
        /* background-color: #28a745; border-color: #28a745; */
    }
    #withdraw-request .form-actions .btn { /* Default/clear button */
        /* background-color: #4a627a; border-color: #4a627a; color: #fff; */
    }
    #withdraw-request .form-actions .btn:hover {
        /* background-color: #5c7a97; border-color: #5c7a97; */
    }


    /* --- Responsive Design --- */
    @media (max-width: 768px) {
        .sidebar {
            position: fixed; left: -250px; top: 0; height: 100vh; width: 250px;
            transition: left 0.3s ease;
            box-shadow: 2px 0 5px rgba(0,0,0,0.2);
            overflow: hidden; /* Hide tooltips on mobile */
        }
        #sidebar-toggle:checked ~ .dashboard-container .sidebar {
            left: 0;
            width: 250px;
        }
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-header h2 { opacity: 1; visibility: visible; }
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-header .logo { max-width: 50px; } /* Adjusted logo size */
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a span { opacity: 1; width: auto; pointer-events: auto; visibility: visible; transition: opacity 0.2s ease 0.1s, visibility 0s linear 0s, width 0s linear 0s; } 
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a i:first-child { margin-right: 15px; }
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a { justify-content: flex-start; padding: 12px 20px; overflow: hidden; } 
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .has-submenu > a .submenu-indicator { display: block; }
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .submenu { display: block; }

        .sidebar .has-submenu:hover > .submenu,
        #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a:hover::after {
            display: none !important;
            opacity: 0 !important;
            visibility: hidden !important;
            max-height: 0 !important;
        }
        .sidebar .has-submenu:not(.open):hover > a .submenu-indicator { /* Fixed: only non-open */
             transform: translateY(-50%) rotate(0deg) !important;
        }
        .sidebar .has-submenu.open:hover > .submenu {
            max-height: var(--submenu-scroll-height, 500px) !important;
             display: block !important; 
             opacity: 1 !important;
             visibility: visible !important;
        }
         .sidebar .has-submenu.open:hover > a .submenu-indicator {
            transform: translateY(-50%) rotate(180deg) !important; 
        }

        .main-content { }
        .sidebar-toggler-wrapper { display: none; }
        .menu-toggle { display: block; }
        .header-left h1 { font-size: 1.2em; margin-left: 10px; }
        .main-header { padding: 10px 15px; }
        .page-content { padding: 15px; }

        /* Responsive Table Styles */
        .table-container { box-shadow: none; background: none; margin-top: 15px; overflow-x: visible; border-radius: 0; }
        table { border: none; border-collapse: separate; border-spacing: 0 10px; margin-top: 0; }
        table, thead, tbody, th, td, tr { display: block; }
        thead { display: none; }
        tr { 
            border: 1px solid #34495e; /* Darker border for rows on dark bg */
            margin-bottom: 15px; border-radius: 8px; 
            background-color: #1e2a38; /* Darker row background */
            box-shadow: 0 1px 3px rgba(0,0,0,0.15); 
            padding: 15px; 
            color: #ecf0f1; /* Light text in rows */
        }
        td { 
            border: none; border-bottom: 1px solid #34495e; /* Darker separator */
            position: relative; padding: 8px 0 8px 40%; text-align: right; 
            white-space: normal; display: flex; justify-content: space-between; 
            align-items: center; min-height: 30px; 
        }
        td:last-child { border-bottom: none; }
        td::before { 
            content: attr(data-label); position: absolute; left: 0; width: 38%; /* Adjusted */
            padding-right: 10px; white-space: nowrap; font-weight: bold; 
            text-align: left; color: #bdc3c7; font-size: 0.9em; /* Lighter label color */
        }
        td[data-label="Actions"] { padding-left: 0; text-align: left; display: block; padding-top: 10px; }
        td[data-label="Actions"]::before { display: none; }
        td[data-label="Actions"] button { margin: 0 5px 5px 0; }
        
        .pagination { flex-direction: column; align-items: stretch; background: none; box-shadow: none; padding: 10px 0; }
        .pagination span { margin-bottom: 10px; text-align: center; margin-right: 0; color: #bdc3c7; }
        .pagination button { width: 100%; margin-left: 0; margin-bottom: 5px; }
        .controls { flex-direction: column; align-items: stretch; }
        .controls > * { width: 100%; margin-bottom: 10px; max-width: none; }
        .controls button { margin-right: 0; }


        /* Responsive Withdraw Form Styles */
        #withdraw-request .withdrawal-form {
            padding: 20px 15px;
        }
        #withdraw-request .form-group {
            margin-bottom: 15px;
        }
        #withdraw-request .form-group label {
            min-width: auto;
            flex-basis: 100%;
            margin-bottom: 5px;
            padding-right: 0;
        }
        #withdraw-request .form-group input[type="text"],
        #withdraw-request .form-group input[type="number"],
        #withdraw-request .form-group select,
        #withdraw-request .form-group textarea {
            max-width: none;
            width: 100%;
        }

        /* Responsive Adjustments for Form Actions */
        #withdraw-request .form-actions {
            padding-left: 0; 
            flex-direction: column; /* Stack buttons on mobile */
            align-items: stretch; /* Make buttons full width */
            gap: 10px; 
        }

        #withdraw-request .form-actions .btn {
            width: 100%; /* Full width buttons on mobile */
        }
    } /* End Media Query */
    
    .mobile-hide {
      /* Styles for when it's not mobile - default is visible */
    }
    @media (max-width: 768px) {
        .mobile-hide {
          display: none !important; /* Important to override other display properties if any */
        }
    }
    /* Success and Error messages */
    .message-container {
        padding: 10px 15px;
        margin-bottom: 20px;
        border-radius: 4px;
        border: 1px solid transparent;
        word-break: break-word;
    }
    .success-message {
        color: #155724;
        background-color: #d4edda;
        border-color: #c3e6cb;
    }
    .error-message {
        color: #721c24;
        background-color: #f8d7da;
        border-color: #f5c6cb;
    }
 /* The styled triangle logo - GOLD VERSION */
    .brand-logo {
        width: 50px;
        height: 50px;
        /* Gold Gradient */
        background: linear-gradient(135deg, #f1c40f, #f39c12);
        color: #fff;
        font-size: 24px;
        line-height: 50px; /* Center vertical */
        text-align: center;
        border-radius: 50%; /* Circle */
        margin: 0 auto 10px auto; /* Center horizontal */
        /* Gold Shadow */
        box-shadow: 0 0 15px rgba(241, 196, 15, 0.5);
        display: block;
    }
   </style>
</head>
<body>
    <input type="checkbox" id="sidebar-toggle" class="sidebar-toggle-checkbox">

    <div class="dashboard-container">
     <?php 
include('inc/sidebar.php');
?>

        <main class="main-content" id="main-content"  style="background-color: #02101c;">
            <header class="main-header">
                <div class="header-left">
                    <label for="sidebar-toggle" class="menu-toggle" id="menu-toggle" aria-label="Toggle navigation">
                        <i class="ion-navicon-round"></i>
                    </label>
                    <h1 id="page-title">Withdraw Request</h1>
                </div>
            </header>

            <div class="page-content"  style="background-color: #02101c;">
                <?php if (!empty($success_message)): ?>
                    <div class="message-container success-message">
                        <?php echo $success_message; ?>
                    </div>
                <?php endif; ?>
                <?php if (!empty($error_message)): ?>
                    <div class="message-container error-message">
                        <?php echo $error_message; ?>
                    </div>
                <?php endif; ?>
               
                <div id="withdraw-request" class="content-section " style="background-color: #02101c;">
                    <h2>Create New Withdrawal Request</h2>

                    <form class="withdrawal-form"  action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
                        <div class="form-group">
                            <label for="withdraw-amount">Amount</label>
                            <input type="number" id="withdraw-amount" name="amount" placeholder="e.g., 500.00" step="0.01" min="0.01" required value="<?php echo isset($_POST['amount']) ? htmlspecialchars($_POST['amount']) : ''; ?>">
                        </div>

                        <div class="form-group">
                            <label for="withdraw-currency">Currency</label>
                            <select id="withdraw-currency" name="currency" required>
                                <option value="">Select Currency</option>
                                <option value="BTC" <?php echo (isset($_POST['currency']) && $_POST['currency'] == 'BTC') ? 'selected' : ''; ?>>BTC</option>
                                <option value="USDT (TRC-20)" <?php echo (isset($_POST['currency']) && $_POST['currency'] == 'USDT (TRC-20)') ? 'selected' : ''; ?>>USDT (TRC-20)</option>
                                <option value="USDT (ERC-20)" <?php echo (isset($_POST['currency']) && $_POST['currency'] == 'USDT (ERC-20)') ? 'selected' : ''; ?>>USDT (ERC-20)</option>
                                <option value="USDT (BEP-20)" <?php echo (isset($_POST['currency']) && $_POST['currency'] == 'USDT (BEP-20)') ? 'selected' : ''; ?>>USDT (BEP-20)</option>
                                <option value="ETH" <?php echo (isset($_POST['currency']) && $_POST['currency'] == 'ETH') ? 'selected' : ''; ?>>ETH</option>
                                <option value="LTC" <?php echo (isset($_POST['currency']) && $_POST['currency'] == 'LTC') ? 'selected' : ''; ?>>LTC</option>
                            </select>
                        </div>

                         <div class="form-group">
                            <label for="network-chain">Network / Chain</label>
                             <input type="text" id="network-chain" name="network" placeholder="e.g., Bitcoin, ERC-20, TRC-20" value="<?php echo isset($_POST['network']) ? htmlspecialchars($_POST['network']) : ''; ?>">
                             <small style="flex-basis: 100%; padding-left: 195px; color: #7f8c8d; font-size:0.8em;">(Optional, but recommended for USDT, ETH, etc.)</small>
                         </div>
                     
                        <div class="form-actions">
                             <button type="submit" class="btn btn-success" name="submit">Submit Withdrawal Request</button>
                             <button type="reset" class="btn">Clear Form</button>
                        </div>
                    </form>  
                </div> 

                <div class="table-container" style="margin-top: 30px;">
                   <h2 style="color: #fff; padding: 15px 15px 0 15px; border-bottom: 1px solid #34495e; margin-bottom:0;">Withdrawal History</h2> 
                   <table>
                       <thead>
                           <tr>
                               <th style="background-color: #02101c;color: #fff; border-bottom: 1px solid #34495e;">Date</th>
                               <th style="background-color: #02101c;color: #fff; border-bottom: 1px solid #34495e;">User</th>
                               <th style="background-color: #02101c;color: #fff; border-bottom: 1px solid #34495e;">Currency</th>
                               <th style="background-color: #02101c;color: #fff; border-bottom: 1px solid #34495e;">Amount</th>
                               <th style="background-color: #02101c;color: #fff; border-bottom: 1px solid #34495e;">Status</th>
                           </tr>
                       </thead>
                       <tbody>
                           <?php
                           if ($conn && empty($conn->connect_error) && !empty($email)) {
                               // Changed table from 'transaction' to 'withdrawal'
                               // Assuming 'withdrawal' table has: id, created_at, email, currency, amount, status
                               $history_sql = "SELECT id, created_at, email, currency, amount, status FROM withdrawal WHERE email = ? ORDER BY id DESC";
                               $history_stmt = $conn->prepare($history_sql);

                               if ($history_stmt) {
                                   $history_stmt->bind_param("s", $email);
                                   $history_stmt->execute();
                                   $result = $history_stmt->get_result();

                                   if ($result->num_rows > 0) {
                                       while ($row = $result->fetch_assoc()) {
                                           $status_val = htmlspecialchars($row['status']);
                                           $status_class = strtolower($status_val); // For CSS class e.g., 'pending', 'approved'

                                           echo "<tr style=\"background-color: #0e1a2b;color: #fff; border-bottom: 1px solid #1c2e45;\">"; // Slightly different row bg for contrast
                                           echo "<td data-label=\"Date\">" . htmlspecialchars(date("Y-m-d H:i", strtotime($row['created_at']))) . "</td>";
                                           echo "<td data-label=\"User\">" . htmlspecialchars($row['email']) . "</td>";
                                           echo "<td data-label=\"Currency\">" . htmlspecialchars($row['currency']) . "</td>";
                                           echo "<td data-label=\"Amount\">" . htmlspecialchars(number_format(floatval($row['amount']), 2)) . "</td>";
                                           echo "<td data-label=\"Status\"><span class=\"status " . $status_class . "\">" . $status_val . "</span></td>";
                                           echo "</tr>";
                                       }
                                   } else {
                                       echo "<tr style=\"background-color: #0e1a2b;color: #fff;\"><td colspan=\"5\" style=\"text-align:center; padding: 15px;\">No withdrawal history found.</td></tr>";
                                   }
                                   $history_stmt->close();
                               } else {
                                   echo "<tr style=\"background-color: #0e1a2b;color: #fff;\"><td colspan=\"5\" style=\"text-align:center; padding: 15px;\">Error fetching withdrawal history: " . htmlspecialchars($conn->error) . "</td></tr>";
                                   error_log("Withdrawal History query prepare failed: " . $conn->error);
                               }
                           } else {
                               echo "<tr style=\"background-color: #0e1a2b;color: #fff;\"><td colspan=\"5\" style=\"text-align:center; padding: 15px;\">Cannot display history. DB connection or user session issue.</td></tr>";
                           }
                           ?>
                       </tbody>
                   </table>
               </div>
            </div>
        </main>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const sidebarToggleCheckbox = document.getElementById('sidebar-toggle');
            const sidebar = document.getElementById('sidebar');
            const sidebarNav = sidebar?.querySelector('.sidebar-nav');

            if (sidebarNav && sidebarToggleCheckbox) {
                sidebarNav.addEventListener('click', (event) => {
                    const link = event.target.closest('a');
                    if (link && link.getAttribute('href') && link.getAttribute('href') !== '#') {
                        if (window.innerWidth <= 768 && sidebarToggleCheckbox.checked) {
                           sidebarToggleCheckbox.checked = false;
                        }
                    }
                });
            }

            if (sidebar && sidebarToggleCheckbox) {
                sidebar.addEventListener('click', function(event) {
                    const parentLink = event.target.closest('.has-submenu > a.nav-link');
                    if (parentLink && window.innerWidth <= 768 && sidebarToggleCheckbox.checked && parentLink.getAttribute('href') === '#') {
                        event.preventDefault(); 
                        const parentLi = parentLink.closest('.has-submenu');
                        const submenu = parentLi?.querySelector('.submenu');

                        if (parentLi && submenu) {
                            const isOpen = parentLi.classList.contains('open');
                            const currentlyOpenSubmenus = sidebar.querySelectorAll('.has-submenu.open');
                            currentlyOpenSubmenus.forEach(otherLi => {
                                if (otherLi !== parentLi) { 
                                    otherLi.classList.remove('open');
                                    const otherSubmenu = otherLi.querySelector('.submenu');
                                    if (otherSubmenu) {
                                        otherSubmenu.style.maxHeight = null; 
                                    }
                                }
                            });
                            if (isOpen) {
                                submenu.style.maxHeight = null;
                                parentLi.classList.remove('open');
                            } else {
                                submenu.style.maxHeight = submenu.scrollHeight + "px";
                                parentLi.classList.add('open');
                            }
                        }
                    }
                });
             }

            const mobileMediaQuery = window.matchMedia('(max-width: 768px)');
            function handleMobileSidebarClass() {
                if(!sidebar || !sidebarToggleCheckbox) return; 
                if (mobileMediaQuery.matches && sidebarToggleCheckbox.checked) {
                    sidebar.classList.add('mobile-forced-open');
                } else {
                    sidebar.classList.remove('mobile-forced-open');
                }
                 if (mobileMediaQuery.matches && !sidebarToggleCheckbox.checked) {
                    sidebar.querySelectorAll('.has-submenu.open').forEach(li => {
                        li.classList.remove('open');
                        const submenu = li.querySelector('.submenu');
                        if(submenu) submenu.style.maxHeight = null;
                    });
                 }
            }
            if(sidebarToggleCheckbox) {
                handleMobileSidebarClass();
                sidebarToggleCheckbox.addEventListener('change', handleMobileSidebarClass);
            }
            mobileMediaQuery.addEventListener('change', handleMobileSidebarClass);
        });
    </script>
<?php
// Close the database connection at the very end of the script
if (isset($conn) && $conn instanceof mysqli) {
    $conn->close();
}
?> 
</body>
</html>