<?php
require_once '../../config/database.php';
require_once '../../includes/functions.php';

if (!isset($_GET['id'])) {
    header('Location: index.php');
    exit();
}

$id = (int)$_GET['id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama_ruang = sanitize($_POST['nama_ruang']);
    $nama_barang = sanitize($_POST['nama_barang']);
    $spesifikasi = sanitize($_POST['spesifikasi']);
    $jumlah = (int)$_POST['jumlah'];
    $kondisi = sanitize($_POST['kondisi']);

    $query = "UPDATE pdt_barang SET 
              nama_ruang = ?, 
              nama_barang = ?, 
              spesifikasi = ?, 
              jumlah = ?, 
              kondisi = ? 
              WHERE id_barang = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("sssisi", $nama_ruang, $nama_barang, $spesifikasi, $jumlah, $kondisi, $id);

    if ($stmt->execute()) {
        showAlert('success', 'Sukses!', 'Data barang berhasil diperbarui.', 'index.php');
    } else {
        showAlert('error', 'Gagal!', 'Gagal memperbarui data barang.', 'edit.php?id='.$id);
    }
    exit();
}

// Ambil data barang untuk diedit
$query = "SELECT * FROM pdt_barang WHERE id_barang = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$barang = $result->fetch_assoc();

if (!$barang) {
    showAlert('error', 'Error!', 'Data barang tidak ditemukan.', 'index.php');
    exit();
}

require_once '../../includes/header.php';
require_once '../../includes/navbar.php';
?>

<div class="container mt-4">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            <h3 class="card-title">Edit Barang</h3>
            <a href="index.php" class="btn btn-light btn-sm float-end">
                <i class="fas fa-arrow-left"></i> Kembali
            </a>
        </div>
        <div class="card-body">
            <form method="POST" id="formEditBarang">
                <div class="mb-3">
                    <label for="nama_ruang" class="form-label">Nama Ruang</label>
                    <input type="text" class="form-control" id="nama_ruang" name="nama_ruang" 
                           value="<?= htmlspecialchars($barang['nama_ruang']); ?>" required>
                </div>
                <div class="mb-3">
                    <label for="nama_barang" class="form-label">Nama Barang</label>
                    <input type="text" class="form-control" id="nama_barang" name="nama_barang" 
                           value="<?= htmlspecialchars($barang['nama_barang']); ?>" required>
                </div>
                <div class="mb-3">
                    <label for="spesifikasi" class="form-label">Spesifikasi</label>
                    <textarea class="form-control" id="spesifikasi" name="spesifikasi" rows="3"><?= 
                        htmlspecialchars($barang['spesifikasi']); 
                    ?></textarea>
                </div>
                <div class="mb-3">
                    <label for="jumlah" class="form-label">Jumlah</label>
                    <input type="number" class="form-control" id="jumlah" name="jumlah" 
                           value="<?= $barang['jumlah']; ?>" min="1" required>
                </div>
                <div class="mb-3">
                    <label for="kondisi" class="form-label">Kondisi</label>
                    <select class="form-select" id="kondisi" name="kondisi" required>
                        <option value="Baik" <?= $barang['kondisi'] == 'Baik' ? 'selected' : ''; ?>>Baik</option>
                        <option value="Rusak Ringan" <?= $barang['kondisi'] == 'Rusak Ringan' ? 'selected' : ''; ?>>Rusak Ringan</option>
                        <option value="Rusak Berat" <?= $barang['kondisi'] == 'Rusak Berat' ? 'selected' : ''; ?>>Rusak Berat</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> Simpan Perubahan
                </button>
            </form>
        </div>
    </div>
</div>

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