<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$result = $result ?? [];
$userName = $userName ?? '';
$enrollmentNo = $enrollmentNo ?? '';
$branchCode = $branchCode ?? '';
$lang = function_exists('getCurrentLocale') ? getCurrentLocale() : 'en';
$isHindi = $lang === 'hi';
$needsHindi = in_array($result['language'] ?? 'en', ['hi','both'], true);

$pct = (float)($result['percentage'] ?? 0);
$obt = (float)($result['total_obtained'] ?? 0);
$tot = (float)($result['total_marks'] ?? 0);
$status = $result['status'] ?? 'fail';
$isPass = $status === 'pass';

// Grade calc (matches BranchResultController::calculateGrade)
$grade = 'F';
if ($pct >= 90) $grade = 'A+';
elseif ($pct >= 80) $grade = 'A';
elseif ($pct >= 70) $grade = 'B+';
elseif ($pct >= 60) $grade = 'B';
elseif ($pct >= 45) $grade = 'C';
elseif ($pct >= 33) $grade = 'D';

$gradeColor = ['A+'=>'#27ae60','A'=>'#27ae60','B+'=>'#3498db','B'=>'#3498db','C'=>'#f39c12','D'=>'#f39c12','F'=>'#e84118'][$grade] ?? '#666';

// Include site header
include __DIR__ . '/../layouts/header.php';
?>
<style>
.result-wrap { max-width: 720px; margin: 40px auto; padding: 0 20px; }
.result-card { background: #fff; border-radius: 18px; overflow: hidden; box-shadow: 0 6px 24px rgba(0,0,0,0.07); }
.result-banner {
    background: linear-gradient(135deg, <?= $isPass ? '#0d3b3b,#16a085' : '#7f1d1d,#581616' ?>);
    color: #fff; padding: 30px; text-align: center;
}
.result-icon {
    width: 80px; height: 80px; background: rgba(255,255,255,0.15);
    border-radius: 50%; display: inline-flex;
    align-items: center; justify-content: center;
    font-size: 2.4rem; margin-bottom: 12px;
}
.result-title { margin: 0; font-size: 1.7rem; font-weight: 700; }
.result-subtitle { margin: 8px 0 0; opacity: 0.85; font-size: 0.92rem; }
.result-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 24px; padding: 30px; }
.result-stat { text-align: center; padding: 14px; background: #f9f9f9; border-radius: 10px; }
.result-stat-val { font-size: 1.6rem; font-weight: 700; color: #0d3b3b; }
.result-stat-lbl { font-size: 0.72rem; color: #999; text-transform: uppercase; }
.result-info-table { width: 100%; font-size: 0.86rem; border-collapse: collapse; padding: 0 30px 30px; }
.result-info-table td { padding: 8px 0; border-bottom: 1px solid #f0f0f0; }
.result-info-table td:first-child { color: #999; width: 50%; }
.result-info-table td:last-child { font-weight: 600; }
.result-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; padding: 0 30px 30px; }
.result-actions a, .result-actions button {
    padding: 12px 26px; border-radius: 8px; font-weight: 600;
    font-size: 0.9rem; text-decoration: none; cursor: pointer; border: none;
}
.btn-result-primary { background: linear-gradient(135deg,#16a085,#117a65); color: #fff; }
.btn-result-secondary { background: #fff; color: #666; border: 1px solid #ddd !important; }
</style>

<div class="container result-wrap">
    <div class="result-card">
        <div class="result-banner">
            <div class="result-icon"><?= $isPass ? '🎉' : '📚' ?></div>
            <h1 class="result-title">
                <?= $isPass
                    ? ($isHindi ? 'बधाई हो! आप उत्तीर्ण हुए' : 'Congratulations! You Passed')
                    : ($isHindi ? 'परीक्षा में अनुत्तीर्ण' : 'Exam Not Cleared') ?>
            </h1>
            <p class="result-subtitle">
                <?= htmlspecialchars($result['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                <?php if ($needsHindi && !empty($result['title_hindi'])): ?>
                    <span style="font-family:'Noto Sans Devanagari',sans-serif;"> · <?= htmlspecialchars($result['title_hindi'], ENT_QUOTES, 'UTF-8') ?></span>
                <?php endif; ?>
            </p>
        </div>

        <div style="padding: 30px;">
            <div class="result-stats">
                <div class="result-stat"><div class="result-stat-val"><?= $obt ?></div><div class="result-stat-lbl"><?= $isHindi ? 'प्राप्तांक' : 'Obtained' ?></div></div>
                <div class="result-stat"><div class="result-stat-val" style="color:#666;"><?= $tot ?></div><div class="result-stat-lbl"><?= $isHindi ? 'कुल' : 'Total' ?></div></div>
                <div class="result-stat" style="background:#eafaf1;"><div class="result-stat-val" style="color:#27ae60;"><?= $pct ?>%</div><div class="result-stat-lbl"><?= $isHindi ? 'प्रतिशत' : 'Percentage' ?></div></div>
                <div class="result-stat" style="background:#eaf0fb;"><div class="result-stat-val" style="color:<?= $gradeColor ?>;"><?= $grade ?></div><div class="result-stat-lbl"><?= $isHindi ? 'ग्रेड' : 'Grade' ?></div></div>
            </div>

            <table class="result-info-table">
                <tr><td><?= $isHindi ? 'विद्यार्थी नाम' : 'Student Name' ?></td><td><?= htmlspecialchars($userName, ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td><?= $isHindi ? 'एनरोलमेंट नंबर' : 'Enrollment No' ?></td><td style="font-family:monospace;"><?= htmlspecialchars($enrollmentNo, ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td><?= $isHindi ? 'शाखा कोड' : 'Branch Code' ?></td><td><?= htmlspecialchars($branchCode, ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td><?= $isHindi ? 'समय सीमा' : 'Duration' ?></td><td><?= (int)($result['duration_minutes'] ?? 60) ?> min</td></tr>
                <tr><td><?= $isHindi ? 'उत्तीर्ण अंक' : 'Pass Marks' ?></td><td><?= (int)($result['pass_marks'] ?? 40) ?></td></tr>
                <tr><td><?= $isHindi ? 'जमा करने का समय' : 'Submitted At' ?></td><td><?= !empty($result['completed_at']) ? date('d M Y, g:i A', strtotime($result['completed_at'])) : '—' ?></td></tr>
                <tr><td><?= $isHindi ? 'स्थिति' : 'Status' ?></td><td style="color:<?= $isPass ? '#27ae60' : '#e84118' ?>;font-weight:700;"><?= $isPass ? ($isHindi ? 'उत्तीर्ण ✓' : 'PASS ✓') : ($isHindi ? 'अनुत्तीर्ण ✗' : 'FAIL ✗') ?></td></tr>
            </table>

            <?php if (!$isPass): ?>
            <div style="background:#fff5f5;border-left:4px solid #e84118;padding:12px 16px;border-radius:6px;font-size:0.84rem;color:#e84118;margin:18px 0;text-align:left;">
                💪 <?= $isHindi
                    ? 'इस बार नहीं रहे। अगली बार अधिक मेहनत करें। अपनी शाखा से संपर्क करें।'
                    : 'Don\'t be discouraged. Keep studying and try again. Contact your branch.' ?>
            </div>
            <?php endif; ?>

            <div class="result-actions">
                <a href="<?= $bp ?>/branch-exam/dashboard" class="btn-result-primary">
                    <?= $isHindi ? '← वापस परीक्षाओं पर' : '← Back to My Exams' ?>
                </a>
                <form method="POST" action="<?= $bp ?>/branch-exam/logout" style="display:inline;">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    <button type="submit" class="btn-result-secondary"><?= $isHindi ? 'लॉगआउट' : 'Logout' ?></button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php
include __DIR__ . '/../layouts/footer.php';

