<?php
require_once '../../config/database.php';
require_once '../../includes/auth.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);
?>

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

<div class="container mt-4">
    <div class="card shadow">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h4 class="mb-0">Daftar Inventaris Barang</h4>
            <a href="cetak.php" target="_blank" class="btn btn-light">
                <i class="bi bi-printer"></i> Cetak
            </a>
        </div>
        <div class="card-body">
            <div class="mb-3 d-flex justify-content-between">
                <a href="tambah.php" class="btn btn-success">
                    <i class="bi bi-plus-circle"></i> Tambah Barang
                </a>
                <form method="GET" class="form-inline">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" placeholder="Cari barang..." value="<?= isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>">
                        <button class="btn btn-outline-secondary" type="submit">
                            <i class="bi bi-search"></i>
                        </button>
                    </div>
                </form>
            </div>
            
            <?php if (isset($_SESSION['success'])) : ?>
                <div class="alert alert-success"><?= $_SESSION['success']; unset($_SESSION['success']); ?></div>
            <?php endif; ?>
            
            <?php if (isset($_SESSION['error'])) : ?>
                <div class="alert alert-danger"><?= $_SESSION['error']; unset($_SESSION['error']); ?></div>
            <?php endif; ?>
            
            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped">
                    <thead class="table-dark">
                        <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>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ($result->num_rows > 0) : ?>
                            <?php $no = 1; ?>
                            <?php while ($row = $result->fetch_assoc()) : ?>
                            <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 bg-<?= 
                                        $row['kondisi'] == 'Baik' ? 'success' : 
                                        ($row['kondisi'] == 'Rusak Ringan' ? 'warning' : 'danger') 
                                    ?>">
                                        <?= htmlspecialchars($row['kondisi']) ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="edit.php?id=<?= $row['id_barang'] ?>" class="btn btn-sm btn-primary">
                                        <i class="fas fa-edit"></i> Edit
                                    </a>
                                    <button onclick="confirmDelete(<?= $row['id_barang']; ?>)" class="btn btn-sm btn-danger btn-delete">
                                        <i class="fas fa-trash"></i> Hapus
                                    </button>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        <?php else : ?>
                            <tr>
                                <td colspan="8" class="text-center">Tidak ada data</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script src="../../assets/js/jquery.min.js"></script>
<script src="../../assets/js/bootstrap.bundle.min.js"></script>
<script src="../../assets/js/sweetalert2.min.js"></script>
<script>
function confirmDelete(id) {
    Swal.fire({
        title: 'Apakah Anda yakin?',
        text: "Data yang dihapus tidak dapat dikembalikan!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Ya, hapus!',
        cancelButtonText: 'Batal'
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = 'hapus.php?id=' + id;
        }
    });
}
</script>

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