<div class="row mb-3">
    <div class="col-lg-12">
        <h3 class="page-header"><strong>Jadwal Mengajar</strong></h3>
    </div>
</div>

<?php
// AMBIL DATA GURU BERDASARKAN SESSION LOGIN
$id_user_login = $_SESSION['idu']; 
$sqlGuru = mysqli_query($koneksi, "SELECT * FROM guru WHERE idg = '$id_user_login'");
$guru = mysqli_fetch_assoc($sqlGuru);

// PENGAMANAN: Jika data guru tidak ditemukan (mencegah error null offset)
if ($guru) {
?>

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-primary">
            <div class="panel-heading">
                Jadwal Mengajar: <strong><?php echo htmlspecialchars($guru['nama']); ?></strong>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                        <thead>
                            <tr>
                                <th class="text-center">No</th>
                                <th class="text-center">Hari</th>
                                <th class="text-center">Jam</th>
                                <th class="text-center">Kelas</th>
                                <th class="text-center">Mata Pelajaran</th>
                                <th class="text-center">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
<?php
$no = 1;
// Query mengambil jadwal khusus untuk guru ini
$sqlJadwal = mysqli_query($koneksi, "
    SELECT 
        jadwal.idj, 
        hari.hari, 
        kelas.nama AS nama_kelas, 
        mata_pelajaran.nama_mp, 
        jadwal.jam_mulai, 
        jadwal.jam_selesai 
    FROM jadwal
    JOIN hari ON jadwal.idh = hari.idh
    JOIN kelas ON jadwal.idk = kelas.idk
    JOIN guru ON jadwal.idg = guru.idg
    JOIN mata_pelajaran ON jadwal.idm = mata_pelajaran.idm
    WHERE jadwal.idg = '{$guru['idg']}'
    ORDER BY jadwal.idh ASC, jadwal.jam_mulai ASC
");

if (mysqli_num_rows($sqlJadwal) > 0) {
    while ($row = mysqli_fetch_assoc($sqlJadwal)) :
?>
                            <tr>
                                <td class="text-center"><?= $no++; ?></td>
                                <td class="text-center"><?= htmlspecialchars($row['hari']); ?></td>
                                <td class="text-center"><?= $row['jam_mulai'] . ' - ' . $row['jam_selesai']; ?></td>
                                <td class="text-center"><?= htmlspecialchars($row['nama_kelas']); ?></td>
                                <td><?= htmlspecialchars($row['nama_mp']); ?></td>
                                <td class="text-center">
                                    <a href="media.php?module=absen&idj=<?= $row['idj']; ?>" class="btn btn-info btn-sm">
                                        <i class="fa fa-edit"></i> Absen
                                    </a>
                                    <a href="media.php?module=rekap_g&idj=<?= $row['idj']; ?>" class="btn btn-warning btn-sm">
                                        <i class="fa fa-book"></i> Rekap
                                    </a>
                                </td>
                            </tr>
<?php
    endwhile;
} else {
    echo "<tr><td colspan='6' class='text-center'>Jadwal mengajar belum tersedia.</td></tr>";
}
?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php 
} else {
    echo "<div class='alert alert-danger'>Data Guru tidak ditemukan. Silakan login ulang.</div>";
}
?>