<!-- Daftar riwayat barang -->
<?php
    
    include 'koneksi.php';

    $sql = "SELECT i.nama_barang, i.rfid_barang, sl.status, sl.tanggal_waktu 
            FROM inventaris i
            INNER JOIN status_log sl ON i.rfid_barang = sl.rfid_barang
            ORDER BY sl.tanggal_waktu DESC";

    $result = $conn->query($sql);

    if ($result && $result->num_rows > 0) {
        echo "<h2>Riwayat Keluar Masuk Barang</h2>";
        echo "<table border='1'>
                <tr>
                    <th>RFID barang</th>
                    <th>Nama Barang</th>
                    <th>Status</th>
                    <th>Tanggal & Waktu</th>
                    <th>Aksi</th>
                </tr>";
        while($row = $result->fetch_assoc()) {
            echo "<tr>
                    <td>".$row["rfid_barang"]."</td>
                    <td>".$row["nama_barang"]."</td>
                    <td>".$row["status"]."</td>
                    <td>".$row["tanggal_waktu"]."</td>
                    <td>
                        <form action='history.php' method='POST' onsubmit='return confirm(\"Apakah Anda yakin ingin menghapus riwayat ini?\");'>
                            <input type='hidden' name='rfid_tag' value='".$row["rfid_tag"]."'>
                            <input type='hidden' name='delete_log' value='1'>
                            <input type='submit' value='Hapus'>
                        </form>
                    </td>
                  </tr>";
        }
        echo "</table>";
    } else {
        echo "Tidak ada riwayat keluar masuk barang.";
    }

    // Proses penghapusan riwayat
    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['delete_log'])) {
        $rfid_barang = $_POST['rfid_barang'];

        $sql_delete_log = "DELETE FROM status_log WHERE rfid_barang = '$rfid_barang'";
        
        if ($conn->query($sql_delete_log) === TRUE) {
            echo "<p>Riwayat berhasil dihapus.</p>";
        } else {
            echo "<p>Error menghapus riwayat: " . $sql_delete_log . "<br>" . $conn->error . "</p>";
        }
    }

    // Tutup koneksi ke database
    $conn->close();
    ?>
