<?php
/**
 * @var object $points
 * @var array $customers
 * @var string $customerId
 * @var string $type
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-star me-2 text-gold"></i>Loyalty Points</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">Points Transactions</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('loyalty/rules') ?>" class="btn btn-sm btn-outline-gold">
                <i class="fas fa-cog me-1"></i>Rules
            </a>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('loyalty') ?>" class="row mb-3 g-2">
            <div class="col-md-4">
                <select name="customer_id" class="form-select form-select-sm bg-transparent">
                    <option value="">All Customers</option>
                    <?php foreach ($customers as $c): ?>
                        <option value="<?= $c->id ?>" <?= $customerId == $c->id ? 'selected' : '' ?>>
                            <?= e($c->first_name . ' ' . $c->last_name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <select name="type" class="form-select form-select-sm bg-transparent">
                    <option value="">All Types</option>
                    <option value="earned" <?= $type === 'earned' ? 'selected' : '' ?>>Earned</option>
                    <option value="redeemed" <?= $type === 'redeemed' ? 'selected' : '' ?>>Redeemed</option>
                    <option value="expired" <?= $type === 'expired' ? 'selected' : '' ?>>Expired</option>
                    <option value="adjusted" <?= $type === 'adjusted' ? 'selected' : '' ?>>Adjusted</option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-sm btn-gold w-100">
                    <i class="fas fa-filter me-1"></i>Filter
                </button>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Date</th>
                        <th>Customer</th>
                        <th>Type</th>
                        <th class="text-end">Points</th>
                        <th>Reference</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($points->data)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-star fa-2x mb-2"></i>
                                <p class="mb-0">No points transactions found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($points->currentPage - 1) * $points->perPage + 1; ?>
                        <?php foreach ($points->data as $p): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= formatDateTime($p->created_at) ?></td>
                                <td>
                                    <strong><?= e($p->first_name . ' ' . $p->last_name) ?></strong>
                                    <small class="text-muted d-block"><?= e($p->phone) ?></small>
                                </td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($p->type) ?>">
                                        <?= e(ucfirst($p->type)) ?>
                                    </span>
                                </td>
                                <td class="text-end fw-bold <?= in_array($p->type, ['redeemed', 'expired']) ? 'text-danger' : 'text-success' ?>">
                                    <?= $p->points > 0 ? '+' : '' ?><?= (int)$p->points ?>
                                </td>
                                <td><code><?= e($p->reference ?? '-') ?></code></td>
                                <td><?= e(truncate($p->description ?? '-', 40)) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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