<?php
/**
 * @var object $refunds
 * @var string $dateFrom
 * @var string $dateTo
 * @var string $search
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-undo-alt me-2 text-gold"></i>Refunds</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 Refunds</h6>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('pos/refunds') ?>" 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-4">
                <label class="form-label small">Search</label>
                <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                       placeholder="Invoice #, customer, or reason..." 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/refunds') ?>" 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 class="text-end">Refund Amount</th>
                        <th>Reason</th>
                        <th>Refunded By</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($refunds->data)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-undo-alt fa-2x mb-2"></i>
                                <p class="mb-0">No refunds found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($refunds->currentPage - 1) * $refunds->perPage + 1; ?>
                        <?php foreach ($refunds->data as $r): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('invoices/' . $r->invoice_id) ?>" class="text-gold text-decoration-none">
                                        <?= e($r->invoice_number ?? '-') ?>
                                    </a>
                                </td>
                                <td><?= e(($r->customer_first_name ?? '') . ' ' . ($r->customer_last_name ?? '')) ?: '-' ?></td>
                                <td class="text-end text-danger"><?= currency($r->amount) ?></td>
                                <td>
                                    <span class="text-muted small" title="<?= e($r->reason) ?>">
                                        <?= e(truncate($r->reason, 40)) ?>
                                    </span>
                                </td>
                                <td><?= e(($r->user_first_name ?? '') . ' ' . ($r->user_last_name ?? '')) ?></td>
                                <td><?= formatDateTime($r->created_at) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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