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

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

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

    $terminal = trim($_POST['terminal']); // Get terminal status



    // Prevent SQL injection

    $class = mysqli_real_escape_string($con, $class);

    $section = mysqli_real_escape_string($con, $section);

    $terminal = mysqli_real_escape_string($con, $terminal);



    // Check if class-section combination already exists

    $check_query = mysqli_query($con, "SELECT * FROM class_section WHERE class='$class' AND section='$section'");

    if (mysqli_num_rows($check_query) > 0) {

        flash_alert('This Class & Arm combination already exists.');

    } else {

        // Insert new class-section with terminal flag

        $query = mysqli_query($con, "INSERT INTO class_section (class, section, terminal) VALUES ('$class', '$section', '$terminal')");

        if ($query) {

            flash_alert('Class & Arm Added Successfully.');

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

        } else {

            flash_alert('Something Went Wrong. Please try again.');

        }

    }

}

?>



<!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-chalkboard"></i>

                        </div>

                        <div>

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

                                Class Management

                                <span class="premium-subtitle text-secondary">Class & Arm</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">Class & Arm</li>

                        </ol>

                    </nav>

                </div>

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

                                    <h5 class="card-title font-weight-bold"><i class="fas fa-chalkboard"></i> Add Class & Arm</h5>

                                </div>

                                <form role="form" method="POST">

                                    <div class="card-body">

                                        <div class="row">

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

                                                <label for="class">Class (eg. JSS 1, NUR 1, STAGE 1,...)</label>

                                                <input type="text" class="form-control" id="class" name="class" oninput="this.value = this.value.toUpperCase()" required>

                                            </div>

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

                                                <label for="section">Arm (eg. A,B,GOLD,...)</label>

                                                <input type="text" class="form-control" id="section" name="section" oninput="this.value = this.value.toUpperCase()" required>

                                            </div>

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

                                                <label for="terminal">Terminal Class?</label>

                                                <select class="form-control" id="terminal" name="terminal">

                                                    <option value="0">No</option>

                                                    <option value="1">Yes</option>

                                                </select>

                                                <span  style="font-size: 12px; color: red;" class="hint">N:B:-The most Senior class in both Primary and Secondary should be the terminal class</span>



                                            </div>

                                        </div>

                                    </div>

                                    <div class=" form-group card-footer">

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

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </section>

        </div>

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

    </div>

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

</body>

</html>

