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

<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-door-closed" style="color: var(--brand);"></i> <?= $room ? 'Edit Room' : 'New Room' ?>
            </div>
            <div class="card-body">
                <form action="<?= $room ? site_url('rooms/' . $room->id) : site_url('rooms') ?>" method="post">
                    <?= csrf_field() ?>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="room_number">Room Number</label>
                            <input type="text" class="form-control" id="room_number" name="room_number" required
                                   value="<?= esc(old('room_number', $room->room_number ?? '')) ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="floor">Floor <span class="text-muted fw-normal">(optional)</span></label>
                            <input type="text" class="form-control" id="floor" name="floor"
                                   value="<?= esc(old('floor', $room->floor ?? '')) ?>">
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label small fw-semibold" for="room_type_id">Room Type</label>
                        <select class="form-select" id="room_type_id" name="room_type_id" required>
                            <option value="">Select a room type…</option>
                            <?php foreach ($roomTypes as $type): ?>
                                <option value="<?= $type->id ?>" <?= (old('room_type_id', $room->room_type_id ?? '') == $type->id) ? 'selected' : '' ?>>
                                    <?= esc($type->name) ?> — <?= format_money($type->base_rate) ?>/night
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="mb-4">
                        <label class="form-label small fw-semibold" for="status">Status</label>
                        <select class="form-select" id="status" name="status">
                            <?php foreach ($lookups->roomStatuses as $key => $meta): ?>
                                <option value="<?= esc($key) ?>" <?= (old('status', $room->status ?? 'clean') === $key) ? 'selected' : '' ?>>
                                    <?= esc($meta['label']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check2 me-1"></i>Save Room</button>
                        <a href="<?= $room ? site_url('rooms/' . $room->id) : site_url('rooms') ?>" class="btn btn-outline-secondary">Cancel</a>
                        <?php if ($room): ?>
                            <form action="<?= site_url('rooms/' . $room->id . '/delete') ?>" method="post" class="ms-auto" onsubmit="return confirm('Delete this room?');">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-outline-danger"><i class="bi bi-trash me-1"></i>Delete</button>
                            </form>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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