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

<h1 class="h3 mb-3"><?= $plan ? 'Edit Rate Plan' : 'New Rate Plan' ?></h1>

<div class="card shadow-sm" style="max-width: 640px;">
    <div class="card-body">
        <form action="<?= $plan ? site_url('rate-plans/' . $plan->id) : site_url('rate-plans') ?>" method="post">
            <?= csrf_field() ?>

            <div class="mb-3">
                <label class="form-label">Name</label>
                <input type="text" class="form-control" name="name" required value="<?= esc(old('name', $plan->name ?? '')) ?>" placeholder="e.g. Christmas &amp; New Year">
            </div>

            <div class="mb-3">
                <label class="form-label">Description <span class="text-muted fw-normal">(optional)</span></label>
                <textarea class="form-control" name="description" rows="2" maxlength="255" placeholder="A short note shown on the Rate Plans list, e.g. Peak season premium rates for holidays and festivities."><?= esc(old('description', $plan->description ?? '')) ?></textarea>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Start Date</label>
                    <input type="date" class="form-control" name="date_start" required value="<?= esc(old('date_start', $plan->date_start ?? '')) ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">End Date</label>
                    <input type="date" class="form-control" name="date_end" required value="<?= esc(old('date_end', $plan->date_end ?? '')) ?>">
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Adjustment Type</label>
                    <select class="form-select" name="adjustment_type" required>
                        <option value="flat" <?= ($plan->adjustment_type ?? '') === 'flat' ? 'selected' : '' ?>>Flat rate (GHS override)</option>
                        <option value="percentage" <?= ($plan->adjustment_type ?? '') === 'percentage' ? 'selected' : '' ?>>Percentage adjustment</option>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Value</label>
                    <input type="number" step="0.01" class="form-control" name="adjustment_value" required value="<?= esc(old('adjustment_value', $plan->adjustment_value ?? '')) ?>">
                    <div class="form-text">Flat: the override rate in GHS. Percentage: e.g. 15 for +15%, -10 for 10% off.</div>
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label">Applies to Room Types</label>
                <?php foreach ($roomTypes as $type): ?>
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="room_type_ids[]" value="<?= $type->id ?>" id="rt<?= $type->id ?>"
                               <?= in_array($type->id, $selectedTypeIds, true) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="rt<?= $type->id ?>"><?= esc($type->name) ?></label>
                    </div>
                <?php endforeach; ?>
            </div>

            <div class="form-check mb-3">
                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="is_active" <?= ($plan->is_active ?? 1) ? 'checked' : '' ?>>
                <label class="form-check-label" for="is_active">Active</label>
            </div>

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

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