<?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-sm border-0">
        <div class="card-header bg-gradient-primary text-white">
            <div class="d-flex justify-content-between align-items-center">
                <h4 class="mb-0"><i class="bi bi-box-seam me-2"></i>Detail Barang Inventaris</h4>
                <a href="index.php" class="btn btn-light btn-sm rounded-pill">
                    <i class="bi bi-arrow-left me-1"></i> Kembali
                </a>
            </div>
        </div>
        
        <div class="card-body">
            <div class="row">
                <!-- Kolom Foto -->
                <div class="col-md-4 mb-4">
                    <div class="card h-100 shadow-none border">
                        <div class="card-body text-center p-4">
                            <?php if (!empty($barang['foto'])): ?>
                                <img src="../../uploads/gambar/<?= htmlspecialchars($barang['foto']) ?>" 
                                     alt="Foto Barang" class="img-fluid rounded" style="max-height: 250px; object-fit: contain;">
                            <?php else: ?>
                                <div class="d-flex align-items-center justify-content-center bg-light" style="height: 250px;">
                                    <div class="text-muted">
                                        <i class="bi bi-image" style="font-size: 3rem;"></i>
                                        <p class="mt-2 mb-0">Tidak ada foto</p>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                
                <!-- Kolom Informasi -->
                <div class="col-md-8">
                    <div class="row">
                        <!-- Informasi Utama -->
                        <div class="col-md-6">
                            <div class="card mb-4 shadow-sm border-0">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0"><i class="bi bi-info-circle me-2"></i>Informasi Utama</h5>
                                </div>
                                <div class="card-body">
                                    <dl class="row mb-0">
                                        <dt class="col-sm-5 text-muted">Kode Barang</dt>
                                        <dd class="col-sm-7"><?= htmlspecialchars($barang['kode_barang']) ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Nama Barang</dt>
                                        <dd class="col-sm-7"><?= htmlspecialchars($barang['nama_barang']) ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Jenis</dt>
                                        <dd class="col-sm-7"><?= htmlspecialchars($barang['nama_jenis']) ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Lokasi</dt>
                                        <dd class="col-sm-7"><?= htmlspecialchars($barang['nama_lokasi']) ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Asal Perolehan</dt>
                                        <dd class="col-sm-7"><?= htmlspecialchars($barang['sumber_asal'] ?? '-') ?></dd>
                                    </dl>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Spesifikasi -->
                        <div class="col-md-6">
                            <div class="card mb-4 shadow-sm border-0">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0"><i class="bi bi-gear me-2"></i>Spesifikasi</h5>
                                </div>
                                <div class="card-body">
                                    <dl class="row mb-0">
                                        <dt class="col-sm-5 text-muted">Merk/Type</dt>
                                        <dd class="col-sm-7">
                                            <?= htmlspecialchars($barang['merk'] ?? '-') ?>
                                            <?= $barang['merk'] && $barang['type'] ? ' / ' : '' ?>
                                            <?= htmlspecialchars($barang['type'] ?? '') ?>
                                        </dd>
                                        
                                        <dt class="col-sm-5 text-muted">Ukuran</dt>
                                        <dd class="col-sm-7"><?= htmlspecialchars($barang['ukuran'] ?? '-') ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Bahan</dt>
                                        <dd class="col-sm-7"><?= htmlspecialchars($barang['bahan'] ?? '-') ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Jumlah</dt>
                                        <dd class="col-sm-7"><?= $barang['jumlah'] ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Tahun</dt>
                                        <dd class="col-sm-7"><?= $barang['tahun_pembelian'] ?></dd>
                                        
                                        <dt class="col-sm-5 text-muted">Kondisi</dt>
                                        <dd class="col-sm-7">
                                            <span class="badge rounded-pill <?= $barang['kondisi'] == 'Baik' ? 'bg-success' : ($barang['kondisi'] == 'Rusak Ringan' ? 'bg-warning text-dark' : 'bg-danger') ?>">
                                                <?= $barang['kondisi'] ?>
                                            </span>
                                        </dd>
                                    </dl>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Deskripsi dan Keterangan -->
                    <div class="row">
                        <div class="col-md-6">
                            <div class="card mb-4 shadow-sm border-0">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0"><i class="bi bi-card-text me-2"></i>Deskripsi</h5>
                                </div>
                                <div class="card-body">
                                    <?php if (!empty($barang['deskripsi'])): ?>
                                        <p class="mb-0"><?= nl2br(htmlspecialchars($barang['deskripsi'])) ?></p>
                                    <?php else: ?>
                                        <p class="text-muted mb-0"><i>Tidak ada deskripsi</i></p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="card mb-4 shadow-sm border-0">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0"><i class="bi bi-chat-square-text me-2"></i>Keterangan</h5>
                                </div>
                                <div class="card-body">
                                    <?php if (!empty($barang['keterangan'])): ?>
                                        <p class="mb-0"><?= nl2br(htmlspecialchars($barang['keterangan'])) ?></p>
                                    <?php else: ?>
                                        <p class="text-muted mb-0"><i>Tidak ada keterangan</i></p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Tombol Aksi -->
            <div class="d-flex justify-content-between mt-4">
                <div>
                    <small class="text-muted">
                        <i class="bi bi-clock-history me-1"></i> 
                        Terakhir diperbarui: <?= date('d M Y H:i', strtotime($barang['created_at'])) ?>
                    </small>
                </div>
                <div>
                    <a href="edit.php?id=<?= $barang['id_barang'] ?>" class="btn btn-warning rounded-pill">
                        <i class="bi bi-pencil-square me-1"></i> Edit
                    </a>
                    <a href="index.php" class="btn btn-outline-secondary rounded-pill ms-2">
                        <i class="bi bi-arrow-left me-1"></i> Kembali ke Daftar
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .card-header.bg-gradient-primary {
        background: linear-gradient(135deg, #068496 0%, #0062cc 100%);
    }
    .card {
        border-radius: 0.5rem;
    }
    dt {
        font-weight: 500;
    }
    dd {
        font-weight: 400;
    }
    .img-placeholder {
        background-color: #f8f9fa;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
        color: #6c757d;
    }
</style>

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