<?php
// Include database connection
include('db_connection.php');
include 'header.php';
// Get POST data
$student_id = isset($_POST['student_id']) ? $_POST['student_id'] : null;
$year = isset($_POST['year']) ? $_POST['year'] : null;
$term = isset($_POST['term']) ? $_POST['term'] : null;

// Initialize variable
$history_result = null;

if ($student_id && $year && $term) {
    // Fetch academic history for the selected year and term
    $history_query = "SELECT * FROM academic_history WHERE student_id = ? AND year = ? AND term = ?";
    $stmt = $conn->prepare($history_query);
    $stmt->bind_param("iss", $student_id, $year, $term);
    $stmt->execute();
    $history_result = $stmt->get_result();
    $stmt->close();
}

if ($history_result && $history_result->num_rows > 0): ?>
    <table border="1">
        <thead>
            <tr>
                <th>History ID</th>
                <th>Subject</th>
                <th>Grade</th>
                <th>Term</th>
                <th>Year</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($row = $history_result->fetch_assoc()): ?>
            <tr>
                <td><?php echo htmlspecialchars($row['history_id']); ?></td>
                <td><?php echo htmlspecialchars($row['subject']); ?></td>
                <td><?php echo htmlspecialchars($row['grade']); ?></td>
                <td><?php echo htmlspecialchars($row['term']); ?></td>
                <td><?php echo htmlspecialchars($row['year']); ?></td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
<?php else: ?>
    <p>No academic history found for the selected year and term.</p>
<?php endif; ?>

<?php
// Close database connection
$conn->close();
?>
