<?php
/**
 * Branch Admin — Exams Listing View
 *
 * Table listing all exams belonging to the branch with
 * columns: Title, Course, Duration, Total Marks, Published, Actions.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>

<div class="admin-card">
    <div class="admin-card-header">
        <h2 class="admin-card-title">All Exams</h2>
        <a href="<?= $bp ?>/branch/exams/create" class="admin-btn admin-btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16" style="vertical-align:middle;margin-right:4px;"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Create Exam
        </a>
    </div>
    <div class="admin-card-body">
        <?php if (empty($exams)): ?>
        <p class="admin-empty-text">No exams found. Click "Create Exam" to create your first exam.</p>
        <?php else: ?>
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Course</th>
                        <th>Duration</th>
                        <th>Total Marks</th>
                        <th>Published</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($exams as $exam): ?>
                    <tr>
                        <td>
                            <strong><?= htmlspecialchars($exam['title'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                        </td>
                        <td><?= htmlspecialchars($exam['course_name'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= (int) ($exam['duration_minutes'] ?? 0) ?> min</td>
                        <td><?= number_format((float) ($exam['total_marks'] ?? 0)) ?></td>
                        <td>
                            <?php if (!empty($exam['is_published'])): ?>
                            <span class="admin-badge admin-badge-success">Published</span>
                            <?php else: ?>
                            <span class="admin-badge admin-badge-secondary">Draft</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <div style="display:flex;gap:6px;flex-wrap:wrap;">
                                <a href="<?= $bp ?>/branch/exams/<?= (int) ($exam['id'] ?? 0) ?>/questions" class="admin-btn admin-btn-sm admin-btn-secondary" title="Manage Questions">Questions</a>
                                <a href="<?= $bp ?>/branch/exams/<?= (int) ($exam['id'] ?? 0) ?>/edit" class="admin-btn admin-btn-sm admin-btn-primary" title="Edit">Edit</a>
                                <form method="POST" action="<?= $bp ?>/branch/exams/<?= (int) ($exam['id'] ?? 0) ?>/delete" style="display:inline;" class="delete-form">
                                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                    <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" title="Delete" onclick="return confirm('Are you sure you want to delete this exam and all its questions? This cannot be undone.')">Delete</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>
