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

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

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

// Ambil parameter filter
$filter_ruang = $_GET['ruang'] ?? '';
$filter_kondisi = $_GET['kondisi'] ?? '';

// Query data dengan filter
$query = "SELECT * FROM pdt_barang WHERE 1=1";
$params = [];

if (!empty($filter_ruang)) {
    $query .= " AND nama_ruang = ?";
    $params[] = $filter_ruang;
}

if (!empty($filter_kondisi)) {
    $query .= " AND kondisi = ?";
    $params[] = $filter_kondisi;
}

$query .= " ORDER BY nama_ruang, nama_barang";

// Prepare statement
$stmt = $conn->prepare($query);

if (!empty($params)) {
    $types = str_repeat('s', count($params));
    $stmt->bind_param($types, ...$params);
}

$stmt->execute();
$result = $stmt->get_result();

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

use Dompdf\Dompdf;
use Dompdf\Options;

// 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: 10px; }
        .filter-info { text-align: center; margin-bottom: 20px; font-style: italic; color: #555; }
        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; }
        .footer { margin-top: 30px; text-align: right; font-size: 12px; color: #666; }
    </style>
</head>
<body>
    <h1>Laporan Data Barang</h1>
    
    <!-- Informasi Filter -->
    <div class="filter-info">';
    
if (!empty($filter_ruang)) {
    $html .= 'Ruang: ' . htmlspecialchars($filter_ruang) . ' | ';
}

if (!empty($filter_kondisi)) {
    $html .= 'Kondisi: ' . htmlspecialchars($filter_kondisi) . ' | ';
}

$html .= 'Tanggal: ' . date('d F Y') . '
    </div>
    
    <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>';
}

// Hitung total
$total_query = "SELECT COUNT(*) as total, SUM(jumlah) as total_jumlah FROM pdt_barang WHERE 1=1";
if (!empty($filter_ruang)) {
    $total_query .= " AND nama_ruang = '$filter_ruang'";
}
if (!empty($filter_kondisi)) {
    $total_query .= " AND kondisi = '$filter_kondisi'";
}
$total_result = $conn->query($total_query);
$total_row = $total_result->fetch_assoc();

$html .= '
        </tbody>
    </table>
    
    <div class="footer">
        <p>Total Barang: ' . $total_row['total'] . '</p>
        <p>Total Jumlah: ' . ($total_row['total_jumlah'] ?? 0) . '</p>
        <p>Dicetak pada: ' . date('d/m/Y H:i:s') . '</p>
    </div>
</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();
?>