<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/config.php';require_once __DIR__ . '/../includes/db.php';require_once __DIR__ . '/../includes/functions.php';require_once __DIR__ . '/../includes/student-auth.php';require_once __DIR__ . '/../includes/student-course-context.php';require_once __DIR__ . '/../includes/grade-engine.php';
$pdo=db();requireStudent();$student=currentStudent($pdo);if(!$student){studentLogout();redirect('login.php');}$courseId=(int)($_GET['course_id']??($_SESSION['selected_core_course_id']??0));if(!$courseId||!isStudentEnrolledInCourse($pdo,(int)$student['id'],$courseId)){$courses=getStudentEnrolledCourses($pdo,(int)$student['id']);$courseId=(int)($courses[0]['id']??0);} $course=$courseId?getStudentCourse($pdo,(int)$student['id'],$courseId):null;
$certificate=$courseId?safeFetch($pdo,'SELECT * FROM core_certificates WHERE student_id=? AND course_id=? ORDER BY id DESC LIMIT 1',[(int)$student['id'],$courseId]):null;$final=$courseId?getStudentFinalGrade($pdo,(int)$student['id'],$courseId):[];$percentage=(float)($final['final_percentage']??0);$eligible=$course&&$percentage>=70&&in_array((string)($course['status']??''),['completed','closed'],true);
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Certificate Status · SMI</title><style>:root{font-family:Inter,Arial,sans-serif;background:#f3f6fb;color:#0f172a}*{box-sizing:border-box}body{margin:0}.page{max-width:1050px;margin:auto;padding:32px}.top{display:flex;justify-content:space-between;align-items:center}.top h1{font-size:44px;margin:0}.btn{border:0;border-radius:14px;padding:12px 16px;background:#2563eb;color:white;text-decoration:none;font-weight:850}.hero{margin-top:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:white;border-radius:30px;padding:34px;display:flex;justify-content:space-between;align-items:center;gap:20px}.score{font-size:58px;font-weight:900}.grid{display:grid;grid-template-columns:repeat(3,1fr);gap:16px;margin-top:20px}.card{background:white;padding:22px;border-radius:23px;box-shadow:0 15px 42px rgba(15,23,42,.07)}.pill{display:inline-block;padding:7px 11px;border-radius:999px;background:#eff6ff;color:#1d4ed8;font-weight:850}.good{background:#dcfce7;color:#166534}.warn{background:#fff7ed;color:#9a3412}.meta{color:#64748b;line-height:1.8}.download{margin-top:18px;display:inline-flex}@media(max-width:750px){.grid{grid-template-columns:1fr}.hero{align-items:flex-start;flex-direction:column}.page{padding:18px}.top h1{font-size:34px}}</style></head><body><main class="page"><div class="top"><div><h1>Certificate status</h1><p><?=e($course['title']??'No active course')?></p></div><a class="btn" href="dashboard.php<?= $courseId?'?course_id='.$courseId:'' ?>">Dashboard</a></div><section class="hero"><div><h2><?=e($student['full_name'])?></h2><p>Your eligibility is calculated from the official final grade for this course.</p></div><div class="score"><?=e(number_format($percentage,1))?>%</div></section><div class="grid"><article class="card"><small>Academic result</small><h2><?=e($final['grade_letter']??gradeLetter($percentage))?></h2><span class="pill <?=getPassStatus($percentage)==='passed'?'good':'warn'?>"><?=e(getPassStatus($percentage))?></span></article><article class="card"><small>Eligibility</small><h2><?= $eligible?'Eligible':'Not yet eligible' ?></h2><p class="meta">Requires at least 70% and a completed or closed course.</p></article><article class="card"><small>Certificate</small><h2><?=e(ucfirst((string)($certificate['status']??'not issued')))?></h2><?php if($certificate):?><p class="meta">Number: <?=e($certificate['certificate_number']?:'-')?><br>Issued: <?=e(formatDateNice($certificate['issued_at']))?></p><?php endif;?></article></div><?php if($certificate&&$certificate['status']==='issued'&&!empty($certificate['file_path'])):?><a class="btn download" target="_blank" rel="noopener" href="../<?=e(ltrim((string)$certificate['file_path'],'/'))?>">Open certificate</a><?php endif;?></main></body></html>
