<?php
session_start();
include 'koneksi.php';

if (!isset($_SESSION['username'])) {
    header("Location: index.php");
    exit();
}

// Ambil data barang dari database
$sql = "SELECT * FROM inventaris";
$result = $conn->query($sql);

// Cek apakah query berhasil
if (!$result) {
    die("Query gagal: " . $conn->error); // Menampilkan kesalahan jika query gagal
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Barang</title>
    <link rel="stylesheet" href="adminlte/plugins/fontawesome-free/css/all.min.css">
    <link rel="stylesheet" href="adminlte/dist/css/adminlte.min.css">
    <link rel="stylesheet" href="adminlte/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" href="adminlte/plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
</head>
<body class="hold-transition sidebar-mini">

<!-- Wrapper -->
<div class="wrapper">
    <!-- Navbar -->
    <nav class="main-header navbar navbar-expand navbar-white navbar-light">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
            </li>
        </ul>
        <span class="navbar-right company-name">
            Global Netz
        </span>
    </nav>

    <!-- Main Sidebar Container -->
    <aside class="main-sidebar sidebar-dark-primary elevation-4">
        <div class="sidebar">
            <nav class="mt-2">
                <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
                    <li class="nav-item">
                        <a href="index.php" class="nav-link">
                            <i class="nav-icon fas fa-user"></i>
                            <p>Dashboard</p>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a href="tambah_barang.php" class="nav-link">
                            <i class="nav-icon fas fa-plus"></i>
                            <p>Tambah Barang</p>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a href="laporan_barang.php" class="nav-link">
                            <i class="nav-icon fas fa-clipboard-list"></i>
                            <p>Laporan Barang</p>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a href="riwayat_barang.php" class="nav-link">
                            <i class="nav-icon fas fa-history"></i>
                            <p>Riwayat Barang</p>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </aside>

    <!-- Content Wrapper -->
    <div class="content-wrapper">
        <div class="container">
            <h2 class="text-center mt-4">Laporan Barang</h2>
            <div class="card mt-4">
                <div class="card-header">
                    <h3 class="card-title">Daftar Barang</h3>
                </div>
                <div class="card-body">
                    <table id="laporanTable" class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>RFID Barang</th>
                                <th>Nama Barang</th>
                                <th>Jumlah Barang</th>
                                <th>Foto Barang</th>
                                <th>Keterangan</th>
                                <th>Tempat Barang</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            if ($result->num_rows > 0) {
                                while ($row = $result->fetch_assoc()) {
                                    echo "<tr>
                                            <td>".$row['rfid_barang']."</td>
                                            <td>".$row['nama_barang']."</td>
                                            <td>".$row['jumlah_barang']."</td>
                                            <td><img src='".$row['foto']."' width='100'></td>
                                            <td>".$row['keterangan']."</td>
                                            <td>".$row['tempat_barang']."</td> <!-- Menambahkan tempat_barang di sini -->
                                          </tr>";
                                }
                            } else {
                                echo "<tr><td colspan='6'>Tidak ada data barang.</td></tr>";
                            }
                            ?>
                            <div class="card mt-4">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="main-footer">
        <strong>Copyright &copy; 2023 <a href="#">Global Netz</a>.</strong> All rights reserved.
    </footer>
</div>

<!-- DataTables Script -->
<script src="adminlte/plugins/jquery/jquery.min.js"></script>
<script src="adminlte/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="adminlte/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="adminlte/dist/js/adminlte.min.js"></script>

<script>
    $(function () {
        $('#laporanTable').DataTable({
            "responsive": true, 
            "lengthChange": false, 
            "autoWidth": false
        });
    });
</script>
</body>
</html>
