<?php

// Add Event
if (isset($_POST['add_event_btn'])) {
    $name = trim($_POST['name']);
    $description = $_POST['description'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $start_time = $_POST['start_time'];
    $end_time = $_POST['end_time'];

    // Validate date and time using DateTime
    $startDateTime = new DateTime("$start_date $start_time");
    $endDateTime = new DateTime("$end_date $end_time");

    if ($startDateTime > $endDateTime) {
        $_SESSION['msg'] = "Start date/time cannot be after end date/time.";
        $_SESSION['type'] = "error";
        header("Location: event");
        exit();
    }

    // Insert event into the database
    $stmt = $connection->prepare("INSERT INTO event (name, description, start_date, end_date, start_time, end_time) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssss", $name, $description, $start_date, $end_date, $start_time, $end_time);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Event added successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Failed to add event.";
        $_SESSION['type'] = "error";
    }

    $stmt->close();
    header("Location: event");
    exit();
}

// Edit Event
if (isset($_POST['edit_event_btn'])) {
    $id = (int)$_POST['id'];
    $name = $_POST['name'];
    $description = $_POST['description'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $start_time = $_POST['start_time'];
    $end_time = $_POST['end_time'];

    // Validate datetime
    $startDateTime = new DateTime("$start_date $start_time");
    $endDateTime = new DateTime("$end_date $end_time");

    if ($startDateTime > $endDateTime) {
        $_SESSION['msg'] = "Start date/time cannot be after nd date/time.";
        $_SESSION['type'] = "error";
        header("Location: event");
        exit();
    }

    $stmt = $connection->prepare("UPDATE event SET name=?, description=?, start_date=?, end_date=?, start_time=?, end_time=? WHERE id=?");
    $stmt->bind_param("ssssssi", $name, $description, $start_date, $end_date, $start_time, $end_time, $id);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Event updated successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Failed to update event.";
        $_SESSION['type'] = "error";
    }

    $stmt->close();
    header("Location: event");
    exit();
}

// Delete Event
if (isset($_POST['del_event_btn'])) {
    $id = (int)$_POST['id'];

    $stmt = $connection->prepare("DELETE FROM event WHERE id=?");
    $stmt->bind_param("i", $id);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Event deleted successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Failed to delete event.";
        $_SESSION['type'] = "error";
    }

    $stmt->close();
    header("Location: event");
    exit();
}
?>




<div class="card">
    <div class='row'>
        <div class='col-xs-12 col-sm-12 col-md-12'>
            <div class='panel panel-primary ch'>
                <div class='panel-heading'>
                    <h4 class='text-center'>EVENT MANAGEMENT</h4>
                </div>

                <div class='panel-body'>
                    <div class='mb-3 text-right'>
                        <button class='btn btn-sm btn-success' data-toggle='modal' data-target='#addEventModal'>➕ Add Event</button>
                    </div>

                    <?php
                    $query = "SELECT * FROM `event` ORDER BY start_date DESC";
                    $run_query = mysqli_query($connection, $query);
                    if (mysqli_num_rows($run_query) > 0):
                        $i = 0;
                        $events = []; // store events for modal
                    ?>
                    <div class="table-responsive">
                        <table class="table table-hover table-bordered" id="myTable">
                            <thead>
                                <tr>
                                    <th>S/N</th>
                                    <th>Event Name</th>
                                    <th>Description</th>
                                    <th>Date</th>
                                    <th>Time</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($event = mysqli_fetch_assoc($run_query)):
                                    $i++;
                                    $event_id = $event['id'];
                                    $events[] = $event;
                                ?>
                                <tr>
                                    <td><?= $i ?></td>
                                    <td><?= htmlspecialchars($event['name']) ?></td>
                                    <td><?= htmlspecialchars($event['description']) ?></td>
                                    <td><?= htmlspecialchars($event['start_date']) ?> to <?= htmlspecialchars($event['end_date']) ?></td>
                                    <td><?= htmlspecialchars($event['start_time']) ?> - <?= htmlspecialchars($event['end_time']) ?></td>
                                    <td>
                                        <form method="POST" style="display:inline-block;">
                                            <input type="hidden" name="id" value="<?= $event_id ?>" />
                                            <input type="submit" name="del_event_btn" value="Delete" class="btn btn-xs btn-danger" />
                                        </form>
                                        <button type="button" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#editEventModal<?= $event_id ?>">
                                            Edit
                                        </button>
                                    </td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- All Edit Modals -->
                    <?php foreach ($events as $event): ?>
                        <?php $event_id = $event['id']; ?>
                        <div class="modal fade" id="editEventModal<?= $event_id ?>" tabindex="-1" role="dialog" aria-labelledby="editEventModalLabel<?= $event_id ?>" aria-hidden="true">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <form method="POST">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="editEventModalLabel<?= $event_id ?>">Edit Event</h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span>&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body">
                                            <input type="hidden" name="id" value="<?= $event_id ?>" />
                                            <div class="form-group">
                                                <label>Event Name</label>
                                                <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($event['name']) ?>" placeholder="Enter event name" required />
                                            </div>
                                            <div class="form-group">
                                                <label>Description</label>
                                                <textarea name="description" class="form-control" placeholder="Enter event description" required><?= htmlspecialchars($event['description']) ?></textarea>
                                            </div>
                                            <div class="form-group">
                                                <label>Start Date</label>
                                                <input type="date" name="start_date" class="form-control" value="<?= $event['start_date'] ?>" required />
                                            </div>
                                            <div class="form-group">
                                                <label>End Date</label>
                                                <input type="date" name="end_date" class="form-control" value="<?= $event['end_date'] ?>" required />
                                            </div>
                                            <div class="form-group">
                                                <label>Start Time</label>
                                                <input type="time" name="start_time" class="form-control" value="<?= $event['start_time'] ?>" required />
                                            </div>
                                            <div class="form-group">
                                                <label>End Time</label>
                                                <input type="time" name="end_time" class="form-control" value="<?= $event['end_time'] ?>" required />
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="submit" name="edit_event_btn" class="btn btn-success">Update Event</button>
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                    <?php endif; ?>

                </div>
            </div>
        </div>
    </div>
</div>


<!-- Add Event Modal -->
<div class="modal fade" id="addEventModal" tabindex="-1" role="dialog" aria-labelledby="addEventModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form method="POST">
                <div class="modal-header">
                    <h5 class="modal-title">Add New Event</h5>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>Event Name</label>
                        <input type="text" name="name" class="form-control" placeholder="Enter event name" required />
                    </div>
                    <div class="form-group">
                        <label>Description</label>
                        <textarea name="description" class="form-control" placeholder="Enter event description" required></textarea>
                    </div>
                    <div class="form-group">
                        <label>Start Date</label>
                        <input type="date" name="start_date" class="form-control" required />
                    </div>
                    <div class="form-group">
                        <label>End Date</label>
                        <input type="date" name="end_date" class="form-control" required />
                    </div>
                    <div class="form-group">
                        <label>Start Time</label>
                        <input type="time" name="start_time" class="form-control" required />
                    </div>
                    <div class="form-group">
                        <label>End Time</label>
                        <input type="time" name="end_time" class="form-control" required />
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" name="add_event_btn" class="btn btn-primary">Add Event</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>
