<?php
// Fetch student name if not in session
if (empty($student_name)) {
    $get_name = mysqli_query($connection, "SELECT name, class FROM subjects WHERE reg_number = '$student_reg_number' LIMIT 1");
    if ($get_name && mysqli_num_rows($get_name) > 0) {
        $student_data = mysqli_fetch_assoc($get_name);
        $student_name = $student_data['name'];
        $student_class = $student_data['class'];
    } else {
        $student_name = "Unknown Student";
    }
}

// Fetch results for the current term and session
$query = mysqli_query($connection, "
    SELECT 
        s.subjects, 
        r.ca1, 
        r.ca2, 
        r.exam, 
        r.subject_rank
    FROM subjects s
    LEFT JOIN results1 r 
        ON s.reg_number = r.reg_number
        AND s.subjects = r.subjects
        AND s.term = r.term
        AND s.session = r.session
    WHERE s.reg_number = '$student_reg_number'
      AND s.class = '$student_class'
      AND s.term = '$current_term'
      AND s.session = '$current_session'
    ORDER BY s.subjects ASC
");
?>

<div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Result Management</h1>
    <p>Student Termly Performance</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'>
          <h4 class='text-center text-uppercase'><?= htmlspecialchars($student_name) ?> (<?= htmlspecialchars($student_reg_number) ?>)</h4>
          <p class='text-center'>
            <b>CLASS:</b> <?= htmlspecialchars($student_class) ?> &nbsp;&nbsp;
            <b>TERM:</b> <?= htmlspecialchars($current_term) ?> &nbsp;&nbsp;
            <b>SESSION:</b> <?= htmlspecialchars($current_session) ?>
          </p>
        </div>
        <div class='panel-body'>
          <div class='table-responsive'>
            <table class='table table-bordered table-striped table-sm text-center'>
              <thead>
                <tr>
                  <th>S/N</th>
                  <th>Subject</th>
                  <th>CA1</th>
                  <th>CA2</th>
                  <th>Exam</th>
                  <th>Total</th>
                  <th>Rank</th>
                </tr>
              </thead>
              <tbody>
                <?php
                $count = 1;
                while ($row = mysqli_fetch_assoc($query)) {
                    $ca1 = (float)$row['ca1'];
                    $ca2 = (float)$row['ca2'];
                    $exam = (float)$row['exam'];
                    $total = $ca1 + $ca2 + $exam;

                    echo "
                      <tr>
                        <td>{$count}</td>
                        <td>{$row['subjects']}</td>
                        <td>{$ca1}</td>
                        <td>{$ca2}</td>
                        <td>{$exam}</td>
                        <td><b>{$total}</b></td>
                        <td>{$row['subject_rank']}</td>
                      </tr>
                    ";
                    $count++;
                }
                ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
