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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama_lokasi = $_POST['nama_lokasi'];
    
    // Check if location already exists
    $check = $conn->prepare("SELECT id_lokasi FROM pdta_lokasi WHERE nama_lokasi = ?");
    $check->bind_param("s", $nama_lokasi);
    $check->execute();
    $check->store_result();
    
    if ($check->num_rows > 0) {
        $_SESSION['error'] = "Lokasi sudah ada";
    } else {
        $stmt = $conn->prepare("INSERT INTO pdta_lokasi (nama_lokasi) VALUES (?)");
        $stmt->bind_param("s", $nama_lokasi);
        
        if ($stmt->execute()) {
            $_SESSION['success'] = "Lokasi berhasil ditambahkan!";
            header("Location: index.php");
            exit();
        } else {
            $_SESSION['error'] = "Gagal menambahkan lokasi: " . $conn->error;
        }
    }
}
?>

<?php include '../../includes/header.php'; ?>
<?php include '../../includes/navbar.php'; ?>

<div class="container mt-4">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            <h4 class="mb-0"><i class="bi bi-plus-circle"></i> Tambah Lokasi</h4>
        </div>
        <div class="card-body">
            <form method="POST">
                <div class="mb-3">
                    <label for="nama_lokasi" class="form-label">Nama Lokasi <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" id="nama_lokasi" name="nama_lokasi" required>
                    <small class="text-muted">Contoh: Ruang Guru, Lab Komputer, Perpustakaan</small>
                </div>
                
                <div class="d-flex justify-content-between mt-4">
                    <a href="index.php" class="btn btn-secondary">
                        <i class="bi bi-arrow-left"></i> Kembali
                    </a>
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-save"></i> Simpan
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="../../assets/js/jquery.min.js"></script>
<script src="../../assets/js/bootstrap.bundle.min.js"></script>

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