<?php
// Tambahkan di awal file cetak.php
ob_start(); // Mulai output buffering

require_once '../../config/database.php';
require_once '../../includes/functions.php';

// Bersihkan semua output sebelum generate PDF
while (ob_get_level()) {
    ob_end_clean();
}

// Load library dompdf
require_once '../../vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

// Query data barang
$query = "SELECT * FROM pdt_barang ORDER BY nama_ruang, nama_barang";
$result = $conn->query($query);

// Buat HTML untuk PDF
$html = '
<!DOCTYPE html>
<html>
<head>
    <title>Laporan Data Barang</title>
    <style>
        @page {
                    margin-top: 5mm; /* Sesuaikan nilai sesuai kebutuhan */
                    margin-bottom: 20mm;
                    margin-left: 20mm;
                    margin-right: 20mm;
                }
        body { font-family: Arial, sans-serif; margin: 20px; font-size: 12px;}
        h1 { text-align: center; color: #333; margin-bottom: 0px; margin-top: 0px;}
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th { background-color: #f8f9fa; text-align: left; padding: 8px; border: 1px solid #ddd; }
        td { padding: 8px; border: 1px solid #ddd; }
        .badge { padding: 3px 8px; border-radius: 3px; font-size: 12px; display: inline-block; }
        .baik { background-color: #28a745; color: white; }
        .rusakringan { background-color: #ffc107; color: black; }
        .rusakberat { background-color: #dc3545; color: white; }
        .page-break { page-break-after: always; }
    </style>
</head>
<body>
    <h1>Laporan Data Barang</h1>
    <h1>SMAN 1 BANTARAN</h1>
    <p style="text-align: center;">Tanggal: ' . date('d F Y') . '</p>
    
    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Nama Ruang</th>
                <th>Nama Barang</th>
                <th>Spesifikasi</th>
                <th>Jumlah</th>
                <th>Kondisi</th>
                <th>Keterangan</th>
            </tr>
        </thead>
        <tbody>';

$no = 1;
while ($row = $result->fetch_assoc()) {
    $kondisi_class = strtolower(str_replace(' ', '', $row['kondisi']));
    $html .= '
            <tr>
                <td align=center>' . $no++ . '.</td>
                <td>' . htmlspecialchars($row['nama_ruang'] ?? '') . '</td>
                <td>' . htmlspecialchars($row['nama_barang'] ?? '') . '</td>
                <td>' . htmlspecialchars($row['spesifikasi'] ?? '') . '</td>
                <td align=center>' . $row['jumlah'] . '</td>
                <td><span class="badge ' . $kondisi_class . '">' . $row['kondisi'] . '</span></td>
                <td>' . htmlspecialchars($row['keterangan'] ?? '') . '</td>
            </tr>';
}

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

// Konfigurasi dompdf
$options = new Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);

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

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

// Output PDF langsung ke browser
$dompdf->stream('laporan_data_barang_' . date('Y-m-d') . '.pdf', [
    'Attachment' => false // false untuk preview di browser, true untuk download otomatis
]);

exit();
?>