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

// Get form inputs
$id = $_POST['id'];
$event_id = $_POST['event_id'];
$title = $_POST['title'];
$description = $_POST['description'];

// Fetch old image paths
$stmt = $mysqli->prepare("SELECT small_image, big_image FROM event_picture WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($old_small_image, $old_big_image);
$stmt->fetch();
$stmt->close();

// Handle small image upload
$small_image_path = $old_small_image;
if (!empty($_FILES['small_image']['name'])) {
    $small_name = basename($_FILES['small_image']['name']);
    $small_target = 'image_gallary/events/' . time() . '_small_' . $small_name;
    move_uploaded_file($_FILES['small_image']['tmp_name'], $small_target);
    $small_image_path = $small_target;
}

// Handle big image upload
$big_image_path = $old_big_image;
if (!empty($_FILES['big_image']['name'])) {
    $big_name = basename($_FILES['big_image']['name']);
    $big_target = 'image_gallary/events/' . time() . '_big_' . $big_name;
    move_uploaded_file($_FILES['big_image']['tmp_name'], $big_target);
    $big_image_path = $big_target;
}

// Update query
$query = "UPDATE event_picture SET event_id = ?, title = ?, small_image = ?, big_image = ?, description = ? WHERE id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("issssi", $event_id, $title, $small_image_path, $big_image_path, $description, $id);

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