<?php
    error_reporting(0);
    if(isset($_POST['promote_btn'])){
        for($j = 0; $j < (int)$_POST["total_num"]; $j++){
            $student_id = $_POST['student_id'][$j];
            $promote_to = $_POST['promote_to'];
            $student_initial_class = $_POST['student_initial_class'];
            
            $update_query = " UPDATE `students` SET class = '{$promote_to}' WHERE id = '{$student_id}' ";

            $run_update_query = mysqli_query($connection, $update_query);
        }
        if($run_update_query == true){
         $_SESSION['msg'] = "Students Promotion Successful";
        $_SESSION['type'] = "success";            
        }else{
         $_SESSION['msg'] = "Students Promotion Failed";
        $_SESSION['type'] = "error";
        }
            // Redirect once after logic
    header("Location: modules.php?promote_selected_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 Selected Students</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 Selection</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'>Promote From</span>
                                    <select name='view_student_in_class' 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>
                            <button type = 'submit' name='view_students_btn' class = 'btn btn-primary btn-md' style='margin-left:20px;'><span class= 'glyphicon glyphicon-user'></span>&nbsp; View Students</button><br/> 
                        </div>
                    </form>
                </div>
            </div>
            
           <?php
if (isset($_POST['view_students_btn'])) {
    $view_student_in_class = $_POST['view_student_in_class'];

    $query = "SELECT * FROM students WHERE class = '{$view_student_in_class}' AND status = 'active' ";
    $run_query = mysqli_query($connection, $query);

    if (mysqli_num_rows($run_query) > 0) {
        echo "<form name='myform' method='POST' action=''>";
        echo "<table id='myTable' class='table table-striped table-bordered'>";
        echo "<thead>
                <tr>
                    <th>#</th>
                    <th>Full Name</th>
                    <th>Class</th>
                    <th>Reg Number</th>
                    <th>Select</th>
                </tr>
              </thead>
              <tbody>";

        $i = 0;
        while ($result = mysqli_fetch_assoc($run_query)) {
            $i++;
            $id = $result['id'];
            $firstname = $result['firstname'];
            $lastname = $result['lastname'];
            $othername = $result['othername'];
            $class = $result['class'];
            $reg_no = $result['reg_number'];

            echo "<tr>
                    <td>{$i}</td>
                    <td><b>{$firstname} {$lastname} {$othername}</b></td>
                    <td>{$class}</td>
                    <td>{$reg_no}</td>
                    <td>
                        <input type='hidden' name='student_hidden_id[]' value='{$id}' />
                        <input type='hidden' name='student_initial_class' value='{$class}' />
                        <input type='checkbox' name='student_id[]' value='{$id}' class='form-control student-check' />
                    </td>
                  </tr>";
        }

        echo "</tbody></table>";

        // Promotion section
        echo "
        <div class='form-group'>
            <label for='selectall' id='selectControl'>Select All</label>
            <input type='checkbox' id='selectall' />
        </div>
        <div class='form-group'>
            <label>Promote To</label>
            <select name='promote_to' class='form-control'>
                <option>Select Class</option>
                <option>Graduated</option>";

        $query1 = "SELECT * FROM `classes`";
        $run_query1 = mysqli_query($connection, $query1);
        if (mysqli_num_rows($run_query1) > 0) {
            while ($result = mysqli_fetch_assoc($run_query1)) {
                $target_classes = $result['classes'];
                echo "<option>{$target_classes}</option>";
            }
        }

        echo "</select>
        </div>
        <input type='hidden' name='total_num' value='{$i}' />
        <button type='submit' name='promote_btn' class='btn btn-primary'>
            <span class='glyphicon glyphicon-user'></span> Promote
        </button>
        </form>";
    }
    else{
    	$_SESSION['msg'] = "No Student in Class";
        $_SESSION['type'] = "error";
        
                    // Redirect once after logic
    header("Location: modules.php?promote_selected_students");
    exit();    
        }

}
?>

<script type="text/javascript">
    $(document).ready(function () {
        // Initialize DataTable
        $('#myTable').DataTable();

        // Select All / Unselect All functionality
        $('#selectControl').on('click', function () {
            let checkboxes = $('.student-check');
            let isChecked = $('#selectall').prop('checked');

            checkboxes.prop('checked', isChecked);
            $(this).text(isChecked ? 'Unselect All' : 'Select All');
        });

        // If checkbox is toggled manually, sync label text
        $('#selectall').on('change', function () {
            $('#selectControl').text(this.checked ? 'Unselect All' : 'Select All');
        });
    });
</script>
