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

<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-bucket" style="color: var(--brand);"></i> <?= isset($task) ? 'Edit Housekeeping Task' : 'New Housekeeping Task' ?>
            </div>
            <div class="card-body">
                <form id="hkTaskForm" action="<?= isset($task) ? site_url('housekeeping/' . $task->id) : site_url('housekeeping') ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Room</label>
                        <div class="dropdown">
                            <input type="text" class="form-control" id="room_search" autocomplete="off"
                                   placeholder="Search by room number or type…">
                            <input type="hidden" name="room_id" id="room_id_hidden" required>
                            <div class="dropdown-menu w-100" id="room_dropdown" style="max-height: 280px; overflow-y: auto;"></div>
                        </div>
                    </div>
                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Task Type</label>
                            <select class="form-select" name="task_type" required>
                                <?php foreach ($lookups->housekeepingTaskTypes as $key => $meta): ?>
                                    <option value="<?= $key ?>" <?= isset($task) && $task->task_type === $key ? 'selected' : '' ?>><?= 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="<?= $key ?>" <?= (isset($task) ? $task->priority === $key : $key === 'medium') ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="mb-3">
                        <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 $person): ?>
                                <option value="<?= $person->id ?>" <?= isset($task) && (int) $task->assigned_to === (int) $person->id ? 'selected' : '' ?>><?= esc($person->full_name) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-4">
                        <label class="form-label small fw-semibold">Notes <span class="text-muted fw-normal">(optional)</span></label>
                        <textarea class="form-control" name="notes" rows="2"><?= esc($task->notes ?? '') ?></textarea>
                    </div>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check2 me-1"></i><?= isset($task) ? 'Save Changes' : 'Create Task' ?></button>
                        <a href="<?= isset($task) ? site_url('housekeeping/' . $task->id) : site_url('housekeeping') ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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

<?= $this->section('scripts') ?>
<script>
initRoomPicker({
    inputId: 'room_search',
    hiddenId: 'room_id_hidden',
    dropdownId: 'room_dropdown',
    rooms: <?= json_encode(array_map(static fn ($r) => [
        'id'    => $r->id,
        'label' => $r->room_number . ' — ' . $r->room_type_name . ' (' . ucfirst($r->status) . ')',
    ], $rooms)) ?>,
    selectedId: <?= isset($task) ? (int) $task->room_id : 'null' ?>,
});

document.getElementById('hkTaskForm').addEventListener('submit', function (e) {
    var hidden = document.getElementById('room_id_hidden');
    if (! hidden.value) {
        e.preventDefault();
        document.getElementById('room_search').classList.add('is-invalid');
        document.getElementById('room_search').focus();
    }
});
</script>
<?= $this->endSection() ?>
