<?php

// POST ACTION TO ADD NEW SESSION INTO DATABASE
if (isset($_POST['add_session_btn'])) {
    $add_session = inject_checker($connection, $_POST['add_session']);

    if (empty($add_session)) {
        $_SESSION['msg'] = "You need to type the session you want to add.";
        $_SESSION['type'] = "warning";
    } else {
        $query = "SELECT * FROM `sessions` WHERE `sessions` = '{$add_session}'";
        $run_query = mysqli_query($connection, $query);

        if (mysqli_num_rows($run_query) > 0) {
            $_SESSION['msg'] = "Sorry, {$add_session} session has already been added.";
            $_SESSION['type'] = "warning";
        } else {
            $query = "INSERT INTO `sessions`(`sessions`, `date_added`) VALUES('{$add_session}', '$date')";
            $run_query = mysqli_query($connection, $query);

            if ($run_query) {
                $_SESSION['msg'] = "New session successfully added.";
                $_SESSION['type'] = "success";
            } else {
                $_SESSION['msg'] = "Failed to add new session.";
                $_SESSION['type'] = "error";
            }
        }        
    }
            header("Location: modules.php?current_session");
	    exit();
}

// POST ACTION TO SPECIFY CURRENT TERM
if (isset($_POST['current_term_btn'])) {
    $current_term = inject_checker($connection, $_POST['current_term']);

    if ($current_term == "Select Current Term") {
        $_SESSION['msg'] = "Error: Please select a term to change current term.";
        $_SESSION['type'] = "warning";
    } else {
        $query = "UPDATE `current_season` SET `current_term` = '{$current_term}'";
        $run_query = mysqli_query($connection, $query);

        if ($run_query) {
            $_SESSION['msg'] = "Current term successfully updated.";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "Current term update failed.";
            $_SESSION['type'] = "error";
        }
    }
            header("Location: modules.php?current_session");
	    exit();
}

// POST ACTION TO SPECIFY CURRENT SESSION
if (isset($_POST['current_session_btn'])) {
    $current_session = inject_checker($connection, $_POST['current_session']);

    if ($current_session === "Select Current Session") {
        $_SESSION['msg'] = "Error: Please select a session to change current session.";
        $_SESSION['type'] = "warning";
    } else {
        $query = "UPDATE `current_season` SET `current_session` = '{$current_session}'";
        $run_query = mysqli_query($connection, $query);

        if ($run_query) {
            $_SESSION['msg'] = "Current session successfully updated.";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "Current session update failed.";
            $_SESSION['type'] = "error";
        }
    }
            header("Location: modules.php?current_session");
	    exit();
}

// DISPLAY CURRENT TERM AND SESSION
$query = "SELECT * FROM `current_season`";
$run_query = mysqli_query($connection, $query);
if (mysqli_num_rows($run_query) == 1) {
    $result = mysqli_fetch_assoc($run_query);
    $show_current_term = $result['current_term'];
    $show_current_session = $result['current_session'];
}
?>

<!-- Combined HTML Output -->
<div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Administrative Manager</h1>
    <p>Session and Term Management</p>
  </div>
</div>

<div class='card'>
    <form action='' method='POST'>
    
        <div class='panel panel-primary ch'>
            <div class='panel-heading text-center'>
                <h4>Current Term = <?php echo $show_current_term ?? 'Not Set'; ?> <br /> Current Session = <?php echo $show_current_session ?? 'Not Set'; ?></h4>
            </div>
            <div class='panel-body'>
            	<!-- Add New Session Modal Trigger Button -->
<div class="text-right mb-3">
    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addSessionModal">
        <i class="fa fa-plus"></i> Add New Session
    </button>
</div>

<!-- Add Session Modal -->
<div class="modal fade" id="addSessionModal" tabindex="-1" role="dialog" aria-labelledby="addSessionModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <form method="POST" action="">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="addSessionModalLabel">Add New Session</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <label for="add_session">Enter New Session:</label>
          <input type="text" name="add_session" class="form-control" id="add_session" required placeholder="e.g., 2025/2026">
        </div>
        <div class="modal-footer">
          <button type="submit" name="add_session_btn" class="btn btn-primary">Save Session</button>
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        </div>
      </div>
    </form>
  </div>
</div>

                <div class='row'>

                    <!-- Term Selection -->
                    <div class='col-md-6'>
                        <table class='table table-striped'>
                            <tr>
                                <td><b>Change Current Term</b></td>
                                <td>
                                    <select name='current_term' class='form-control'>
                                        <option selected>Select Current Term</option>
                                        <option>First Term</option>
                                        <option>Second Term</option>
                                        <option>Third Term</option>
                                    </select>
                                </td>
                            </tr>
                        </table>
                        <button name='current_term_btn' class='btn btn-primary' style='border-radius:10px;' type='submit'>Save Term</button>
                    </div>

                    <!-- Session Selection -->
                    <div class='col-md-6'>
                        <table class='table table-striped'>
                            <tr>
                                <td><b>Change Current Session</b></td>
                                <td>
                                    <select name='current_session' class='form-control'>
                                        <option selected>Select Current Session</option>
                                        <?php
                                            $query = "SELECT * FROM `sessions`";
                                            $run_query = mysqli_query($connection, $query);
                                            if(mysqli_num_rows($run_query) > 0){
                                                while($result = mysqli_fetch_assoc($run_query)){
                                                    $all_sessions = $result['sessions'];
                                                    echo "<option>{$all_sessions}</option>";
                                                }
                                            }
                                        ?>
                                    </select>
                                </td>
                            </tr>
                        </table>
                        <button name='current_session_btn' class='btn btn-primary' style='border-radius:10px;' type='submit'>Save Session</button>
                    </div>

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

