<?php
require_once __DIR__ . '/../config/db.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Role yang boleh export
$allowed = ['superadmin', 'operator', 'guru'];

if (!isset($_SESSION['user']) || !in_array($_SESSION['user']['role'], $allowed)) {
    header("Location: /presensi_v1/preview.php");
    exit;
}

/* ================================
   FILTER
================================ */
$from = $_GET["from"] ?? date("Y-m-01");
$to   = $_GET["to"] ?? date("Y-m-d");

$kelas_combo = $_GET["kelas_combo"] ?? "";
$kelas = $jurusan = $rombel = "";

if ($kelas_combo !== "") {
    $p = explode("-", $kelas_combo);
    if (count($p) === 3) {
        $kelas = $p[0];
        $jurusan = $p[1];
        $rombel = $p[2];
    }
}

/* ================================
   FILTER QUERY
================================ */
$where = ["p.tanggal BETWEEN :from AND :to"];
$params = [":from"=>$from, ":to"=>$to];

if ($kelas)   { $where[] = "s.kelas = :kelas";     $params[":kelas"]   = $kelas; }
if ($jurusan) { $where[] = "s.jurusan = :jurusan"; $params[":jurusan"] = $jurusan; }
if ($rombel)  { $where[] = "s.rombel = :rombel";   $params[":rombel"]  = $rombel; }

$where_sql = implode(" AND ", $where);

/* ================================
   GET DATA
================================ */
$sql = "
SELECT p.*, s.nama, s.nis, s.kelas, s.jurusan, s.rombel
FROM presensi p
JOIN siswa s ON s.id = p.siswa_id
WHERE $where_sql
ORDER BY tanggal, waktu
";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();

/* ================================
   LOAD TCPDF
================================ */
require_once __DIR__ . '/../assets/vendor/tcpdf/tcpdf.php';

class PDF extends TCPDF {
    public function Header() {
        $this->Line(10, 12, 287, 12);
    }
}

$pdf = new PDF('L', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetMargins(10, 20, 10);
$pdf->AddPage();
$pdf->SetFont('helvetica','',10);

$html = "
<h2 style='margin-bottom:0;'>Rekap Presensi</h2>
<p style='font-size:12px; margin-top:2px;'>
Periode: <b>{$from}</b> s/d <b>{$to}</b>
</p>
";

$html .= "
<table cellpadding='4' cellspacing='0' border='1' style='border-collapse:collapse; font-size:11px;'>
<thead>
<tr style='text-align:center; font-weight:bold;'>
    <th width='32mm'>Tanggal</th>
    <th width='20mm'>Jam</th>
    <th width='25mm'>NIS</th>
    <th width='90mm'>Nama</th>
    <th width='100mm'>Kelas</th>
    <th width='10mm'>Ket</th>
</tr>
</thead>
<tbody>
";

foreach ($rows as $r) {
    $tgl  = date("d/m/Y", strtotime($r["tanggal"]));
    $jam  = substr($r["waktu"], 0, 5);
    $kelas_full = $r["kelas"] . " " . $r["jurusan"] . " - " . $r["rombel"];

$html .= "
<tr>
    <td>{$tgl}</td>
    <td style='text-align:center'>{$jam}</td>
    <td>{$r['nis']}</td>
    <td>{$r['nama']}</td>
    <td>{$kelas_full}</td>
    <td style='text-align:center'>{$r['keterangan']}</td>
</tr>
";
}

$html .= "</tbody></table>";

$pdf->writeHTML($html, true, false, false, false, "");

if (ob_get_length()) ob_end_clean();

$pdf->Output("rekap_presensi.pdf", "I");
exit;
