<?php require_once __DIR__.'/../includes/auth.php'; require_login(['staff','manager','admin']); $uid=current_user()['id']; $courseId=(int)($_GET['course']??0);
$stmt=db()->prepare('SELECT c.*, cert.certificate_code, cert.issued_at FROM certificates cert JOIN courses c ON c.id=cert.course_id WHERE cert.user_id=? AND cert.course_id=? LIMIT 1'); $stmt->execute([$uid,$courseId]); $cert=$stmt->fetch(); if(!$cert){
  $check=db()->prepare('SELECT cp.*,c.* FROM course_progress cp JOIN courses c ON c.id=cp.course_id WHERE cp.user_id=? AND cp.course_id=? AND cp.status="passed"'); $check->execute([$uid,$courseId]); $passed=$check->fetch(); if($passed && !$passed['requires_practical']){ db()->prepare('INSERT INTO certificates(user_id,course_id,certificate_code,issued_at) VALUES(?,?,?,NOW())')->execute([$uid,$courseId,'FSA-'.strtoupper(bin2hex(random_bytes(4)))]); header('Location: view.php?course='.$courseId); exit; } exit('Certificate not yet available. Practical sign-off may still be required.'); }
require __DIR__.'/../includes/header.php'; $u=current_user(); ?><div class="certificate"><img src="../assets/chuck-logo.png" style="max-width:160px"><div class="cert-title">Certificate of Completion</div><p>This certifies that</p><h1><?= htmlspecialchars($u['first_name'].' '.$u['last_name']) ?></h1><p>has completed</p><h2><?= htmlspecialchars($cert['title']) ?></h2><p>Faithorn & Stag Academy • Chuck & Blade Burgers</p><p>Issued: <?= date('d M Y', strtotime($cert['issued_at'])) ?></p><p class="small">Certificate ID: <?= htmlspecialchars($cert['certificate_code']) ?></p><button class="no-print" onclick="window.print()">Print / Save PDF</button></div><?php require __DIR__.'/../includes/footer.php'; ?>
