<?php $title = 'Member Dashboard - Ghana Anaesthesiologists Society'; $extraScripts = '<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>'; ?>

<?php if ($completionPct < 100): ?>
<div class="reminder-banner">
    <i class="fas fa-exclamation-triangle"></i>
    <div class="flex-grow-1">
        <strong>Your membership profile is <?= $completionPct ?>% complete.</strong>
        Please complete your profile to access all member features.
    </div>
    <a href="<?= $baseUrl ?>/member/profile" class="glass-btn glass-btn-warning btn-sm">Complete Now</a>
</div>
<?php endif; ?>

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold">Member Dashboard</h1>
    <a href="<?= $baseUrl ?>/member/profile" class="glass-btn"><i class="fas fa-id-card mr-1"></i>Edit Profile</a>
</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">Profile Completion</div>
                    <div class="stat-value"><?= $completionPct ?>%</div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(78,115,223,0.3); color: #4e73df;"><i class="fas fa-id-card"></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">Total Contributions</div>
                    <div class="stat-value">GHS <?= number_format($totalContributions, 2) ?></div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(28,200,138,0.3); color: #1cc88a;"><i class="fas fa-hand-holding-usd"></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">Events Attended</div>
                    <div class="stat-value"><?= count($recentAttendance) ?></div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(54,185,204,0.3); color: #36b9cc;"><i class="fas fa-calendar-check"></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">Unread Messages</div>
                    <div class="stat-value"><?= $unreadMessages ?></div>
                </div>
                <div class="col-auto"><div class="stat-icon" style="background: rgba(246,194,62,0.3); color: #f6c23e;"><i class="fas fa-envelope"></i></div></div>
            </div>
        </div>
    </div>
</div>

<?php if ($totalArrears > 0): ?>
<div class="row mb-4">
    <div class="col-12">
        <div class="glass-card" style="border-left: 4px solid #e74a3b;">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h5 class="font-weight-bold mb-1" style="color: #e74a3b;"><i class="fas fa-exclamation-triangle mr-2"></i>Outstanding Balance</h5>
                    <p class="mb-0" style="color: rgba(255,255,255,0.6);">You have <strong style="color: #fff;"><?= count($unpaidObligations) ?></strong> unpaid obligation(s) totaling</p>
                </div>
                <div class="text-right">
                    <div style="font-size: 2rem; font-weight: 700; color: #e74a3b;">GHS <?= number_format($totalArrears, 2) ?></div>
                    <a href="<?= $baseUrl ?>/payment/make" class="glass-btn glass-btn-primary mt-2"><i class="fas fa-credit-card mr-1"></i>Pay Now</a>
                </div>
            </div>
            <?php if (!empty($unpaidObligations)): ?>
            <div class="mt-3" style="border-top: 1px solid rgba(255,255,255,0.1); padding-top: 12px;">
                <?php foreach (array_slice($unpaidObligations, 0, 5) as $o): ?>
                <div class="d-flex justify-content-between align-items-center mb-1" style="font-size: 0.9rem;">
                    <span style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($o->contribution_type_name) ?> — <?= date('M Y', strtotime($o->paid_month . '-01')) ?></span>
                    <span style="color: #f6c23e; font-weight: 600;">GHS <?= number_format($o->balance, 2) ?></span>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<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-bullhorn mr-2 text-primary"></i>Quick Links</h5>
            <div class="list-group list-group-flush">
                <a href="<?= $baseUrl ?>/announcements" class="list-group-item list-group-item-action" style="background: transparent; color: white; border-color: rgba(255,255,255,0.1);"><i class="fas fa-bullhorn mr-2"></i>View Announcements</a>
                <a href="<?= $baseUrl ?>/events" class="list-group-item list-group-item-action" style="background: transparent; color: white; border-color: rgba(255,255,255,0.1);"><i class="fas fa-calendar-alt mr-2"></i>Upcoming Events</a>
                <a href="<?= $baseUrl ?>/forum" class="list-group-item list-group-item-action" style="background: transparent; color: white; border-color: rgba(255,255,255,0.1);"><i class="fas fa-comments mr-2"></i>Forum Discussions</a>
                <a href="<?= $baseUrl ?>/documents" class="list-group-item list-group-item-action" style="background: transparent; color: white; border-color: rgba(255,255,255,0.1);"><i class="fas fa-folder-open mr-2"></i>Document Library</a>
                <a href="<?= $baseUrl ?>/messages/compose" class="list-group-item list-group-item-action" style="background: transparent; color: white; border-color: rgba(255,255,255,0.1);"><i class="fas fa-envelope mr-2"></i>Send a Message</a>
                <a href="<?= $baseUrl ?>/contributions/member" class="list-group-item list-group-item-action" style="background: transparent; color: white; border-color: rgba(255,255,255,0.1);"><i class="fas fa-hand-holding-usd mr-2"></i>My Contributions</a>
            </div>
        </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-clock mr-2 text-warning"></i>Recent Activity</h5>
            <?php if (!empty($recentAttendance)): ?>
                <?php foreach (array_slice($recentAttendance, 0, 5) as $att): ?>
                    <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><i class="fas fa-calendar-check mr-2 text-info"></i><?= htmlspecialchars($att->event_title ?? 'Event') ?></div>
                        <small style="color: rgba(255,255,255,0.5);"><?= isset($att->start_datetime) ? date('M d, Y', strtotime($att->start_datetime)) : '' ?></small>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="empty-state"><i class="fas fa-calendar-alt"></i><p>No recent activity</p></div>
            <?php endif; ?>
        </div>
    </div>
</div>
