<?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 = $stmt->get_result()->fetch_assoc();

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

$parent_id = $parent['parent_id'];

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

/* ---------------- FILTERS ---------------- */
$student_id = $_GET['student_id'] ?? null;
$term       = $_GET['term'] ?? '';
$year       = $_GET['year'] ?? '';

/* ---------------- VALIDATE STUDENT ---------------- */
$valid_student = false;

if ($student_id) {
    $check = $conn->prepare("
        SELECT student_id FROM students 
        WHERE student_id = ? AND parent_id = ?
    ");
    $check->bind_param("ii", $student_id, $parent_id);
    $check->execute();
    $valid_student = $check->get_result()->num_rows > 0;
}

/* ---------------- FETCH REPORT ---------------- */
$report = null;

if ($valid_student && $term && $year) {
    $stmt = $conn->prepare("
        SELECT * FROM transcripts
        WHERE student_id = ? AND term = ? AND year = ?
    ");
    $stmt->bind_param("iss", $student_id, $term, $year);
    $stmt->execute();
    $report = $stmt->get_result()->fetch_assoc();
}

/* ---------------- SUBJECT RESULTS ---------------- */
$subjects = [];

if ($valid_student && $term && $year) {

    $sql = "
        SELECT subject, grade
        FROM academic_history
        WHERE student_id = ? AND term = ? AND year = ?
    ";

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

    if (!$stmt) {
        die("Prepare failed: " . $conn->error);
    }

    $stmt->bind_param("iss", $student_id, $term, $year);
    $stmt->execute();
    $subjects = $stmt->get_result();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Performance Reports</title>

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

<style>
body {
    background: #f4f6fb;
}
.card-soft {
    background: #fff;
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.05);
}
</style>
</head>

<body>

<div class="container mt-4">

    <h3 class="mb-4">📊 Performance Reports</h3>

    <!-- FILTER -->
    <div class="card-soft mb-4">
        <form method="GET" class="row g-3">

            <div class="col-md-4">
                <label>Select Child</label>
                <select name="student_id" class="form-control" required>
                    <option value="">-- Select --</option>
                    <?php while ($child = $children->fetch_assoc()): ?>
                        <option value="<?= $child['student_id'] ?>"
                            <?= ($student_id == $child['student_id']) ? 'selected' : '' ?>>
                            <?= htmlspecialchars($child['name']) ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>

            <div class="col-md-3">
                <label>Term</label>
                <select name="term" class="form-control" required>
                    <option value="">Select Term</option>
                    <option <?= $term=='1st Term'?'selected':'' ?>>1st Term</option>
                    <option <?= $term=='2nd Term'?'selected':'' ?>>2nd Term</option>
                    <option <?= $term=='3rd Term'?'selected':'' ?>>3rd Term</option>
                </select>
            </div>

            <div class="col-md-3">
                <label>Session (Year)</label>
                <input type="text" name="year" class="form-control" placeholder="2024/2025"
                       value="<?= htmlspecialchars($year) ?>" required>
            </div>

            <div class="col-md-2 d-flex align-items-end">
                <button class="btn btn-primary w-100">View</button>
            </div>

        </form>
    </div>

    <!-- REPORT -->
    <?php if ($report): ?>
        <div class="card-soft mb-4">

            <h5>Report Summary</h5>
            <hr>

            <div class="row">
                <div class="col-md-3"><strong>GPA:</strong> <?= $report['gpa'] ?></div>
                <div class="col-md-3"><strong>Position:</strong> <?= $report['class_position'] ?></div>
                <div class="col-md-3"><strong>Status:</strong> <?= $report['promotion_status'] ?></div>
                <div class="col-md-3"><strong>Term:</strong> <?= $report['term'] ?></div>
            </div>

        </div>

        <!-- SUBJECT TABLE -->
        <div class="card-soft">
            <h5>Subject Breakdown</h5>
            <hr>

            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Subject</th>
                        <th>Grade</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($sub = $subjects->fetch_assoc()): ?>
                        <tr>
                            <td><?= htmlspecialchars($sub['subject']) ?></td>
                            <td><?= $sub['grade'] ?></td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>

    <?php elseif ($student_id): ?>
        <div class="alert alert-warning">
            No report found for selected term and session.
        </div>
    <?php endif; ?>

</div>

</body>
</html>