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

<?php $featuresText = $plan ? implode("\n", (new \App\Models\Control\PlanModel())->featuresList($plan)) : ''; ?>

<div class="row justify-content-center">
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header"><?= $plan ? 'Edit Plan' : 'New Plan' ?></div>
            <div class="card-body">
                <form action="<?= $plan ? site_url('admin/plans/' . $plan->id) : site_url('admin/plans') ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Name</label>
                            <input type="text" class="form-control" name="name" value="<?= esc(old('name', $plan->name ?? '')) ?>" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Slug</label>
                            <input type="text" class="form-control" name="slug" value="<?= esc(old('slug', $plan->slug ?? '')) ?>" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Price (GHS)</label>
                            <input type="text" class="form-control" name="price" value="<?= esc(old('price', $plan->price ?? '')) ?>" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Billing Interval</label>
                            <select name="billing_interval" class="form-select">
                                <?php foreach ((new \Config\Lookups())->planBillingIntervals as $key => $i): ?>
                                    <option value="<?= $key ?>" <?= ($plan->billing_interval ?? 'monthly') === $key ? 'selected' : '' ?>><?= esc($i['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Max Rooms</label>
                            <input type="number" class="form-control" name="max_rooms" value="<?= esc(old('max_rooms', $plan->max_rooms ?? '')) ?>" placeholder="Blank = unlimited">
                        </div>
                        <div class="col-12">
                            <label class="form-label small fw-semibold">Description</label>
                            <input type="text" class="form-control" name="description" value="<?= esc(old('description', $plan->description ?? '')) ?>">
                        </div>
                        <div class="col-12">
                            <label class="form-label small fw-semibold">Features (one per line)</label>
                            <textarea class="form-control" name="features" rows="4"><?= esc(old('features', $featuresText)) ?></textarea>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Sort Order</label>
                            <input type="number" class="form-control" name="sort_order" value="<?= esc(old('sort_order', $plan->sort_order ?? 0)) ?>">
                        </div>
                        <div class="col-md-8 d-flex align-items-end">
                            <div class="form-check">
                                <input type="checkbox" class="form-check-input" id="is_active" name="is_active" value="1" <?= ($plan->is_active ?? 1) ? 'checked' : '' ?>>
                                <label class="form-check-label small" for="is_active">Active (available to assign to new hotels)</label>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex gap-2 mt-3">
                        <button type="submit" class="btn btn-primary">Save Plan</button>
                        <a href="<?= site_url('admin/plans') ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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