
<?php
// Get statistics
 $stmt = $pdo->query("SELECT COUNT(*) FROM students WHERE application_status = 'Pending'");
 $pendingCount = $stmt->fetchColumn();

 $stmt = $pdo->query("SELECT COUNT(*) FROM students WHERE application_status = 'Approved'");
 $approvedCount = $stmt->fetchColumn();

 $stmt = $pdo->query("SELECT COUNT(*) FROM students WHERE application_status = 'Rejected'");
 $rejectedCount = $stmt->fetchColumn();

 $stmt = $pdo->query("SELECT COUNT(*) FROM students WHERE payment_status = 'Paid'");
 $paidCount = $stmt->fetchColumn();

 $stmt = $pdo->query("SELECT COUNT(*) FROM students");
 $totalCount = $stmt->fetchColumn();
?>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                Admin Dashboard
            </div>
            <div class="card-body">
                <h4>Welcome, <?php echo $_SESSION['full_name']; ?>!</h4>
                <p>Here's an overview of the IJMB registration system.</p>
                
                <div class="row mt-4">
                    <div class="col-md-2">
                        <div class="stats-card">
                            <h3><?php echo $totalCount; ?></h3>
                            <p>Total Applications</p>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="stats-card">
                            <h3><?php echo $pendingCount; ?></h3>
                            <p>Pending</p>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="stats-card">
                            <h3><?php echo $approvedCount; ?></h3>
                            <p>Approved</p>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="stats-card">
                            <h3><?php echo $rejectedCount; ?></h3>
                            <p>Rejected</p>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="stats-card">
                            <h3><?php echo $paidCount; ?></h3>
                            <p>Paid</p>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="stats-card">
                            <h3><?php echo $totalCount - $paidCount; ?></h3>
                            <p>Unpaid</p>
                        </div>
                    </div>
                </div>
                
                <div class="row mt-4">
                    <div class="col-md-12">
                        <div class="card dashboard-card">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                Recent Applications
                                <a href="?page=admin_applications" class="btn btn-sm btn-primary">View All</a>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Reg. No.</th>
                                                <th>Name</th>
                                                <th>Email</th>
                                                <th>Payment</th>
                                                <th>Status</th>
                                                <th>Date</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $stmt = $pdo->query("SELECT * FROM students ORDER BY created_at DESC LIMIT 5");
                                            while ($student = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                                echo "<tr>";
                                                echo "<td>" . $student['registration_no'] . "</td>";
                                                echo "<td>" . $student['first_name'] . " " . $student['surname'] . "</td>";
                                                echo "<td>" . $student['email'] . "</td>";
                                                echo "<td>" . $student['payment_status'] . "</td>";
                                                echo "<td><span class=\"status-badge status-" . strtolower(str_replace(' ', '-', $student['application_status'])) . "\">" . $student['application_status'] . "</span></td>";
                                                echo "<td>" . date('d M Y', strtotime($student['created_at'])) . "</td>";
                                                echo "<td>
                                                        <a href=\"?page=admin_applications&action=view&id=" . $student['id'] . "\" class=\"btn btn-sm btn-primary\">View</a>
                                                      </td>";
                                                echo "</tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="row mt-4">
                    <div class="col-md-6">
                        <div class="card dashboard-card">
                            <div class="card-header">
                                Quick Actions
                            </div>
                            <div class="card-body">
                                <div class="d-grid gap-2">
                                    <a href="?page=admin_applications" class="btn btn-primary">
                                        <i class="bi bi-file-earmark-text"></i> Manage Applications
                                    </a>
                                    <a href="?page=admin_settings" class="btn btn-secondary">
                                        <i class="bi bi-gear"></i> System Settings
                                    </a>
                                    <a href="?page=reports" class="btn btn-info">
                                        <i class="bi bi-graph-up"></i> Generate Reports
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="card dashboard-card">
                            <div class="card-header">
                                System Information
                            </div>
                            <div class="card-body">
                                <table class="table table-borderless">
                                    <tr>
                                        <td><strong>Academic Session:</strong></td>
                                        <td>
                                            <?php
                                            $stmt = $pdo->prepare("SELECT setting_value FROM system_settings WHERE setting_key = 'academic_session'");
                                            $stmt->execute();
                                            echo $stmt->fetchColumn();
                                            ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Application Deadline:</strong></td>
                                        <td>
                                            <?php
                                            $stmt = $pdo->prepare("SELECT setting_value FROM system_settings WHERE setting_key = 'application_deadline'");
                                            $stmt->execute();
                                            echo date('d M Y', strtotime($stmt->fetchColumn()));
                                            ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Email Notifications:</strong></td>
                                        <td>
                                            <?php
                                            $stmt = $pdo->prepare("SELECT setting_value FROM system_settings WHERE setting_key = 'email_notifications'");
                                            $stmt->execute();
                                            echo $stmt->fetchColumn() === 'enabled' ? 'Enabled' : 'Disabled';
                                            ?>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
