<?php
session_start();
require_once __DIR__ . '/config/db.php';

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

/* -------------------------------
   FETCH CLASS REPORT DATA
-------------------------------- */

$stmt = $pdo->prepare("
SELECT student_roll, student_name, primary_style, secondary_style, pdf_path
FROM learning_style_results
ORDER BY student_roll ASC
");

$stmt->execute();
$students = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<?php require_once 'includes/header.php'; ?>

<div class="dashboard-wrap">

<h2>Class Learning Style Report</h2>

<p class="sub">
Teacher: <?php echo htmlspecialchars($_SESSION['teacher_name'] ?? ''); ?> |
Class: <?php echo htmlspecialchars($_SESSION['class_name'] ?? ''); ?>
</p>


<table style="width:100%; margin-top:30px; border-collapse:collapse;">

<thead>

<tr>
<th align="left">Roll No</th>
<th align="left">Student Name</th>
<th align="left">Primary Style</th>
<th align="left">Secondary Style</th>
<th align="left">Report</th>
</tr>

</thead>

<tbody>

<?php if(count($students) > 0): ?>

<?php foreach ($students as $s): ?>

<tr>

<td><?php echo htmlspecialchars($s['student_roll']); ?></td>

<td><?php echo htmlspecialchars($s['student_name']); ?></td>

<td><?php echo htmlspecialchars($s['primary_style']); ?></td>

<td><?php echo htmlspecialchars($s['secondary_style']); ?></td>

<td>

<?php if(!empty($s['pdf_path'])): ?>

<a href="<?php echo $s['pdf_path']; ?>" target="_blank" style="color:#38bdf8;">
Download PDF
</a>

<?php else: ?>

<span style="color:#ef4444;">Not Generated</span>

<?php endif; ?>

</td>

</tr>

<?php endforeach; ?>

<?php else: ?>

<tr>
<td colspan="5">No assessment data available</td>
</tr>

<?php endif; ?>

</tbody>

</table>


<form action="generate_class_pdf.php" method="POST" style="margin-top:30px;">

<button type="submit">
⬇ Download Class Report (PDF)
</button>

</form>


<a href="dashboard.php" class="back-link">
← Back to Dashboard
</a>

</div>

</body>
</html>