<?php

include('includes/session.php');

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];


// ✅ Get the student ID from the URL

$student_id = isset($_GET['student_id']) ? intval($_GET['student_id']) : 0;



// Check if student_id is valid

if ($student_id <= 0) {

  echo '<div class="alert alert-danger m-3">Invalid student ID.</div>';

  exit();

}

?>

<!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"); ?>



    <!-- Main Sidebar Container -->

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



    <!-- Content Wrapper -->

    <div class="content-wrapper">

      <!-- 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-book-open"></i>

                  </div>

                  <div>

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

                      Student Allocated Subjects

                      <span class="premium-subtitle text-secondary">Subjects Assigned</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="oldstdboard.php" class="text-primary">Home</a></li>

                    <li class="breadcrumb-item active" aria-current="page">Allocated Subjects</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 font-weight-bold">

                    <i class="fas fa-list"></i> Subjects Assigned to Student

                  </h5>

                </div>



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

                  <?php

                  // ✅ Fetch allocated subjects using the passed student_id

                  $query = $con->prepare("

                      SELECT subject_name, class, section, created_at 

                      FROM subject_allocations 

                      WHERE student_id = ? 

                      ORDER BY created_at DESC

                  ");

                  $query->bind_param("i", $student_id);

                  $query->execute();

                  $result = $query->get_result();



                  if ($result->num_rows > 0) {

                    ?>

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

                      <thead class="bg-primary text-white">

                        <tr>

                          <th>#</th>

                          <th>Subject Name</th>

                          <th>Class</th>

                          <th>Arm</th>

                        </tr>

                      </thead>

                      <tbody>

                        <?php

                        $cnt = 1;

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

                          ?>

                          <tr>

                            <td><?php echo $cnt++; ?></td>

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

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

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

                          </tr>

                        <?php } ?>

                      </tbody>

                    </table>

                    <?php

                  } else {

                    echo '<div class="alert alert-warning mt-3">

                            <i class="fas fa-info-circle"></i> No subjects have been allocated to this student yet.

                          </div>';

                  }

                  ?>

                </div>

              </div>

            </div>

          </div>

        </div>

      </section>

    </div>



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

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

  </div>

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

</body>



</html>