<?php $this->start('body');
    $types = $this->data['types'] ?? [];
    $sel   = $this->v('type');
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-receipt me-2 text-muted"></i>Customer Receipts</h5>
</div>

<div class="mb-2 d-flex flex-wrap gap-1">
    <a href="<?= PROOT; ?>Receipts" class="badge <?= $sel===''?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">All</a>
    <?php foreach ($types as $k => $t): ?>
        <a href="<?= PROOT; ?>Receipts?type=<?= $k; ?>" class="badge <?= $sel===$k?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">
            <i class="<?= $t['icon']; ?> me-1"></i><?= htmlspecialchars($t['label']); ?>
        </a>
    <?php endforeach; ?>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Receipt #</th><th>Type</th><th>Date</th><th>Invoice</th><th>Customer</th>
                    <th>EFD #</th><th>Method</th><th class="text-end">Amount</th><th class="text-end">Of which VAT</th><th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($this->data['rows'] as $r):
                    $t = $types[$r['receipt_type']] ?? ['label'=>$r['receipt_type'],'icon'=>'fas fa-receipt'];
                    $cls = ['EFD_VAT'=>'bg-success','EFD_NON_VAT'=>'bg-info text-white','NORMAL'=>'bg-light text-muted'][$r['receipt_type']] ?? 'bg-light text-muted';
                    $invoiceRef = $r['sale_no'] ?: $r['invoice_ref'] ?: '—';
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>Receipts/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['receipt_no']); ?></strong></a></td>
                        <td><span class="badge <?= $cls; ?>"><i class="<?= $t['icon']; ?> me-1"></i><?= htmlspecialchars($t['label']); ?></span></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['receipt_date']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($invoiceRef); ?></td>
                        <td><?= htmlspecialchars($r['customer_name']); ?></td>
                        <td class="text-muted small">
                            <?php if ($r['efd_receipt_number']): ?>
                                <code class="text-body"><?= htmlspecialchars($r['efd_receipt_number']); ?></code>
                                <div class="text-muted" style="font-size:.65rem;"><?= htmlspecialchars($r['efd_serial']); ?></div>
                            <?php else: ?>—<?php endif; ?>
                        </td>
                        <td class="text-muted"><?= htmlspecialchars(ucwords(strtolower(str_replace('_',' ',$r['payment_method'])))); ?><?php if ($r['bank_reference']): ?><div class="text-muted small"><?= htmlspecialchars($r['bank_reference']); ?></div><?php endif; ?></td>
                        <td class="text-end" style="font-weight:600;"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['amount']); ?></td>
                        <td class="text-end text-muted small"><?= (float)$r['efd_output_vat'] > 0 ? number_format((float)$r['efd_output_vat']) : '—'; ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Receipts/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($this->data['rows'])): ?>
                    <tr><td colspan="10" class="text-center text-muted py-4">No receipts <?= $sel ? 'of type ' . htmlspecialchars($sel) : ''; ?>. Record one from an issued invoice.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->end(); ?>
