<div class="page-title hidden-print">
  <div>
    <h1><i class="fa fa-money"></i> View a Student Fee Record</h1>
  </div>
</div>

<div class="card">
  <div class="row">
    <div class="col-md-12">
      <div class="panel panel-primary ch">
        <div class="panel-heading">
          <h4 class="txt">View Student Fee Record</h4>
        </div>
        <br />
        <div class="panel-body">
          <form method="POST" action="">
            <div class="row">
              <div class="col-md-8">
                <div class="input-group">
                  <span class="input-group-addon">Reg No</span>
                  <input type="text" name="reg_no" class="form-control" required>
                </div>
                <br />
              </div>
              <div class="col-md-2">
                <button type="submit" name="view_btn" class="btn btn-primary" style="margin-left:20px;">
                  <span class="glyphicon glyphicon-user"></span>&nbsp; View
                </button>
                <br />
              </div>
            </div>
          </form>
          <br />

          <?php
          if (isset($_POST['view_btn'])) {
              $reg_no = trim($_POST['reg_no']);

              if (empty($reg_no)) {
                  $_SESSION['msg'] = "Please type a registration number.";
                  $_SESSION['type'] = "warning";
              } else {
                  // Get student info
                  $stmt = $connection->prepare("SELECT student_name, student_class FROM students WHERE reg_number = ?");
                  $stmt->bind_param("s", $reg_no);
                  $stmt->execute();
                  $result = $stmt->get_result();

                  if ($result->num_rows > 0) {
                      $student = $result->fetch_assoc();
                      echo "<h5><strong>Student Name:</strong> {$student['student_name']}</h5>";
                      echo "<h5><strong>Class:</strong> {$student['student_class']}</h5>";
                  } else {
                      $_SESSION['msg'] = "Student not found!";
                      $_SESSION['type'] = "danger";
                  }
                  $stmt->close();

                  // Total fee calculation
                  $stmt = $connection->prepare("SELECT SUM(amount_paid) AS total_paid FROM fee_payment WHERE reg_number = ?");
                  $stmt->bind_param("s", $reg_no);
                  $stmt->execute();
                  $result = $stmt->get_result();
                  $total_fee_paid = $result->fetch_assoc()['total_paid'] ?? 0;
                  $total_fee_paid = number_format($total_fee_paid);

                  // Fetch all fee records
                  $stmt = $connection->prepare("SELECT * FROM fee_payment WHERE reg_number = ?");
                  $stmt->bind_param("s", $reg_no);
                  $stmt->execute();
                  $result = $stmt->get_result();

                  if ($result->num_rows > 0) {
                      echo "
                        <div class='table-responsive noprint'>
                          <table class='table table-bordered'>
                            <thead>
                              <tr class='bg-primary'>
                                <th>S/N</th>
                                <th>REG NUMBER</th>
                                <th>NAME</th>
                                <th>CLASS</th>
                                <th>STUDENT TYPE</th>
                                <th>TERM</th>
                                <th>SESSION</th>
                                <th>AMOUNT PAID (₦)</th>
                                <th>BALANCE (₦)</th>
                                <th>TELLER NO</th>
                                <th>DATE</th>
                                <th>STATUS</th>
                              </tr>
                            </thead>
                            <tbody>";

                      $i = 0;
                      while ($fee = $result->fetch_assoc()) {
                          $i++;
                          $status_icon = $fee['payment_status'] == "Cleared"
                              ? "<span class='text-success'><b>Cleared <span class='glyphicon glyphicon-ok'></span></b></span>"
                              : "<span class='text-danger'><b>Owing <span class='glyphicon glyphicon-remove'></span></b></span>";

                          echo "
                            <tr>
                              <td>{$i}</td>
                              <td>{$fee['reg_number']}</td>
                              <td>{$fee['student_name']}</td>
                              <td>{$fee['student_class']}</td>
                              <td>{$fee['student_type']}</td>
                              <td>{$fee['fee_term']}</td>
                              <td>{$fee['fee_session']}</td>
                              <td class='text-success'><b>" . number_format($fee['amount_paid']) . "</b></td>
                              <td class='text-danger'><b>" . number_format($fee['student_balance']) . "</b></td>
                              <td>{$fee['teller_no']}</td>
                              <td>{$fee['date_of_upload']}</td>
                              <td>{$status_icon}</td>
                            </tr>";
                      }

                      echo "
                            </tbody>
                            <tfoot>
                              <tr>
                                <th colspan='7'>Total Fees Recorded</th>
                                <th>{$total_fee_paid}</th>
                                <td colspan='4'></td>
                              </tr>
                            </tfoot>
                          </table>
                        </div>";
                  } else {
                      $_SESSION['msg'] = "No fee records found for this student.";
                      $_SESSION['type'] = "warning";
                  }
                  $stmt->close();
              }
          }
          ?>
        </div>
      </div>
    </div>
  </div>
</div>

