<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include("auth.php");
include("config/db.php");

if (!isset($_GET['id'])) {
    die("Invalid Request");
}

$id = intval($_GET['id']);

$query = mysqli_query($conn, "SELECT * FROM social_events WHERE id=$id");
$data = mysqli_fetch_assoc($query);

if (!$data) {
    die("Invalid Event ID");
}

if ($_SERVER['REQUEST_METHOD'] == "POST") {

    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $desc  = mysqli_real_escape_string($conn, $_POST['description']);
    $date  = mysqli_real_escape_string($conn, $_POST['event_date']);
    $start = mysqli_real_escape_string($conn, $_POST['start_time']);
    $end   = mysqli_real_escape_string($conn, $_POST['end_time']);
    $loc   = mysqli_real_escape_string($conn, $_POST['event_location']);

    // IMAGE UPDATE (OPTIONAL)
    if (!empty($_FILES['image']['name'])) {

        $imgName = time() . "_" . basename($_FILES['image']['name']);
        $targetPath = "uploads/events/" . $imgName;

        if (move_uploaded_file($_FILES['image']['tmp_name'], $targetPath)) {

            mysqli_query($conn,
                "UPDATE social_events 
                 SET image='$imgName' 
                 WHERE id=$id"
            );
        }
    }

    // UPDATE EVENT DATA
    mysqli_query($conn,
        "UPDATE social_events 
         SET title='$title',
             description='$desc',
             event_date='$date',
             start_time='$start',
             end_time='$end',
             event_location='$loc'
         WHERE id=$id"
    );

    header("Location: event.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="hi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>कार्यक्रम संपादित करें</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body{
    background:#f5f7fa;
    font-family: Arial, sans-serif;
}

.wrapper{
    min-height:100vh;
    display:flex;
    align-items:center;
    justify-content:center;
    padding:20px;
}

.preview-img{
    width:200px;
    height:130px;
    object-fit:cover;
    border-radius:8px;
    border:1px solid #ddd;
    margin-bottom:15px;
}
</style>
</head>

<body>

<div class="wrapper">

<div class="card shadow-sm w-100" style="max-width:600px;">
    <div class="card-header fw-bold text-center">
        सामाजिक कार्यक्रम संपादित करें
    </div>

    <div class="card-body">

        <form method="post" enctype="multipart/form-data">

            <?php if (!empty($data['image'])) { ?>
                <div class="text-center">
                    <img src="uploads/events/<?php echo htmlspecialchars($data['image']); ?>" class="preview-img">
                </div>
            <?php } ?>

            <div class="mb-3">
                <label class="form-label">शीर्षक</label>
                <input type="text" name="title"
                       class="form-control"
                       value="<?php echo htmlspecialchars($data['title']); ?>"
                       required>
            </div>

            <div class="mb-3">
                <label class="form-label">कार्यक्रम तिथि</label>
                <input type="date"
                       name="event_date"
                       class="form-control"
                       value="<?php echo htmlspecialchars($data['event_date']); ?>"
                       required>
            </div>

            <div class="mb-3">
                <label class="form-label">शुरू होने का समय</label>
                <input type="time"
                       name="start_time"
                       class="form-control"
                       value="<?php echo htmlspecialchars($data['start_time']); ?>"
                       required>
            </div>

            <div class="mb-3">
                <label class="form-label">समाप्ति समय</label>
                <input type="time"
                       name="end_time"
                       class="form-control"
                       value="<?php echo htmlspecialchars($data['end_time']); ?>"
                       required>
            </div>

            <div class="mb-3">
                <label class="form-label">कार्यक्रम स्थान</label>
                <input type="text"
                       name="event_location"
                       class="form-control"
                       value="<?php echo htmlspecialchars($data['event_location']); ?>"
                       required>
            </div>

            <div class="mb-3">
                <label class="form-label">विवरण</label>
                <textarea name="description"
                          class="form-control"
                          rows="4"
                          required><?php echo htmlspecialchars($data['description']); ?></textarea>
            </div>

            <div class="mb-3">
                <label class="form-label">नई छवि (वैकल्पिक)</label>
                <input type="file" name="image" class="form-control" accept="image/*">
            </div>

            <button type="submit" class="btn btn-primary w-100">
                अपडेट करें
            </button>

            <a href="event.php" class="btn btn-secondary w-100 mt-2">
                वापस जाएँ
            </a>

        </form>

    </div>
</div>

</div>

</body>
</html>
