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

$stmt = $pdo->query("
  SELECT a.*, u.full_name AS creator,
  (SELECT COUNT(*) FROM audit_findings f WHERE f.audit_id = a.id) AS total_findings,
  (SELECT COUNT(*) FROM audit_findings f WHERE f.audit_id = a.id AND f.status = 'closed') AS closed_findings
  FROM audits a
  LEFT JOIN users u ON a.created_by = u.id
  ORDER BY a.created_at DESC
");
$audits = $stmt->fetchAll(PDO::FETCH_ASSOC);

ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body { font-family: DejaVu Sans, sans-serif; font-size: 11px; }
h3 { text-align: center; color: #084D36; margin-bottom: 10px; }
p { text-align: center; margin-top: 0; font-size: 10px; color: #333; }
table { width: 100%; border-collapse: collapse; margin-top: 10px; }
th, td { border: 1px solid #444; padding: 5px; }
th { background-color: #084D36; color: #fff; }
.finding { background: #f6f6f6; font-size: 10px; }
.footer { margin-top: 20px; text-align: center; font-size: 10px; color: #555; }
</style>
</head>
<body>

<h3>LAPORAN AUDIT MUTU INTERNAL</h3>
<p><strong>Tanggal Cetak:</strong> <?= date('d M Y H:i') ?></p>

<table>
  <thead>
    <tr>
      <th>No</th>
      <th>Judul Audit</th>
      <th>Periode</th>
      <th>Status</th>
      <th>Temuan</th>
      <th>Progress</th>
      <th>Pembuat</th>
    </tr>
  </thead>
  <tbody>
  <?php $no=1; foreach ($audits as $a): 
    $progress = $a['total_findings'] > 0 ? round(($a['closed_findings'] / $a['total_findings']) * 100) : 0;
  ?>
    <tr>
      <td style="text-align:center;"><?= $no++ ?></td>
      <td><?= htmlspecialchars($a['audit_title']) ?></td>
      <td><?= htmlspecialchars($a['audit_period_start']) ?> - <?= htmlspecialchars($a['audit_period_end']) ?></td>
      <td style="text-align:center;"><?= ucfirst($a['status']) ?></td>
      <td style="text-align:center;"><?= $a['total_findings'] ?> (<?= $a['closed_findings'] ?> selesai)</td>
      <td style="text-align:center;"><?= $progress ?>%</td>
      <td><?= htmlspecialchars($a['creator'] ?? '-') ?></td>
    </tr>

    <?php
    $findStmt = $pdo->prepare("SELECT * FROM audit_findings WHERE audit_id=? ORDER BY id ASC");
    $findStmt->execute([$a['id']]);
    $findings = $findStmt->fetchAll(PDO::FETCH_ASSOC);

    if ($findings):
      foreach ($findings as $f): ?>
        <tr class="finding">
          <td></td>
          <td colspan="6">
            <strong><?= htmlspecialchars($f['finding_title']) ?></strong><br>
            <em>Severity:</em> <?= $f['severity'] ?> |
            <em>Unit:</em> <?= htmlspecialchars($f['responsible_unit'] ?? '-') ?> |
            <em>Batas Waktu:</em> <?= htmlspecialchars($f['due_date'] ?? '-') ?> |
            <em>Status:</em> <?= ucfirst($f['status'] ?? '-') ?><br>
            <em>Rekomendasi:</em> <?= htmlspecialchars($f['recommendation'] ?? '-') ?><br>
            <em>Deskripsi:</em> <?= htmlspecialchars($f['description'] ?? '-') ?>
          </td>
        </tr>
      <?php endforeach;
    endif; ?>

  <?php endforeach; ?>
  </tbody>
</table>

<div class="footer">
  Dicetak otomatis oleh Sistem Informasi LPM UNIRU - <?= date('Y') ?>
</div>

</body>
</html>
<?php
$html = ob_get_clean();

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream('Laporan_Audit_Mutu_' . date('Ymd_His') . '.pdf', ['Attachment' => true]);
exit;
?>
