<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-file-alt mr-2"></i>Member Report</h1>
    <div>
        <a href="<?= $baseUrl ?>/reports/members" class="glass-btn btn-sm"><i class="fas fa-arrow-left mr-1"></i>Back to List</a>
        <a href="<?= $baseUrl ?>/members/<?= $user->id ?>" class="glass-btn btn-sm"><i class="fas fa-user mr-1"></i>Member Profile</a>
        <button onclick="window.print()" class="glass-btn glass-btn-primary btn-sm"><i class="fas fa-print mr-1"></i>Print</button>
    </div>
</div>

<div class="row">
    <div class="col-lg-4 col-md-5 mb-4">
        <div class="glass-card text-center">
            <img src="<?= $baseUrl ?>/img/undraw_profile.svg" class="rounded-circle mb-3" style="width: 100px; height: 100px; object-fit: cover; background: rgba(255,255,255,0.1); padding: 5px;">
            <h4 class="font-weight-bold"><?= htmlspecialchars($user->first_name ? trim($user->first_name . ' ' . $user->last_name) : $user->name) ?></h4>
            <p style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($user->email) ?></p>
            <?php if ($profile): ?>
                <?php if ($profile->mobile_phone): ?><p style="color: rgba(255,255,255,0.5); font-size: 0.9rem;"><i class="fas fa-phone mr-1"></i><?= htmlspecialchars($profile->mobile_phone) ?></p><?php endif; ?>
                <?php if ($profile->institution_hospital): ?><p style="color: rgba(255,255,255,0.5); font-size: 0.9rem;"><i class="fas fa-hospital mr-1"></i><?= htmlspecialchars($profile->institution_hospital) ?></p><?php endif; ?>
                <?php if ($profile->membership_category): ?><span class="glass-badge"><?= htmlspecialchars($profile->membership_category) ?></span><?php endif; ?>
            <?php endif; ?>
            <div class="mt-3" style="border-top: 1px solid rgba(255,255,255,0.1); padding-top: 15px;">
                <div class="row">
                    <div class="col-6">
                        <div style="font-size: 1.5rem; font-weight: 700; color: #fff;">GHS <?= number_format($totalContributions, 2) ?></div>
                        <div style="color: rgba(255,255,255,0.5); font-size: 0.8rem;">Total Paid</div>
                    </div>
                    <div class="col-6">
                        <div style="font-size: 1.5rem; font-weight: 700; color: <?= count($obligations) > 0 ? '#e74a3b' : '#1cc88a' ?>;"><?= count(array_filter($obligations, fn($o) => !$o->is_fully_paid)) ?></div>
                        <div style="color: rgba(255,255,255,0.5); font-size: 0.8rem;">Obligations Due</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-8 col-md-7 mb-4">
        <div class="glass-card mb-4">
            <h5 class="font-weight-bold mb-3"><i class="fas fa-filter mr-2"></i>Filter by Contribution Type</h5>
            <form method="GET" class="form-inline">
                <select name="contribution_type_id" class="form-control glass-input mr-2" style="min-width: 250px;">
                    <option value="">-- All Assigned Types --</option>
                    <?php foreach ($allTypes as $t): ?>
                    <option value="<?= $t->id ?>" <?= $filterTypeId == $t->id ? 'selected' : '' ?>><?= htmlspecialchars($t->name) ?> (GHS <?= number_format($t->amount, 2) ?>)</option>
                    <?php endforeach; ?>
                </select>
                <button type="submit" class="glass-btn glass-btn-primary"><i class="fas fa-filter mr-1"></i>Apply</button>
                <?php if ($filterTypeId): ?>
                <a href="<?= $baseUrl ?>/reports/members/<?= $user->id ?>" class="glass-btn btn-sm ml-2"><i class="fas fa-times mr-1"></i>Clear</a>
                <?php endif; ?>
            </form>
        </div>

        <?php if (empty($billingMonths)): ?>
        <div class="empty-state"><i class="fas fa-file-invoice"></i><p>No contribution types assigned to this member yet</p></div>
        <?php else: ?>
            <?php foreach ($billingMonths as $bm): ?>
            <div class="glass-card mb-4">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <div>
                        <h5 class="font-weight-bold mb-0"><?= htmlspecialchars($bm->type_name) ?></h5>
                        <span style="color: rgba(255,255,255,0.5); font-size: 0.85rem;">
                            <i class="fas fa-calendar-alt mr-1"></i>Assigned: <?= date('M Y', strtotime($bm->assigned_at)) ?>
                            <span class="ml-3"><i class="fas fa-sync-alt mr-1"></i><?= ucfirst($bm->frequency) ?></span>
                            <?php if ($bm->duration_months): ?><span class="ml-3"><i class="fas fa-hourglass-half mr-1"></i><?= $bm->duration_months ?> months</span><?php endif; ?>
                            <span class="ml-3"><i class="fas fa-money-bill mr-1"></i>GHS <?= number_format($bm->expected_amount, 2) ?>/<?= ['daily' => 'day', 'weekly' => 'wk', 'monthly' => 'mo', 'quarterly' => 'qrt', 'annual' => 'yr', 'one-time' => ''][$bm->frequency] ?? 'mo' ?></span>
                        </span>
                    </div>
                    <div class="text-right">
                        <div style="font-size: 1.2rem; font-weight: 700; color: <?= $bm->owed_count > 0 ? '#e74a3b' : '#1cc88a' ?>;">
                            <?= $bm->owed_count ?> / <?= $bm->total_months ?> owed
                        </div>
                        <div style="color: rgba(255,255,255,0.5); font-size: 0.8rem;">
                            GHS <?= number_format($bm->total_paid, 2) ?> paid of GHS <?= number_format($bm->total_billed, 2) ?>
                        </div>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-sm" style="margin-bottom: 0;">
                        <thead>
                            <tr>
                                <th>Period</th>
                                <th>Expected (GHS)</th>
                                <th>Paid (GHS)</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($bm->billed_months as $bmn): ?>
                            <tr>
                                <td><?= htmlspecialchars($bmn['label']) ?></td>
                                <td><?= number_format($bmn['expected'], 2) ?></td>
                                <td><?= number_format($bmn['paid'], 2) ?></td>
                                <td>
                                    <?php if ($bmn['status'] === 'paid'): ?>
                                        <span class="badge badge-success">Paid</span>
                                    <?php elseif ($bmn['status'] === 'partial'): ?>
                                        <span class="badge badge-warning">Partial</span>
                                    <?php else: ?>
                                        <span class="badge badge-danger">Unpaid</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <?php endforeach; ?>
        <?php endif; ?>

        <div class="glass-card mb-4">
            <h5 class="font-weight-bold mb-3"><i class="fas fa-calendar-check mr-2"></i>Events Attended</h5>
            <?php if (!empty($eventsAttended)): ?>
            <div class="table-responsive">
                <table class="table table-sm" style="margin-bottom: 0;">
                    <thead><tr><th>Event</th><th>Date</th><th>Status</th></tr></thead>
                    <tbody>
                        <?php foreach ($eventsAttended as $ea): ?>
                        <tr>
                            <td><?= htmlspecialchars($ea->event_title) ?></td>
                            <td><?= date('M d, Y', strtotime($ea->start_datetime)) ?></td>
                            <td>
                                <?php if ($ea->status === 'attended'): ?>
                                    <span class="badge badge-success">Attended</span>
                                <?php elseif ($ea->status === 'registered'): ?>
                                    <span class="badge badge-info">Registered</span>
                                <?php elseif ($ea->status === 'absent'): ?>
                                    <span class="badge badge-secondary">Absent</span>
                                <?php else: ?>
                                    <span class="badge badge-dark"><?= ucfirst($ea->status) ?></span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <p style="color: rgba(255,255,255,0.5);">No event attendance recorded</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<style media="print">
body { background: #fff !important; color: #000 !important; }
.glass-card { background: #fff !important; border: 1px solid #ddd !important; box-shadow: none !important; }
.glass-btn { display: none !important; }
.table { color: #000 !important; }
.badge-success { background: #28a745 !important; color: #fff !important; }
.badge-warning { background: #ffc107 !important; color: #000 !important; }
.badge-danger { background: #dc3545 !important; color: #fff !important; }
.badge-info { background: #17a2b8 !important; color: #fff !important; }
h1, h4, h5 { color: #000 !important; }
</style>
