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

<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-exclamation-octagon" style="color: var(--brand);"></i> Log Complaint
            </div>
            <div class="card-body">
                <form action="<?= site_url('complaints') ?>" method="post">
                    <?= csrf_field() ?>
                    <input type="hidden" name="guest_id" id="guest_id" value="<?= (int) ($guest->id ?? 0) ?>">

                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Guest</label>
                        <?php if ($guest): ?>
                            <div class="form-control bg-light"><?= esc($guest->full_name) ?> — <?= esc($guest->phone) ?></div>
                        <?php else: ?>
                            <div class="input-group">
                                <input type="text" class="form-control" id="phone_lookup" placeholder="Search by phone — 024XXXXXXX">
                                <button type="button" class="btn btn-outline-secondary" id="lookup-btn">Look up</button>
                            </div>
                            <div id="lookup-result" class="form-text"></div>
                        <?php endif; ?>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Category</label>
                            <select class="form-select" name="category" required>
                                <?php foreach ($lookups->complaintCategories as $key => $meta): ?>
                                    <option value="<?= esc($key) ?>"><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Priority</label>
                            <select class="form-select" name="priority">
                                <?php foreach ($lookups->taskPriorities as $key => $meta): ?>
                                    <option value="<?= esc($key) ?>" <?= $key === 'medium' ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label small fw-semibold">What happened</label>
                        <textarea class="form-control" name="description" rows="4" required placeholder="Describe the complaint..."></textarea>
                    </div>

                    <div class="mb-4">
                        <label class="form-label small fw-semibold">Assign To <span class="text-muted fw-normal">(optional)</span></label>
                        <select class="form-select" name="assigned_to">
                            <option value="">— Unassigned —</option>
                            <?php foreach ($staff as $s): ?>
                                <option value="<?= $s->id ?>"><?= esc($s->full_name) ?></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>Log Complaint</button>
                        <a href="<?= site_url('complaints') ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
<?php if (! $guest): ?>
<?= $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('complaints/lookup-guest') ?>?phone=' + encodeURIComponent(phone))
        .then((res) => res.json())
        .then((data) => {
            if (data.found) {
                document.getElementById('guest_id').value = data.guest.id;
                resultEl.innerHTML = '<span class="text-success">Guest: ' + data.guest.full_name + '</span>';
            } else {
                document.getElementById('guest_id').value = 0;
                resultEl.innerHTML = '<span class="text-danger">No guest with that phone on file.</span>';
            }
        });
});
</script>
<?= $this->endSection() ?>
<?php endif; ?>
