<?php
session_start();
require_once '../koneksi.php';
require_once '../auth.php';

// 1. CEK AKSES DINAMIS
// Jangan panggil kedua fungsi sekaligus secara paksa. 
// Gunakan logika berdasarkan role yang tersimpan di session.
if (isset($_SESSION['role'])) {
    if ($_SESSION['role'] === 'siswa') {
        cekSiswa();
    } else {
        cekAdminAtauGuru();
    }
} else {
    // Jika tidak ada session login, arahkan ke login
    header("Location: ../login.php");
    exit;
}

$id_guru_login = $_SESSION['id_guru'] ?? 0;

// 2. AMBIL ID ROMBEL
// Jika siswa, ambil ID Rombel langsung dari session agar sesuai dengan data loginnya
// Jika admin/guru, tetap ambil dari URL atau session default
if ($_SESSION['role'] === 'siswa') {
    $id_rombel_aktif = $_SESSION['id_rombel'] ?? 0;
} else {
    $id_rombel_aktif = $_GET['id_rombel'] ?? ($_SESSION['id_rombel'] ?? 0);
}

// Fallback jika id_rombel masih 0
if ($id_rombel_aktif == 0) {
    $q_default = mysqli_query($koneksi, "SELECT id_rombel FROM rombel LIMIT 1");
    $d_default = mysqli_fetch_assoc($q_default);
    $id_rombel_aktif = $d_default['id_rombel'] ?? 0;
}

// 3. PEMANGGILAN LAYOUT
// Jika siswa, gunakan layout siswa. Jika admin/guru gunakan layout guru/admin
if ($_SESSION['role'] === 'siswa') {
    include 'layout_header.php'; // Atau layout yang sudah Anda buat untuk siswa
    // ... isi konten halaman ...
    include 'layout_footer.php';
} else {
    include 'layout.php';
}
?>

<div class="container-fluid mt-4">
    <div class="mb-3"><?php $q=mysqli_query($koneksi,"SELECT * FROM rombel"); while($k=mysqli_fetch_array($q)) echo "<a href='?id_rombel={$k['id_rombel']}' class='btn btn-outline-primary me-2'>{$k['kelas']}</a> "; ?></div>
    
    <table class="table table-bordered text-center">
        <thead class="table-primary">
            <tr><th>WAKTU</th><th>SENIN</th><th>SELASA</th><th>RABU</th><th>KAMIS</th><th>JUMAT</th><th>SABTU</th></tr>
        </thead>
        <tbody>
            <?php 
            $q_jam = mysqli_query($koneksi, "SELECT * FROM jadwal_waktu_rombels 
                                 WHERE id_rombel = '$id_rombel_aktif' 
                                 ORDER BY waktu_mulai ASC");
            while($jam = mysqli_fetch_array($q_jam)):
                if($jam['is_istirahat'] == 1): ?>
                    <tr class="table-danger"><td>ISTIRAHAT</td><td colspan="6">ISTIRAHAT</td></tr>
                <?php else: ?>
                    <tr><td class="fw-bold"><?= substr($jam['waktu_mulai'],0,5) ?> - <?= substr($jam['waktu_selesai'],0,5) ?></td>
                    <?php foreach(['Senin','Selasa','Rabu','Kamis','Jumat','Sabtu'] as $h):
    // Ubah m.nama_mapel menjadi m.kode_mapel
    $q_j = mysqli_query($koneksi, "SELECT m.kode_mapel FROM jadwal_pelajaran j JOIN master_mapel m ON j.id_mapel=m.id_mapel WHERE j.id_rombel='$id_rombel_aktif' AND j.hari='$h' AND j.jam_mulai='{$jam['waktu_mulai']}'");
    $j_data = mysqli_fetch_assoc($q_j);
    
    // Tampilkan kode_mapel
    echo "<td>".($j_data['kode_mapel'] ?? '')."</td>";
endforeach; ?>
                    </tr>
                <?php endif;
            endwhile; ?>
        </tbody>
    </table>
</div>
<?php include 'layout_footer.php'; ?>