<?php $title = 'Dashboard - Ghana Anaesthesiologists Society'; $extraScripts = '<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>'; ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold">Dashboard</h1>
    <div>
        <a href="<?= $baseUrl ?>/member/dashboard" class="glass-btn mr-2"><i class="fas fa-user-circle mr-1"></i>My Dashboard</a>
        <?php if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?>
        <a href="<?= $baseUrl ?>/users/create" class="glass-btn mr-2"><i class="fas fa-user-plus mr-1"></i>Create Member</a>
        <a href="<?= $baseUrl ?>/users/pending" class="glass-btn glass-btn-primary"><i class="fas fa-clock mr-1"></i>Pending Approvals <?= isset($pendingCount) && $pendingCount > 0 ? '<span class="badge badge-danger ml-1">' . $pendingCount . '</span>' : '' ?></a>
        <?php endif; ?>
    </div>
</div>
<div class="row">
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="glass-stat-card border-left-primary">
            <div class="row no-gutters align-items-center">
                <div class="col mr-2">
                    <div class="stat-label">Total Members</div>
                    <div class="stat-value"><?= $stats->totalUsers ?? 0 ?></div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(78,115,223,0.3);"><i class="fas fa-users" style="color: #4e73df;"></i></div></div>
            </div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="glass-stat-card border-left-success">
            <div class="row no-gutters align-items-center">
                <div class="col mr-2">
                    <div class="stat-label">Active Members</div>
                    <div class="stat-value"><?= $stats->activeUsers ?? 0 ?></div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(28,200,138,0.3);"><i class="fas fa-user-check" style="color: #1cc88a;"></i></div></div>
            </div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="glass-stat-card border-left-info">
            <div class="row no-gutters align-items-center">
                <div class="col mr-2">
                    <div class="stat-label">Activity Logs</div>
                    <div class="stat-value"><?= $stats->totalAuditLogs ?? 0 ?></div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(54,185,204,0.3);"><i class="fas fa-list" style="color: #36b9cc;"></i></div></div>
            </div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="glass-stat-card border-left-warning">
            <div class="row no-gutters align-items-center">
                <div class="col mr-2">
                    <div class="stat-label">Upcoming Events</div>
                    <div class="stat-value"><?= $stats->upcomingEvents ?? 0 ?></div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(246,194,62,0.3);"><i class="fas fa-calendar" style="color: #f6c23e;"></i></div></div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-6 mb-4">
        <div class="glass-card h-100">
            <h5 class="font-weight-bold mb-3"><i class="fas fa-users mr-2 text-primary"></i>Recent Members</h5>
            <?php if (!empty($recentUsers)): ?>
                <?php foreach ($recentUsers as $user): ?>
                <div class="d-flex justify-content-between align-items-center mb-2" style="border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 8px;">
                    <div>
                        <span class="font-weight-bold"><?= htmlspecialchars($user->first_name ? trim($user->first_name . ' ' . $user->last_name) : $user->name) ?></span>
                        <small class="d-block" style="color: rgba(255,255,255,0.5);"><?= htmlspecialchars($user->email) ?></small>
                    </div>
                    <span class="glass-badge badge-<?= ($user->approval_status ?? '') === 'approved' ? 'success' : (($user->approval_status ?? '') === 'pending' ? 'warning' : 'danger') ?>"><?= htmlspecialchars($user->approval_status ?? 'N/A') ?></span>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <p style="color: rgba(255,255,255,0.5);">No recent members</p>
            <?php endif; ?>
        </div>
    </div>
    <div class="col-lg-6 mb-4">
        <div class="glass-card h-100">
            <h5 class="font-weight-bold mb-3"><i class="fas fa-calendar-check mr-2 text-warning"></i>Upcoming Events</h5>
            <?php if (!empty($upcomingEvents)): ?>
                <?php foreach ($upcomingEvents as $evt): ?>
                <div class="d-flex justify-content-between align-items-center mb-2" style="border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 8px;">
                    <div>
                        <span class="font-weight-bold"><?= htmlspecialchars($evt->title ?? '') ?></span>
                        <small class="d-block" style="color: rgba(255,255,255,0.5);"><?= $evt->start_datetime ? date('M d, Y H:i', strtotime($evt->start_datetime)) : '' ?></small>
                    </div>
                    <span class="glass-badge"><?= htmlspecialchars($evt->event_type ?? '') ?></span>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <p style="color: rgba(255,255,255,0.5);">No upcoming events</p>
            <?php endif; ?>
        </div>
    </div>
</div>
