<?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();
error_reporting(0);
include('includes/dbconnection.php');

if (strlen($_SESSION['sid']) == 0) {
    header('location:logout.php');
}

if (isset($_POST['add_session'])) {
    $session_name = trim($_POST['session_name']);
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];

    // Validate input
    if (empty($session_name) || strtotime($start_date) >= strtotime($end_date)) {
        $message = "Invalid session name or date range.";
    } else {
        // Insert into database using prepared statement
        $stmt = $con->prepare("INSERT INTO school_sessions (session_name, start_date, end_date) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $session_name, $start_date, $end_date);

        if ($stmt->execute()) {
            $message = "New academic session added successfully: <strong>$session_name</strong>";
        } else {
            $message = "Error adding session. Please try again.";
        }
        $stmt->close();
    }
}
?>
<!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">
                <div class="container-fluid">
                    <div class="row mb-2">
                        <div class="col-sm-6"></div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                                <li class="breadcrumb-item active">Add New Session</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </section>

            <section class="content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card card-primary">
                                <div class="card-header">
                                    <h3 class="card-title">Add New Academic Session</h3>
                                </div>

                                <?php if (isset($message)): ?>
                                    <div class="alert alert-info"><?php echo $message; ?></div>
                                <?php endif; ?>

                                <form method="POST" action="">
                                    <div class="card-body">
                                        <span style="color: brown"><h5>Session Details</h5></span>
                                        <hr>

                                        <div class="form-group col-md-6">
                                            <label for="session_name">Session Name (e.g., 2024/2025):</label>
                                            <input type="text" class="form-control" id="session_name" name="session_name" placeholder="Enter session name" required>
                                        </div>
                                        <div class="form-group col-md-6">
                                            <label for="start_date">Start Date:</label>
                                            <input type="date" class="form-control" id="start_date" name="start_date" required>
                                        </div>
                                        <div class="form-group col-md-6">
                                            <label for="end_date">End Date:</label>
                                            <input type="date" class="form-control" id="end_date" name="end_date" required>
                                        </div>

                                        <button type="submit" name="add_session" class="btn btn-primary">Add Session</button>
                                    </div>
                                </form>

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

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

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