<div class="space-y-6">
    <div class="flex flex-wrap items-center justify-between gap-3">
        <div class="flex items-center gap-3 flex-wrap">
            <?php if (!empty($setting)): ?>
                <p class="text-sm text-slate-500">Attempts for <span class="font-semibold text-slate-800"><?= esc($setting['title']) ?></span></p>
                <a href="<?= site_url('/admin/quiz-attempts') ?>" class="text-xs font-semibold text-violet-600 hover:text-violet-700">← Show all</a>
            <?php else: ?>
                <p class="text-sm text-slate-500"><?= count($attempts ?? []) ?> total attempt(s)</p>
            <?php endif; ?>
        </div>
        <a href="<?= site_url('/admin/quiz-settings') ?>" class="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl text-sm font-semibold bg-slate-100 hover:bg-slate-200 text-slate-700 border border-slate-200 transition">
            <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" /><path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" /></svg>
            Quiz Settings
        </a>
    </div>

    <div class="glass rounded-2xl shadow-sm border border-slate-200/60 p-5">
        <form method="get" action="<?= site_url('/admin/quiz-attempts') ?>" class="space-y-4">
            <?= csrf_field() ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-600 mb-1.5">Agent</label>
                    <select name="agent_id" class="w-full rounded-xl border border-slate-300 bg-white px-3.5 py-2.5 text-sm focus:ring-2 focus:ring-violet-500/30 focus:border-violet-500 outline-none transition">
                        <option value="">— All Agents —</option>
                        <?php foreach ($agentUsers as $au):
                            $sel = (string)($filterAgentId ?? '') === (string)$au['id'] ? 'selected' : '';
                        ?>
                            <option value="<?= (int)$au['id'] ?>" <?= $sel ?>><?= esc($au['full_name'] ?? ($au['username'] ?? 'Agent #' . (int)$au['id'])) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-600 mb-1.5">Quiz</label>
                    <select name="quiz_id" class="w-full rounded-xl border border-slate-300 bg-white px-3.5 py-2.5 text-sm focus:ring-2 focus:ring-violet-500/30 focus:border-violet-500 outline-none transition">
                        <option value="">— All Quizzes —</option>
                        <?php foreach ($allQuizzes as $q):
                            $sel = (string)($filterQuizSelId ?? '') === (string)$q['id'] ? 'selected' : '';
                        ?>
                            <option value="<?= (int)$q['id'] ?>" <?= $sel ?>><?= esc($q['title'] ?? 'Quiz #' . (int)$q['id']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-600 mb-1.5">Date From</label>
                    <input type="date" name="from" value="<?= esc($fromDate ?? '') ?>" class="w-full rounded-xl border border-slate-300 bg-white px-3.5 py-2.5 text-sm focus:ring-2 focus:ring-violet-500/30 focus:border-violet-500 outline-none transition">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-600 mb-1.5">Date To</label>
                    <input type="date" name="to" value="<?= esc($toDate ?? '') ?>" class="w-full rounded-xl border border-slate-300 bg-white px-3.5 py-2.5 text-sm focus:ring-2 focus:ring-violet-500/30 focus:border-violet-500 outline-none transition">
                </div>
            </div>
            <div class="flex items-center gap-2">
                <button type="submit" class="px-4 py-2.5 rounded-xl text-sm font-semibold bg-slate-900 text-white hover:bg-slate-800 transition">
                    <span class="inline-flex items-center gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>
                        Filter
                    </span>
                </button>
                <a href="<?= site_url('/admin/quiz-attempts') ?>" class="px-4 py-2.5 rounded-xl text-sm font-semibold text-slate-700 bg-slate-100 hover:bg-slate-200 transition">Reset</a>
            </div>
        </form>
    </div>

    <div class="glass rounded-2xl shadow-sm border border-slate-200/60 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead>
                    <tr class="bg-slate-50/70 border-b border-slate-200/60">
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Date</th>
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Agent</th>
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Quiz</th>
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">#</th>
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Correct</th>
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Score</th>
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Duration</th>
                        <th class="px-6 py-3.5 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Result</th>
                        <th class="px-6 py-3.5 text-right text-xs font-semibold text-slate-500 uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    <?php if (empty($attempts)): ?>
                        <tr><td colspan="9" class="px-6 py-16 text-center">
                            <div class="flex flex-col items-center gap-3">
                                <div class="w-16 h-16 rounded-2xl bg-slate-100 flex items-center justify-center">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="w-8 h-8 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2" /></svg>
                                </div>
                                <p class="text-sm font-medium text-slate-600">No attempts yet — agents need to start a quiz first.</p>
                            </div>
                        </td></tr>
                    <?php else: ?>
                        <?php foreach ($attempts as $a):
                            $score = (float)($a['score_pct'] ?? 0);
                            $passed = !empty($a['passed']);
                            $status = strtolower(trim((string)($a['status'] ?? 'in_progress')));
                            $statusBadge = $status === 'completed' ? ($passed ? 'bg-emerald-100 text-emerald-700' : 'bg-rose-100 text-rose-700') : 'bg-slate-100 text-slate-700';
                            $statusText = $status === 'completed' ? ($passed ? 'PASSED' : 'FAILED') : ucwords(str_replace('_', ' ', $status));
                            $duration = '';
                            $start = !empty($a['started_at']) ? strtotime($a['started_at']) : 0;
                            $end   = !empty($a['completed_at']) ? strtotime($a['completed_at']) : 0;
                            if ($start && $end) {
                                $secs = max(0, $end - $start);
                                $duration = (int)($secs / 60) . 'm ' . ($secs % 60) . 's';
                            } elseif ($start) {
                                $duration = 'started at ' . date('H:i', $start);
                            }
                            $type = ($a['quiz_type'] ?? '') === 'fill_up' ? 'Fill-Up' : 'MC';
                            $typeClass = ($a['quiz_type'] ?? '') === 'fill_up' ? 'bg-sky-50 text-sky-700' : 'bg-indigo-50 text-indigo-700';
                            $correct = (int)($a['correct_count'] ?? 0);
                            $total = (int)($a['total_questions'] ?? 0);
                        ?>
                            <tr class="hover:bg-slate-50/50 transition">
                                <td class="px-6 py-4 text-sm text-slate-600"><?= !empty($a['created_at']) ? date('M j, Y H:i', strtotime($a['created_at'])) : '—' ?></td>
                                <td class="px-6 py-4">
                                    <div class="flex flex-col">
                                        <span class="text-sm font-semibold text-slate-800"><?= esc($a['user_name'] ?? 'Unknown') ?></span>
                                        <span class="text-[11px] text-slate-500"><?= esc($a['user_email'] ?? '') ?></span>
                                    </div>
                                </td>
                                <td class="px-6 py-4">
                                    <div class="flex items-center gap-2 flex-wrap">
                                        <span class="text-sm font-medium text-slate-700"><?= esc($a['quiz_title'] ?? 'Deleted quiz') ?></span>
                                        <span class="inline-flex px-1.5 py-0.5 rounded text-[10px] font-semibold <?= $typeClass ?>"><?= $type ?></span>
                                    </div>
                                </td>
                                <td class="px-6 py-4 text-sm text-slate-600">#<?= (int)($a['attempt_no'] ?? 1) ?></td>
                                <td class="px-6 py-4 text-sm font-semibold text-slate-700"><?= $correct ?>/<?= $total ?></td>
                                <td class="px-6 py-4">
                                    <span class="text-sm font-bold <?= $score >= 70 ? 'text-emerald-600' : ($score >= 50 ? 'text-amber-600' : 'text-rose-600') ?>"><?= number_format($score, 1) ?>%</span>
                                </td>
                                <td class="px-6 py-4 text-sm text-slate-600"><?= $duration !== '' ? $duration : '—' ?></td>
                                <td class="px-6 py-4"><span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-[11px] font-semibold <?= $statusBadge ?>"><?= $statusText ?></span></td>
                                <td class="px-6 py-4 text-right">
                                    <a href="<?= site_url('/admin/quiz-attempts/view/' . (int)$a['id']) ?>" class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-[12px] font-semibold bg-violet-50 text-violet-700 hover:bg-violet-100 transition">
                                        <svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" /><path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" /></svg>
                                        Review
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
