<?= $this->extend('admin/layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <p class="text-muted mb-0">Subscription tiers sold to hotels.</p>
    <a href="<?= site_url('admin/plans/new') ?>" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>New Plan</a>
</div>

<div class="row g-3">
    <?php if (empty($plans)): ?>
        <div class="col-12"><div class="card"><div class="card-body text-center text-muted py-5">No plans yet — create one to start onboarding hotels with billing.</div></div></div>
    <?php endif; ?>
    <?php foreach ($plans as $p): $features = (new \App\Models\Control\PlanModel())->featuresList($p); ?>
        <div class="col-lg-4 col-md-6">
            <div class="card h-100 <?= ! $p->is_active ? 'opacity-50' : '' ?>">
                <div class="card-body d-flex flex-column">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <h5 class="fw-bold mb-0"><?= esc($p->name) ?></h5>
                        <span class="badge bg-<?= $p->is_active ? 'success' : 'secondary' ?>"><?= $p->is_active ? 'Active' : 'Inactive' ?></span>
                    </div>
                    <div class="mb-2">
                        <span class="fs-3 fw-bold"><?= format_money($p->price) ?></span>
                        <span class="text-muted small">/ <?= esc($p->billing_interval) ?></span>
                    </div>
                    <div class="text-muted small mb-3"><?= $p->max_rooms ? "Up to {$p->max_rooms} rooms" : 'Unlimited rooms' ?></div>
                    <?php if ($p->description): ?><p class="small text-muted"><?= esc($p->description) ?></p><?php endif; ?>
                    <ul class="list-unstyled small flex-grow-1">
                        <?php foreach ($features as $f): ?>
                            <li class="mb-1"><i class="bi bi-check-circle-fill text-success me-1"></i><?= esc($f) ?></li>
                        <?php endforeach; ?>
                    </ul>
                    <div class="d-flex gap-2 mt-3">
                        <a href="<?= site_url('admin/plans/' . $p->id . '/edit') ?>" class="btn btn-sm btn-outline-primary flex-grow-1">Edit</a>
                        <form action="<?= site_url('admin/plans/' . $p->id . '/toggle') ?>" method="post" class="flex-grow-1">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline-secondary w-100"><?= $p->is_active ? 'Deactivate' : 'Activate' ?></button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<?= $this->endSection() ?>
