<?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_term'])) {
    $session_id = $_POST['session_id']; // Selected session
    $term_name = trim($_POST['term_name']);
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];

    // Validate input
    if (empty($session_id) || empty($term_name) || strtotime($start_date) >= strtotime($end_date)) {
        $message = "Invalid term details. Please check your inputs.";
    } else {
        // Check if the term already exists for the selected session
        $check_stmt = $con->prepare("SELECT id FROM school_terms WHERE session_id = ? AND term_name = ?");
        $check_stmt->bind_param("is", $session_id, $term_name);
        $check_stmt->execute();
        $check_stmt->store_result();
        
        if ($check_stmt->num_rows > 0) {
            $message = "This term already exists for the selected session!";
        } else {
            // Insert into database
            $stmt = $con->prepare("INSERT INTO school_terms (session_id, term_name, start_date, end_date) VALUES (?, ?, ?, ?)");
            $stmt->bind_param("isss", $session_id, $term_name, $start_date, $end_date);

            if ($stmt->execute()) {
                $message = "New term added successfully: <strong>$term_name</strong>";
            } else {
                $message = "Error adding term. Please try again.";
            }
            $stmt->close();
        }
        $check_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 Term</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 Term</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>Term Details</h5></span>
                                        <hr>

                                        <div class="form-group col-md-6">
                                            <label for="session_id">Select Academic Session:</label>
                                            <select class="form-control" id="session_id" name="session_id" required>
                                                <option value="">-- Select Session --</option>
                                                <?php
                                                $query = $con->query("SELECT id, session_name FROM school_sessions ORDER BY id DESC");
                                                while ($row = $query->fetch_assoc()) {
                                                    echo "<option value='".$row['id']."'>".$row['session_name']."</option>";
                                                }
                                                ?>
                                            </select>
                                        </div>

                                        <div class="form-group col-md-6">
                                            <label for="term_name">Term Name:</label>
                                            <select class="form-control" id="term_name" name="term_name" required>
                                                <option value="">-- Select Term --</option>
                                                <option value="First Term">First Term</option>
                                                <option value="Second Term">Second Term</option>
                                                <option value="Third Term">Third Term</option>
                                            </select>
                                        </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_term" class="btn btn-primary">Add Term</button>
                                    </div>
                                </form>

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

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

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