<?php $title = 'Contributions'; ?>
<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-hand-holding-usd mr-2"></i>Contributions</h1>
    <?php if ($isAdmin): ?>
    <a href="<?= $baseUrl ?>/contributions/create" class="glass-btn glass-btn-primary"><i class="fas fa-plus mr-1"></i>Record Contribution</a>
    <?php endif; ?>
</div>

<?php if ($isAdmin): ?>
<div class="glass-card mb-4">
    <form method="GET" class="form-inline">
        <div class="form-group mr-2 mb-2">
            <select name="user_id" class="form-control glass-input" style="min-width: 200px;">
                <option value="">All Members</option>
                <?php foreach ($members as $m): ?>
                <option value="<?= $m->id ?>" <?= ($_GET['user_id'] ?? '') == $m->id ? 'selected' : '' ?>><?= htmlspecialchars($m->first_name ? trim($m->first_name . ' ' . $m->last_name) : $m->name) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="glass-btn btn-sm mb-2"><i class="fas fa-filter mr-1"></i>Filter</button>
        <a href="<?= $baseUrl ?>/contributions" class="glass-btn btn-sm mb-2 ml-1"><i class="fas fa-times mr-1"></i>Clear</a>
    </form>
</div>
<?php endif; ?>

<div class="glass-table">
    <table class="table">
        <thead><tr>
            <th>Date</th>
            <?php if ($isAdmin): ?><th>Member</th><?php endif; ?>
            <th>Type</th><th>Amount</th><th>Balance</th><th>Method</th><th>Month</th><th>Status</th><th>Action</th>
        </tr></thead>
        <tbody>
            <?php if (!empty($contributions)): ?>
                <?php foreach ($contributions as $c): ?>
                <tr>
                    <td><?= date('M d, Y', strtotime($c->payment_date)) ?></td>
                    <?php if ($isAdmin): ?>
                    <td><?= htmlspecialchars($c->user_name ?? 'N/A') ?></td>
                    <?php endif; ?>
                    <td><?= htmlspecialchars($c->contribution_type_name ?? 'N/A') ?></td>
                    <td><strong>GHS <?= number_format($c->amount, 2) ?></strong></td>
                    <td>
                        <?php if (($c->balance ?? 0) > 0): ?>
                        <span style="color: #f6c23e; font-weight: 600;">GHS <?= number_format($c->balance, 2) ?></span>
                        <?php else: ?>
                        <span style="color: rgba(255,255,255,0.3);">-</span>
                        <?php endif; ?>
                    </td>
                    <td><?= htmlspecialchars(ucwords(str_replace('_', ' ', $c->payment_method ?? '-'))) ?></td>
                    <td><?= htmlspecialchars($c->paid_month ?? '-') ?></td>
                    <td>
                        <?php if ($c->status === 'completed'): ?>
                        <span class="glass-badge" style="background: rgba(28,200,138,0.2); color: #1cc88a;">Completed</span>
                        <?php elseif ($c->status === 'partial'): ?>
                        <span class="glass-badge" style="background: rgba(246,194,62,0.2); color: #f6c23e;">Partial</span>
                        <?php elseif ($c->status === 'pending'): ?>
                        <span class="glass-badge" style="background: rgba(246,194,62,0.2); color: #f6c23e;">Pending</span>
                        <?php else: ?>
                        <span class="glass-badge" style="background: rgba(231,74,59,0.2); color: #e74a3b;"><?= htmlspecialchars(ucfirst($c->status)) ?></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($isAdmin): ?>
                            <a href="<?= $baseUrl ?>/contributions/<?= $c->id ?>/edit" class="glass-btn btn-sm" title="Edit"><i class="fas fa-edit"></i></a>
                            <?php if (($c->balance ?? 0) > 0): ?>
                            <a href="<?= $baseUrl ?>/contributions/add-payment?user_id=<?= $c->user_id ?>&type_id=<?= $c->contribution_type_id ?>&month=<?= urlencode($c->paid_month) ?>" class="glass-btn btn-sm" style="border-color: rgba(246,194,62,0.3); color: #f6c23e;" title="Add Installment Payment"><i class="fas fa-plus-circle"></i></a>
                            <?php endif; ?>
                            <?php if ($c->status === 'pending'): ?>
                            <form method="POST" action="<?= $baseUrl ?>/contributions/<?= $c->id ?>/approve" style="display:inline;" onsubmit="return confirm('Approve this contribution? A receipt will be generated.');">
                                <button type="submit" class="glass-btn btn-sm" style="border-color: rgba(28,200,138,0.3); color: #1cc88a;" title="Approve"><i class="fas fa-check"></i></button>
                            </form>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php if (!empty($c->receipt_id)): ?>
                        <a href="<?= $baseUrl ?>/contributions/receipt/<?= $c->receipt_id ?>/download" class="glass-btn btn-sm" title="Download Receipt" target="_blank"><i class="fas fa-receipt"></i></a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="<?= $isAdmin ? 9 : 8 ?>" class="text-center" style="color: rgba(255,255,255,0.5);">No contributions recorded</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php if ($isAdmin && $pagination && $pagination->lastPage > 1): ?>
<div class="d-flex justify-content-center mt-3">
    <nav><ul class="pagination" style="gap: 4px;">
        <?php for ($i = 1; $i <= $pagination->lastPage; $i++): ?>
        <li class="page-item <?= $i == $pagination->page ? 'active' : '' ?>">
            <a class="glass-btn btn-sm" href="?page=<?= $i ?><?= !empty($_GET['user_id']) ? '&user_id=' . (int)$_GET['user_id'] : '' ?>"><?= $i ?></a>
        </li>
        <?php endfor; ?>
    </ul></nav>
</div>
<?php endif; ?>
