<?php
session_start();
require_once '../koneksi.php';
require_once '../auth.php';
cekSiswa(); // Pastikan hanya siswa yang bisa akses

include 'layout_header.php';

// Ambil id_rombel siswa yang login dari tabel anggota_rombel
$id_siswa = $_SESSION['id_siswa'];
$query_rombel = mysqli_query($koneksi, "
    SELECT r.id_rombel, r.kelas 
    FROM anggota_rombel ar 
    JOIN rombel r ON ar.id_rombel = r.id_rombel 
    WHERE ar.id_siswa = '$id_siswa' 
    ORDER BY ar.id_anggota DESC LIMIT 1
");
$data_rombel = mysqli_fetch_assoc($query_rombel);
$id_rombel = $data_rombel['id_rombel'] ?? 0;
$nama_kelas = $data_rombel['kelas'] ?? 'Belum terdaftar';

// Query Jadwal yang disesuaikan dengan nama tabel 'jadwal_pelajaran'
$query_jadwal = mysqli_query($koneksi, "
    SELECT j.*, m.nama_mapel, g.nama 
    FROM jadwal_pelajaran j 
    LEFT JOIN master_mapel m ON j.id_mapel = m.id_mapel 
    LEFT JOIN master_guru g ON j.id_guru = g.id_guru 
    WHERE j.id_rombel = '$id_rombel'
    ORDER BY j.hari ASC, j.jam_mulai ASC
");
$query_info = mysqli_query($koneksi, "
    SELECT s.nama, r.kelas, tp.tahun_pelajaran, sem.nama_semester
    FROM master_siswa s
    JOIN anggota_rombel ar ON s.id_siswa = ar.id_siswa
    JOIN rombel r ON ar.id_rombel = r.id_rombel
    JOIN tahun_pelajaran tp ON ar.id_tp_tahun = tp.id_tp_tahun
    JOIN semester sem ON ar.id_semester = sem.id_semester
    WHERE s.id_siswa = '$id_siswa'
    ORDER BY ar.id_anggota DESC LIMIT 1
");
$info = mysqli_fetch_assoc($query_info);
?>

<div class="container-fluid">
    <div class="card mb-4 border-left-primary shadow">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <p><strong>Nama Siswa:</strong> <?= htmlspecialchars($info['nama']) ?></p>
                    <p><strong>Kelas:</strong> <?= htmlspecialchars($info['kelas']) ?></p>
                </div>
                <div class="col-md-6">
                    <p><strong>Semester:</strong> <?= htmlspecialchars($info['nama_semester']) ?></p>
                    <p><strong>Tahun Ajaran:</strong> <?= htmlspecialchars($info['tahun_pelajaran']) ?></p>
                </div>
            </div>
        </div>
    </div>

    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h4 class="h4 mb-0 text-gray-800">Jadwal Pelajaran Kelas <?= htmlspecialchars($info['kelas']) ?></h4>
    </div>

    <div class="table-responsive">
        </div>
</div>

<style>
    /* Styling agar tabel lebih profesional */
    .table thead { background-color: #0d6efd; color: white; }
    .table-striped tbody tr:nth-of-type(odd) { background-color: #f8f9fa; }
    
    /* Warna khusus untuk pemisah hari */
    .row-hari-baru { 
        background-color: #e9ecef !important; 
        border-top: 3px solid #0d6efd !important;
        font-weight: bold;
        color: #0d6efd;
    }
</style>

<div class="table-responsive">
    <table class="table table-bordered table-striped" width="100%">
        <thead>
            <tr>
                <th>Hari</th>
                <th>Jam</th>
                <th>Mata Pelajaran</th>
                <th>Guru</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $last_hari = ''; 
            while($row = mysqli_fetch_assoc($query_jadwal)) : 
                $is_new_day = ($row['hari'] !== $last_hari);
                
                // Jika hari baru, ganti class-nya
                $tr_class = $is_new_day ? 'row-hari-baru' : '';
            ?>
            
            <tr class="<?= $tr_class ?>">
                <td>
                    <?php 
                    if ($is_new_day) {
                        echo strtoupper($row['hari']); // Huruf kapital agar lebih tegas
                        $last_hari = $row['hari']; 
                    } else {
                        echo ''; 
                    }
                    ?>
                </td>
                <td><?= htmlspecialchars($row['jam_mulai']) ?> - <?= htmlspecialchars($row['jam_selesai']) ?></td>
                <td><?= htmlspecialchars($row['nama_mapel']) ?></td>
                <td><?= htmlspecialchars($row['nama']) ?></td>
            </tr>
            <?php endwhile; ?>
            
            <?php if (mysqli_num_rows($query_jadwal) == 0): ?>
            <tr>
                <td colspan="4" class="text-center text-muted"><em>Belum ada jadwal untuk kelas ini.</em></td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php include 'layout_footer.php'; ?>