<?php
declare(strict_types=1);
require_once '../config.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/includes/diagram_renderer.php';
$conn = getDBConnection(); if(!$conn instanceof mysqli){http_response_code(500); exit('DB failed');}
$paperId=(int)($_GET['paper_id']??0); $variant=($_GET['variant']??'A')==='B'?'B':'A'; $showAnswers=(int)($_GET['show_answers']??0)===1;
$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 eq.*, epq.marks FROM exam_paper_questions epq INNER JOIN exam_questions eq ON epq.question_id=eq.id WHERE epq.paper_id=? ORDER BY epq.order_no ASC, epq.id ASC"); $stmt->bind_param('i',$paperId); $stmt->execute(); $result=$stmt->get_result(); $questions=[]; while($row=$result->fetch_assoc()){$questions[]=$row;} $stmt->close();
if($variant==='B'){mt_srand(crc32($paperId.'B')); shuffle($questions);}
$sections=['mcq'=>[],'fill_blank'=>[],'theory'=>[]]; foreach($questions as $q){$sections[$q['question_type']][]=$q;}
$settings=examsGetSettings($conn); $logo=$settings['school_logo']??''; $school=$settings['school_name']??SITE_NAME;
?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Print Paper</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}.section{page-break-before:always}.container{max-width:100%}*{background:none!important}svg{display:block!important}}body{font-size:12pt}.answer{color:#c00;font-weight:700}</style></head><body><div class="container py-4"><div class="no-print d-flex justify-content-end gap-2 mb-3"><button onclick="window.print()" class="btn btn-primary">Print</button><a class="btn btn-outline-secondary" href="?paper_id=<?= $paperId ?>&variant=A<?= $showAnswers?'&show_answers=1':'' ?>">Variant A</a><a class="btn btn-outline-secondary" href="?paper_id=<?= $paperId ?>&variant=B<?= $showAnswers?'&show_answers=1':'' ?>">Variant B</a><a class="btn btn-outline-secondary" href="?paper_id=<?= $paperId ?>&variant=<?= $variant ?>&show_answers=<?= $showAnswers?0:1 ?>"><?= $showAnswers?'Hide':'Show' ?> Answers</a></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><?= htmlspecialchars((string)$paper['title']) ?></h4><div>Class: <?= htmlspecialchars((string)$paper['class_name']) ?> | Subject: <?= htmlspecialchars((string)($paper['subject_name']??'')) ?> | Duration: <?= (int)$paper['duration_minutes'] ?> mins | Total Marks: <?= (int)$paper['total_marks'] ?> | Date: ________</div><?php if(!empty($paper['instructions'])): ?><div class="mt-3 border rounded p-3 text-start"><strong>Instructions:</strong><br><?= nl2br(htmlspecialchars((string)$paper['instructions'])) ?></div><?php endif; ?></div><?php foreach([['mcq','SECTION A - MCQ'],['fill_blank','SECTION B - Fill in the Blank'],['theory','SECTION C - Theory']] as [$type,$title]): if(!$sections[$type]) continue; ?><div class="section"><h5><?= $title ?></h5><ol><?php foreach($sections[$type] as $q): ?><li class="mb-3"><?= nl2br(htmlspecialchars((string)$q['question'])) ?> <?php if(!empty($q['diagram_required']) && !empty($q['diagram_spec'])): ?><?php $spec = is_array($q['diagram_spec']) ? $q['diagram_spec'] : json_decode((string)$q['diagram_spec'], true); if (is_array($spec)) { echo renderDiagram($spec); } ?><?php endif; ?><?php if($type==='mcq'): ?><div class="ms-3">A. <span class="<?= $showAnswers && $q['correct_answer']==='A'?'answer':'' ?>"><?= htmlspecialchars((string)$q['option_a']) ?></span></div><div class="ms-3">B. <span class="<?= $showAnswers && $q['correct_answer']==='B'?'answer':'' ?>"><?= htmlspecialchars((string)$q['option_b']) ?></span></div><div class="ms-3">C. <span class="<?= $showAnswers && $q['correct_answer']==='C'?'answer':'' ?>"><?= htmlspecialchars((string)$q['option_c']) ?></span></div><div class="ms-3">D. <span class="<?= $showAnswers && $q['correct_answer']==='D'?'answer':'' ?>"><?= htmlspecialchars((string)$q['option_d']) ?></span></div><?php elseif($type==='fill_blank' && $showAnswers): ?><div class="answer mt-2">Answer: <?= htmlspecialchars((string)$q['correct_answer']) ?></div><?php else: ?><div style="height:80px;border-bottom:1px solid #aaa;margin-top:10px"></div><?php endif; ?></li><?php endforeach; ?></ol></div><?php endforeach; ?></div></body></html>
