<?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; }
        h1 { text-align: center; margin-bottom: 20px; }
        .event-info { margin-bottom: 30px; text-align: center; }
        .event-date { font-weight: bold; }
        .pengesah { margin-top: 50px; text-align: right; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #000; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .signature-col { width: 150px; }
        .no-col { width: 30px; }
        .footer { margin-top: 50px; font-size: 12px; text-align: center; }
    </style>
</head>
<body>
    <h1>DAFTAR HADIR</h1>
    
    <div class="event-info">
        <div class="event-name">' . htmlspecialchars($currentEvent['nama_event']) . '</div>
        <div class="event-date">' . 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>' . $no++ . '</td>
                <td>' . htmlspecialchars($row['nama_lengkap']) . '</td>
                <td>' . htmlspecialchars($row['jabatan']) . '</td>
                <td>';
        
        if (!empty($row['tanda_tangan'])) {
            $html .= '<img src="' . htmlspecialchars($row['tanda_tangan']) . '" style="max-height: 50px;">';
        }
        
        $html .= '</td>
            </tr>';
    }
}

$html .= '
        </tbody>
    </table>
    
    <div class="pengesah">
        <div>' . htmlspecialchars($currentEvent['pengesah'] ?? '') . '</div>
        <div style="margin-top: 50px;">_________________________</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();