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

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

// kelas
$sqlkss = mysqli_query($koneksi, "SELECT * FROM kelas WHERE idk='{$rss['idk']}'");
$kss = mysqli_fetch_assoc($sqlkss);

// 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);

// tanggal sekarang
$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"; ?>
            </strong>

            <form action="module/laporan/guru/cetak_rekap.php" method="post">
                <input type="hidden" name="idj" value="<?= $acuan; ?>">
                <input type="hidden" name="tgl_lengkap" value="<?= $tgl_lengkap; ?>">
                <input style="float:right; margin-top:-40px;"
                       class="btn btn-success btn-lg"
                       type="submit"
                       value="Cetak">
            </form>
        </h3>
    </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-primary">
            <div class="panel-heading">
                Data Absensi Kelas
                <b><?= "{$kss['nama']} | {$nama_mp['nama_mp']}"; ?></b>
            </div>

            <div class="panel-body">
                <div class="table-responsive">

<?php
$sqlabsen = mysqli_query(
    $koneksi,
    "SELECT DISTINCT tgl FROM absen WHERE idj='$acuan' ORDER BY tgl ASC"
);
$jumlahtanggal = mysqli_num_rows($sqlabsen);
?>

<table class="table table-striped table-bordered table-hover">
    <tr>
        <td colspan="<?= $jumlahtanggal + 1; ?>" class="text-center info">
            <b><?= "{$nama_hari['hari']}, {$rss['jam_mulai']} - {$rss['jam_selesai']}"; ?></b>
        </td>
    </tr>

    <tr>
        <td class="success text-center" rowspan="2"><b>Siswa</b></td>
        <td colspan="<?= $jumlahtanggal; ?>" class="success text-center"><b>Tanggal</b></td>
    </tr>

    <tr>
<?php
$daftar_tanggal = [];
while ($tgl = mysqli_fetch_assoc($sqlabsen)) {
    $daftar_tanggal[] = $tgl['tgl'];
    $pecah = explode("-", $tgl['tgl']);
    echo "<td class='text-center warning'><b>{$pecah[2]}/{$pecah[1]}</b></td>";
}
?>
    </tr>

<?php
$sqlsiswa = mysqli_query(
    $koneksi,
    "SELECT * FROM siswa WHERE idk='{$rss['idk']}' ORDER BY nama ASC"
);

while ($siswa = mysqli_fetch_assoc($sqlsiswa)) {
?>
<tr>
    <td class="warning"><?= $siswa['nama']; ?></td>

<?php
foreach ($daftar_tanggal as $tgl) {
    $sqlket = mysqli_query(
        $koneksi,
        "SELECT ket FROM absen 
         WHERE ids='{$siswa['ids']}' 
         AND idj='$acuan' 
         AND tgl='$tgl'"
    );

    $ket = mysqli_fetch_assoc($sqlket);
    echo "<td class='text-center'>" . ($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>
