<?php
include '../includes/auth.php';
include '../includes/functions.php';

// Pastikan event_id ada dan valid
$event_id = isset($_GET['event_id']) ? (int)$_GET['event_id'] : 0;
if ($event_id <= 0) {
    die('Event ID tidak valid');
}

// Ambil data event dan absensi
$currentEvent = $conn->query("SELECT * FROM dafdir_event WHERE id_event = $event_id")->fetch_assoc();
if (!$currentEvent) {
    die('Event tidak ditemukan');
}

$attendance = $conn->query("SELECT * FROM dafdir_absensi WHERE id_event = $event_id ORDER BY sequence ASC")->fetch_all(MYSQLI_ASSOC);

// Load library DOMPDF
require_once '../vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;

// Konfigurasi DOMPDF
$options = new Options();
$options->set('isRemoteEnabled', true);
$options->set('defaultFont', 'Arial');

$dompdf = new Dompdf($options);

// Buat konten HTML untuk PDF
$html = '
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Daftar Hadir - ' . htmlspecialchars($currentEvent['nama_event']) . '</title>
    <style>
        body { font-family: Arial, sans-serif; margin-top: 5px;}
         @page {
                    margin-top: 5mm; /* Sesuaikan nilai sesuai kebutuhan */
                    /* margin-bottom: 20mm;
                    margin-left: 20mm;
                    margin-right: 20mm; */
                }
        h4 { text-align: center; margin-bottom: 10px; }
        .event-info { margin-bottom: 20px; text-align: center; font-size: 12px;}
        .event-name { font-size: 13px; font-weight: bold; }
        .event-date { font-weight: light; }
        .pengesah { margin-top: 50px; text-align: left; margin-left: 400px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #000; padding: 8px; }
        th { background-color: #f2f2f2; text-align: center; }
        .signature-col { width: 150px; }
        .no-col { width: 30px; }
        .footer { margin-top: 50px; font-size: 12px; text-align: center; }
        th.no-col,
    td.no-col {
        width: 8%;
    }
    th.signature-col,
    td.signature-col {
        width: 25%;
    }
    th:nth-child(2),
    td:nth-child(2) {
        width: 40%;
    }
    th:nth-child(3),
    td:nth-child(3) {
        width: 27%;
    }
    </style>
</head>
<body>
    <div style="text-align: right;">
        <!-- <img src="http://localhost:8888/dafdir_digital/gambar/logo.png" style="height: 60px; display: block; margin-bottom: 1px; margin-top: 1px;"> -->
        <img src="https://sman1bantaran.sch.id/daftarhadir/gambar/logo.png" style="height: 60px; display: block; margin-bottom: 1px; margin-top: 1px;">
    </div>
    <h4>DAFTAR HADIR</h4>
    <div class="event-info">
        <div class="event-name">' . htmlspecialchars($currentEvent['nama_event']) . '</div>
        <div class="event-date"> Tanggal : ' . date('d F Y', strtotime($currentEvent['tanggal_event'])) . '</div>
    </div>
    
    <table>
        <thead>
            <tr>
                <th class="no-col">No</th>
                <th>Nama Lengkap</th>
                <th>Jabatan</th>
                <th class="signature-col">Tanda Tangan</th>
            </tr>
        </thead>
        <tbody>';

// Isi tabel dengan data absensi
if (empty($attendance)) {
    $html .= '<tr><td colspan="4" style="text-align: center;">Tidak ada data absensi</td></tr>';
} else {
    $no = 1;
    foreach ($attendance as $row) {
        $html .= '
            <tr>
                <td align=center>' . $no++ . '.</td>
                <td>' . htmlspecialchars($row['nama_lengkap']) . '</td>
                <td>' . htmlspecialchars($row['jabatan']) . '</td>
                <td align=center>';
        
        if (!empty($row['tanda_tangan'])) {
            $html .= '<img src="' . htmlspecialchars($row['tanda_tangan']) . '" style="max-height: 50px; background: none; border: none; mix-blend-mode: multiply;">';
        }
        
        $html .= '</td>
            </tr>';
    }
}

$html .= '
        </tbody>
    </table>
    
    <div class="pengesah">
        <div>Kepala Sekolah,</div>
        <div style="margin-top: 70px; text-decoration: underline;">' . htmlspecialchars($currentEvent['pengesah'] ?? '') . '</div>
        <div>NIP. ' . htmlspecialchars($currentEvent['nip'] ?? '') . '</div>
       <!-- <div>Pengesah</div> -->
    </div>
    
  <!--  <div class="footer">
        Dicetak pada: ' . date('d/m/Y H:i') . '
    </div> -->
</body>
</html>';

// Load HTML ke DOMPDF
$dompdf->loadHtml($html);

// Set ukuran dan orientasi kertas
$dompdf->setPaper('A4', 'portrait');

// Render PDF
$dompdf->render();

// Output PDF ke browser
$dompdf->stream('Daftar_Hadir_' . $currentEvent['nama_event'] . '.pdf', [
    'Attachment' => false // false untuk preview di browser, true untuk download otomatis
]);

exit();