<?php
session_start();

// Redirect to login if the user is not authenticated
if (!isset($_SESSION['admin_id'])) {
    header("Location: index.php"); // Assuming your login file is index.php
    exit();
}

// Include Database Connection
require_once "../account/include/db.php";

// Fetch user details based on your SQL query
$query = "SELECT `id`, `username`, `full_name`, `email`, `balance`, `country`, `phone`, `created_at` FROM `users` WHERE 1 ORDER BY `created_at` DESC";
// Assuming your database connection variable from db.php is $conn
$result = mysqli_query($conn, $query);

// Include Header and Sidebar
include 'includes/header.php';
include 'includes/sidebar.php';
?>

    <main class="md:ml-64 p-6 min-h-[calc(100vh-65px)] flex flex-col relative z-10">
        
        <div class="mb-8">
            <h1 class="text-2xl font-semibold text-white tracking-tight">System Overview</h1>
            <p class="text-slate-400 mt-1 text-sm mono">Terminal session initialized: <?php echo date('Y-m-d H:i:s'); ?></p>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
            
            <div class="glass-panel p-6 relative rounded-lg">
                <div class="corner-bracket corner-tl"></div>
                <div class="corner-bracket corner-tr"></div>
                <div class="corner-bracket corner-bl"></div>
                <div class="corner-bracket corner-br"></div>
                
                <h3 class="mono text-xs text-slate-400 mb-2">TOTAL_RECORDS</h3>
                <div class="flex items-baseline gap-3">
                    <span class="text-3xl font-light text-slate-200 mono">1,492</span>
                    <span class="text-xs text-emerald-500 mono">+12%</span>
                </div>
            </div>

            <div class="glass-panel p-6 relative rounded-lg">
                <div class="corner-bracket corner-tl"></div>
                <div class="corner-bracket corner-tr"></div>
                <div class="corner-bracket corner-bl"></div>
                <div class="corner-bracket corner-br"></div>
                
                <h3 class="mono text-xs text-slate-400 mb-2">NETWORK_STATUS</h3>
                <div class="flex items-baseline gap-3">
                    <span class="text-3xl font-light text-slate-200 mono">SECURE</span>
                    <span class="text-xs text-emerald-500 mono">0 THREATS</span>
                </div>
            </div>

            <div class="glass-panel p-6 relative rounded-lg border-red-500/20">
                <div class="corner-bracket corner-tl border-red-500/40"></div>
                <div class="corner-bracket corner-tr border-red-500/40"></div>
                <div class="corner-bracket corner-bl border-red-500/40"></div>
                <div class="corner-bracket corner-br border-red-500/40"></div>
                
                <h3 class="mono text-xs text-red-400/80 mb-2">PENDING_ALERTS</h3>
                <div class="flex items-baseline gap-3">
                    <span class="text-3xl font-light text-red-400 mono">03</span>
                    <span class="text-xs text-red-500/70 mono">REQUIRE ACTION</span>
                </div>
            </div>
        </div>

        <div class="glass-panel rounded-lg relative flex-grow mb-6">
            <div class="p-4 border-b border-slate-800 flex justify-between items-center">
                <h2 class="mono text-sm text-slate-300 uppercase tracking-widest">User_Database</h2>
                <button class="mono text-[10px] bg-slate-800 hover:bg-slate-700 text-slate-300 px-3 py-1 rounded transition-colors border border-slate-700">
                    [ EXPORT .CSV ]
                </button>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="border-b border-slate-800 bg-slate-900/50 mono text-[10px] text-slate-500 tracking-wider">
                            <th class="p-4 font-normal">JOINED_DATE</th>
                            <th class="p-4 font-normal">USERNAME</th>
                            <th class="p-4 font-normal">FULL_NAME</th>
                            <th class="p-4 font-normal">EMAIL</th>
                            <th class="p-4 font-normal">BALANCE</th>
                            <th class="p-4 font-normal">COUNTRY</th>
                        </tr>
                    </thead>
                    <tbody class="mono text-xs text-slate-300 divide-y divide-slate-800/50">
                        <?php 
                        if ($result && mysqli_num_rows($result) > 0) {
                            while ($row = mysqli_fetch_assoc($result)) {
                        ?>
                                <tr class="hover:bg-slate-800/30 transition-colors">
                                    <td class="p-4 text-slate-500"><?php echo htmlspecialchars($row['created_at']); ?></td>
                                    <td class="p-4 text-emerald-400"><?php echo htmlspecialchars($row['username']); ?></td>
                                    <td class="p-4"><?php echo htmlspecialchars($row['full_name']); ?></td>
                                    <td class="p-4"><?php echo htmlspecialchars($row['email']); ?></td>
                                    <td class="p-4 font-semibold text-slate-200">
                                        $<?php echo number_format((float)$row['balance'], 2); ?>
                                    </td>
                                    <td class="p-4">
                                        <span class="bg-emerald-500/10 text-emerald-400 border border-emerald-500/20 px-2 py-0.5 rounded text-[10px]">
                                            <?php echo htmlspecialchars($row['country'] ? $row['country'] : 'N/A'); ?>
                                        </span>
                                    </td>
                                </tr>
                        <?php 
                            }
                        } else {
                        ?>
                            <tr>
                                <td colspan="6" class="p-6 text-center text-slate-500 mono">NO_USER_RECORDS_FOUND</td>
                            </tr>
                        <?php 
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>

<?php 
// Include Footer
include 'includes/footer.php'; 
?>