<?php
if (isset($_POST['promote_students_btn'])) {
    $promote_student_from = inject_checker($connection, $_POST['promote_student_from']);
    $promote_student_to = inject_checker($connection, $_POST['promote_student_to']);
    $session = inject_checker($connection, $_POST['session']);

    if ($promote_student_from == "Select Class") {
        $_SESSION['msg'] = "Please Select Class to Promote From.";
        $_SESSION['type'] = "warning";
    } elseif ($promote_student_to == "Select Class") {        
        $_SESSION['msg'] = "Please Select Class to Promote To";
        $_SESSION['type'] = "warning";
    } elseif ($promote_student_from == $promote_student_to) {
        $_SESSION['msg'] = "You cannot promote students to the same Class";
        $_SESSION['type'] = "error";
    } else {
        if ($promote_student_to == "Graduate") {
            // Archive students
            $update_query = "UPDATE `students` 
                             SET `status` = 'archived', `class` = 'Graduate' 
                             WHERE `class` = '{$promote_student_from}' ";
        } else {
            // Normal class promotion
            $update_query = "UPDATE `students` 
                             SET `class` = '{$promote_student_to}' 
                             WHERE `class` = '{$promote_student_from}' ";
        }

        $run_update_query = mysqli_query($connection, $update_query);
        if ($run_update_query) {               
            $_SESSION['msg'] = "Class Promotion Successful";
            $_SESSION['type'] = "success";
        } else {                
            $_SESSION['msg'] = "Class Promotion Failed";
            $_SESSION['type'] = "error";
        }
    }
    header("Location: promote_students");
    exit();
}
?>


<?php
///////////// POST ACTION TO DISPLAY CURRENT SESSION //////////////
$query = " SELECT * FROM `current_season` ";
$run_query = mysqli_query($connection, $query);
if (mysqli_num_rows($run_query) == 1) {
    while ($result = mysqli_fetch_assoc($run_query)) {
        $show_current_session = $result['current_session'];
    }
}
?>

<div class='page-title'>
    <div>
        <h1><i class='fa fa-laptop'></i> Student Management</h1>
        <p>Promote Student</p>
    </div>
</div>

<div class="card">
    <div class='row'>
        <div class='col-xs-12 col-sm-12 col-md-12'>
            <div class='panel panel-primary ch'>
                <div class='panel-heading'>
                    <p class = 'text-center'> Promotion by Class</p>
                </div>
                <br />

                <div class='panel-body'>
                    <form method='POST' action=''>
                        <div class='row'>
                            <div class='col-xs-12 col-sm-12 col-md-4'>
                                <div class='input-group'>
                                    <span class='input-group-addon' id='sizing-addon2'>CLASS FROM</span>
                                    <select name='promote_student_from' class='form-control'>
                                        <option>Select Class</option>
                                        <?php
                                        $query = " SELECT * FROM `classes` ";
                                        $run_query = mysqli_query($connection, $query);
                                        if (mysqli_num_rows($run_query) > 0) {
                                            while ($result = mysqli_fetch_assoc($run_query)) {
                                                $target_classes = $result['classes'];
                                                echo "
                                                        <option>{$target_classes}</option>
                                                    ";
                                            }
                                        }
                                        ?>
                                    </select>
                                </div>
                                <br />
                            </div>
                            <div class='col-xs-12 col-sm-12 col-md-4'>
                                <div class='input-group'>
                                    <span class='input-group-addon' id='sizing-addon2'>CLASS TO:</span>
                                    <select name='promote_student_to' class='form-control'>
                                        <option>Select Class</option>
                                        <?php
                                        $query = " SELECT * FROM `classes` ";
                                        $run_query = mysqli_query($connection, $query);
                                        if (mysqli_num_rows($run_query) > 0) {
                                            while ($result = mysqli_fetch_assoc($run_query)) {
                                                $target_classes = $result['classes'];
                                                echo "
                                                        <option>{$target_classes}</option>
                                                    ";
                                            }
                                        }
                                        ?>
                                        <option>Graduate</option>
                                    </select>
                                </div>
                                <br />
                            </div>
                            <div class='col-xs-12 col-sm-12 col-md-4'>
                                <div class='input-group'>
                                    <span class='input-group-addon' id='sizing-addon2'>Session:</span>
                                    <select class='form-control' name='session'>
                                        <option><?php echo $show_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>
                                </div>
                                <br />
                            </div>
                            <br />
                            <button type='submit' name='promote_students_btn' class='btn btn-primary btn-md' style='margin-left:20px;'><span class='glyphicon glyphicon-user'></span>&nbsp Promote Students</button><br />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>