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

<?php
$existingAmenities = $roomType ? (new \App\Models\RoomTypeModel())->amenitiesList($roomType) : [];
$curatedKeys        = array_keys(\App\Models\RoomTypeModel::CURATED_AMENITIES);
$checkedAmenities    = old('amenities', array_values(array_intersect($existingAmenities, $curatedKeys)));
$otherAmenities      = old('amenities_other', implode(', ', array_diff($existingAmenities, $curatedKeys)));
?>

<div class="row justify-content-center">
    <div class="col-lg-7">
        <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> <?= $roomType ? 'Edit Room Type' : 'New Room Type' ?>
            </div>
            <div class="card-body">
                <form action="<?= $roomType ? site_url('room-types/' . $roomType->id) : site_url('room-types') ?>" method="post" enctype="multipart/form-data">
                    <?= csrf_field() ?>

                    <div class="row g-3 mb-3">
                        <div class="col-md-8">
                            <label class="form-label small fw-semibold" for="name">Name</label>
                            <input type="text" class="form-control" id="name" name="name" required
                                   value="<?= esc(old('name', $roomType->name ?? '')) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="code">Code <span class="text-muted fw-normal">(optional)</span></label>
                            <input type="text" class="form-control text-uppercase" id="code" name="code" maxlength="10" placeholder="e.g. DLX"
                                   value="<?= esc(old('code', $roomType->code ?? '')) ?>">
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label small fw-semibold" for="description">Description</label>
                        <textarea class="form-control" id="description" name="description" rows="3"><?= esc(old('description', $roomType->description ?? '')) ?></textarea>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="base_rate">Base Rate (GHS / night)</label>
                            <input type="number" step="0.01" min="0" class="form-control" id="base_rate" name="base_rate" required
                                   value="<?= esc(old('base_rate', $roomType->base_rate ?? '')) ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="capacity">Capacity (guests)</label>
                            <input type="number" min="1" class="form-control" id="capacity" name="capacity" required
                                   value="<?= esc(old('capacity', $roomType->capacity ?? 2)) ?>">
                        </div>
                    </div>

                    <div class="row align-items-end mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Photo <span class="text-muted fw-normal">(JPG, PNG, or WEBP — max 2MB)</span></label>
                            <input type="file" class="form-control" name="photo" accept="image/jpeg,image/png,image/webp">
                        </div>
                        <div class="col-md-6">
                            <?php if ($roomType && $roomType->photo_path): ?>
                                <div class="text-muted small mb-1">Current photo</div>
                                <img src="<?= site_url('uploads/' . $roomType->photo_path) ?>" alt="<?= esc($roomType->name) ?>" style="max-height: 60px; max-width: 60px; object-fit: cover; border-radius: 6px;" class="border p-1">
                            <?php else: ?>
                                <div class="text-muted small">No photo uploaded yet.</div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Amenities</label>
                        <div class="row row-cols-2 row-cols-md-3 g-2 mb-2">
                            <?php foreach (\App\Models\RoomTypeModel::CURATED_AMENITIES as $label => $icon): ?>
                                <div class="col">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="amenities[]" value="<?= esc($label) ?>" id="am_<?= md5($label) ?>"
                                               <?= in_array($label, $checkedAmenities, true) ? 'checked' : '' ?>>
                                        <label class="form-check-label small" for="am_<?= md5($label) ?>"><i class="bi <?= $icon ?> me-1 text-muted"></i><?= esc($label) ?></label>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <label class="form-label small text-muted" for="amenities_other">Other amenities <span class="fw-normal">(comma-separated)</span></label>
                        <input type="text" class="form-control form-control-sm" id="amenities_other" name="amenities_other" value="<?= esc($otherAmenities) ?>">
                    </div>

                    <div class="form-check form-switch mb-4">
                        <input class="form-check-input" type="checkbox" role="switch" id="is_active" name="is_active" value="1"
                               <?= old('is_active', $roomType->is_active ?? 1) ? 'checked' : '' ?>>
                        <label class="form-check-label small fw-semibold" for="is_active">Active <span class="text-muted fw-normal">— offered for new bookings</span></label>
                    </div>

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