<?php
ob_start();
include_once '../includes/constants.php';
include_once '../includes/functions.php';
include_once '../includes/allstripslashes.php';

$partners = new partners;
$partners->connection($host, $user, $pass, $db);

// Retrieve and sanitize POST input
$name   = isset($_POST['nametxt']) ? trim($_POST['nametxt']) : '';
$status = isset($_POST['status']) ? trim($_POST['status']) : '';

// Validate name
if (empty($name)) {
    $msg = "Please Enter a Valid Event !!";
    header("Location: index.php?Act=event&name=" . urlencode($name) . "&status=" . urlencode($status) . "&msg=" . urlencode($msg));
    exit;
}

// Check for duplicate event name using prepared statement
$stmt = $partners->conn->prepare("SELECT event_name FROM partners_event WHERE event_name = ?");
$stmt->bind_param("s", $name);
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows > 0) {
    $stmt->close();
    $msg = "Event already exists";
    header("Location: index.php?Act=event&name=" . urlencode($name) . "&status=" . urlencode($status) . "&msg=" . urlencode($msg));
    exit;
}
$stmt->close();

// Convert status to yes/no
$st = ($status === "active") ? "yes" : "no";

// Insert into partners_event
$insert_event = $partners->conn->prepare("INSERT INTO partners_event (event_name, event_status, event_type) VALUES (?, ?, 0)");
$insert_event->bind_param("ss", $name, $st);
if (!$insert_event->execute()) {
    die("Unable to insert event.");
}
$insert_event->close();

// Insert into partners_adminmail
$insert_adminmail = $partners->conn->prepare("INSERT INTO partners_adminmail (adminmail_eventname) VALUES (?)");
$insert_adminmail->bind_param("s", $name);
if (!$insert_adminmail->execute()) {
    die("Unable to insert into adminmail.");
}
$insert_adminmail->close();

// Success
$msg = "Event Entered successfully !!";
header("Location: index.php?Act=event&name=" . urlencode($name) . "&status=" . urlencode($status) . "&msg=" . urlencode($msg));
exit;
?>
