<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}
include 'dbconn.php';

// Sanitize and fetch form data
$event_id    = $_POST['event_id'] ?? '';
$title       = trim($_POST['title'] ?? '');
$description = trim($_POST['description'] ?? '');

// Validate required fields
if (!$event_id || !$title || !$description || empty($_FILES['small_image']['name']) || empty($_FILES['big_image']['name'])) {
    die("All fields are required.");
}

// Target upload directory
$uploadDir = 'image_gallary/events/';
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true); // Create if not exists
}

// Handle small image upload
$smallImageName = uniqid('small_') . '_' . basename($_FILES['small_image']['name']);
$smallImagePath = $uploadDir . $smallImageName;
move_uploaded_file($_FILES['small_image']['tmp_name'], $smallImagePath);

// Handle big image upload
$bigImageName = uniqid('big_') . '_' . basename($_FILES['big_image']['name']);
$bigImagePath = $uploadDir . $bigImageName;
move_uploaded_file($_FILES['big_image']['tmp_name'], $bigImagePath);

// Prepare insert query
$stmt = $mysqli->prepare("INSERT INTO event_picture (event_id, title, small_image, big_image, description, created_at) VALUES (?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("issss", $event_id, $title, $smallImagePath, $bigImagePath, $description);

if ($stmt->execute()) {
    header("Location: manage_event_pictures.php?success=1");
    exit;
} else {
    echo "Insert failed: " . $stmt->error;
}
?>
