<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Toppers Management -->
<div class="admin-section">
    <?php if (!($showForm ?? false)): ?>
    <div class="admin-section-header">
        <button type="button" class="admin-btn admin-btn-primary" onclick="document.getElementById('addTopperForm').style.display='block';this.style.display='none';">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add New Topper
        </button>
    </div>
    <?php endif; ?>

    <?php if ($showForm ?? false): ?>
    <!-- Add/Edit Topper Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title"><?= ($editTopper ?? null) ? 'Edit Topper' : 'Add New Topper' ?></h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= ($editTopper ?? null) ? $basePath . '/admin/toppers/' . (int)($editTopper['id'] ?? 0) : $basePath . '/admin/toppers' ?>" enctype="multipart/form-data" class="admin-form" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_name" class="admin-form-label">Student Name <span class="admin-required">*</span></label>
                        <input type="text" id="topper_name" name="name" class="admin-form-input" required placeholder="Enter student name"
                               value="<?= htmlspecialchars($editTopper['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_course" class="admin-form-label">Course <span class="admin-required">*</span></label>
                        <select id="topper_course" name="course_id" class="admin-form-input" required>
                            <option value="">-- Select Course --</option>
                            <?php foreach ($courses ?? [] as $course): ?>
                            <option value="<?= (int) $course['id'] ?>" <?= (($editTopper['course_id'] ?? 0) == $course['id']) ? 'selected' : '' ?>>
                                <?= htmlspecialchars($course['name'], ENT_QUOTES, 'UTF-8') ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_branch" class="admin-form-label">Branch</label>
                        <select id="topper_branch" name="branch_id" class="admin-form-input">
                            <option value="">-- Select Branch (Optional) --</option>
                            <?php foreach ($branches ?? [] as $branch): ?>
                            <option value="<?= (int) $branch['id'] ?>" <?= (($editTopper['branch_id'] ?? 0) == $branch['id']) ? 'selected' : '' ?>>
                                <?= htmlspecialchars($branch['name'] . ' - ' . ($branch['city'] ?? ''), ENT_QUOTES, 'UTF-8') ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_marks" class="admin-form-label">Marks / Percentage</label>
                        <input type="text" id="topper_marks" name="marks" class="admin-form-input" placeholder="e.g. 95% or 480/500"
                               value="<?= htmlspecialchars($editTopper['marks'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_grade" class="admin-form-label">Grade</label>
                        <input type="text" id="topper_grade" name="grade" class="admin-form-input" placeholder="e.g. A+, Distinction"
                               value="<?= htmlspecialchars($editTopper['grade'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_year" class="admin-form-label">Batch Year</label>
                        <input type="number" id="topper_year" name="batch_year" class="admin-form-input" placeholder="e.g. 2025" min="2000" max="2099"
                               value="<?= htmlspecialchars($editTopper['batch_year'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_photo" class="admin-form-label">Student Photo</label>
                        <input type="file" id="topper_photo" name="photo" class="admin-form-input" accept="image/jpeg,image/png,image/gif,image/webp"
                               data-preview="topperPhotoPreview">
                        <?php if ($editTopper ?? null): ?>
                        <?php if (!empty($editTopper['photo'])): ?>
                        <div class="admin-current-image">
                            <span>Current:</span>
                            <img src="<?= htmlspecialchars(upload_url($editTopper['photo']), ENT_QUOTES, 'UTF-8') ?>" alt="Current photo" height="60">
                        </div>
                        <?php endif; ?>
                        <?php endif; ?>
                        <div id="topperPhotoPreview" class="admin-image-preview"></div>
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_sort" class="admin-form-label">Sort Order</label>
                        <input type="number" id="topper_sort" name="sort_order" class="admin-form-input" value="<?= (int) ($editTopper['sort_order'] ?? 0) ?>" min="0">
                    </div>
                </div>

                <div class="admin-form-group">
                    <label class="admin-form-label">Active Status</label>
                    <label class="admin-toggle">
                        <input type="checkbox" name="active" value="1" <?= (($editTopper['active'] ?? 1) == 1) ? 'checked' : '' ?>>
                        <span class="admin-toggle-slider"></span>
                        <span class="admin-toggle-label">Show on website</span>
                    </label>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary"><?= ($editTopper ?? null) ? 'Update Topper' : 'Add Topper' ?></button>
                    <a href="<?= $basePath ?>/admin/toppers" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- Add Form (hidden by default, shown on button click) -->
    <?php if (!($showForm ?? false)): ?>
    <div class="admin-card" style="margin-bottom: 24px; display:none;" id="addTopperForm">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Add New Topper</h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $basePath ?>/admin/toppers" enctype="multipart/form-data" class="admin-form" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_name_add" class="admin-form-label">Student Name <span class="admin-required">*</span></label>
                        <input type="text" id="topper_name_add" name="name" class="admin-form-input" required placeholder="Enter student name">
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_course_add" class="admin-form-label">Course <span class="admin-required">*</span></label>
                        <select id="topper_course_add" name="course_id" class="admin-form-input" required>
                            <option value="">-- Select Course --</option>
                            <?php foreach ($courses ?? [] as $course): ?>
                            <option value="<?= (int) $course['id'] ?>"><?= htmlspecialchars($course['name'], ENT_QUOTES, 'UTF-8') ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_branch_add" class="admin-form-label">Branch</label>
                        <select id="topper_branch_add" name="branch_id" class="admin-form-input">
                            <option value="">-- Select Branch (Optional) --</option>
                            <?php foreach ($branches ?? [] as $branch): ?>
                            <option value="<?= (int) $branch['id'] ?>"><?= htmlspecialchars($branch['name'] . ' - ' . ($branch['city'] ?? ''), ENT_QUOTES, 'UTF-8') ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_marks_add" class="admin-form-label">Marks / Percentage</label>
                        <input type="text" id="topper_marks_add" name="marks" class="admin-form-input" placeholder="e.g. 95% or 480/500">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_grade_add" class="admin-form-label">Grade</label>
                        <input type="text" id="topper_grade_add" name="grade" class="admin-form-input" placeholder="e.g. A+, Distinction">
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_year_add" class="admin-form-label">Batch Year</label>
                        <input type="number" id="topper_year_add" name="batch_year" class="admin-form-input" placeholder="e.g. 2025" min="2000" max="2099">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="topper_photo_add" class="admin-form-label">Student Photo</label>
                        <input type="file" id="topper_photo_add" name="photo" class="admin-form-input" accept="image/jpeg,image/png,image/gif,image/webp"
                               data-preview="topperPhotoPreviewAdd">
                        <div id="topperPhotoPreviewAdd" class="admin-image-preview"></div>
                    </div>
                    <div class="admin-form-group">
                        <label for="topper_sort_add" class="admin-form-label">Sort Order</label>
                        <input type="number" id="topper_sort_add" name="sort_order" class="admin-form-input" value="0" min="0">
                    </div>
                </div>

                <div class="admin-form-group">
                    <label class="admin-form-label">Active Status</label>
                    <label class="admin-toggle">
                        <input type="checkbox" name="active" value="1" checked>
                        <span class="admin-toggle-slider"></span>
                        <span class="admin-toggle-label">Show on website</span>
                    </label>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary">Add Topper</button>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- Toppers Table -->
    <div class="admin-card">
        <div class="admin-card-header">
            <h2 class="admin-card-title">All Toppers (<?= count($toppers ?? []) ?>)</h2>
        </div>
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Photo</th>
                            <th>Name</th>
                            <th>Course</th>
                            <th>Marks</th>
                            <th>Year</th>
                            <th>Branch</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($toppers ?? [] as $t): ?>
                        <tr>
                            <td>
                                <?php if (!empty($t['photo'])): ?>
                                <img src="<?= htmlspecialchars(upload_url($t['photo']), ENT_QUOTES, 'UTF-8') ?>" alt="" class="admin-table-thumb" style="border-radius:50%;">
                                <?php else: ?>
                                <div class="admin-table-thumb" style="border-radius:50%;background:#eee;display:flex;align-items:center;justify-content:center;color:#999;font-size:1.2rem;"><?= strtoupper(mb_substr($t['name'] ?? 'S', 0, 1)) ?></div>
                                <?php endif; ?>
                            </td>
                            <td><strong><?= htmlspecialchars($t['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong></td>
                            <td><?= htmlspecialchars($t['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($t['marks'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($t['batch_year'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($t['branch_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td>
                                <?php if (($t['active'] ?? 0) == 1): ?>
                                <span class="admin-badge admin-badge-success">Active</span>
                                <?php else: ?>
                                <span class="admin-badge admin-badge-warning">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <!-- Edit -->
                                    <a href="<?= $basePath ?>/admin/toppers/<?= (int) ($t['id'] ?? 0) ?>/edit" class="admin-btn-icon" title="Edit">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                                    </a>
                                    <!-- Toggle Active -->
                                    <form method="POST" action="<?= $basePath ?>/admin/toppers/<?= (int) ($t['id'] ?? 0) ?>/toggle" style="display:inline">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn-icon" title="<?= ($t['active'] ?? 0) ? 'Deactivate' : 'Activate' ?>" style="border:none;background:none;cursor:pointer;padding:4px;">
                                            <?php if (($t['active'] ?? 0) == 1): ?>
                                            <span style="color:#27ae60;font-weight:600;font-size:0.8rem;">Active</span>
                                            <?php else: ?>
                                            <span style="color:#999;font-size:0.8rem;">Inactive</span>
                                            <?php endif; ?>
                                        </button>
                                    </form>
                                    <!-- Delete -->
                                    <form method="POST" action="<?= $basePath ?>/admin/toppers/<?= (int) ($t['id'] ?? 0) ?>/delete" style="display:inline" onsubmit="return confirm('Delete this topper? The photo will also be removed.')">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn-icon admin-btn-delete" title="Delete">
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if (empty($toppers)): ?>
                        <tr><td colspan="8" style="text-align:center;padding:24px;color:#999;">No toppers yet. Add your first star performer above.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
