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

/* ================= AUTH ================= */
if (!isset($_SESSION['student_id']) || $_SESSION['role'] !== 'student') {
    header("Location: student_login.php?error=Unauthorized access");
    exit();
}

$student_id = $_SESSION['student_id'];

/* ================= STUDENT INFO ================= */
$stmt = $conn->prepare("
    SELECT s.name, s.class_id, c.class_name
    FROM students s
    JOIN classes c ON s.class_id = c.class_id
    WHERE s.student_id = ?
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();

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

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

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

<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: #f4f6f9;
    font-family: 'Segoe UI', sans-serif;
}

/* ================= SIDEBAR ================= */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 250px;
    height: 100vh;
    background: linear-gradient(180deg, #1f2d3d, #111827);
    color: white;
    padding-top: 20px;
}

.sidebar h4 {
    text-align: center;
    margin-bottom: 10px;
}

.sidebar p {
    text-align: center;
    font-size: 14px;
    color: #cbd5e1;
}

.sidebar a {
    display: block;
    padding: 12px 20px;
    color: white;
    text-decoration: none;
    transition: 0.3s;
    font-size: 15px;
}

.sidebar a:hover {
    background: #374151;
    padding-left: 25px;
}

.sidebar i {
    margin-right: 10px;
}

/* ================= MAIN ================= */
.main {
    margin-left: 250px;
    padding: 20px;
}

/* ================= CARD ================= */
.card {
    border: none;
    border-radius: 12px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.08);
}

.header {
    background: linear-gradient(135deg, #4e73df, #224abe);
    color: white;
    padding: 20px;
    border-radius: 12px;
}

.table th {
    background: #f1f3f6;
}
</style>
</head>

<body>

<!-- ================= SIDEBAR ================= -->
<div class="sidebar">
    <h4><i class="fas fa-user-graduate"></i></h4>
    <p><?php echo htmlspecialchars($student['name']); ?></p>

    <a href="student_dashboard.php"><i class="fas fa-home"></i> Dashboard</a>
    <a href="view_homework.php"><i class="fas fa-book"></i> Homework</a>
    <a href="view_results.php"><i class="fas fa-chart-line"></i> Results</a>
    <a href="view_attendance.php"><i class="fas fa-check-circle"></i> Attendance</a>
    <a href="view_fees.php"><i class="fas fa-wallet"></i> Fees</a>
    <a href="view_timetable.php"><i class="fas fa-calendar"></i> Timetable</a>
    <a href="student_profile.php"><i class="fas fa-user"></i> Profile</a>
    <a href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
</div>

<!-- ================= MAIN CONTENT ================= -->
<div class="main">

<!-- HEADER -->
<div class="card mb-4">
    <div class="header">
        <h3><i class="fas fa-chart-line"></i> My Academic Results</h3>
        <p class="mb-0">
            <?php echo htmlspecialchars($student['name']); ?> |
            <?php echo $student['class_name'] ?? 'Class'; ?>
        </p>
    </div>
</div>

<!-- TRANSCRIPT SUMMARY -->
<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>Term</h6>
            <h4><?php echo $transcript['term'] ?? 'N/A'; ?></h4>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>Year</h6>
            <h4><?php echo $transcript['year'] ?? 'N/A'; ?></h4>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>GPA</h6>
            <h4><?php echo $transcript['gpa'] ?? 'N/A'; ?></h4>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>Position</h6>
            <h4><?php echo $transcript['class_position'] ?? 'N/A'; ?></h4>
        </div>
    </div>
</div>

<!-- PROMOTION STATUS -->
<?php if ($transcript): ?>
<div class="card p-3 mb-4 text-center">
    <h5>Promotion Status</h5>
    <h4 class="text-primary">
        <?php echo htmlspecialchars($transcript['promotion_status']); ?>
    </h4>
</div>
<?php endif; ?>

<!-- RESULTS TABLE -->
<div class="card p-4">
    <h5><i class="fas fa-book"></i> Subject Results</h5>
    <hr>

    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Subject</th>
                    <th>Score</th>
                    <th>Grade</th>
                    <th>Term</th>
                    <th>Year</th>
                </tr>
            </thead>

            <tbody>
            <?php if ($results->num_rows > 0): ?>
                <?php while ($row = $results->fetch_assoc()): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($row['subject_name'] ?? $row['subject']); ?></td>
                        <td><?php echo $row['score']; ?></td>
                        <td>
                            <span class="badge bg-success">
                                <?php echo $row['grade']; ?>
                            </span>
                        </td>
                        <td><?php echo $row['term']; ?></td>
                        <td><?php echo $row['year']; ?></td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="5" class="text-center">No results found</td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</div>

</body>
</html>