<?php
/**
 * @var object $memberships
 * @var string $status
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-id-card me-2 text-gold"></i>Memberships</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 Memberships</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('membership/tiers') ?>" class="btn btn-sm btn-outline-gold me-2">
                <i class="fas fa-layer-group me-1"></i>Tiers
            </a>
            <?php if (can('loyalty_memberships_assign')): ?>
                <a href="<?= url('membership/assign') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Assign Membership
                </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('membership') ?>">All Status</option>
                    <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="expired" <?= $status === 'expired' ? 'selected' : '' ?>>Expired</option>
                    <option value="cancelled" <?= $status === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Customer</th>
                        <th>Contact</th>
                        <th>Tier</th>
                        <th>Start Date</th>
                        <th>End Date</th>
                        <th>Status</th>
                        <th width="100" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($memberships->data)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-id-card fa-2x mb-2"></i>
                                <p class="mb-0">No memberships found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($memberships->currentPage - 1) * $memberships->perPage + 1; ?>
                        <?php foreach ($memberships->data as $m): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <strong><?= e($m->customer_first_name . ' ' . $m->customer_last_name) ?></strong>
                                </td>
                                <td>
                                    <small class="text-muted"><?= e($m->customer_email) ?><br><?= e($m->customer_phone) ?></small>
                                </td>
                                <td>
                                    <span class="badge bg-gold"><?= e($m->tier_name) ?></span>
                                </td>
                                <td><?= formatDate($m->start_date) ?></td>
                                <td><?= $m->end_date ? formatDate($m->end_date) : '-' ?></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($m->status) ?>">
                                        <?= e(ucfirst($m->status)) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <?php if ($m->status === 'active' && can('loyalty_memberships_cancel')): ?>
                                        <button type="button" class="btn btn-sm btn-outline-danger btn-cancel"
                                                data-id="<?= $m->id ?>" title="Cancel Membership">
                                            <i class="fas fa-ban"></i>
                                        </button>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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


