<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}
require 'dbconn.php'; // Make sure this includes your DB connection

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    // Get and sanitize inputs
    $title = trim($_POST['event_title']);
    $date = trim($_POST['event_date']);
    $description = trim($_POST['event_desc']);

    // Basic validation
    if (empty($title) || empty($date) || empty($description)) {
        die("All fields are required.");
    }

    // Prepare and insert
    $stmt = $mysqli->prepare("INSERT INTO events_tb (event_title, event_date, event_desc) VALUES (?, ?, ?)");
    if (!$stmt) {
        die("Prepare failed: " . $mysqli->error);
    }

    $stmt->bind_param("sss", $title, $date, $description);

    if ($stmt->execute()) {
        // Success – redirect to manage page
        header("Location: manage_events.php");
        exit;
    } else {
        echo "Insert failed: " . $stmt->error;
    }

    $stmt->close();
    $mysqli->close();
} else {
    echo "Invalid request.";
}
?>
