<?php
session_start();
require_once __DIR__ . "/config/db.php"; // 🔥 ADDED

$csvFile = __DIR__ . "/uploads/student_list.csv";

$students = [];
$teacher = $_SESSION["teacher_name"] ?? "";
$class   = $_SESSION["class"] ?? "";
$section = $_SESSION["section"] ?? "";

/* 🔥 FETCH PDF STATUS FROM DB */
$stmt = $pdo->prepare("SELECT student_roll, pdf_path FROM learning_style_results");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

$pdfMap = [];
foreach ($results as $row) {
    $pdfMap[$row['student_roll']] = $row['pdf_path'];
}

if (file_exists($csvFile)) {
    if (($handle = fopen($csvFile, "r")) !== FALSE) {
        $header = fgetcsv($handle);

        while (($row = fgetcsv($handle)) !== FALSE) {
            $students[] = [
                "roll" => $row[0],
                "name" => $row[1]
            ];
        }
        fclose($handle);
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Assessment Session | RS Learning Lab</title>

<style>
body{
    margin:0;
    background:radial-gradient(circle at top,#0f172a,#020617);
    font-family:Segoe UI,sans-serif;
    color:#e5e7eb;
}
.wrap{
    max-width:1100px;
    margin:60px auto;
    padding:20px;
}
h1{
    margin-bottom:6px;
}
.sub{
    opacity:.7;
    margin-bottom:30px;
}
table{
    width:100%;
    border-collapse:collapse;
    background:#020617;
    border-radius:14px;
    overflow:hidden;
}
th,td{
    padding:16px;
    border-bottom:1px solid #1f2937;
}
th{
    text-align:left;
    color:#9ca3af;
    font-size:14px;
}
tr:hover{
    background:#020617;
}
.btn{
    padding:8px 14px;
    background:#0ea5e9;
    color:#000;
    font-size:13px;
    border-radius:8px;
    text-decoration:none;
    font-weight:600;
}
.btn:hover{
    background:#38bdf8;
}
.success{
    color:#22c55e;
    font-weight:bold;
}
.empty{
    opacity:.6;
    padding:30px;
    text-align:center;
}
</style>
</head>

<body>

<div class="wrap">
    <h1>📋 Assessment Session</h1>
    <div class="sub">
        Class <?= htmlspecialchars($class) ?> ·
        Section <?= htmlspecialchars($section) ?> ·
        Facilitator: <?= htmlspecialchars($teacher) ?>
    </div>

    <table>
        <thead>
            <tr>
                <th width="15%">Roll No</th>
                <th>Student Name</th>
                <th width="25%">Action</th>
            </tr>
        </thead>
        <tbody>

        <?php if (count($students) > 0): ?>
            <?php foreach ($students as $s): ?>
                <tr>
                    <td><?= htmlspecialchars($s["roll"]) ?></td>
                    <td><?= htmlspecialchars($s["name"]) ?></td>
                    <td>

                    <?php if(isset($pdfMap[$s['roll']]) && !empty($pdfMap[$s['roll']])): ?>

                        <span class="success">✅ PDF Generated</span><br><br>

                        <a href="<?= $pdfMap[$s['roll']] ?>" target="_blank" class="btn">
                            View PDF
                        </a>

                    <?php else: ?>

                        <a href="omr_test.php?roll=<?= urlencode($s['roll']) ?>&name=<?= urlencode($s['name']) ?>" class="btn">
                            Scan OMR
                        </a>

                    <?php endif; ?>

                    </td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td colspan="3" class="empty">
                    No student list found. Please upload CSV to begin.
                </td>
            </tr>
        <?php endif; ?>

        </tbody>
    </table>

    <br><br>

    <a href="bulk_download.php" class="btn">
        Download All Reports
    </a>

</div>

</body>
</html>