<?php
require_once __DIR__ . '/../../config/secure_bootstrap.php';
require_once __DIR__ . '/../../vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

// === Ambil data ===
$search = trim($_POST['search_export'] ?? '');
$params = [];
$sql = "SELECT a.*, u.full_name AS creator FROM activities a LEFT JOIN users u ON a.created_by = u.id";
if ($search !== '') {
  $sql .= " WHERE a.title LIKE ? OR a.description LIKE ? OR a.organizer LIKE ? OR a.location LIKE ?";
  $like = "%$search%";
  $params = [$like, $like, $like, $like];
}
$sql .= " ORDER BY a.date_start DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// === Siapkan HTML ===
$judul = "LAPORAN DATA KEGIATAN LPM UNIVERSITAS RIYADLUL ULUM";
$tanggal = "Tanggal Cetak: " . date('d F Y, H:i');

$html = '
<html>
<head>
  <style>
    body {
      font-family: DejaVu Sans, Arial, Helvetica, sans-serif;
      font-size: 11pt;
      margin: 40px;
    }
    h2 {
      text-align: center;
      font-size: 16pt;
      margin-bottom: 0;
    }
    p.date {
      text-align: center;
      font-size: 11pt;
      margin-top: 2px;
      margin-bottom: 20px;
      color: #555;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 10px;
    }
    th, td {
      border: 1px solid #333;
      padding: 6px 8px;
    }
    th {
      background-color: #d9ead3; /* hijau muda */
      text-align: center;
      font-weight: bold;
    }
    td {
      vertical-align: top;
    }
    tr:nth-child(even) td {
      background-color: #f9f9f9;
    }
    footer {
      position: fixed;
      bottom: -20px;
      left: 0;
      right: 0;
      text-align: right;
      font-size: 9pt;
      color: #777;
    }
  </style>
</head>
<body>
  <h2>' . htmlspecialchars($judul) . '</h2>
  <p class="date">' . htmlspecialchars($tanggal) . '</p>

  <table>
    <thead>
      <tr>
        <th style="width:4%;">No</th>
        <th style="width:20%;">Judul</th>
        <th style="width:18%;">Tanggal</th>
        <th style="width:15%;">Tempat</th>
        <th style="width:20%;">Penyelenggara</th>
        <th style="width:15%;">Dibuat Oleh</th>
      </tr>
    </thead>
    <tbody>';

// isi data
if ($rows) {
  foreach ($rows as $i => $r) {
    $html .= '<tr>
      <td style="text-align:center;">' . ($i + 1) . '</td>
      <td>' . htmlspecialchars($r['title'] ?? '-') . '</td>
      <td>' . htmlspecialchars($r['date_start'] ?? '-') . ' s.d ' . htmlspecialchars($r['date_end'] ?? '-') . '</td>
      <td>' . htmlspecialchars($r['location'] ?? '-') . '</td>
      <td>' . htmlspecialchars($r['organizer'] ?? '-') . '</td>
      <td>' . htmlspecialchars($r['creator'] ?? '-') . '</td>
    </tr>';
  }
} else {
  $html .= '<tr><td colspan="6" style="text-align:center; color:#999;">Tidak ada data kegiatan.</td></tr>';
}

$html .= '
    </tbody>
  </table>

  <footer>
    Dicetak otomatis oleh Sistem Informasi LPM • ' . date('d/m/Y H:i') . '
  </footer>
</body>
</html>
';

// === Render ke PDF ===
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);

$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// === Output ===
$filename = 'Laporan_Kegiatan_' . date('Ymd_His') . '.pdf';
$dompdf->stream($filename, ['Attachment' => true]);
exit;
