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

$show_all_class_teachers = isset($_POST['show_all_class_teachers']) && $_POST['show_all_class_teachers'] == 1;
$teacher_id = isset($_POST['teacher_id']) && $_POST['teacher_id'] !== '' ? intval($_POST['teacher_id']) : 0;
$subject_name = isset($_POST['subject_name']) && $_POST['subject_name'] !== '' ? trim($_POST['subject_name']) : '';

if ($show_all_class_teachers) {
  // ✅ Fetch all class teachers and their assigned classes
  $query = "
    SELECT 
      u.id AS teacher_id,
      CONCAT(u.title, ' ', u.name, ' ', u.lastname) AS teacher_name,
      cs.class,
      cs.section
    FROM teacher_assignments ta
    INNER JOIN tblusers u ON ta.teacher_id = u.id
    INNER JOIN class_section cs ON ta.class_id = cs.id
    WHERE ta.is_class_teacher = 1
    ORDER BY cs.class ASC, cs.section ASC
  ";
} elseif ($teacher_id > 0) {
  // ✅ Search by teacher
  $teacher_id_safe = intval($teacher_id);
  $query = "
    SELECT 
      ta.id,
      ta.is_class_teacher,
      s.Subject_name,
      s.Class AS sub_class,
      s.section AS sub_section,
      cs.class AS class_class,
      cs.section AS class_section,
      u.title, u.name, u.lastname
    FROM teacher_assignments ta
    LEFT JOIN subjectcombination s ON ta.subject_id = s.id
    LEFT JOIN class_section cs ON ta.class_id = cs.id
    LEFT JOIN tblusers u ON ta.teacher_id = u.id
    WHERE ta.teacher_id = $teacher_id_safe
    ORDER BY s.Subject_name ASC, class_class ASC
  ";
} elseif (!empty($subject_name)) {
  // ✅ Search by subject
  $subject_esc = mysqli_real_escape_string($con, $subject_name);
  $query = "
    SELECT 
      ta.id,
      ta.is_class_teacher,
      s.Subject_name,
      s.Class AS sub_class,
      s.section AS sub_section,
      cs.class AS class_class,
      cs.section AS class_section,
      u.title, u.name, u.lastname
    FROM teacher_assignments ta
    LEFT JOIN subjectcombination s ON ta.subject_id = s.id
    LEFT JOIN class_section cs ON ta.class_id = cs.id
    LEFT JOIN tblusers u ON ta.teacher_id = u.id
    WHERE s.Subject_name LIKE '%$subject_esc%'
    ORDER BY u.name ASC, class_class ASC
  ";
} else {
  echo '<tr><td colspan="6" class="text-center text-muted">Please select a teacher or subject.</td></tr>';
  exit;
}

$result = mysqli_query($con, $query);

if ($result && mysqli_num_rows($result) > 0) {
  $cnt = 1;
  while ($row = mysqli_fetch_assoc($result)) {
    if ($show_all_class_teachers) {
      // ✅ When “Show All Class Teachers” is clicked
      $teacher_name = htmlentities($row['teacher_name']);
      $class_display = htmlentities($row['class'] . ' ' . $row['section']);
      echo '<tr>';
      echo '<td>' . $cnt++ . '</td>';
      echo '<td>' . $teacher_name . '</td>';
      echo '<td>' . $class_display . '</td>';
      echo '<td><span class="badge badge-success">Yes</span></td>';
      echo '</tr>';
    } else {
      // ✅ Normal teacher/subject search
      $class_display = '';
      if (!empty($row['sub_class']) && !empty($row['sub_section'])) {
        $class_display = $row['sub_class'] . ' ' . $row['sub_section'];
      } else {
        $class_display = trim(($row['class_class'] ?? '') . ' ' . ($row['class_section'] ?? ''));
      }
      $subject = $row['Subject_name'] ?? '-';
      $teacher_name = trim(($row['title'] ?? '') . ' ' . ($row['name'] ?? '') . ' ' . ($row['lastname'] ?? ''));

      echo '<tr>';
      echo '<td>' . $cnt++ . '</td>';
      echo '<td>' . htmlentities($teacher_name) . '</td>';
      echo '<td>' . htmlentities($class_display ?: '-') . '</td>';
      echo '<td>' . htmlentities($subject) . '</td>';
      echo '<td>' . ($row['is_class_teacher'] ? '<span class="badge badge-success">Yes</span>' : '<span class="badge badge-secondary">No</span>') . '</td>';
      echo '</tr>';
    }
  }
} else {
  echo '<tr><td colspan="6" class="text-center text-muted">No records found.</td></tr>';
}
?>
