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

// Get all inventory items with related data
$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
          ORDER BY b.created_at DESC";
$result = mysqli_query($conn, $query);
?>

<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-box-seam"></i> Daftar Inventaris Barang</h4>
                <a href="tambah.php" class="btn btn-light btn-sm">
                    <i class="bi bi-plus-circle"></i> Tambah Barang
                </a>
            </div>
        </div>
        <div class="card-body">
            <?php display_flash_message(); ?>
            
            <div class="table-responsive">
                <table class="table table-striped table-bordered table-hover" id="tabelInventaris">
                    <thead class="table-dark">
                        <tr>
                            <th width="5%">No</th>
                            <th>Kode Barang</th>
                            <th>Nama Barang</th>
                            <th>Jenis</th>
                            <th>Lokasi</th>
                            <th>Jumlah</th>
                            <th>Kondisi</th>
                            <th width="20%">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(mysqli_num_rows($result) > 0): ?>
                            <?php $no = 1; while($row = mysqli_fetch_assoc($result)): ?>
                                <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 align=center><?= $row['jumlah'] ?></td>
                                    <td>
                                        <span class="badge <?= $row['kondisi'] == 'Baik' ? 'bg-success' : ($row['kondisi'] == 'Rusak Ringan' ? 'bg-warning text-dark' : 'bg-danger') ?>">
                                            <?= $row['kondisi'] ?>
                                        </span>
                                    </td>
                                    <td>
                                        <!-- Tombol Detail -->
                                        <a href="detail.php?id=<?= $row['id_barang'] ?>" class="btn btn-sm btn-info" title="Detail">
                                            <i class="bi bi-eye"></i>
                                        </a>
                                        
                                        <!-- Tombol Edit -->
                                        <a href="edit.php?id=<?= $row['id_barang'] ?>" class="btn btn-sm btn-warning" title="Edit">
                                            <i class="bi bi-pencil-square"></i>
                                        </a>
                                        
                                        <!-- Tombol Hapus -->
                                        <!-- <button onclick="confirmDelete(<?= $row['id_barang']; ?>)" class="btn btn-sm btn-danger btn-delete">
                                            <i class="bi bi-trash"></i>
                                        </button> -->
                                        <?php if (is_logged_in()): ?>
                                            <button onclick="confirmDelete(<?= $row['id_barang']; ?>)" class="btn btn-sm btn-danger btn-delete" title="Hapus">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        <?php else: ?>
                                            <button class="btn btn-sm btn-danger" disabled title="Login untuk menghapus">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                        
                                        
                                        <!-- Tombol Cetak -->
                                        <a href="cetak.php?id=<?= $row['id_barang'] ?>" class="btn btn-sm btn-secondary" title="Cetak" target="_blank">
                                            <i class="bi bi-printer"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="8" class="text-center">Tidak ada data barang</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;
        }
    });
}

$(document).ready(function() {
    // Initialize DataTable
    $('#tabelInventaris').DataTable({
        language: {
            url: '//cdn.datatables.net/plug-ins/1.10.25/i18n/Indonesian.json'
        }
    });
});
</script>

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