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

// Ambil ID barang dari parameter URL
$id_barang = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Query untuk mendapatkan detail barang
$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);
$barang = mysqli_fetch_assoc($result);

if (!$barang) {
    $_SESSION['error'] = 'Data barang tidak ditemukan';
    header('Location: index.php');
    exit();
}
?>

<div class="container mt-4">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            <div class="d-flex justify-content-between align-items-center">
                <h4><i class="bi bi-info-circle"></i> Detail Barang Inventaris</h4>
                <a href="index.php" class="btn btn-light btn-sm">
                    <i class="bi bi-arrow-left"></i> Kembali
                </a>
            </div>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-8">
                    <table class="table table-bordered">
                        <tr>
                            <th width="30%">Kode Barang</th>
                            <td><?= htmlspecialchars($barang['kode_barang']) ?></td>
                        </tr>
                        <tr>
                            <th>Nama Barang</th>
                            <td><?= htmlspecialchars($barang['nama_barang']) ?></td>
                        </tr>
                        <tr>
                            <th>Jenis Barang</th>
                            <td><?= htmlspecialchars($barang['nama_jenis']) ?></td>
                        </tr>
                        <tr>
                            <th>Lokasi</th>
                            <td><?= htmlspecialchars($barang['nama_lokasi']) ?></td>
                        </tr>
                        <tr>
                            <th>Asal Perolehan</th>
                            <td><?= htmlspecialchars($barang['sumber_asal'] ?? '-') ?></td>
                        </tr>
                        <tr>
                            <th>Deskripsi</th>
                            <td><?= nl2br(htmlspecialchars($barang['deskripsi'] ?? '-')) ?></td>
                        </tr>
                        <tr>
                            <th>Merk/Type</th>
                            <td>
                                <?= htmlspecialchars($barang['merk'] ?? '-') ?>
                                <?= $barang['merk'] && $barang['type'] ? ' / ' : '' ?>
                                <?= htmlspecialchars($barang['type'] ?? '') ?>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="col-md-4">
                    <table class="table table-bordered">
                        <tr>
                            <th width="40%">Ukuran</th>
                            <td><?= htmlspecialchars($barang['ukuran'] ?? '-') ?></td>
                        </tr>
                        <tr>
                            <th>Bahan</th>
                            <td><?= htmlspecialchars($barang['bahan'] ?? '-') ?></td>
                        </tr>
                        <tr>
                            <th>Jumlah</th>
                            <td><?= $barang['jumlah'] ?></td>
                        </tr>
                        <tr>
                            <th>Tahun Pembelian</th>
                            <td><?= $barang['tahun_pembelian'] ?></td>
                        </tr>
                        <tr>
                            <th>Kondisi</th>
                            <td>
                                <span class="badge <?= $barang['kondisi'] == 'Baik' ? 'bg-success' : ($barang['kondisi'] == 'Rusak Ringan' ? 'bg-warning text-dark' : 'bg-danger') ?>">
                                    <?= $barang['kondisi'] ?>
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <th>Keterangan</th>
                            <td><?= nl2br(htmlspecialchars($barang['keterangan'] ?? '-')) ?></td>
                        </tr>
                    </table>
                    
                    <?php if (!empty($barang['foto'])): ?>
                    <div class="text-center mt-3">
                        <img src="../../uploads/gambar/<?= htmlspecialchars($barang['foto']) ?>" 
                             alt="Foto Barang" class="img-fluid rounded" style="max-height: 200px;">
                        <p class="mt-2 text-muted">Foto Barang</p>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="mt-4 text-end">
                <a href="edit.php?id=<?= $barang['id_barang'] ?>" class="btn btn-warning">
                    <i class="bi bi-pencil-square"></i> Edit
                </a>
                <a href="index.php" class="btn btn-secondary">
                    <i class="bi bi-arrow-left"></i> Kembali
                </a>
            </div>
        </div>
    </div>
</div>

<?php include '../../includes/footer.php'; ?>