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

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

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']);

    if (!empty($_FILES['image']['name'])) {

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

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

            mysqli_query($conn,
                "INSERT INTO social_events
                (title, description, event_date, start_time, end_time, event_location, image)
                VALUES
                ('$title','$desc','$date','$start','$end','$loc','$img')"
            );

            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;
}
</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">

            <div class="mb-3">
                <label class="form-label">शीर्षक</label>
                <input type="text" name="title" class="form-control" required>
            </div>

            <!-- DATE PICKER -->
            <div class="mb-3">
                <label class="form-label">कार्यक्रम तिथि</label>
                <input type="date" name="event_date" class="form-control" required>
            </div>

            <!-- START TIME -->
            <div class="mb-3">
                <label class="form-label">शुरू होने का समय</label>
                <input type="time" name="start_time" class="form-control" required>
            </div>

            <!-- END TIME -->
            <div class="mb-3">
                <label class="form-label">समाप्ति समय</label>
                <input type="time" name="end_time" class="form-control" required>
            </div>

            <div class="mb-3">
                <label class="form-label">कार्यक्रम स्थान</label>
                <input type="text" name="event_location" class="form-control" required>
            </div>

            <div class="mb-3">
                <label class="form-label">विवरण</label>
                <textarea name="description" class="form-control" rows="4" required></textarea>
            </div>

            <div class="mb-3">
                <label class="form-label">छवि</label>
                <input type="file" name="image" class="form-control" accept="image/*" required>
            </div>

            <button class="btn btn-success w-100">
                जोड़ें
            </button>

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

        </form>

    </div>
</div>

</div>

</body>
</html>
