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

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 = "INSERT INTO pdt_barang (nama_ruang, nama_barang, spesifikasi, jumlah, kondisi) 
              VALUES (?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("sssis", $nama_ruang, $nama_barang, $spesifikasi, $jumlah, $kondisi);

    if ($stmt->execute()) {
        showAlert('success', 'Sukses!', 'Data barang berhasil ditambahkan.', 'index.php');
    } else {
        showAlert('error', 'Gagal!', 'Gagal menambahkan data barang.', 'tambah.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">Tambah 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="formTambahBarang">
                <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" 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" required>
                </div>
                <div class="mb-3">
                    <label for="spesifikasi" class="form-label">Spesifikasi</label>
                    <textarea class="form-control" id="spesifikasi" name="spesifikasi" rows="3"></textarea>
                </div>
                <div class="mb-3">
                    <label for="jumlah" class="form-label">Jumlah</label>
                    <input type="number" class="form-control" id="jumlah" name="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">Baik</option>
                        <option value="Rusak Ringan">Rusak Ringan</option>
                        <option value="Rusak Berat">Rusak Berat</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> Simpan
                </button>
            </form>
        </div>
    </div>
</div>

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