<?php $title = 'Arrears Management'; ?>
<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-exclamation-triangle text-warning mr-2"></i>Arrears</h1>
    <div>
        <span class="glass-badge mr-2" style="background: rgba(231,74,59,0.2); color: #e74a3b; font-size: 1rem;">
            Total GHS <?= number_format($totalArrearsAll, 2) ?>
        </span>
        <span class="glass-badge" style="font-size: 1rem;">
            <?= $totalMembers ?> member<?= $totalMembers !== 1 ? 's' : '' ?> in arrears
        </span>
    </div>
</div>

<div class="glass-card mb-4">
    <form method="GET" class="form-inline">
        <input type="text" name="search" class="form-control glass-input mr-2" style="min-width: 250px;" placeholder="Search member name or email..." value="<?= htmlspecialchars($search) ?>">
        <button type="submit" class="glass-btn btn-sm mr-2"><i class="fas fa-search mr-1"></i>Search</button>
        <a href="<?= $baseUrl ?>/contributions/arrears" class="glass-btn btn-sm"><i class="fas fa-times mr-1"></i>Clear</a>
    </form>
</div>

<?php if ($detailMember && !empty($detail)): ?>
<div class="glass-card mb-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h5 class="font-weight-bold mb-0"><i class="fas fa-user mr-2"></i><?= htmlspecialchars($detailMember->first_name ? trim($detailMember->first_name . ' ' . $detailMember->last_name) : $detailMember->name) ?> — Arrears Breakdown</h5>
        <a href="<?= $baseUrl ?>/contributions/arrears<?= $search ? '?search=' . urlencode($search) : '' ?>" class="glass-btn btn-sm"><i class="fas fa-arrow-left mr-1"></i>Back to All</a>
    </div>
    <?php
    $totalDetail = 0;
    foreach ($detail as $d) { $totalDetail += $d->balance; }
    ?>
    <p style="color: rgba(255,255,255,0.5);">Total outstanding: <strong style="color: #e74a3b;">GHS <?= number_format($totalDetail, 2) ?></strong> across <?= count($detail) ?> obligation(s)</p>
    <div class="glass-table">
        <table class="table">
            <thead><tr>
                <th>Contribution Type</th><th>Period</th><th>Expected (GHS)</th><th>Paid (GHS)</th><th>Balance (GHS)</th><th>Payments</th><th>Last Payment</th><th>Status</th>
            </tr></thead>
            <tbody>
                <?php foreach ($detail as $d): ?>
                <tr>
                    <td><strong><?= htmlspecialchars($d->contribution_type_name) ?></strong></td>
                    <td><?= htmlspecialchars($d->paid_month) ?></td>
                    <td><?= number_format($d->total_amount ?: $d->expected_amount, 2) ?></td>
                    <td><?= number_format($d->total_paid, 2) ?></td>
                    <td><strong style="color: #e74a3b;"><?= number_format($d->balance, 2) ?></strong></td>
                    <td><?= $d->payment_count ?></td>
                    <td><?= $d->last_payment_date ? date('M d, Y', strtotime($d->last_payment_date)) : '-' ?></td>
                    <td>
                        <?php if ($d->is_overdue): ?>
                        <span class="glass-badge" style="background: rgba(231,74,59,0.2); color: #e74a3b;">Overdue</span>
                        <?php else: ?>
                        <span class="glass-badge" style="background: rgba(246,194,62,0.2); color: #f6c23e;">Current</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<div class="glass-card p-0">
    <?php if (!empty($members)): ?>
    <div class="glass-table">
        <table class="table">
            <thead><tr>
                <th>#</th><th>Member</th><th>Email</th><th>Phone</th><th>Total Arrears (GHS)</th><th>Unpaid Obligations</th><th>Earliest Arrear</th><th>Latest Arrear</th><th>Actions</th>
            </tr></thead>
            <tbody>
                <?php $i = 1; foreach ($members as $m): ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td><strong><?= htmlspecialchars($m->first_name ? trim($m->first_name . ' ' . $m->last_name) : $m->name) ?></strong></td>
                    <td><?= htmlspecialchars($m->email) ?></td>
                    <td><?= htmlspecialchars($m->mobile_phone ?: $m->alternative_phone ?: '-') ?></td>
                    <td><strong style="color: #e74a3b;"><?= number_format($m->total_arrears, 2) ?></strong></td>
                    <td><?= $m->unpaid_obligations ?></td>
                    <td><?= htmlspecialchars($m->earliest_arrear_month) ?></td>
                    <td><?= htmlspecialchars($m->latest_arrear_month) ?></td>
                    <td>
                        <a href="<?= $baseUrl ?>/contributions/arrears?member_id=<?= $m->id ?><?= $search ? '&search=' . urlencode($search) : '' ?>" class="glass-btn btn-sm"><i class="fas fa-eye mr-1"></i>View</a>
                        <a href="<?= $baseUrl ?>/contributions?user_id=<?= $m->id ?>" class="glass-btn btn-sm"><i class="fas fa-list mr-1"></i>History</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div class="empty-state"><i class="fas fa-check-circle text-success" style="font-size: 3rem;"></i><p>No members in arrears. All obligations are fully paid!</p></div>
    <?php endif; ?>
</div>