<?php
session_start();
if(!isset($_SESSION['username'])){
    header("Location: Login.php");
    exit();
}
require "koneksi.php";
$con = open_connection();

// Ambil data murid + nama kelasnya (JOIN ke tabel kelas)
$query = "SELECT murid.id_murid, murid.nama, murid.nisn, kelas.nama_kelas 
          FROM murid 
          LEFT JOIN kelas ON murid.kode_kelas = kelas.kode_kelas
          ORDER BY murid.nama ASC";
$hasil = mysqli_query($con, $query);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Data Murid</title>
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/murid.css">
    <script type="text/javascript" src="assets/js/bootstrap.bundle.min.js"></script>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dashboard">
        <div class="container-fluid">
            <a class="navbar-brand" href="index.php">Little King - Pre School</a>
            <div>
                <span class="text-white me-3">Halo, <?php echo $_SESSION['username']; ?></span>
                <a href="logout.php" class="btn btn-outline-light btn-sm">Logout</a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h3 class="page-title">Data Murid</h3>
            <a href="tambah_murid.php" class="btn btn-tambah">+ Tambah Murid</a>
        </div>

        <?php if(isset($_GET['pesan'])): ?>
            <div class="alert alert-success"><?php echo htmlspecialchars($_GET['pesan']); ?></div>
        <?php endif; ?>

        <table class="table table-bordered table-murid">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Nama</th>
                    <th>NISN</th>
                    <th>Kelas</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1; while($baris = mysqli_fetch_assoc($hasil)): ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo htmlspecialchars($baris['nama']); ?></td>
                    <td><?php echo htmlspecialchars($baris['nisn']); ?></td>
                    <td><?php echo htmlspecialchars($baris['nama_kelas'] ?? '-'); ?></td>
                    <td>
                        <a href="edit_murid.php?id=<?php echo $baris['id_murid']; ?>" class="btn btn-sm btn-edit">Edit</a>
                        <a href="hapus_murid.php?id=<?php echo $baris['id_murid']; ?>" class="btn btn-sm btn-hapus" onclick="return confirm('Yakin mau hapus murid ini?');">Hapus</a>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</body>
</html>