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

// Helper: localized badge
function _badge(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;">
        <h2 style="margin:0;font-size:1.1rem;color:#0d3b3b;">Exams</h2>
        <a href="<?= $bp ?>/branch/exams/create" style="background:#16a085;color:#fff;text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.86rem;font-weight:600;">+ Create Exam</a>
    </div>

    <div style="background:#fff;border-radius:10px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <?php if (empty($exams)): ?>
            <div style="text-align:center;padding:50px 20px;color:#999;">
                <p style="margin:0 0 14px;">No exams yet. Click "Create Exam" to add your first one.</p>
                <a href="<?= $bp ?>/branch/exams/create" style="background:#16a085;color:#fff;text-decoration:none;padding:8px 18px;border-radius:6px;font-size:0.82rem;font-weight:600;">+ Create Exam</a>
            </div>
        <?php else: ?>
        <table style="width:100%;border-collapse:collapse;font-size:0.84rem;">
            <thead><tr style="background:#f8f9fa;border-bottom:2px solid #dee2e6;">
                <th style="padding:10px;text-align:left;">Title</th>
                <th style="padding:10px;text-align:center;">Lang</th>
                <th style="padding:10px;text-align:center;">Course</th>
                <th style="padding:10px;text-align:center;">Qs</th>
                <th style="padding:10px;text-align:center;">Schedule</th>
                <th style="padding:10px;text-align:center;">Status</th>
                <th style="padding:10px;text-align:right;">Actions</th>
            </tr></thead>
            <tbody>
            <?php foreach ($exams as $e):
                $isActive    = !empty($e['is_active']);
                $isPublished = !empty($e['is_published']);
                $showResult  = !empty($e['show_result']);
                $lang        = $e['language'] ?? 'en';
                $langLabel   = ['en'=>'EN','hi'=>'हि','both'=>'EN+हि'][$lang] ?? 'EN';
                $hasSchedule = !empty($e['start_time']) || !empty($e['end_time']);
                $schedLabel  = $hasSchedule
                    ? (!empty($e['start_time']) ? date('d M, g:i A', strtotime($e['start_time'])) : '?')
                      . ' —<br>'
                      . (!empty($e['end_time']) ? date('d M, g:i A', strtotime($e['end_time'])) : '?')
                    : '<span style="color:#bbb;">Always</span>';
            ?>
                <tr style="border-bottom:1px solid #f0f0f0;">
                    <td style="padding:10px;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; ?>
                    </td>
                    <td style="padding:10px;text-align:center;"><?= _badge($langLabel, '#1a4fa0', '#eaf0fb') ?></td>
                    <td style="padding:10px;color:#666;text-align:center;"><?= htmlspecialchars($e['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                    <td style="padding:10px;text-align:center;font-weight:600;"><?= (int)($e['question_count'] ?? 0) ?></td>
                    <td style="padding:10px;text-align:center;font-size:0.74rem;color:#666;"><?= $schedLabel ?></td>
                    <td style="padding:10px;text-align:center;white-space:nowrap;">
                        <?= $isActive ? _badge('Active','#1a7a4f','#eafaf1') : _badge('Inactive','#666','#f0f0f0') ?>
                        <?= $isPublished ? _badge('Published','#1a4fa0','#eaf0fb') : _badge('Draft','#999','#f8f8f8') ?>
                        <?= $showResult ? _badge('Show Result','#1a7a4f','#eafaf1') : _badge('Hide Result','#e84118','#fff5f5') ?>
                    </td>
                    <td style="padding:10px;text-align:right;white-space:nowrap;">
                        <a href="<?= $bp ?>/branch/exams/<?= (int)$e['id'] ?>/questions" style="background:#3498db;color:#fff;text-decoration:none;padding:4px 10px;border-radius:4px;font-size:0.72rem;">Questions</a>
                        <form method="POST" action="<?= $bp ?>/branch/exams/<?= (int)$e['id'] ?>/toggle-active" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" style="background:<?= $isActive?'#f39c12':'#27ae60' ?>;color:#fff;border:none;padding:4px 10px;border-radius:4px;font-size:0.72rem;cursor:pointer;"><?= $isActive?'Deactivate':'Activate' ?></button>
                        </form>
                        <form method="POST" action="<?= $bp ?>/branch/exams/<?= (int)$e['id'] ?>/toggle-publish" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" style="background:<?= $isPublished?'#7f8c8d':'#1a4fa0' ?>;color:#fff;border:none;padding:4px 10px;border-radius:4px;font-size:0.72rem;cursor:pointer;"><?= $isPublished?'Unpublish':'Publish' ?></button>
                        </form>
                        <form method="POST" action="<?= $bp ?>/branch/exams/<?= (int)$e['id'] ?>/toggle-result" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" title="Toggle whether students see results immediately after submission" style="background:<?= $showResult?'#7f8c8d':'#8e44ad' ?>;color:#fff;border:none;padding:4px 10px;border-radius:4px;font-size:0.72rem;cursor:pointer;"><?= $showResult?'Hide Result':'Show Result' ?></button>
                        </form>
                        <form method="POST" action="<?= $bp ?>/branch/exams/<?= (int)$e['id'] ?>/delete" onsubmit="return confirm('Delete exam permanently? All questions and results will be lost.');" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" style="background:#e84118;color:#fff;border:none;padding:4px 10px;border-radius:4px;font-size:0.72rem;cursor:pointer;">Delete</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>

    <div style="margin-top:16px;padding:14px;background:#eaf0fb;border-radius:8px;font-size:0.78rem;color:#1a4fa0;">
        <strong>📌 Quick Help:</strong> Active + Published = visible to students on the public /branch-exam portal. "Show Result" ON = students see their score immediately after submitting. OFF = students see "Thank you, results will be published later."
    </div>
</div>
