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

/* ================= AUTH ================= */
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();
$parent_id = $stmt->get_result()->fetch_assoc()['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 = $stmt->get_result();

/* ================= SELECT CHILD ================= */
$student_id = $_GET['student_id'] ?? null;

if (!$student_id) {
    $first = $children->fetch_assoc();
    $student_id = $first['student_id'] ?? 0;
    $children->data_seek(0);
}

/* ================= SECURITY CHECK ================= */
$stmt = $conn->prepare("
SELECT s.*, c.class_name 
FROM students s
JOIN classes c ON s.class_id = c.class_id
WHERE s.student_id=? AND s.parent_id=?
");
$stmt->bind_param("ii", $student_id, $parent_id);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();

if (!$student) {
    die("Unauthorized access.");
}

/* ================= TRANSCRIPT SUMMARY ================= */
$stmt = $conn->prepare("
SELECT * FROM transcripts
WHERE student_id=?
ORDER BY year DESC, term DESC
LIMIT 1
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$summary = $stmt->get_result()->fetch_assoc();

/* ================= SUBJECT RESULTS ================= */
$stmt = $conn->prepare("
SELECT ah.*, sub.subject_name
FROM academic_history ah
JOIN subject sub ON ah.subject = sub.subject_name
WHERE ah.student_id = ?
ORDER BY ah.year DESC, ah.term DESC
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$results = $stmt->get_result();
?>

<!DOCTYPE html>
<html>
<head>
<title>Transcript</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

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

/* CONTAINER */
.wrapper{
    max-width:1000px;
    margin:30px auto;
}

/* CHILD SWITCH */
.child-btn{
    padding:8px 15px;
    border-radius:20px;
    background:#e5e7eb;
    margin-right:5px;
    text-decoration:none;
    color:#111;
    display:inline-block;
}

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

/* CARD */
.card-box{
    background:#fff;
    border-radius:14px;
    padding:25px;
    box-shadow:0 8px 30px rgba(0,0,0,0.08);
}

/* HEADER */
.header{
    text-align:center;
    border-bottom:2px solid #3a57e8;
    margin-bottom:20px;
    padding-bottom:10px;
}

/* TABLE */
.table th{
    background:#3a57e8;
    color:#fff;
    text-align:center;
}

.table td{
    text-align:center;
}

/* BADGE */
.status{
    padding:6px 12px;
    border-radius:20px;
    color:#fff;
}

.pass{ background:#22c55e; }
.fail{ background:#ef4444; }
</style>
</head>

<body>

<div class="wrapper">

<!-- CHILD SELECTOR -->
<div class="mb-3">
<?php while($c = $children->fetch_assoc()): ?>
    <a href="?student_id=<?= $c['student_id'] ?>"
       class="child-btn <?= ($c['student_id']==$student_id?'active-child':'') ?>">
        👤 <?= htmlspecialchars($c['name']) ?>
    </a>
<?php endwhile; ?>
</div>

<div class="card-box">

<!-- HEADER -->
<div class="header">
    <h4>📄 OFFICIAL TRANSCRIPT</h4>
</div>

<!-- STUDENT INFO -->
<div class="row mb-3">
    <div class="col-md-6">
        <strong>Name:</strong> <?= htmlspecialchars($student['name']) ?><br>
        <strong>Reg No:</strong> <?= htmlspecialchars($student['regNumber']) ?>
    </div>

    <div class="col-md-6 text-end">
        <strong>Class:</strong> <?= htmlspecialchars($student['class_name']) ?><br>
        <strong>Term:</strong> <?= $summary['term'] ?? '-' ?><br>
        <strong>Year:</strong> <?= $summary['year'] ?? '-' ?>
    </div>
</div>

<!-- SUBJECT TABLE -->
<table class="table table-bordered">

<tr>
<th>Subject</th>
<th>Grade</th>
<th>Term</th>
<th>Year</th>
</tr>

<?php if($results->num_rows > 0): ?>
<?php while($row = $results->fetch_assoc()): ?>
<tr>
<td><?= htmlspecialchars($row['subject_name']) ?></td>
<td><?= htmlspecialchars($row['grade']) ?></td>
<td><?= htmlspecialchars($row['term']) ?></td>
<td><?= htmlspecialchars($row['year']) ?></td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr><td colspan="4">No records found</td></tr>
<?php endif; ?>

</table>

<!-- SUMMARY -->
<div class="row mt-3">
    <div class="col-md-4">
        <strong>GPA:</strong> <?= $summary['gpa'] ?? '-' ?>
    </div>

    <div class="col-md-4">
        <strong>Position:</strong> <?= $summary['class_position'] ?? '-' ?>
    </div>

    <div class="col-md-4">
        <strong>Status:</strong>
        <span class="status <?= ($summary['promotion_status']=='Promoted'?'pass':'fail') ?>">
            <?= $summary['promotion_status'] ?? '-' ?>
        </span>
    </div>
</div>

<!-- VERIFY -->
<div class="mt-4">
    <strong>Verification:</strong><br>
    <a href="<?= $summary['verify_url'] ?? '#' ?>" target="_blank">
        <?= $summary['verify_url'] ?? 'N/A' ?>
    </a>
</div>

</div>

</div>

</body>
</html>