<?php
require_once '../includes/init.php';

$user = new User($pdo);
if (!$user->isLoggedIn() || !$user->isStudent()) {
    header("Location: ../index.php");
    exit;
}

if (!isset($_GET['exam_id'])) {
    header("Location: dashboard.php");
    exit;
}

$exam_id = $_GET['exam_id'];
$user_id = $_SESSION['user_id'];

// Fetch Result
$stmt = $pdo->prepare("
    SELECT r.*, 
           e.title as exam_title, 
           e.exam_type, 
           e.total_score, 
           e.show_result_to_student,
           u.username 
    FROM results r 
    JOIN exams e ON r.exam_id = e.exam_id 
    JOIN users u ON r.user_id = u.user_id 
    WHERE r.user_id = ? AND r.exam_id = ?
    ORDER BY r.result_id DESC
");
$stmt->execute([$user_id, $exam_id]);
$result = $stmt->fetch();

if (!$result) {
    die("Result not found.");
}

// Check if result viewing is allowed
if (empty($result['show_result_to_student'])) {
    $hide_score = true;
} else {
    $hide_score = false;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Result Slip</title>
    <style>
        body { font-family: sans-serif; text-align: center; padding: 50px; }
        .result-card { border: 2px solid #333; padding: 30px; display: inline-block; border-radius: 10px; max-width: 500px; width: 100%; }
        h1 { color: #007bff; }
        .score { font-size: 3em; font-weight: bold; margin: 20px 0; color: #28a745; }
        .details { text-align: left; margin: 20px 0; font-size: 1.1em; }
        .btn { padding: 10px 20px; background: #007bff; color: white; text-decoration: none; border-radius: 4px; }
    </style>
</head>
<body>
    <div class="result-card">
        <?php include '../includes/page_header.php'; ?>
        <h1>Exam Completed</h1>
        <div class="details">
            <p><strong>Student:</strong> <?php echo htmlspecialchars($result['username']); ?></p>
            <p><strong>Exam:</strong> <?php echo htmlspecialchars($result['exam_title']); ?></p>
            <p><strong>Type:</strong> <?php echo htmlspecialchars($result['exam_type'] ?? 'test'); ?> &nbsp; | &nbsp; <strong>Total Score:</strong> <?php echo (int)($result['total_score'] ?? 20); ?></p>
            <p><strong>Date:</strong> <?php echo htmlspecialchars($result['date_taken']); ?></p>
        </div>
        
        <?php if (!$hide_score): ?>
            <?php 
                $pct = $result['total_questions'] > 0 ? ($result['score'] / $result['total_questions']) * 100 : 0;
                $scaled = isset($result['total_score']) ? ($pct / 100) * (int)$result['total_score'] : $pct;
            ?>
            <div class="score">
                <?php echo number_format($scaled, 2); ?> / <?php echo (int)($result['total_score'] ?? 100); ?>
            </div>
            <div style="margin: 8px 0; color:#475569;">
                Percentage: <?php echo number_format($pct, 2); ?>%
            </div>
        <?php else: ?>
            <div style="margin: 20px 0; padding: 20px; background: #f8f9fa; border: 1px dashed #ccc;">
                <p>Thank you for taking the exam.</p>
                <p>Your result has been recorded.</p>
            </div>
        <?php endif; ?>
        
        <p>
            <a href="dashboard.php" class="btn">Back to Dashboard</a>
            <?php if (!$hide_score): ?>
            <button onclick="window.print()" class="btn" style="background: #333;">Print Result</button>
            <?php endif; ?>
        </p>
        
        <div style="margin-top: 30px; font-size: 0.8em; color: #666;">
            &copy; 2026 Designed by FGV
        </div>
    </div>
</body>
</html>
