<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Dashboard Stat Cards -->
<div class="admin-stats-grid">
    <div class="admin-stat-card admin-stat-students">
        <div class="admin-stat-icon">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87"/><path d="M16 3.13a4 4 0 010 7.75"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-number"><?= number_format($studentCount ?? 0) ?></span>
            <span class="admin-stat-label">Total Students</span>
        </div>
    </div>

    <div class="admin-stat-card admin-stat-courses">
        <div class="admin-stat-icon">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 19.5A2.5 2.5 0 016.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 014 19.5v-15A2.5 2.5 0 016.5 2z"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-number"><?= number_format($courseCount ?? 0) ?></span>
            <span class="admin-stat-label">Active Courses</span>
        </div>
    </div>

    <div class="admin-stat-card admin-stat-branches">
        <div class="admin-stat-icon">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-number"><?= number_format($branchCount ?? 0) ?></span>
            <span class="admin-stat-label">Branches</span>
        </div>
    </div>

    <div class="admin-stat-card admin-stat-testimonials">
        <div class="admin-stat-icon">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-number"><?= number_format($pendingTestimonials ?? 0) ?></span>
            <span class="admin-stat-label">Pending Testimonials</span>
        </div>
    </div>
</div>

<!-- Practice Test Traffic Stats -->
<?php $ps = $practiceStats ?? []; ?>
<div class="admin-card" style="margin-top: 24px;">
    <div class="admin-card-header">
        <h2 class="admin-card-title">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20" style="vertical-align:middle;margin-right:6px;"><path d="M2 3h6a4 4 0 014 4v14a3 3 0 00-3-3H2z"/><path d="M22 3h-6a4 4 0 00-4 4v14a3 3 0 013-3h7z"/></svg>
            Practice Test Traffic & Statistics
        </h2>
        <div class="admin-card-actions">
            <a href="<?= $basePath ?>/admin/practice-sets" class="admin-btn admin-btn-sm admin-btn-secondary">Manage Practice Sets</a>
        </div>
    </div>
    <div class="admin-card-body">
        <div class="admin-stats-grid" style="margin-bottom:0;">
            <div class="admin-stat-card" style="background:linear-gradient(135deg,#0d2b6e,#1a4fa0);color:#fff;">
                <div class="admin-stat-info" style="width:100%;text-align:center;">
                    <span class="admin-stat-number" style="color:#fff;"><?= number_format($ps['total_attempts'] ?? 0) ?></span>
                    <span class="admin-stat-label" style="color:rgba(255,255,255,0.8);">Total Practice Attempts</span>
                </div>
            </div>
            <div class="admin-stat-card" style="background:linear-gradient(135deg,#27ae60,#2ecc71);color:#fff;">
                <div class="admin-stat-info" style="width:100%;text-align:center;">
                    <span class="admin-stat-number" style="color:#fff;"><?= number_format($ps['attempts_today'] ?? 0) ?></span>
                    <span class="admin-stat-label" style="color:rgba(255,255,255,0.8);">Attempts Today</span>
                </div>
            </div>
            <div class="admin-stat-card" style="background:linear-gradient(135deg,#f39c12,#e67e22);color:#fff;">
                <div class="admin-stat-info" style="width:100%;text-align:center;">
                    <span class="admin-stat-number" style="color:#fff;"><?= number_format(($ps['unique_students'] ?? 0) + ($ps['unique_guests'] ?? 0)) ?></span>
                    <span class="admin-stat-label" style="color:rgba(255,255,255,0.8);">Unique Students (<?= $ps['unique_students'] ?? 0 ?> logged in + <?= $ps['unique_guests'] ?? 0 ?> guests)</span>
                </div>
            </div>
            <div class="admin-stat-card" style="background:linear-gradient(135deg,#8e44ad,#9b59b6);color:#fff;">
                <div class="admin-stat-info" style="width:100%;text-align:center;">
                    <span class="admin-stat-number" style="color:#fff;"><?= number_format($ps['avg_percentage'] ?? 0, 1) ?>%</span>
                    <span class="admin-stat-label" style="color:rgba(255,255,255,0.8);">Avg Score | Pass: <?= $ps['pass_count'] ?? 0 ?> / Fail: <?= $ps['fail_count'] ?? 0 ?></span>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Recent Practice Attempts -->
<?php if (!empty($ps['recent_attempts'])): ?>
<div class="admin-card" style="margin-top: 16px;">
    <div class="admin-card-header">
        <h2 class="admin-card-title">Recent Practice Attempts</h2>
    </div>
    <div class="admin-card-body">
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Student Name</th>
                        <th>Test</th>
                        <th>Score</th>
                        <th>Percentage</th>
                        <th>Status</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($ps['recent_attempts'] as $attempt): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($attempt['student_name'] ?? 'Guest', ENT_QUOTES, 'UTF-8') ?></strong></td>
                        <td><?= htmlspecialchars($attempt['set_title'] ?? 'Unknown', ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= (float) ($attempt['total_obtained'] ?? 0) ?> / <?= (int) ($attempt['total_marks'] ?? 0) ?></td>
                        <td><?= number_format((float) ($attempt['percentage'] ?? 0), 1) ?>%</td>
                        <td>
                            <?php if (($attempt['status'] ?? '') === 'pass'): ?>
                            <span class="admin-badge admin-badge-success">Pass</span>
                            <?php else: ?>
                            <span class="admin-badge admin-badge-danger">Fail</span>
                            <?php endif; ?>
                        </td>
                        <td><?= date('M j, Y g:i A', strtotime($attempt['created_at'] ?? 'now')) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Quick Actions & Recent Activity -->
<div class="admin-dashboard-grid">
    <!-- Quick Actions -->
    <div class="admin-card">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Quick Actions</h2>
        </div>
        <div class="admin-card-body">
            <div class="admin-quick-actions">
                <a href="<?= $basePath ?>/admin/courses/create" class="admin-quick-action-btn">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
                    Add Course
                </a>
                <a href="<?= $basePath ?>/admin/exams/create" class="admin-quick-action-btn">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
                    Create Exam
                </a>
                <a href="<?= $basePath ?>/admin/sliders/create" class="admin-quick-action-btn">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
                    Add Slider
                </a>
                <a href="<?= $basePath ?>/admin/testimonials" class="admin-quick-action-btn">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
                    Review Testimonials
                </a>
                <a href="<?= $basePath ?>/admin/settings" class="admin-quick-action-btn">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83 0 2 2 0 010-2.83l.06-.06A1.65 1.65 0 004.68 15a1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 012.83-2.83l.06.06A1.65 1.65 0 009 4.68a1.65 1.65 0 001-1.51V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 2.83l-.06.06A1.65 1.65 0 0019.4 9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
                    Site Settings
                </a>
                <a href="<?= $basePath ?>/admin/faqs/create" class="admin-quick-action-btn">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
                    Add FAQ
                </a>
            </div>
        </div>
    </div>

    <!-- Recent Contacts -->
    <div class="admin-card">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Recent Contacts</h2>
            <?php if (($unreadContacts ?? 0) > 0): ?>
            <span class="admin-badge admin-badge-warning"><?= (int) $unreadContacts ?> unread</span>
            <?php endif; ?>
        </div>
        <div class="admin-card-body">
            <?php if (empty($recentContacts)): ?>
            <p class="admin-empty-text">No contact submissions yet.</p>
            <?php else: ?>
            <div class="admin-table-responsive">
                <table class="admin-table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Subject</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentContacts as $contact): ?>
                        <tr>
                            <td><?= htmlspecialchars($contact['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($contact['subject'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= date('M j, Y', strtotime($contact['created_at'] ?? 'now')) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Recent Exam Results -->
<?php if (!empty($recentResults)): ?>
<div class="admin-card" style="margin-top: 24px;">
    <div class="admin-card-header">
        <h2 class="admin-card-title">Recent Exam Results</h2>
    </div>
    <div class="admin-card-body">
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Student</th>
                        <th>Exam</th>
                        <th>Score</th>
                        <th>Status</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recentResults as $result): ?>
                    <tr>
                        <td><?= htmlspecialchars($result['student_name'] ?? 'Unknown', ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= htmlspecialchars($result['exam_title'] ?? 'Unknown', ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= (int) ($result['total_obtained'] ?? 0) ?> / <?= (int) ($result['total_marks'] ?? 0) ?></td>
                        <td>
                            <?php if (($result['status'] ?? '') === 'pass'): ?>
                            <span class="admin-badge admin-badge-success">Pass</span>
                            <?php else: ?>
                            <span class="admin-badge admin-badge-danger">Fail</span>
                            <?php endif; ?>
                        </td>
                        <td><?= date('M j, Y', strtotime($result['completed_at'] ?? $result['created_at'] ?? 'now')) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php endif; ?>
