<?php

namespace App\Livewire;

use App\Models\User;
use Livewire\Component;

class UserSearch extends Component
{
    public $search = '';
    public $selectedUser;

    public function selectUser($userId)
    {
        $this->selectedUser = $userId;
        $this->emitUp('userSelected', $userId);
    }

    public function render()
    {
        $users = User::query()
            ->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%"))
            ->limit(5)
            ->get();

        return view('livewire.user-search', compact('users'));
    }
}
