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

// 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>
        body { font-family: Arial, sans-serif; }
        h1 { text-align: center; color: #333; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th { background-color: #f8f9fa; text-align: left; padding: 8px; }
        td { padding: 8px; border-bottom: 1px solid #ddd; }
        .badge { padding: 5px 10px; border-radius: 20px; font-size: 12px; }
        .baik { background-color: #28a745; color: white; }
        .ringan { background-color: #ffc107; color: black; }
        .berat { background-color: #dc3545; color: white; }
    </style>
</head>
<body>
    <h1>Laporan Data Barang</h1>
    <p>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>
            </tr>
        </thead>
        <tbody>';

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

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

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

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

// Output PDF
$dompdf->stream('laporan_data_barang_' . date('Ymd') . '.pdf', [
    'Attachment' => false
]);

exit();
?>