<?php

session_name('super_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];

include('includes/flash.php');



if (empty($_SESSION['sid'])) {

    header('location:logout.php');

    exit();

}



if (isset($_POST['submit'])) {

    $title = trim($_POST['title']);

    $date = trim($_POST['event_date']);



    // Prevent SQL Injection

    $stmt = $con->prepare("INSERT INTO events (title, event_date) VALUES (?, ?)");

    $stmt->bind_param("ss", $title, $date);



    if ($stmt->execute()) {

        flash_alert('Event Added Successfully!', 'add_event.php');
        header('Location: add_event.php'); exit;

    } else {

        flash_alert('Something went wrong. Please try again.');

    }

    $stmt->close();

}

// Handle adding session

if (isset($_POST['add_event'])) {

    $event_namee = strtoupper(trim($_POST['event_name']));

    if (!empty($event_namee)) {

        // Check if fee name already exists

        $check_stmt = $con->prepare("SELECT id FROM event_name WHERE name = ?");

        $check_stmt->bind_param("s", $event_namee);

        $check_stmt->execute();

        $check_stmt->store_result();



        if ($check_stmt->num_rows == 0) {

            // Insert new fee name

            $stmt = $con->prepare("INSERT INTO event_name (name) VALUES (?)");

            $stmt->bind_param("s", $event_namee);

            if ($stmt->execute()) {

                flash_alert('Event name added successfully!');

                header('Location: add_event.php'); exit;

            } else {

                flash_alert('Failed to add event name. Please try again.');

                header('Location: add_event.php'); exit;

            }

        } else {

            flash_alert('Event name already exists.');

            header('Location: add_event.php'); exit;

        }

    } else {

        flash_alert('Please enter a event name.');

        header('Location: add_event.php'); exit;

    }

}

?>



<!DOCTYPE html>

<html>

<?php @include("includes/head.php"); ?>



<body class="hold-transition sidebar-mini layout-fixed">

    <div class="wrapper">

        <?php @include("includes/header.php"); ?>

        <?php @include("includes/sidebar.php"); ?>



        <div class="content-wrapper">

<section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

                <div class="col-md-8">

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-calender"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                Event Management

                                <span class="premium-subtitle text-secondary">Manage Event</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="dashboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Add Event</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>





            <section class="content">

                <div class="container-fluid">

                    <div class="row">

                        <!-- Add Subject -->

                        <div class="col-md-12">

                            <div class="card shadow">

                                <div class="card-header bg-primary text-white">

                                    <h5 class="card-title mb-0 font-weight-bold"> <i class="fas fa-calendar"></i> Add New Event Name</h5>

                                </div>

                                <div class="card-body">

                                    <h5 style="color: brown">Event Details</h5>

                                    <hr>

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

                                        <div class="form-group col-md-12">

                                            <label for="session">Event Name (eg. Open Day)</label>

                                            <input type="text" class="form-control" name="event_name" placeholder="Enter event name" required>

                                        </div>

                                        <div class="form-group text-center">

                                            <button type="submit" name="add_event" class="btn btn-primary"><i class="fas fa-check-circle"></i> Add Event Name</button>

                                        </div>

                                    </form>

                                </div>

                            </div>

                        </div>

                        <div class="col-md-12">

                            <div class="card card-primary">

                                <div class="card-header">

                                    <h3 class="card-title font-weight-bold"><i class="fas fa-calendar-plus"></i> Add School Event</h3>

                                </div>

                                <form method="POST">

                                    <div class="card-body">

                                        <div class="row">

                                            <div class="form-group col-md-6">

                                                <label for="session">Event Name</label>

                                                <select class="form-control" name="title" id="title" required>

                                                    <option value="">-- Select Event--</option>

                                                    <option value="ENTRANCE EXAMINATION">ENTRANCE EXAMINATION</option>

                                                    <?php

                                                    $query = $con->query("SELECT id, name FROM event_name ORDER BY id DESC");

                                                    while ($row = $query->fetch_assoc()) {



                                                        echo "<option value='{$row['name']}'>{$row['name']}</option>";

                                                    }

                                                    ?>

                                                </select>

                                            </div>

                                            <div class="form-group col-md-6">

                                                <label for="event_date">Event Date</label>

                                                <input type="date" class="form-control" id="event_date" name="event_date" required>

                                            </div>

                                        </div>

                                    </div>

                                    <div class="card-footer text-center">

                                        <button type="submit" name="submit" class="btn btn-primary"><i class="fas fa-check-circle"></i> Submit</button>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>



                    <!-- Existing Events Table -->

                    <div class="card shadow mt-3">

                        <div class="card-header bg-primary text-white">

                            <h5 class="card-title mb-0 font-weight-bold"><i class="fas fa-calendar"></i> Existing Events</h5>

                        </div>

                        <div class="card-body mt-2">

                            <table id="example1" class="table table-bordered table-hover">

                                <thead>

                                    <tr>

                                        <th>ID</th>

                                        <th>Event Name</th>

                                        <th>Event Date</th>

                                        <th>Action</th>

                                    </tr>

                                </thead>

                                <tbody>

                                    <?php

                                    $query = mysqli_query($con, "SELECT * FROM events ORDER BY id DESC");

                                    $cnt = 1;

                                    while ($row = mysqli_fetch_array($query)) { ?>

                                        <tr>

                                            <td><?php echo htmlentities($cnt); ?></td>

                                            <td><?php echo htmlentities($row['title']); ?></td>

                                            <td><?php echo htmlentities($row['event_date']); ?></td>

                                            <td>

                                                <button class="btn btn-success btn-xs edit_data" data-id="<?php echo $row['id']; ?>"><i class="fa fa-edit"></i> Edit</button>

                                                <a href="delete_event.php?id=<?php echo $row['id']; ?>" onClick="return confirm('Are you sure you want to delete?')" class="btn btn-danger btn-xs"><i class="fa fa-trash"></i> Delete</a>

                                            </td>

                                        </tr>

                                    <?php $cnt++;

                                    } ?>

                                </tbody>

                            </table>

                        </div>

                    </div>



                    <!-- Edit Event Modal -->

                    <div id="editData" class="modal fade">

                        <div class="modal-dialog">

                            <div class="modal-content">

                                <div class="modal-header">

                                    <h5 class="modal-title">Edit Event</h5>

                                    <button type="button" class="close" data-dismiss="modal">

                                        <span>&times;</span>

                                    </button>

                                </div>

                                <div class="modal-body" id="info_update"></div>

                            </div>

                        </div>

                    </div>



                </div>

            </section>

        </div>



        <?php @include("includes/footer.php"); ?>

    </div>



    <?php @include("includes/foot.php"); ?>

    <script src="dist/js/select2.min.js"></script>

    <script>

        $(document).ready(function() {

            $('#title').select2();



        });

    </script>



    <script type="text/javascript">

        $(document).ready(function() {

            // Open Edit Modal & Fetch Data

            $(document).on('click', '.edit_data', function() {

                var edit_id = $(this).data('id');

                $.ajax({

                    url: "edit_event.php",

                    type: "POST",

                    data: {

                        edit_id: edit_id

                    },

                    success: function(response) {

                        $("#info_update").html(response);

                        $("#editData").modal('show');

                    }

                });

            });



            // Handle Update Form Submission

            $(document).on('submit', '#updateEventForm', function(e) {

                e.preventDefault();

                $.ajax({

                    url: "update_event.php",

                    type: "POST",

                    data: $(this).serialize(),

                    success: function(response) {

                        if (response === "success") {

                            alert("Event updated successfully!");

                            location.reload();

                        } else {

                            alert("Something went wrong. Try again.");

                        }

                    }

                });

            });

        });

    </script>



</body>



</html>