<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Demo Class Requests Management -->
<div class="admin-section">
    <!-- Status Summary Cards -->
    <div class="admin-stats-grid" style="grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin-bottom: 24px;">
        <div class="admin-stat-card" style="border-left: 4px solid #f59e0b;">
            <div class="admin-stat-value"><?= (int) ($statusCounts['pending'] ?? 0) ?></div>
            <div class="admin-stat-label" style="color: #f59e0b;">Pending</div>
        </div>
        <div class="admin-stat-card" style="border-left: 4px solid #3b82f6;">
            <div class="admin-stat-value"><?= (int) ($statusCounts['contacted'] ?? 0) ?></div>
            <div class="admin-stat-label" style="color: #3b82f6;">Contacted</div>
        </div>
        <div class="admin-stat-card" style="border-left: 4px solid #8b5cf6;">
            <div class="admin-stat-value"><?= (int) ($statusCounts['scheduled'] ?? 0) ?></div>
            <div class="admin-stat-label" style="color: #8b5cf6;">Scheduled</div>
        </div>
        <div class="admin-stat-card" style="border-left: 4px solid #10b981;">
            <div class="admin-stat-value"><?= (int) ($statusCounts['completed'] ?? 0) ?></div>
            <div class="admin-stat-label" style="color: #10b981;">Completed</div>
        </div>
        <div class="admin-stat-card" style="border-left: 4px solid #ef4444;">
            <div class="admin-stat-value"><?= (int) ($statusCounts['cancelled'] ?? 0) ?></div>
            <div class="admin-stat-label" style="color: #ef4444;">Cancelled</div>
        </div>
    </div>

    <!-- Requests Table -->
    <?php if (!empty($requests)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Student</th>
                            <th>Contact</th>
                            <th>Course / Batch</th>
                            <th>Demo Date</th>
                            <th>Status</th>
                            <th>Submitted</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($requests as $req): ?>
                        <tr style="<?= empty($req['is_read']) ? 'background: #fffbeb; font-weight: 500;' : '' ?>">
                            <td>
                                <strong><?= htmlspecialchars($req['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                                <?php if (empty($req['is_read'])): ?>
                                    <span class="admin-badge admin-badge-warning" style="margin-left:4px;">New</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?= htmlspecialchars($req['email'] ?? '', ENT_QUOTES, 'UTF-8') ?><br>
                                <small><?= htmlspecialchars($req['contact'] ?? '', ENT_QUOTES, 'UTF-8') ?></small>
                            </td>
                            <td>
                                <?php if (!empty($req['course_name'])): ?>
                                    <?= htmlspecialchars($req['course_name'], ENT_QUOTES, 'UTF-8') ?>
                                <?php else: ?>
                                    <span class="admin-text-muted">N/A</span>
                                <?php endif; ?>
                                <?php if (!empty($req['batch'])): ?>
                                    <br><small class="admin-text-muted"><?= htmlspecialchars($req['batch'], ENT_QUOTES, 'UTF-8') ?></small>
                                <?php endif; ?>
                            </td>
                            <td>
                                <strong><?= htmlspecialchars($req['demo_date'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                            </td>
                            <td>
                                <?php
                                $statusColors = [
                                    'pending'   => 'admin-badge-warning',
                                    'contacted' => 'admin-badge-info',
                                    'scheduled' => 'admin-badge-primary',
                                    'completed' => 'admin-badge-success',
                                    'cancelled' => 'admin-badge-danger',
                                ];
                                $status = $req['status'] ?? 'pending';
                                $badgeClass = $statusColors[$status] ?? 'admin-badge-warning';
                                ?>
                                <span class="admin-badge <?= $badgeClass ?>"><?= ucfirst($status) ?></span>
                            </td>
                            <td>
                                <small class="admin-text-muted"><?= date('M j, Y g:i A', strtotime($req['created_at'] ?? 'now')) ?></small>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <!-- Quick Status Update -->
                                    <form method="POST" action="<?= $basePath ?>/admin/demo-class/<?= (int) $req['id'] ?>/status" style="display:inline-flex; align-items:center; gap:4px;">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <input type="hidden" name="admin_note" value="<?= htmlspecialchars($req['admin_note'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <select name="status" onchange="this.form.submit()" style="padding:4px 8px; border:1px solid #d1d5db; border-radius:6px; font-size:0.8rem; cursor:pointer;">
                                            <?php foreach (['pending', 'contacted', 'scheduled', 'completed', 'cancelled'] as $s): ?>
                                            <option value="<?= $s ?>" <?= $status === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </form>
                                    <?php if (empty($req['is_read'])): ?>
                                    <form method="POST" action="<?= $basePath ?>/admin/demo-class/<?= (int) $req['id'] ?>/read" style="display:inline;">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn-icon" title="Mark as Read" style="color:#10b981;">
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
                                        </button>
                                    </form>
                                    <?php endif; ?>
                                    <button type="button" class="admin-btn-icon admin-btn-delete" title="Delete"
                                            data-delete-url="<?= $basePath ?>/admin/demo-class/<?= (int) $req['id'] ?>/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>
                                </div>
                                <?php if (!empty($req['admin_note'])): ?>
                                <div style="margin-top:6px; padding:6px 8px; background:#f0fdf4; border-radius:6px; font-size:0.78rem; color:#166534;">
                                    <strong>Note:</strong> <?= htmlspecialchars($req['admin_note'], ENT_QUOTES, 'UTF-8') ?>
                                </div>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php else: ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <p class="admin-empty-text">No demo class requests found. Requests will appear here when students submit the demo class form on the homepage.</p>
        </div>
    </div>
    <?php endif; ?>
</div>
