<?php
require_once '../config/config.php';
require_admin(); // Security Check

// Fetch Stats
$stats = $pdo->query("
    SELECT 
        COUNT(*) as total,
        SUM(CASE WHEN current_status = 'pending' THEN 1 ELSE 0 END) as pending,
        SUM(CASE WHEN current_status = 'in_progress' THEN 1 ELSE 0 END) as processing,
        SUM(CASE WHEN current_status = 'completed' THEN 1 ELSE 0 END) as completed
    FROM replacement_requests
")->fetch();

require_once 'includes/header.php';
?>

<h2 class="h2">Admin Dashboard</h2>
<div class="row mt-4">
    <div class="col-md-3">
        <div class="card text-white bg-warning mb-3">
            <div class="card-header">Pending</div>
            <div class="card-body">
                <h5 class="card-title display-6"><?php echo $stats['pending'] ?? 0; ?></h5>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-white bg-info mb-3">
            <div class="card-header">In Progress</div>
            <div class="card-body">
                <h5 class="card-title display-6"><?php echo $stats['processing'] ?? 0; ?></h5>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-white bg-success mb-3">
            <div class="card-header">Completed</div>
            <div class="card-body">
                <h5 class="card-title display-6"><?php echo $stats['completed'] ?? 0; ?></h5>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-light mb-3">
            <div class="card-header">Total Requests</div>
            <div class="card-body">
                <h5 class="card-title display-6"><?php echo $stats['total']; ?></h5>
            </div>
        </div>
    </div>
</div>

<h3 class="mt-4">Recent Requests</h3>
<div class="table-responsive">
    <table class="table table-striped table-sm">
        <thead>
            <tr>
                <th>ID</th>
                <th>Reseller</th>
                <th>Customer</th>
                <th>Plan</th>
                <th>Status</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $stmt = $pdo->query("
                SELECT r.*, u.business_name as reseller_name 
                FROM replacement_requests r 
                JOIN users u ON r.user_id = u.id 
                ORDER BY r.created_at DESC LIMIT 5
            ");
            while ($row = $stmt->fetch()) {
                echo "<tr>";
                echo "<td>#{$row['id']}</td>";
                echo "<td>" . htmlspecialchars($row['reseller_name']) . "</td>";
                echo "<td>" . htmlspecialchars($row['customer_name']) . "</td>";
                echo "<td>" . htmlspecialchars($row['adobe_plan']) . "</td>";
                echo "<td><span class='badge bg-" . ($row['current_status'] == 'pending' ? 'warning' : 'secondary') . "'>{$row['current_status']}</span></td>";
                echo "<td>" . date('d M', strtotime($row['created_at'])) . "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
</div>

</main>
</div>
</div>
</body>
</html>