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

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

// Get all inventory items with related data
$query = "SELECT i.*, l.nama_lokasi, j.nama_jenis, a.sumber_asal 
          FROM pdta_inventaris_barang i
          LEFT JOIN pdta_lokasi l ON i.id_lokasi = l.id_lokasi
          LEFT JOIN pdta_jenis_barang j ON i.id_jenis = j.id_jenis
          LEFT JOIN pdta_asal_usul a ON i.id_asal = a.id_asal
          ORDER BY i.created_at DESC";
$result = $conn->query($query);

// Generate HTML content for PDF
$html = '
<!DOCTYPE html>
<html>
<head>
    <title>Laporan Inventaris Barang</title>
    <style>
        body { font-family: Arial, sans-serif; }
        h1 { text-align: center; margin-bottom: 20px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .badge { padding: 3px 6px; border-radius: 3px; font-size: 12px; }
        .badge-success { background-color: #28a745; color: white; }
        .badge-warning { background-color: #ffc107; color: black; }
        .badge-danger { background-color: #dc3545; color: white; }
    </style>
</head>
<body>
    <h1>Laporan Inventaris Barang</h1>
    <p>Tanggal Cetak: ' . date('d/m/Y H:i') . '</p>
    
    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Kode Barang</th>
                <th>Nama Barang</th>
                <th>Jenis</th>
                <th>Lokasi</th>
                <th>Jumlah</th>
                <th>Kondisi</th>
                <th>Tahun Pembelian</th>
            </tr>
        </thead>
        <tbody>';

$no = 1;
while ($row = $result->fetch_assoc()) {
    $kondisi_class = '';
    if ($row['kondisi'] == 'Baik') {
        $kondisi_class = 'badge-success';
    } elseif ($row['kondisi'] == 'Rusak Ringan') {
        $kondisi_class = 'badge-warning';
    } else {
        $kondisi_class = 'badge-danger';
    }
    
    $html .= '
            <tr>
                <td>' . $no++ . '</td>
                <td>' . htmlspecialchars($row['kode_barang']) . '</td>
                <td>' . htmlspecialchars($row['nama_barang']) . '</td>
                <td>' . htmlspecialchars($row['nama_jenis']) . '</td>
                <td>' . htmlspecialchars($row['nama_lokasi']) . '</td>
                <td>' . htmlspecialchars($row['jumlah']) . '</td>
                <td><span class="badge ' . $kondisi_class . '">' . htmlspecialchars($row['kondisi']) . '</span></td>
                <td>' . htmlspecialchars($row['tahun_pembelian']) . '</td>
            </tr>';
}

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

// Create PDF
use Dompdf\Dompdf;
use Dompdf\Options;

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

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream('laporan_inventaris_barang.pdf', ['Attachment' => false]);
?>