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

<h1 class="h3 mb-3">New Reservation</h1>

<div class="card shadow-sm" style="max-width: 640px;">
    <div class="card-body">
        <form action="<?= site_url('dining/reservations') ?>" method="post" id="res-form">
            <?= csrf_field() ?>
            <input type="hidden" name="guest_id" id="guest_id" value="0">

            <div class="mb-3">
                <label class="form-label">Guest phone (existing guest, optional)</label>
                <div class="input-group">
                    <input type="text" class="form-control" id="phone_lookup" placeholder="024XXXXXXX">
                    <button type="button" class="btn btn-outline-secondary" id="lookup-btn">Look up</button>
                </div>
                <div id="lookup-result" class="form-text"></div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Walk-in Name (if not an existing guest)</label>
                    <input type="text" class="form-control" name="walk_in_name" id="walk_in_name">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Walk-in Phone</label>
                    <input type="text" class="form-control" name="walk_in_phone" id="walk_in_phone">
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label class="form-label">Party Size</label>
                    <input type="number" class="form-control" name="party_size" min="1" value="2" required>
                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label">Date</label>
                    <input type="date" class="form-control" name="reservation_date" value="<?= esc($date) ?>" required>
                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label">Time</label>
                    <input type="time" class="form-control" name="reservation_time" required>
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label">Table (optional)</label>
                <select class="form-select" name="dining_table_id">
                    <option value="">Assign later</option>
                    <?php foreach ($tables as $table): ?>
                        <option value="<?= $table->id ?>"><?= esc($table->table_number) ?> — <?= esc($table->area_name) ?> (<?= (int) $table->seating_capacity ?> seats)</option>
                    <?php endforeach; ?>
                </select>
            </div>

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

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

    fetch('<?= site_url('dining/reservations/lookup-guest') ?>?phone=' + encodeURIComponent(phone))
        .then((res) => res.json())
        .then((data) => {
            if (data.found) {
                document.getElementById('guest_id').value = data.guest.id;
                document.getElementById('walk_in_name').value = data.guest.full_name;
                document.getElementById('walk_in_name').disabled = true;
                const vipBadge = data.guest.segmentLabel ? ' <span class="badge bg-' + data.guest.segmentBadge + '">' + data.guest.segmentLabel + '</span>' : '';
                const diningBadge = data.guest.diningLabel ? ' <span class="badge bg-' + data.guest.diningBadge + '">' + data.guest.diningLabel + ' Dining</span>' : '';
                resultEl.innerHTML = '<span class="text-success">Existing guest: ' + data.guest.full_name + '</span>' + vipBadge + diningBadge;
            } else {
                document.getElementById('guest_id').value = 0;
                document.getElementById('walk_in_name').disabled = false;
                resultEl.innerHTML = '<span class="text-muted">No existing guest — enter walk-in name below.</span>';
            }
        });
});
</script>
<?= $this->endSection() ?>
