<?php

include('includes/session.php');

error_reporting(0);

include('includes/dbconnection.php');

require_once __DIR__ . '/includes/session_term_helper.php';
// Fetch current session and term (with gap/fallback support)
$stResult = resolveCurrentSessionTerm($con);
$currentSessionId   = $stResult['session_id'];
$currentSessionName = $stResult['session_name'];
$currentTermId      = $stResult['term_id'];
$currentTermName    = $stResult['term_name'];
$studentId = $_SESSION['student_id'];







$studentId = $_SESSION['student_id'];

// fetch student details for display

$stmt = $con->prepare("SELECT studentName, email FROM students WHERE id = ?");

$stmt->bind_param("i", $studentId);

$stmt->execute();

$student = $stmt->get_result()->fetch_assoc();

$stmt->close();



$fullname = trim($student['studentName']);

$email = $student['email'];





// Heal the database: Link old name-only payments to this student's new ID

$updateSql = "UPDATE payment SET student_id = ? 

              WHERE studentName = ? AND (student_id IS NULL OR student_id = 0)";

$updateStmt = $con->prepare($updateSql);

if ($updateStmt) {

    $updateStmt->bind_param("is", $studentId, $fullname);

    $updateStmt->execute();

    $updateStmt->close();

}

?>



<!DOCTYPE html>

<html>

<?php @include("includes/head.php"); ?>

<body class="hold-transition sidebar-mini layout-fixed">

  <div class="wrapper">

    <!-- Navbar -->

    <?php @include("includes/headerstd.php"); ?>

    <!-- /.navbar -->



    <!-- Main Sidebar Container -->

    <?php @include("includes/sidebarstdboard.php"); ?>



    <!-- Content Wrapper. Contains page content -->

    <div class="content-wrapper">

      <!-- Content Header (Page header) -->

                         <section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

                <div class="col-md-8">

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-wallet"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                            Payment Management

                                <span class="premium-subtitle text-secondary">Manage Payment</span>

                            </h1>
                    <div class="mt-1">
                      <?php if ($currentSessionName): ?>
                        <span class="badge badge-primary mr-1">
                          <i class="fas fa-calendar-alt"></i>
                          <?= htmlspecialchars($currentSessionName) ?>
                        </span>
                      <?php endif; ?>
                      <?php if ($currentTermName): ?>
                        <span class="badge badge-success">
                          <i class="fas fa-bookmark"></i>
                          <?= htmlspecialchars($currentTermName) ?>
                        </span>
                      <?php else: ?>
                        <span class="badge badge-warning">
                          <i class="fas fa-exclamation-triangle"></i> No active term
                        </span>
                      <?php endif; ?>
                    </div>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="newstdboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Payment History</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section> 



      <!-- Main content -->

      <section class="content">

        <div class="container-fluid">

          <div class="row">

            <div class="col-12">

              <div class="card card-primary">

                <div class="card-header">

                  <h5 class="card-title"> <i class="fas fa-history"></i> Recent Payment</h5>

                  <div class="card-tools">

                  </div>

                </div>



                <div class="card-body mt-2">

                  <table id="example1" class="table table-bordered table-hover">

                    <thead> 

                      <tr> 

                        <th>#</th>

                        <th>Admission No.</th>

                        <th>Student Name</th>

                        <th>Payment ID</th>

                        <th>Class</th>

                        <th>Payment For</th>

                        <th>Amount</th>

                        <th>Payment Date</th>

                        <th>Action</th>

                      </tr> 

                    </thead> 

                    <tbody>

                    <?php

                    $cnt = 1;

                    $sql = "SELECT * FROM payment WHERE student_id = ? order by payment_date desc";

                    $stmt = $con->prepare($sql);

                    $stmt->bind_param("i", $studentId);

                    $stmt->execute();

                    $query = $stmt->get_result();

                    while ($row = $query->fetch_assoc()) {

                        ?>

                        <tr>

                            <td><?php echo htmlentities($cnt); ?></td>

                            <td><?php echo htmlentities($row['studentno']); ?></td>

                            <td><?php echo htmlentities($row['studentName']); ?></td>

                            <td><?php echo htmlentities($row['pay_id']); ?></td>

                            <td><?php echo htmlentities($row['class']); ?></td>

                            <td><?php echo htmlentities($row['pay_for']); ?></td>

                            <td><?php echo htmlentities($row['amount']); ?></td>

                            <td><?php echo date("M d, Y H:i A", strtotime($row['payment_date'])); ?></td>

                            <td>

                              <a href="downloadreceipt.php?id=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm"><i class="fa fa-download"></i> Download</a>

                            </td>

                        </tr>

                        <?php $cnt++;

                    } ?>

                    </tbody>

                  </table>

                </div>

                <!-- /.card-body -->

              </div>

              <!-- /.card -->

            </div>

            <!-- /.col -->

          </div>

          <!-- /.row -->

        </div>

        <!-- /.container-fluid -->

      </section>

      <!-- /.content -->

    </div>

    <!-- /.content-wrapper -->

    <?php @include("includes/footer.php"); ?>



    <!-- Control Sidebar -->

    <aside class="control-sidebar control-sidebar-dark">

      <!-- Control sidebar content goes here -->

    </aside>

    <!-- /.control-sidebar -->

  </div>



  <!-- ./wrapper -->

  <?php @include("includes/foot.php"); ?>

</body>

</html>

