<?php
/**
 * @var array $tiers
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-layer-group me-2 text-gold"></i>Membership Tiers</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">Manage Tiers</h6>
        </div>
        <div class="card-header-right">
            <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#tierModal">
                <i class="fas fa-plus me-1"></i>Add Tier
            </button>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Level</th>
                        <th>Name</th>
                        <th class="text-end">Min Points</th>
                        <th>Benefits</th>
                        <th>Status</th>
                        <th width="80" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($tiers)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-layer-group fa-2x mb-2"></i>
                                <p class="mb-0">No tiers defined</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = 1; ?>
                        <?php foreach ($tiers as $tier): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><span class="badge bg-gold">Level <?= (int)$tier->priority ?></span></td>
                                <td><strong><?= e($tier->name) ?></strong></td>
                                <td class="text-end"><?= number_format((int)$tier->min_points) ?></td>
                                <td><?= e(truncate($tier->benefits ?? '-', 60)) ?></td>
                                <td>
                                    <span class="badge bg-<?= $tier->is_active ? 'success' : 'secondary' ?>">
                                        <?= $tier->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <button type="button" class="btn btn-sm btn-outline-gold btn-edit-tier"
                                            data-id="<?= $tier->id ?>"
                                            data-name="<?= e($tier->name) ?>"
                                            data-level="<?= (int)$tier->priority ?>"
                                            data-min_points="<?= (int)$tier->min_points ?>"
                                            data-benefits="<?= e($tier->benefits ?? '') ?>"
                                            data-is_active="<?= $tier->is_active ?>"
                                            title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="tierModal" 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><span id="tierModalTitle">Add</span> Tier</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('membership/tiers/store') ?>">
                <?= csrf_field() ?>
                <input type="hidden" name="id" id="tierId">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Tier Name</label>
                        <input type="text" name="name" id="tierName" class="form-control bg-transparent" required maxlength="100">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Tier Level</label>
                        <input type="number" name="tier_level" id="tierLevel" class="form-control bg-transparent" required min="1" max="10">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Minimum Points</label>
                        <input type="number" name="min_points" id="tierMinPoints" class="form-control bg-transparent" required min="0">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Benefits</label>
                        <textarea name="benefits" id="tierBenefits" class="form-control bg-transparent" rows="3" maxlength="2000"></textarea>
                    </div>
                    <div class="mb-3">
                        <div class="form-check form-switch">
                            <input type="hidden" name="is_active" value="0">
                            <input type="checkbox" name="is_active" id="tierIsActive" class="form-check-input" value="1" checked>
                            <label class="form-check-label" for="tierIsActive">Active</label>
                        </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">Save Tier</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
$('.btn-edit-tier').click(function() {
    $('#tierModalTitle').text('Edit');
    $('#tierId').val($(this).data('id'));
    $('#tierName').val($(this).data('name'));
    $('#tierLevel').val($(this).data('level'));
    $('#tierMinPoints').val($(this).data('min_points'));
    $('#tierBenefits').val($(this).data('benefits'));
    $('#tierIsActive').prop('checked', $(this).data('is_active') == 1);
    $('#tierModal').modal('show');
});

$('#tierModal').on('hidden.bs.modal', function() {
    $('#tierModalTitle').text('Add');
    $('#tierId').val('');
    $('#tierName').val('');
    $('#tierLevel').val('');
    $('#tierMinPoints').val('');
    $('#tierBenefits').val('');
    $('#tierIsActive').prop('checked', true);
});
EOS;
?>
