<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$exams = $exams ?? [];

function _badge2(string $label, string $color, string $bg): string {
    return "<span style=\"display:inline-block;padding:2px 8px;border-radius:10px;font-size:0.7rem;font-weight:600;color:{$color};background:{$bg};\">{$label}</span>";
}
?>
<div class="admin-section">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;flex-wrap:wrap;gap:10px;">
        <div>
            <h2 style="margin:0;font-size:1.15rem;color:#0d3b3b;">📊 Exam Results</h2>
            <p style="margin:4px 0 0;font-size:0.82rem;color:#666;">View all students who appeared in each exam, edit scores, approve results, and generate marksheet + certificate.</p>
        </div>
        <a href="<?= $bp ?>/branch/exams" style="color:#16a085;text-decoration:none;font-size:0.84rem;font-weight:600;">← Back to Exams</a>
    </div>

    <?php if (empty($exams)): ?>
        <div style="background:#fff;border-radius:10px;padding:50px 20px;text-align:center;color:#999;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <p style="margin:0 0 14px;">No exams yet. Create an exam first, then students will appear here once they take it.</p>
            <a href="<?= $bp ?>/branch/exams/create" style="background:#16a085;color:#fff;text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.84rem;font-weight:600;">+ Create Exam</a>
        </div>
    <?php else: ?>
        <div style="background:#fff;border-radius:10px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <table style="width:100%;border-collapse:collapse;font-size:0.84rem;">
                <thead><tr style="background:#f8f9fa;border-bottom:2px solid #dee2e6;">
                    <th style="padding:12px;text-align:left;">Exam Title</th>
                    <th style="padding:12px;text-align:center;">Lang</th>
                    <th style="padding:12px;text-align:center;">Questions</th>
                    <th style="padding:12px;text-align:center;">Total Marks</th>
                    <th style="padding:12px;text-align:center;">Pass Marks</th>
                    <th style="padding:12px;text-align:center;">📅 Appeared</th>
                    <th style="padding:12px;text-align:center;">✓ Pass</th>
                    <th style="padding:12px;text-align:center;">✗ Fail</th>
                    <th style="padding:12px;text-align:center;">Pass %</th>
                    <th style="padding:12px;text-align:right;">Actions</th>
                </tr></thead>
                <tbody>
                <?php foreach ($exams as $e):
                    $appeared = (int)($e['appeared_count'] ?? 0);
                    $passCount = (int)($e['pass_count'] ?? 0);
                    $failCount = (int)($e['fail_count'] ?? 0);
                    $passPct = $appeared > 0 ? round(($passCount / $appeared) * 100, 1) : 0;
                    $lang = $e['language'] ?? 'en';
                    $langLabel = ['en'=>'EN','hi'=>'हि','both'=>'EN+हि'][$lang] ?? 'EN';
                ?>
                    <tr style="border-bottom:1px solid #f0f0f0;">
                        <td style="padding:12px;font-weight:600;color:#0d3b3b;">
                            <?= htmlspecialchars($e['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                            <?php if (!empty($e['title_hindi'])): ?>
                                <br><span style="font-weight:400;color:#666;font-size:0.78rem;font-family:'Noto Sans Devanagari',sans-serif;"><?= htmlspecialchars($e['title_hindi'], ENT_QUOTES, 'UTF-8') ?></span>
                            <?php endif; ?>
                            <div style="margin-top:4px;">
                                <?= !empty($e['is_active']) ? _badge2('Active','#1a7a4f','#eafaf1') : _badge2('Inactive','#666','#f0f0f0') ?>
                                <?= !empty($e['is_published']) ? _badge2('Published','#1a4fa0','#eaf0fb') : _badge2('Draft','#999','#f8f8f8') ?>
                            </div>
                        </td>
                        <td style="padding:12px;text-align:center;"><?= _badge2($langLabel, '#1a4fa0', '#eaf0fb') ?></td>
                        <td style="padding:12px;text-align:center;font-weight:600;"><?= (int)($e['question_count'] ?? 0) ?></td>
                        <td style="padding:12px;text-align:center;"><?= (int)($e['total_marks'] ?? 100) ?></td>
                        <td style="padding:12px;text-align:center;color:#e84118;font-weight:600;"><?= (int)($e['pass_marks'] ?? 40) ?></td>
                        <td style="padding:12px;text-align:center;">
                            <span style="font-size:1.2rem;font-weight:700;color:#0d3b3b;"><?= $appeared ?></span>
                        </td>
                        <td style="padding:12px;text-align:center;">
                            <span style="font-size:1.05rem;font-weight:700;color:#27ae60;"><?= $passCount ?></span>
                        </td>
                        <td style="padding:12px;text-align:center;">
                            <span style="font-size:1.05rem;font-weight:700;color:#e84118;"><?= $failCount ?></span>
                        </td>
                        <td style="padding:12px;text-align:center;">
                            <?php if ($appeared > 0): ?>
                                <div style="background:#f0f0f0;border-radius:8px;height:8px;width:80px;margin:0 auto;overflow:hidden;">
                                    <div style="background:linear-gradient(90deg,#27ae60,#1e7e44);height:100%;width:<?= $passPct ?>%;"></div>
                                </div>
                                <div style="font-size:0.72rem;color:#666;margin-top:3px;"><?= $passPct ?>%</div>
                            <?php else: ?>
                                <span style="color:#bbb;">—</span>
                            <?php endif; ?>
                        </td>
                        <td style="padding:12px;text-align:right;">
                            <a href="<?= $bp ?>/branch/exam-results/<?= (int)$e['id'] ?>" style="background:#16a085;color:#fff;text-decoration:none;padding:6px 14px;border-radius:6px;font-size:0.78rem;font-weight:600;">📋 View Results</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div style="margin-top:16px;padding:14px;background:#eaf0fb;border-radius:8px;font-size:0.8rem;color:#1a4fa0;line-height:1.6;">
            <strong>📌 How it works:</strong>
            <ol style="margin:6px 0 0 18px;padding:0;">
                <li>Click "View Results" on any exam to see all students who appeared.</li>
                <li>On the next page, you can edit a student's score (override auto-grading), approve the result, and generate marksheet + certificate.</li>
                <li>Marksheet + Certificate buttons unlock only AFTER you approve a result.</li>
            </ol>
        </div>
    <?php endif; ?>
</div>
