<?php
/**
 * @var object $payments
 * @var string $dateFrom
 * @var string $dateTo
 * @var string $method
 * @var string $search
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-credit-card me-2 text-gold"></i>Payments</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Payments</h6>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('pos/payments') ?>" class="row g-2 mb-3">
            <div class="col-md-3">
                <label class="form-label small">Date From</label>
                <input type="date" name="date_from" class="form-control form-control-sm bg-transparent"
                       value="<?= e($dateFrom) ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label small">Date To</label>
                <input type="date" name="date_to" class="form-control form-control-sm bg-transparent"
                       value="<?= e($dateTo) ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small">Method</label>
                <select name="method" class="form-select form-select-sm bg-transparent">
                    <option value="">All Methods</option>
                    <?php foreach (paymentMethods() as $val => $label): ?>
                        <option value="<?= $val ?>" <?= $method === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Search</label>
                <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                       placeholder="Invoice #..." value="<?= e($search) ?>">
            </div>
            <div class="col-md-2 d-flex align-items-end gap-1">
                <button type="submit" class="btn btn-sm btn-gold"><i class="fas fa-filter me-1"></i>Filter</button>
                <a href="<?= url('pos/payments') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-times"></i></a>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Invoice</th>
                        <th>Customer</th>
                        <th>Method</th>
                        <th class="text-end">Amount</th>
                        <th>Reference</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($payments->data)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-credit-card fa-2x mb-2"></i>
                                <p class="mb-0">No payments found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($payments->currentPage - 1) * $payments->perPage + 1; ?>
                        <?php foreach ($payments->data as $p): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('invoices/' . $p->invoice_id) ?>" class="text-gold text-decoration-none">
                                        <?= e($p->invoice_number ?? '-') ?>
                                    </a>
                                </td>
                                <td><?= e(($p->customer_first_name ?? '') . ' ' . ($p->customer_last_name ?? '')) ?: '-' ?></td>
                                <td>
                                    <span class="badge bg-info">
                                        <?= e(ucwords(str_replace('_', ' ', $p->payment_method))) ?>
                                    </span>
                                </td>
                                <td class="text-end"><?= currency($p->amount) ?></td>
                                <td><?= e($p->reference_number ?? '-') ?></td>
                                <td><?= formatDateTime($p->created_at) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($payments->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($payments->currentPage - 1) * $payments->perPage) + 1 ?>
                    to <?= min($payments->currentPage * $payments->perPage, $payments->total) ?>
                    of <?= $payments->total ?> entries
                </div>
                <?= paginateLinks($payments, url('payments')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>
