<?php
include '../../includes/auth.php';

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $_SESSION['error'] = 'ID barang tidak valid';
    header('Location: index.php');
    exit();
}

$id_barang = intval($_GET['id']);

// Get the item data with related information
$query = "SELECT b.*, l.nama_lokasi, j.nama_jenis, a.sumber_asal 
          FROM pdta_inventaris_barang b
          LEFT JOIN pdta_lokasi l ON b.id_lokasi = l.id_lokasi
          LEFT JOIN pdta_jenis_barang j ON b.id_jenis = j.id_jenis
          LEFT JOIN pdta_asal_usul a ON b.id_asal = a.id_asal
          WHERE b.id_barang = $id_barang";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) == 0) {
    $_SESSION['error'] = 'Barang tidak ditemukan';
    header('Location: index.php');
    exit();
}

$item = mysqli_fetch_assoc($result);

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

// Create HTML content for PDF
$html = '
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Cetak Detail Barang - '.htmlspecialchars($item['nama_barang']).'</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            font-size: 12px;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }
        .title {
            font-size: 16px;
            font-weight: bold;
        }
        .title2 {
            font-size: 20px;
            font-weight: bold;
            color: blue;
        }
        .subtitle {
            font-size: 16px;
        }
        .table-detail {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .table-detail th, .table-detail td {
            border: 1px solid #ddd;
            padding: 8px;
        }
        .table-detail th {
            background-color: #f2f2f2;
            width: 30%;
        }
        .foto-container {
            text-align: center;
            margin-top: 20px;
        }
        .foto-barang {
            max-width: 300px;
            max-height: 300px;
            border: 1px solid #ddd;
        }
        .ttd-area {
            margin-top: 50px;
            text-align: right;
        }
        .ttd-space {
            height: 80px;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="title">SISTEM INVENTARIS BARANG</div>
        <div class="title2">SMAN 1 BANTARAN</div>
        <div class="subtitle">Detail Barang Inventaris</div>
    </div>
    
    <table class="table-detail">
        <tr>
            <th>Kode Barang</th>
            <td>'.htmlspecialchars($item['kode_barang']).'</td>
        </tr>
        <tr>
            <th>Nama Barang</th>
            <td>'.htmlspecialchars($item['nama_barang']).'</td>
        </tr>
        <tr>
            <th>Jenis Barang</th>
            <td>'.htmlspecialchars($item['nama_jenis'] ?? '-').'</td>
        </tr>
        <tr>
            <th>Lokasi</th>
            <td>'.htmlspecialchars($item['nama_lokasi'] ?? '-').'</td>
        </tr>
        <tr>
            <th>Merk/Type</th>
            <td>'.htmlspecialchars($item['merk'] ?? '-').' / '.htmlspecialchars($item['type'] ?? '-').'</td>
        </tr>
        <tr>
            <th>Ukuran/Bahan</th>
            <td>'.htmlspecialchars($item['ukuran'] ?? '-').' / '.htmlspecialchars($item['bahan'] ?? '-').'</td>
        </tr>
        <tr>
            <th>Jumlah</th>
            <td>'.$item['jumlah'].'</td>
        </tr>
        <tr>
            <th>Tahun Pembelian</th>
            <td>'.$item['tahun_pembelian'].'</td>
        </tr>
        <tr>
            <th>Kondisi</th>
            <td>'.$item['kondisi'].'</td>
        </tr>
        <tr>
            <th>Sumber Dana/Asal Usul</th>
            <td>'.htmlspecialchars($item['sumber_asal'] ?? '-').'</td>
        </tr>
        <tr>
            <th>Deskripsi/Spesifikasi</th>
            <td>'.nl2br(htmlspecialchars($item['deskripsi'] ?? '-')).'</td>
        </tr>
        <tr>
            <th>Keterangan</th>
            <td>'.nl2br(htmlspecialchars($item['keterangan'] ?? '-')).'</td>
        </tr>
    </table>';

// Add photo if exists
if ($item['foto']) {
    $imagePath = '../../uploads/gambar/'.$item['foto'];
    if (file_exists($imagePath)) {
        $imageData = file_get_contents($imagePath);
        $base64Image = base64_encode($imageData);
        $html .= '
    <div class="foto-container">
        <div><strong>Foto Barang:</strong></div>
        <img src="data:image/jpeg;base64,'.$base64Image.'" class="foto-barang">
    </div>';
    }
}

$html .= '
    <div class="ttd-area">
        <div>'.date('d F Y').'</div>
        <div class="ttd-space"></div>
        <div>(_______________________)</div>
    </div>
</body>
</html>';

// Create DomPDF instance
use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('defaultFont', 'Arial');

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

// Output the generated PDF to browser
$dompdf->stream('detail_barang_'.$item['kode_barang'].'.pdf', [
    'Attachment' => false // false untuk menampilkan di browser, true untuk download otomatis
]);

exit();