<?php
declare(strict_types=1);
require_once '../config.php';
require_once __DIR__ . '/helpers.php';
$conn = getDBConnection(); if(!$conn instanceof mysqli){http_response_code(500); exit('DB failed');}
$paperId=(int)($_GET['paper_id']??0);
$stmt=$conn->prepare("SELECT ep.*, s.subject_name FROM exam_papers ep LEFT JOIN subjects s ON ep.subject_id=s.id WHERE ep.id=? LIMIT 1"); $stmt->bind_param('i',$paperId); $stmt->execute(); $paper=$stmt->get_result()->fetch_assoc(); $stmt->close(); if(!$paper){exit('Paper not found');}
$stmt=$conn->prepare("SELECT er.score, er.grade, er.remarks, st.first_name, st.last_name, st.student_id AS student_code
                      FROM exam_results er
                      INNER JOIN students st ON er.student_id=st.id
                      WHERE er.paper_id=?
                      ORDER BY er.score DESC, st.first_name ASC, st.last_name ASC");
$stmt->bind_param('i',$paperId); $stmt->execute(); $res=$stmt->get_result(); $rows=[]; while($row=$res->fetch_assoc()){$rows[]=$row;} $stmt->close();
$scores=array_map(fn($r)=>(float)($r['score']??0),$rows); $avg=$scores?array_sum($scores)/count($scores):0; $hi=$scores?max($scores):0; $lo=$scores?min($scores):0; $pass=$scores?count(array_filter($scores,fn($s)=>$s>=50)) / count($scores) * 100:0;
$settings=examsGetSettings($conn); $logo=$settings['school_logo']??''; $school=$settings['school_name']??SITE_NAME;
?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Print Results</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"><style>@media print{.no-print{display:none!important}body{font-size:12pt}.container{max-width:100%}*{background:none!important}}body{font-size:12pt}</style></head><body><div class="container py-4"><div class="no-print d-flex justify-content-end mb-3"><button onclick="window.print()" class="btn btn-primary">Print</button></div><div class="text-center mb-4"><?php if($logo!==''): ?><img src="../<?= htmlspecialchars($logo) ?>" style="max-height:70px" alt="Logo"><?php endif; ?><h3><?= htmlspecialchars($school) ?></h3><h4>Results Sheet: <?= htmlspecialchars((string)$paper['title']) ?></h4><div>Class: <?= htmlspecialchars((string)$paper['class_name']) ?> | Subject: <?= htmlspecialchars((string)($paper['subject_name']??'')) ?> | Term: <?= htmlspecialchars((string)$paper['term']) ?> | Year: <?= htmlspecialchars((string)$paper['academic_year']) ?> | Date Printed: <?= date('Y-m-d') ?></div></div><table class="table table-bordered"><thead><tr><th>Rank</th><th>Student Name</th><th>Student ID</th><th>Score</th><th>Grade</th><th>Remarks</th></tr></thead><tbody><?php foreach($rows as $i=>$row): ?><tr><td><?= $i+1 ?></td><td><?= htmlspecialchars(trim(($row['first_name']??'').' '.($row['last_name']??''))) ?></td><td><?= htmlspecialchars((string)$row['student_code']) ?></td><td><?= htmlspecialchars(number_format((float)($row['score']??0),2)) ?></td><td><?= htmlspecialchars((string)($row['grade']??'')) ?></td><td><?= htmlspecialchars((string)($row['remarks']??'')) ?></td></tr><?php endforeach; ?></tbody></table><div class="mt-4"><strong>Average:</strong> <?= number_format($avg,2) ?> | <strong>Highest:</strong> <?= number_format($hi,2) ?> | <strong>Lowest:</strong> <?= number_format($lo,2) ?> | <strong>Pass Rate:</strong> <?= number_format($pass,1) ?>%</div><div class="mt-3">Prepared by: ____________________ | Date: ____________________</div></div></body></html>
