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

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

                    <div class="row g-3 mb-1">
                        <div class="col-md-8">
                            <label class="form-label small fw-semibold" for="full_name">Full Name</label>
                            <input type="text" class="form-control" id="full_name" name="full_name" required
                                   value="<?= esc(old('full_name', $guest->full_name ?? '')) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="phone">Phone</label>
                            <input type="text" class="form-control" id="phone" name="phone" placeholder="024XXXXXXX" required
                                   value="<?= esc(old('phone', $guest->phone ?? '')) ?>">
                        </div>
                    </div>

                    <div class="row g-3 mt-1 mb-1">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="email">Email</label>
                            <input type="email" class="form-control" id="email" name="email"
                                   value="<?= esc(old('email', $guest->email ?? '')) ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="segment">Segment <span class="text-muted fw-normal">(optional)</span></label>
                            <select class="form-select" id="segment" name="segment">
                                <option value="">— None —</option>
                                <?php foreach ($lookups->guestSegments as $key => $meta): ?>
                                    <option value="<?= esc($key) ?>" <?= old('segment', $guest->segment ?? '') === $key ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <div class="row g-3 mt-1 mb-1">
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="id_type">ID Type</label>
                            <select class="form-select" id="id_type" name="id_type">
                                <option value="">—</option>
                                <?php foreach ($lookups->guestIdTypes as $key => $meta): ?>
                                    <option value="<?= esc($key) ?>" <?= old('id_type', $guest->id_type ?? '') === $key ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="id_number">ID Number</label>
                            <input type="text" class="form-control" id="id_number" name="id_number"
                                   value="<?= esc(old('id_number', $guest->id_number ?? '')) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="nationality">Nationality / Country</label>
                            <input type="text" class="form-control" id="nationality" name="nationality"
                                   value="<?= esc(old('nationality', $guest->nationality ?? '')) ?>">
                        </div>
                    </div>

                    <div class="row g-3 mt-1 mb-1">
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="date_of_birth">Date of Birth</label>
                            <input type="date" class="form-control" id="date_of_birth" name="date_of_birth"
                                   value="<?= esc(old('date_of_birth', $guest->date_of_birth ?? '')) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="anniversary_date">Anniversary Date <span class="text-muted fw-normal">(optional)</span></label>
                            <input type="date" class="form-control" id="anniversary_date" name="anniversary_date"
                                   value="<?= esc(old('anniversary_date', $guest->anniversary_date ?? '')) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold" for="address">Address</label>
                            <input type="text" class="form-control" id="address" name="address"
                                   value="<?= esc(old('address', $guest->address ?? '')) ?>">
                        </div>
                    </div>

                    <h6 class="text-muted text-uppercase small fw-bold mt-4 mb-2">Stay Preferences <span class="text-muted fw-normal text-lowercase">(optional)</span></h6>
                    <div class="row g-3 mb-1">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="pref_room_type_id">Preferred Room Type</label>
                            <select class="form-select" id="pref_room_type_id" name="pref_room_type_id">
                                <option value="">— None —</option>
                                <?php foreach ($roomTypes as $rt): ?>
                                    <option value="<?= $rt->id ?>" <?= (int) old('pref_room_type_id', $preferences->room_type_id ?? 0) === (int) $rt->id ? 'selected' : '' ?>><?= esc($rt->name) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="pref_bed_type">Bed Type</label>
                            <input type="text" class="form-control" id="pref_bed_type" name="pref_bed_type" placeholder="e.g. King, Twin"
                                   value="<?= esc(old('pref_bed_type', $preferences->bed_type ?? '')) ?>">
                        </div>
                    </div>
                    <div class="row g-3 mt-1 mb-1">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="pref_seating">Restaurant Seating Preference</label>
                            <input type="text" class="form-control" id="pref_seating" name="pref_seating" placeholder="e.g. Window, Quiet area"
                                   value="<?= esc(old('pref_seating', $preferences->seating_preference ?? '')) ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="pref_smoking">Smoking Preference</label>
                            <select class="form-select" id="pref_smoking" name="pref_smoking">
                                <option value="">— None —</option>
                                <?php foreach ($lookups->smokingPreferences as $key => $meta): ?>
                                    <option value="<?= esc($key) ?>" <?= old('pref_smoking', $preferences->smoking_preference ?? '') === $key ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="row g-3 mt-1 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="pref_dietary">Dietary Restrictions</label>
                            <input type="text" class="form-control" id="pref_dietary" name="pref_dietary" placeholder="e.g. Vegetarian, Halal, Gluten-free"
                                   value="<?= esc(old('pref_dietary', $preferences->dietary_restrictions ?? '')) ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="pref_allergies">Allergies</label>
                            <input type="text" class="form-control" id="pref_allergies" name="pref_allergies" placeholder="e.g. Peanuts, Shellfish"
                                   value="<?= esc(old('pref_allergies', $preferences->allergies ?? '')) ?>">
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold" for="pref_other">Other Preferences</label>
                        <input type="text" class="form-control" id="pref_other" name="pref_other"
                               value="<?= esc(old('pref_other', $preferences->other_notes ?? '')) ?>">
                    </div>

                    <div class="mt-3 mb-3">
                        <label class="form-label small fw-semibold" for="notes">Notes</label>
                        <textarea class="form-control" id="notes" name="notes" rows="3" placeholder="Anything special about this guest — room preferences, allergies, VIP handling, ..."><?= esc(old('notes', $guest->notes ?? '')) ?></textarea>
                    </div>

                    <div class="form-check mb-4">
                        <input type="checkbox" class="form-check-input" id="is_blacklisted" name="is_blacklisted" value="1" <?= old('is_blacklisted', $guest->is_blacklisted ?? 0) ? 'checked' : '' ?>>
                        <label class="form-check-label small" for="is_blacklisted">
                            Blacklisted <span class="text-muted">— flag this guest as a problem account (front desk will still see this on their profile)</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</button>
                        <a href="<?= $guest ? site_url('guests/' . $guest->id) : site_url('guests') ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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