<?php $title = 'Receipts'; ?>
<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-receipt mr-2"></i>Receipts</h1>
    <a href="<?= $baseUrl ?>/contributions/member" class="glass-btn"><i class="fas fa-arrow-left mr-1"></i>My Contributions</a>
</div>

<div class="glass-card mb-4">
    <form method="GET" class="form-inline">
        <?php if ($isAdmin): ?>
        <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>
        <?php endif; ?>
        <div class="form-group mr-2 mb-2">
            <label style="color: rgba(255,255,255,0.7); margin-right: 6px;">From:</label>
            <input type="date" name="from" class="form-control glass-input" value="<?= htmlspecialchars($from) ?>">
        </div>
        <div class="form-group mr-2 mb-2">
            <label style="color: rgba(255,255,255,0.7); margin-right: 6px;">To:</label>
            <input type="date" name="to" class="form-control glass-input" value="<?= htmlspecialchars($to) ?>">
        </div>
        <button type="submit" class="glass-btn btn-sm mb-2"><i class="fas fa-filter mr-1"></i>Filter</button>
        <a href="<?= $baseUrl ?>/receipts" class="glass-btn btn-sm mb-2 ml-1"><i class="fas fa-times mr-1"></i>Clear</a>
    </form>
</div>

<div class="glass-table">
    <table class="table">
        <thead><tr><th>Receipt #</th><th>Date</th><?php if ($isAdmin): ?><th>Member</th><?php endif; ?><th>Type</th><th>Amount</th><th>Payment Date</th><th>Action</th></tr></thead>
        <tbody>
            <?php if (!empty($receipts)): ?>
                <?php foreach ($receipts as $r): ?>
                <tr>
                    <td><strong><?= htmlspecialchars($r->receipt_number) ?></strong></td>
                    <td><?= date('M d, Y', strtotime($r->issued_date)) ?></td>
                    <?php if ($isAdmin): ?>
                    <td><?= htmlspecialchars($r->user_name ?? 'N/A') ?></td>
                    <?php endif; ?>
                    <td><?= htmlspecialchars($r->contribution_type_name ?? 'N/A') ?></td>
                    <td>GHS <?= number_format($r->amount ?? 0, 2) ?></td>
                    <td><?= isset($r->payment_date) ? date('M d, Y', strtotime($r->payment_date)) : '-' ?></td>
                    <td><a href="<?= $baseUrl ?>/contributions/receipt/<?= $r->id ?>/download" class="glass-btn btn-sm" target="_blank"><i class="fas fa-download mr-1"></i>Download</a></td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="<?= $isAdmin ? 7 : 6 ?>" class="text-center" style="color: rgba(255,255,255,0.5);">No receipts found for the selected period</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
