<?php
// === UPDATE STATUS FOR SELECTED STUDENTS ===
if (isset($_POST['update_status_btn'])) {
    $new_status = $_POST['new_status'];
    $student_ids = $_POST['student_id'] ?? [];

    if (!empty($student_ids)) {
        foreach ($student_ids as $student_id) {
            $student_id = intval($student_id);
            $update_query = "UPDATE students SET status = '{$new_status}' WHERE id = '{$student_id}'";
            mysqli_query($connection, $update_query);
        }
        $_SESSION['msg'] = "Student status updated successfully!";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "No student selected.";
        $_SESSION['type'] = "warning";
    }
    header("Location: manage_student_status");
    exit();
}
?>

<div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Student Management</h1>
    <p>Manage Student Status</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'>Manage Status 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'>Select Class</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);
                      while ($result = mysqli_fetch_assoc($run_query)) {
                        echo "<option>{$result['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>
            </div>
          </form>
        </div>
      </div>

<?php
// === DISPLAY STUDENTS IN SELECTED CLASS ===
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}'";
    $run_query = mysqli_query($connection, $query);

    if (mysqli_num_rows($run_query) > 0) {
        echo "<form 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>Status</th>
                    <th>Select</th>
                </tr>
              </thead>
              <tbody>";

        $i = 0;
        while ($result = mysqli_fetch_assoc($run_query)) {
            $i++;
            echo "<tr>
                    <td>{$i}</td>
                    <td><b>{$result['firstname']} {$result['lastname']} {$result['othername']}</b></td>
                    <td>{$result['class']}</td>
                    <td>{$result['reg_number']}</td>
                    <td><span class='label label-info'>".ucfirst($result['status'])."</span></td>
                    <td>
                        <input type='checkbox' name='student_id[]' value='{$result['id']}' class='form-control student-check' />
                    </td>
                  </tr>";
        }

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

        // Status change section
        echo "
        <div class='form-group'>
            <label>Change Status To</label>
            <select name='new_status' class='form-control' required>
                <option value=''>-- Select Status --</option>
                <option value='active'>Active</option>
                <option value='suspended'>Suspended</option>
                <option value='blocked'>Blocked</option>
                <option value='archived'>Archived (Graduate)</option>
            </select>
        </div>
        <button type='submit' name='update_status_btn' class='btn btn-primary'>
            <span class='glyphicon glyphicon-ok'></span> Update Status
        </button>
        </form>";
    } else {
        $_SESSION['msg'] = "No student in selected class.";
        $_SESSION['type'] = "error";
        header("Location: manage_student_status");
        exit();
    }
}
?>

<script type="text/javascript">
    // Select All toggle
    $('#selectControl').on('click', function () {
        let isChecked = $('#selectall').prop('checked');
        $('.student-check').prop('checked', isChecked);
        $(this).text(isChecked ? 'Unselect All' : 'Select All');
    });

    $('#selectall').on('change', function () {
        $('#selectControl').text(this.checked ? 'Unselect All' : 'Select All');
    });
});
</script>
