<?php
/**
 * @var object $cards
 * @var string $status
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-gift me-2 text-gold"></i>Gift Cards</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 Gift Cards</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('loyalty_giftcards_create')): ?>
                <a href="<?= url('gift-cards/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Create Gift Card
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-3">
                <select class="form-select form-select-sm bg-transparent" onchange="window.location.href=this.value">
                    <option value="<?= url('gift-cards') ?>">All Status</option>
                    <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="inactive" <?= $status === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                    <option value="expired" <?= $status === 'expired' ? 'selected' : '' ?>>Expired</option>
                    <option value="redeemed" <?= $status === 'redeemed' ? 'selected' : '' ?>>Redeemed</option>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Card Number</th>
                        <th>Customer</th>
                        <th class="text-end">Balance</th>
                        <th>Expiry Date</th>
                        <th>Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($cards->data)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-gift fa-2x mb-2"></i>
                                <p class="mb-0">No gift cards found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($cards->currentPage - 1) * $cards->perPage + 1; ?>
                        <?php foreach ($cards->data as $card): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><code class="text-gold"><?= e($card->card_number) ?></code></td>
                                <td><?= e($card->first_name . ' ' . $card->last_name) ?></td>
                                <td class="text-end fw-bold"><?= currency($card->current_balance) ?></td>
                                <td><?= formatDate($card->expiry_date) ?></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($card->status) ?>">
                                        <?= e(ucfirst($card->status)) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <?php if ($card->status === 'active' && can('loyalty_giftcards_reload')): ?>
                                        <button type="button" class="btn btn-sm btn-outline-success btn-reload"
                                                data-id="<?= $card->id ?>"
                                                data-number="<?= e($card->card_number) ?>"
                                                title="Reload">
                                            <i class="fas fa-plus-circle"></i>
                                        </button>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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

<div class="modal fade" id="reloadModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content bg-dark">
            <div class="modal-header border-secondary">
                <h6 class="modal-title"><i class="fas fa-plus-circle text-gold me-2"></i>Reload Gift Card</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" id="reloadForm">
                <?= csrf_field() ?>
                <div class="modal-body">
                    <p>Card: <strong class="text-gold" id="reloadCardNumber"></strong></p>
                    <div class="mb-3">
                        <label class="form-label">Amount to Add</label>
                        <div class="input-group">
                            <span class="input-group-text bg-transparent border-gold"><?= APP_CURRENCY_SYMBOL ?></span>
                            <input type="number" step="0.01" name="amount" class="form-control bg-transparent" required min="0">
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">Reload Card</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
$('.btn-reload').click(function() {
    const id = $(this).data('id');
    const number = $(this).data('number');
    $('#reloadCardNumber').text(number);
    $('#reloadForm').attr('action', '__URL__/gift-cards/reload/' + id);
    $('#reloadModal').modal('show');
});
EOS;
$inlineScripts = str_replace('__URL__', rtrim(APP_URL, '/'), $inlineScripts);
?>
