<?php
session_start();
require 'db_connection.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: parent_login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

/* ===============================
   GET PARENT
================================*/
$stmt = $conn->prepare("SELECT parent_id FROM parents WHERE user_id=?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$res = $stmt->get_result();
$parent = $res->fetch_assoc();

if (!$parent) {
    die("Parent account not found.");
}

$parent_id = $parent['parent_id'];

/* ===============================
   GET CHILDREN
================================*/
$stmt = $conn->prepare("
    SELECT student_id, name 
    FROM students 
    WHERE parent_id=?
");
$stmt->bind_param("i", $parent_id);
$stmt->execute();
$children_res = $stmt->get_result();

$children = [];
while ($row = $children_res->fetch_assoc()) {
    $children[] = $row;
}

/* ===============================
   ACTIVE STUDENT
================================*/
$student_id = $_GET['student_id'] ?? ($children[0]['student_id'] ?? 0);

if ($student_id == 0) {
    die("<div style='padding:20px'>No student found for this parent.</div>");
}

/* ===============================
   GET ENROLLMENTS (SAFE QUERY)
================================*/
$sql = "
    SELECT 
        e.enrollment_id,
        e.term,
        e.session,
        e.enrollment_date,
        s.subject_name,
        s.section
    FROM enrollments e
    JOIN subject s ON e.subject_id = s.subject_id
    WHERE e.student_id = ?
";

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

if (!$stmt) {
    die("SQL PREPARE FAILED: " . $conn->error . "<br>QUERY: " . $sql);
}

$stmt->bind_param("i", $student_id);
$stmt->execute();
$enrollments = $stmt->get_result();

?>

<!DOCTYPE html>
<html>
<head>
<title>Subjects Enrollment</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<style>
body{
    background:#f4f6fb;
    font-family:Segoe UI;
}

/* SIDEBAR */
.sidebar{
    width:260px;
    height:100vh;
    position:fixed;
    background:#111827;
    color:#fff;
    padding:20px;
}

.sidebar a{
    display:block;
    padding:10px;
    color:#ccc;
    text-decoration:none;
    border-radius:8px;
    margin-bottom:5px;
}

.sidebar a:hover{
    background:#1f2937;
    color:#fff;
}

/* CONTENT */
.content{
    margin-left:280px;
    padding:20px;
}

/* HEADER */
.header{
    background:linear-gradient(135deg,#3a57e8,#5b7cfa);
    color:#fff;
    padding:15px 20px;
    border-radius:12px;
}

/* CARD */
.card-soft{
    background:#fff;
    border-radius:14px;
    padding:15px;
    box-shadow:0 6px 18px rgba(0,0,0,0.06);
}

/* CHILD BOX */
.child-box{
    background:#fff;
    border-radius:12px;
    padding:10px;
    margin-bottom:10px;
    transition:0.2s;
}

.child-box:hover{
    background:#eef2ff;
}

.active-child{
    background:#3a57e8;
    color:#fff;
}

/* SUBJECT CARD */
.subject-card{
    text-align:center;
    padding:15px;
    border-radius:12px;
    background:#fff;
    box-shadow:0 4px 12px rgba(0,0,0,0.05);
    transition:0.2s;
}

.subject-card:hover{
    transform:translateY(-4px);
}

.section-badge{
    background:#e0e7ff;
    color:#3a57e8;
    padding:4px 10px;
    border-radius:20px;
    font-size:12px;
}
</style>
</head>

<body>

<!-- SIDEBAR -->
<div class="sidebar">
<h4>Parent Portal</h4>

<a href="parent_dashboard.php"><i class="fas fa-home"></i> Dashboard</a>
<a href="enrollments.php"><i class="fas fa-book"></i> Subjects</a>
<a href="transcript.php"><i class="fas fa-file-alt"></i> Transcript</a>
<a href="notifications.php"><i class="fas fa-bell"></i> Notifications</a>
<a href="payment.php"><i class="fas fa-wallet"></i> Payments</a>
</div>

<!-- CONTENT -->
<div class="content">

<!-- HEADER -->
<div class="header mb-4">
<h4><i class="fas fa-book"></i> Subjects Enrollment</h4>
<p class="mb-0">View all subjects your child(ren) are enrolled in</p>
</div>

<div class="row">

<!-- CHILD LIST -->
<div class="col-md-3">

<h6>Your Children</h6>

<?php foreach($children as $c): ?>
<a href="enrollments.php?student_id=<?= $c['student_id'] ?>">
    <div class="child-box <?= ($student_id == $c['student_id']) ? 'active-child' : '' ?>">
        <i class="fas fa-user"></i>
        <?= htmlspecialchars($c['name']) ?>
    </div>
</a>
<?php endforeach; ?>

</div>

<!-- ENROLLMENTS -->
<div class="col-md-9">

<div class="card-soft">
<h5>Enrolled Subjects</h5>
<hr>

<?php if ($enrollments->num_rows > 0): ?>

<div class="row">

<?php while ($e = $enrollments->fetch_assoc()): ?>

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

<div class="subject-card">

<i class="fas fa-book-open fa-2x text-primary"></i>

<h6 class="mt-2"><?= htmlspecialchars($e['subject_name']) ?></h6>

<span class="section-badge">
<?= htmlspecialchars($e['section'] ?? 'General') ?>
</span>

<hr>

<small class="text-muted">
Term: <?= htmlspecialchars($e['term']) ?><br>
Session: <?= htmlspecialchars($e['session']) ?>
</small>

</div>

</div>

<?php endwhile; ?>

</div>

<?php else: ?>

<div class="text-center text-muted p-4">
<i class="fas fa-info-circle"></i><br>
No subjects enrolled for this student yet.
</div>

<?php endif; ?>

</div>

</div>

</div>

</body>
</html>