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

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

// Ambil data barang berdasarkan RFID Barang
if (isset($_GET['rfid_barang'])) {
    $rfid_barang = $_GET['rfid_barang'];
    $sql = "SELECT * FROM inventaris WHERE rfid_barang = '$rfid_barang'";
    $result = $conn->query($sql);
    
    if ($result && $result->num_rows > 0) {
        $barang = $result->fetch_assoc();
    } else {
        echo "<script>alert('Barang tidak ditemukan.');window.location='laporan_barang.php';</script>";
        exit();
    }
}

// Proses update barang
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $rfid_barang = $_POST['rfid_barang'];
    $nama_barang = $_POST['nama_barang'];
    $jumlah_barang = $_POST['jumlah_barang'];
    $keterangan = $_POST['keterangan'];
    $tempat_barang = $_POST['tempat_barang']; // Tambahkan ini

    // Jika ada foto baru diupload
    if (!empty($_FILES["foto"]["name"])) {
        $foto = 'uploads/' . basename($_FILES["foto"]["name"]);
        move_uploaded_file($_FILES["foto"]["tmp_name"], $foto);
        $sql = "UPDATE inventaris SET nama_barang='$nama_barang', jumlah_barang=$jumlah_barang, foto='$foto', keterangan='$keterangan', tempat_barang='$tempat_barang' WHERE rfid_barang='$rfid_barang'";
    } else {
        $sql = "UPDATE inventaris SET nama_barang='$nama_barang', jumlah_barang=$jumlah_barang, keterangan='$keterangan', tempat_barang='$tempat_barang' WHERE rfid_barang='$rfid_barang'";
    }

    if ($conn->query($sql) === TRUE) {
        echo "<script>alert('Barang berhasil diupdate');window.location='laporan_barang.php';</script>";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Barang</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <!-- Custom CSS for consistency -->
    <link rel="stylesheet" href="adminlte/dist/css/adminlte.min.css">
</head>
<body>
    <div class="container">
        <h2>Edit Barang</h2>
        <form action="edit_barang.php?rfid_barang=<?php echo htmlspecialchars($barang['rfid_barang']); ?>" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="rfid_barang" value="<?php echo htmlspecialchars($barang['rfid_barang']); ?>">
            <div class="form-group">
                <label for="nama_barang">Nama Barang:</label>
                <input type="text" class="form-control" id="nama_barang" name="nama_barang" value="<?php echo htmlspecialchars($barang['nama_barang']); ?>" required>
            </div>
            <div class="form-group">
                <label for="jumlah_barang">Jumlah Barang:</label>
                <input type="number" class="form-control" id="jumlah_barang" name="jumlah_barang" value="<?php echo htmlspecialchars($barang['jumlah_barang']); ?>" required>
            </div>
            <div class="form-group">
                <label for="status_barang">Status Barang:</label>
                <input type="text" class="form-control" id="status_barang" name="status_barang" value="<?php echo htmlspecialchars($barang['status_barang']); ?>" readonly>
            </div>
            <div class="form-group">
                <label for="tempat_barang">Tempat Barang:</label> <!-- Tambahkan ini -->
                <input type="text" class="form-control" id="tempat_barang" name="tempat_barang" value="<?php echo htmlspecialchars($barang['tempat_barang']); ?>" required>
            </div>
            <div class="form-group">
                <label for="foto">Foto Barang:</label>
                <input type="file" class="form-control-file" name="foto">
                <img src="<?php echo htmlspecialchars($barang['foto']); ?>" width="100" alt="Foto Barang">
            </div>
            <div class="form-group">
                <label for="keterangan">Keterangan:</label>
                <input type="text" class="form-control" id="keterangan" name="keterangan" value="<?php echo htmlspecialchars($barang['keterangan']); ?>" required>
            </div>
            <button type="submit" class="btn btn-primary">Simpan</button>
            <a href="laporan_barang.php" class="btn btn-secondary">Batal</a>
        </form>
    </div>
</body>
</html>
    