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

<?= view('bookings/_wizard_steps', ['step' => 3]) ?>

<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-door-open" style="color: var(--brand);"></i> Room &amp; Dates
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-3">
                        <div class="text-muted small text-uppercase fw-bold mb-1">Room</div>
                        <div class="fw-semibold"><?= esc($room->room_number) ?></div>
                        <div class="text-muted small"><?= esc($room->room_type_name) ?></div>
                    </div>
                    <div class="col-md-3">
                        <div class="text-muted small text-uppercase fw-bold mb-1">Rate</div>
                        <div class="fw-semibold"><?= format_money($resolvedRate) ?> <span class="text-muted fw-normal">/ night</span></div>
                        <?php if ($resolvedRate !== $room->base_rate): ?>
                            <span class="badge bg-info text-dark">Rate plan applied</span>
                            <span class="text-muted small text-decoration-line-through ms-1"><?= format_money($room->base_rate) ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="col-md-3">
                        <div class="text-muted small text-uppercase fw-bold mb-1">Check-in</div>
                        <div class="fw-semibold"><?= esc(date('d M Y', strtotime($checkIn))) ?></div>
                    </div>
                    <div class="col-md-3">
                        <div class="text-muted small text-uppercase fw-bold mb-1">Check-out</div>
                        <div class="fw-semibold"><?= esc(date('d M Y', strtotime($checkOut))) ?></div>
                    </div>
                </div>
            </div>
        </div>

        <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 &amp; Booking Details
            </div>
            <div class="card-body">
                <form action="<?= site_url('bookings') ?>" method="post" id="booking-form">
                    <?= csrf_field() ?>
                    <input type="hidden" name="room_id" value="<?= $room->id ?>">
                    <input type="hidden" name="check_in_date" value="<?= esc($checkIn) ?>">
                    <input type="hidden" name="check_out_date" value="<?= esc($checkOut) ?>">
                    <input type="hidden" name="rate_applied" value="<?= esc($resolvedRate) ?>">
                    <input type="hidden" name="guest_id" id="guest_id" value="0">

                    <div class="mb-3">
                        <label class="form-label small fw-semibold" for="phone">Phone</label>
                        <div class="input-group icon-input-group">
                            <span class="input-group-text"><i class="bi bi-telephone"></i></span>
                            <input type="text" class="form-control" id="phone" name="phone" placeholder="024XXXXXXX" required>
                            <button type="button" class="btn btn-outline-secondary" id="lookup-btn"><i class="bi bi-search me-1"></i>Look up</button>
                        </div>
                        <div id="lookup-result" class="form-text"></div>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="full_name">Full Name</label>
                            <div class="input-group icon-input-group">
                                <span class="input-group-text"><i class="bi bi-person"></i></span>
                                <input type="text" class="form-control" id="full_name" name="full_name" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold" for="email">Email <span class="text-muted fw-normal">(optional)</span></label>
                            <div class="input-group icon-input-group">
                                <span class="input-group-text"><i class="bi bi-envelope"></i></span>
                                <input type="email" class="form-control" id="email" name="email">
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label small fw-semibold" for="source">Booking Source</label>
                        <div class="input-group icon-input-group">
                            <span class="input-group-text"><i class="bi bi-signpost-split"></i></span>
                            <select class="form-select" id="source" name="source">
                                <?php foreach ($lookups->bookingSources as $key => $meta): ?>
                                    <option value="<?= esc($key) ?>"><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label small fw-semibold" for="notes">Notes <span class="text-muted fw-normal">(optional)</span></label>
                        <textarea class="form-control" id="notes" name="notes" rows="2" placeholder="Special requests, arrival time, ..."></textarea>
                    </div>

                    <div class="mb-4">
                        <label class="form-label small fw-semibold" for="deposit_amount">Deposit Required <span class="text-muted fw-normal">(GHS, optional)</span></label>
                        <div class="input-group icon-input-group">
                            <span class="input-group-text"><i class="bi bi-cash-coin"></i></span>
                            <input type="number" step="0.01" min="0" class="form-control" id="deposit_amount" name="deposit_amount" placeholder="Leave blank for no deposit">
                        </div>
                        <div class="info-box mt-2 d-flex gap-2">
                            <i class="bi bi-info-circle" style="color: var(--brand);"></i>
                            <div class="small text-muted">If set, the booking is held as <strong class="text-dark">Pending Deposit</strong> and a Hubtel payment link is sent to the guest — it only becomes Confirmed once they pay.</div>
                        </div>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check2-circle me-1"></i>Confirm Booking</button>
                        <a href="<?= site_url('bookings/new') ?>" class="btn btn-outline-secondary">Back</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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

<?= $this->section('styles') ?>
.info-box { background: color-mix(in srgb, var(--brand) 6%, white); border-radius: 8px; padding: 0.65rem 0.85rem; }
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
document.getElementById('lookup-btn').addEventListener('click', function () {
    const phone = document.getElementById('phone').value.trim();
    const resultEl = document.getElementById('lookup-result');
    if (!phone) { return; }

    fetch('<?= site_url('guests/lookup') ?>?phone=' + encodeURIComponent(phone))
        .then((res) => res.json())
        .then((data) => {
            if (data.found) {
                document.getElementById('guest_id').value = data.guest.id;
                document.getElementById('full_name').value = data.guest.full_name;
                document.getElementById('email').value = data.guest.email || '';
                resultEl.innerHTML = '<span class="text-success"><i class="bi bi-check-circle me-1"></i>Existing guest found: ' + data.guest.full_name + '</span>';
            } else {
                document.getElementById('guest_id').value = 0;
                resultEl.innerHTML = '<span class="text-muted">No existing guest with that number — enter their details below.</span>';
            }
        })
        .catch(() => {
            resultEl.innerHTML = '<span class="text-danger">Lookup failed. Enter details manually.</span>';
        });
});

// If the guest edits the name/phone after a lookup matched, don't silently
// keep charging the booking to the old guest_id.
['full_name', 'phone'].forEach((id) => {
    document.getElementById(id).addEventListener('input', () => {
        document.getElementById('guest_id').value = 0;
    });
});
</script>
<?= $this->endSection() ?>
