<?php
session_start();
if (!isset($_SESSION['id_admin'])) {
    header('Location: login_admin.php');
    exit;
}

include 'includes/config.php';
include 'includes/header_admin.php';
include 'includes/sidebar_admin.php';

$sql = "SELECT * FROM pelajar ORDER BY tarikh_daftar DESC";
$result = mysqli_query($conn, $sql);
?>

<div style="margin-left: 180px; margin-top: 100px; padding: 20px;">
    <h2>Senarai Pelajar</h2>
    <a href="daftar_pelajar.php" class="btn btn-success mb-3">
        <i class="bi bi-person-plus"></i> Daftar Pelajar Baru
    </a>
    <table class="table table-bordered table-hover align-middle">
        <thead class="table-dark">
            <tr class="text-center">
                <th>Gambar</th>
                <th>Nama</th>
                <th>No. IC</th>
                <th>No. Pendaftaran</th>
                <th>Jantina</th>
                <th>Program</th>
                <th>Emel</th>
                <th>Status</th>
                <th>Tindakan</th>
            </tr>
        </thead>
        <tbody>
            <?php if (mysqli_num_rows($result) > 0): ?>
                <?php while ($row = mysqli_fetch_assoc($result)): ?>
                    <tr>
                        <td class="text-center">
                            <?php if (!empty($row['gambar']) && file_exists("uploads/" . $row['gambar'])): ?>
                                <img src="uploads/<?= htmlspecialchars($row['gambar']) ?>" 
                                     alt="Gambar Pelajar" width="60" height="60" style="object-fit: cover; border-radius: 50%;">
                            <?php else: ?>
                                <img src="assets/img/default-user.png" 
                                     alt="Default" width="60" height="60" style="object-fit: cover; border-radius: 50%;">
                            <?php endif; ?>
                        </td>
                        <td><?= htmlspecialchars($row['nama']) ?></td>
                        <td><?= htmlspecialchars($row['no_ic']) ?></td>
                        <td><?= htmlspecialchars($row['no_pendaftaran']) ?></td>
                        <td><?= htmlspecialchars($row['jantina']) ?></td>
                        <td><?= htmlspecialchars($row['program']) ?></td>
                        <td><?= htmlspecialchars($row['emel']) ?></td>
                        <td>
                            <?php if ($row['status'] === 'Aktif'): ?>
                                <span class="badge bg-success">Aktif</span>
                            <?php else: ?>
                                <span class="badge bg-secondary">Tidak Aktif</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <a href="kemaskini_pelajar.php?id=<?= $row['id'] ?>" class="btn btn-primary btn-sm">
                                <i class="bi bi-pencil-square"></i> Kemaskini
                            </a>
                            <a href="padam_pelajar.php?id=<?= $row['id'] ?>" 
                               class="btn btn-danger btn-sm"
                               onclick="return confirm('Padam pelajar ini?');">
                                <i class="bi bi-trash"></i> Padam
                            </a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="9" class="text-center">Tiada rekod pelajar.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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