<?php
$acuan = $_GET['idj'] ?? '';

// ambil jadwal
$sqlacuan = mysqli_query($koneksi, "SELECT * FROM jadwal WHERE idj='$acuan'");
$rss = mysqli_fetch_assoc($sqlacuan);

// ambil siswa (pakai session user)
$uidi = $_SESSION['nis'] ?? '';
$sqlrss = mysqli_query($koneksi, "SELECT * FROM siswa WHERE nis='$uidi'");
$siswa = mysqli_fetch_assoc($sqlrss);

// mata pelajaran
$sqlmp = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran WHERE idm='{$rss['idm']}'");
$nama_mp = mysqli_fetch_assoc($sqlmp);

// hari
$sqlidh = mysqli_query($koneksi, "SELECT * FROM hari WHERE idh='{$rss['idh']}'");
$nama_hari = mysqli_fetch_assoc($sqlidh);

// tahun ajaran
$tgl_lengkap = date("d F Y");
$pecah = explode(" ", $tgl_lengkap);
$bulan = $pecah[1];
$tahun1 = $pecah[2];

if (in_array($bulan, ["Juli","Agustus","September","Oktober","November","Desember"])) {
    $tahun2 = $tahun1 + 1;
} else {
    $tahun2 = $tahun1 - 1;
}
?>

<div class="row">
    <div class="col-lg-12">
        <h3 class="page-header">
            <strong>
            Tahun Ajaran <?= "$tahun1 - $tahun2 ({$nama_mp['nama_mp']})"; ?>
            </strong>
        </h3>
    </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-primary">
            <div class="panel-heading">Data Absensi Siswa</div>
            <div class="panel-body">
                <div class="table-responsive">

<?php
$sqlabsen = mysqli_query(
    $koneksi,
    "SELECT DISTINCT tgl FROM absen 
     WHERE idj='$acuan' AND ids='{$siswa['ids']}'"
);
?>

<table class="table table-striped table-bordered table-hover">
    <tr>
        <td colspan="2" class="text-center info">
            <b><?= "{$nama_hari['hari']}, {$rss['jam_mulai']} - {$rss['jam_selesai']}" ?></b>
        </td>
    </tr>
    <tr>
        <td class="success text-center"><b>Tanggal</b></td>
        <td class="success text-center"><b><?= "{$siswa['nama']} | Kelas : {$siswa['idk']}" ?></b></td>
    </tr>

<?php
while ($tglnya = mysqli_fetch_assoc($sqlabsen)) {
    $tgl = explode("-", $tglnya['tgl']);
    $tgl_tampil = $tgl[2] . "/" . $tgl[1];

    $sqlabsen2 = mysqli_query(
        $koneksi,
        "SELECT ket FROM absen 
         WHERE ids='{$siswa['ids']}' 
         AND idj='$acuan' 
         AND tgl='{$tglnya['tgl']}'"
    );
?>
    <tr>
        <td class="text-center warning"><b><?= $tgl_tampil ?></b></td>
        <td class="text-center">
            <?php
            while ($ket = mysqli_fetch_assoc($sqlabsen2)) {
                echo $ket['ket'] . " ";
            }
            ?>
        </td>
    </tr>
<?php } ?>

</table>

                </div>

                <div class="well">
                    <h4>Keterangan Absensi</h4>
                    <p>A = Alpa</p>
                    <p>I = Izin</p>
                    <p>S = Sakit</p>
                    <p>M = Masuk</p>
                </div>

            </div>
        </div>
    </div>
</div>
