<?php

namespace App\Http\Livewire\Buscador\Panel;

use Illuminate\Support\Facades\DB;
use Livewire\WithPagination;
use Livewire\Component;
use App\Models\Clinica;

class Clinicas extends Component
{
    use WithPagination;

    /*****************************/
    /*** ATRIBUTOS              **/
    /*****************************/
    protected $paginationTheme = 'bootstrap';
    public $numeroregistros = 14;
    public $buscador_clinica;
    public $clinica_seleccionado;
    public $clinica_id     ;
    public $display = "d-none";

    /*****************************/
    /*** SELECCIONAR            **/
    /*****************************/
    public function selecionar_clinica($id)
    {
        $clinica =  Clinica::find($id);
        $this->clinica_seleccionado = $clinica->id." ".$clinica->documento." ". $clinica->nombre;
        $this->emit('ev_seleccionar_clinica',$clinica->id);
        $this->display = 'd-none';
    }     
    /*****************************/
    /*** MOSTAR                 **/
    /*****************************/
    public function montar_lista()
    {
        $this->display = 'd-block';
    }    
    /*****************************/
    /*** RENDER                 **/
    /*****************************/
    public function render()
    {
        $clinicas = DB::table('clinicas')
        ->where('clinicas.activo', '!=', '0')
        ->select(
            'clinicas.id',
            'clinicas.nombre',
            'clinicas.activo',
        )
        ->where(function ($query) {
            $query->where('clinicas.nombre', 'like', '%' . $this->buscador_clinica . '%');
        })->orderBy('nombre', 'desc' )
        ->paginate($this->numeroregistros);

        $count_clinicas_busqueda = $clinicas->count();
        return view('livewire.buscador.panel.clinicas',compact('clinicas','count_clinicas_busqueda'));
    }
}
