<?php

namespace App\Components\Platform;

use App\Models\Solicitor\Solicitor;
use Livewire\Component;

class WorkspaceSelector extends Component {
    public string $search = '';

    public function selectWorkspace(int $id) {
        $solicitor = Solicitor::findOrFail($id);

        session()->put('workspace_solicitor_id', $solicitor->id);

        return redirect()->intended(route('dashboard'));
    }

    public function render() {
        $solicitors = Solicitor::query()
            ->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%"))
            ->orderBy('name')
            ->get();

        return view('components.platform.workspace-selector', compact('solicitors'))
            ->layout('layouts.app');
    }
}
