<?php

namespace App\Livewire\Clients;

use App\Models\Clients\Client;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
    use WithPagination;

    public string $search = '';

    protected $queryString = ['search'];

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function render()
    {
        $clients = Client::query()
            ->with('tokens')
            ->when($this->search, function ($query) {
                $query->where('name', 'like', "%{$this->search}%")
                    ->orWhere('shortname', 'like', "%{$this->search}%")
                    ->orWhere('crm_id', 'like', "%{$this->search}%")
                    ->orWhere('cif', 'like', "%{$this->search}%")
                    ->orWhere('razon_social', 'like', "%{$this->search}%");
            })
            ->orderBy('name')
            ->paginate(9);

        return view('livewire.clients.index', [
            'clients' => $clients,
        ])->layout('layouts.app');
    }
}

