<?php 
include('inc/session.php');

// 1. INCLUDE DATABASE CONNECTION
// Make sure this path points to your actual database connection file
include('inc/db.php'); 

// 2. DEFINE THE EMAIL VARIABLE FROM SESSION
// Assuming your session variable is named 'email'
$email = $_SESSION['email']; 
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Transactions</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionic/1.3.2/css/ionic.min.css">
  
   <style>
    /* ... [YOUR EXISTING CSS HERE - NO CHANGES NEEDED TO CSS] ... */
    
    /* Just ensuring the CSS you provided is kept intact */
    * { 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; min-height: 100%; position: relative;
    }
    a { text-decoration: none; color: inherit; }
    ul { list-style: none; }
    .sidebar-toggle-checkbox { position: absolute; opacity: 0; pointer-events: none; width: 0; height: 0; }
    .dashboard-container { display: flex; min-height: 100vh; position: relative; }
    .sidebar { width: 250px; background-color: #2c3e50; color: #ecf0f1; display: flex; flex-direction: column; transition: width 0.3s ease, left 0.3s ease; position: sticky; top: 0; height: 100vh; z-index: 1000; flex-shrink: 0; overflow: visible; }
    .sidebar-header { padding: 5px; text-align: center; border-bottom: 1px solid #34495e; flex-shrink: 0; }
    .sidebar-header .logo { max-width: 30%; height: auto; padding: 5px; border-radius: 4px; display: inline-block; transition: max-width 0.3s ease; vertical-align: middle; }
    .sidebar-header h2 { font-size: 1.2em; font-weight: 600; white-space: nowrap; overflow: hidden; transition: opacity 0.1s ease 0.1s; opacity: 1;}
    .sidebar-nav { flex-grow: 1; overflow-y: auto; overflow-x: hidden; }
    .sidebar-nav ul { padding-top: 20px; }
    .sidebar-nav li { margin-bottom: 5px; position: relative; }
    .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; }
    .sidebar-nav a:hover { background-color: #34495e; color: #ffffff; }
    .sidebar-nav a.active, .sidebar-nav li.active > a { background-color: #34495e; color: #ffffff; font-weight: 500; }
    .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); }
    #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; }
    .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-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-toggle:checked ~ .dashboard-container .sidebar { width: 70px; overflow: visible; }
    #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; }
    #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; }
    #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; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar .sidebar-nav a::after { content: attr(data-tooltip); position: absolute; left: calc(100% + 10px); top: 50%; transform: translateY(-50%); background-color: #2c3e50; color: #ecf0f1; padding: 5px 10px; border-radius: 4px; font-size: 0.85em; white-space: nowrap; z-index: 1010; opacity: 0; visibility: hidden; pointer-events: none; transition: opacity 0.2s ease 0.1s, visibility 0s linear 0.3s; }
    #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; }
    #sidebar-toggle:checked ~ .dashboard-container .sidebar.mobile-forced-open .sidebar-nav a:hover::after { opacity: 0; visibility: hidden; }
    .main-content { flex-grow: 1; display: flex; flex-direction: column; position: relative; height: 100vh; overflow-y: auto; background-color: #f4f7fc; }
    .main-header { background-color: #02101c; padding: 15px 30px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #e0e0e0; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); position: sticky; 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: #2c3e50; font-weight: 600; }
    .header-right { display: flex; align-items: center; }
    .admin-info { display: flex; align-items: center; color: #555; }
    .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: #34495e; display: none; padding: 5px; line-height: 1; }
    .page-content { padding: 30px; flex-grow: 1; }
    h2 { font-size: 1.8em; margin-bottom: 20px; color: #2c3e50; font-weight: 600; }
    .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; }
    .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 { background-color: #d4edda; color: #155724; }
    .status.pending { background-color: #fff3cd; color: #856404; }
    .status.failed { background-color: #f8d7da; color: #721c24; }
    .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"], select { padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 0.9em; min-width: 150px; flex-grow: 1; max-width: 300px; }
    .controls input[type="text"] { max-width: 400px; }
    .controls select { max-width: 200px; flex-grow: 0; }
    .controls button { flex-grow: 0; }
    input[type="text"]:focus, select: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; }
    .widget-placeholder { background-color: #ffffff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); margin-bottom: 20px; border: 1px dashed #ccc; color: #777; }
    .widget-placeholder h3 { font-size: 1.1em; color: #555; margin-bottom: 10px; }
    .placeholder-graphic { background-color: #e9ecef; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #adb5bd; font-style: italic; min-height: 150px; }
    .placeholder-graphic.large { min-height: 250px; }
    @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; }
        #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: 80%; }
        #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:hover > a .submenu-indicator { 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; }
        .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; }
        .table-container { box-shadow: none; background: none; margin-top: 0; 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 #e0e0e0; margin-bottom: 15px; border-radius: 8px; background-color: #ffffff; box-shadow: 0 1px 3px rgba(0,0,0,0.08); padding: 15px; }
        td { border: none; border-bottom: 1px solid #f0f0f0; 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: 35%; padding-right: 10px; white-space: nowrap; font-weight: bold; text-align: left; color: #555; font-size: 0.9em; }
        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; }
        .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; }
    }
    @media (max-width: 768px) {
        .mobile-hide { display: none; }
    }
    .brand-logo { width: 50px; height: 50px; background: linear-gradient(135deg, #f1c40f, #f39c12); color: #fff; font-size: 24px; line-height: 50px; text-align: center; border-radius: 50%; margin: 0 auto 10px auto; box-shadow: 0 0 15px rgba(241, 196, 15, 0.5); display: block; }
   </style>
</head>
<body>
    <!-- Hidden Checkbox for CSS Toggles (Controls sidebar state) -->
    <input type="checkbox" id="sidebar-toggle" class="sidebar-toggle-checkbox">

    <div class="dashboard-container">
        <!-- Sidebar -->
        <?php 
include('inc/sidebar.php');
?>
        <!-- Main Content -->
        <main class="main-content" id="main-content"  style="background-color: #02101c;">
            <!-- Header -->
            <header class="main-header" >
                <div class="header-left">
                    <!-- Mobile Menu Toggle Button (Label) -->
                    <label for="sidebar-toggle" class="menu-toggle" id="menu-toggle" aria-label="Toggle navigation">
                        <i class="ion-navicon-round"></i>
                    </label>
                    <!-- Static title for this specific page -->
                    <h1 id="page-title">Transactions</h1>
                </div>
            </header>

            <!-- Page Content Area (Scrollable within main-content) -->
            <div class="page-content">
                <!-- === Content for Transactions Page ONLY === -->
                <div class="controls">
                    <input type="text" placeholder="Search by ID, User, Type...">
                    <select>
                        <option value="">All Status</option>
                        <option value="completed">Completed</option>
                        <option value="pending">Pending</option>
                        <option value="failed">Failed</option>
                    </select>
                    <button class="btn"><i class="ion-funnel"></i> Filter</button>
                </div>

                <div class="table-container">
                    <table>
                        <thead>
                            <tr>
                                <th>ORDER</th>
                                <th>TIME</th>
                                <th>TYPE</th>
                                <th>SYMBOL</th>
                                <th>VOLUME</th>
                                <th>S/L</th>
                                <th>T/P</th>
                                <th>STATUS</th>
                                <th>EXPIRY TIME</th>
                                <th>PROFIT</th>
                                <th>LOSS</th>
                            </tr>
                        </thead>
                        <tbody>
                            
                        <?php
                            // Ensure the query is valid and $conn is available from inc/db.php
                            $result = mysqli_query($conn, "SELECT * FROM transaction WHERE email ='$email' ORDER BY id DESC") or die(mysqli_error($conn));
                            
                            while ($row = mysqli_fetch_array($result)){
                                // Extract variables
                                $order = $row['trade_order'];
                                $trade_time = $row['order_time'];
                                $type = $row['type'];
                                $symbol = $row['symbol'];
                                $volume = $row['volume'];
                                $sl = $row['s/l'];
                                $tp = $row['t/p'];
                                $status1 = $row['status'];
                                $expiry_time = $row['expiry_time'];
                                $profit = $row['profit'];
                                $loss = $row['loss'];
                                
                                // Determine styling class based on status/profit/loss (optional enhancement)
                                $status_class = 'completed'; // Default
                                if(strtolower($status1) == 'pending') $status_class = 'pending';
                                if(strtolower($status1) == 'failed') $status_class = 'failed';

                                echo "
                                <tr>
                                    <td data-label=\"Order\">$order</td>
                                    <td data-label=\"Time\">$trade_time</td>
                                    <td data-label=\"Type\">$type</td>
                                    <td data-label=\"Symbol\">$symbol</td>
                                    <td data-label=\"Volume\">$volume</td>
                                    <td data-label=\"S/L\">$sl</td>
                                    <td data-label=\"T/P\">$tp</td>
                                    <td data-label=\"Status\"><span class=\"status $status_class\">$status1</span></td>
                                    <td data-label=\"Expiry\">$expiry_time</td>
                                    <td data-label=\"Profit\" style=\"color: #2ecc71;\">$profit</td>
                                    <td data-label=\"Loss\" style=\"color: #e74c3c;\">$loss</td>
                                </tr>
                                ";
                            }
                        ?>
                           
                        </tbody>
                    </table>
                </div>
                   <div class="pagination">
                    <span>Showing results</span>
                    <button class="btn btn-sm" disabled><i class="ion-chevron-left"></i> Prev</button>
                    <button class="btn btn-sm" disabled>Next <i class="ion-chevron-right"></i></button>
                </div>
                <!-- === End Transactions Content === -->

            </div> <!-- End Page Content -->
        </main> <!-- End Main Content -->
    </div> <!-- End Dashboard Container -->

    <!-- Minimal JavaScript -->
    <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) 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;
                    });
                 }
            }
            handleMobileSidebarClass();
            sidebarToggleCheckbox.addEventListener('change', handleMobileSidebarClass);
            mobileMediaQuery.addEventListener('change', handleMobileSidebarClass);
        });
    </script>
</body>
</html>