<?php
session_start();
if (!empty($_SESSION['nama'])) {

include "../../../config/conn.php";
include "../../../config/fungsi.php";
require "../../../config/html2pdf/html2pdf.class.php";

$acuan = $_POST['idj'];
$tgl_lengkap = $_POST['tgl_lengkap'];

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

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

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

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

$pecah = explode(" ", $tgl_lengkap);
$tahun1 = $pecah[2];
$bulan = $pecah[1];

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

$sqlabsen = mysqli_query($koneksi, "SELECT DISTINCT tgl FROM absen WHERE idj='$acuan'");
$jumlahtanggal = mysqli_num_rows($sqlabsen);

$content = "
<h3>Laporan Absensi Kelas {$kss['nama']} | {$nama_mp['nama_mp']}</h3>
<p><b>$tahun_ajaran</b><br>{$nama_hari['hari']}, {$rss['jam_mulai']} - {$rss['jam_selesai']}</p>
<table cellpadding='0' cellspacing='0' border='1'>
<tr>
<td rowspan='2'><b>Siswa</b></td>
<td colspan='$jumlahtanggal'><b>Tanggal</b></td>
</tr>
<tr>";

while ($tgl = mysqli_fetch_array($sqlabsen)) {
    $p = explode("-", $tgl['tgl']);
    $content .= "<td>{$p[2]}/{$p[1]}</td>";
}
$content .= "</tr>";

$sqlsiswa = mysqli_query($koneksi, "SELECT * FROM siswa WHERE idk='{$rss['idk']}'");
while ($s = mysqli_fetch_array($sqlsiswa)) {
    $content .= "<tr><td>{$s['nama']}</td>";
    $sqlket = mysqli_query($koneksi, "SELECT ket FROM absen WHERE ids='{$s['ids']}' AND idj='$acuan'");
    while ($k = mysqli_fetch_array($sqlket)) {
        $content .= "<td>{$k['ket']}</td>";
    }
    $content .= "</tr>";
}

$content .= "</table>";

$pdf = new HTML2PDF('P','A4','fr');
$pdf->writeHTML($content);
$pdf->Output("Laporan_Absensi.pdf");

} else {
    echo "<h2>Anda harus login</h2>";
}
?>
