<?php
namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use App\Traits\OrderByTable;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class CvEmployeeController extends Component
{
    use WithPagination;
    use WithFileUploads;
    use OrderByTable;
    public $roleName, $search,$users, $componentName, $pageTitle, $status, $selected_id,$states;
    public $breadcrumb_show = true;

    private $pagination = 10;

    protected $paginationTheme = 'bootstrap';

    public function mount()
    {
        UserAuthPermission('CvEmployee_index');
        $this->pageTitle = 'Listado';
        $this->componentName = 'Empleados';
        $this->status = 'Seleccionar';
    }

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

    public function render()
    {
        if (Auth::user()->hasRole('SuperAdmin') || Auth::user()->hasRole('Soporte')) {
            if (strlen($this->search) > 0)
            {
                $data= User::role('Empleado')
                            ->where(function ($query) {
                            $query->where('first_name', 'like', '%' . $this->search . '%')
                                    ->orWhere('last_name', 'like', '%' . $this->search . '%')
                                    ->orWhere('document', 'like', '%' . $this->search . '%')
                                    ->join('company_user', 'users.id', '=', 'company_user.user_id')
                            ->join('companies', 'company_user.company_id', '=', 'companies.id')
                            ->select('users.*','companies.name as company_name');
                            })
                            ->orderBy($this->sortBy, $this->sortDirection)->paginate($this->pagination);
            }
            else
            {
                $data= User::role('Empleado')
                            ->join('company_user', 'users.id', '=', 'company_user.user_id')
                            ->join('companies', 'company_user.company_id', '=', 'companies.id')
                            ->select('users.*','companies.name as company_name')
                            ->orderBy($this->sortBy, $this->sortDirection)
                            ->paginate($this->pagination);
            }
        }
        else if (Auth::user()->hasRole('Administrador') || Auth::user()->hasRole('Administrativo')) {
            if (strlen($this->search) > 0)
            {

                $data= User::role('Empleado')
                            ->join('company_user', 'users.id', '=', 'company_user.user_id')
                            ->join('companies', 'company_user.company_id', '=', 'companies.id')
                            ->select('users.*','companies.name as company_name')
                            ->where('companies.id', Auth::user()->CompanyId())
                            ->where(function ($query) {
                            $query->where('first_name', 'like', '%' . $this->search . '%')
                                    ->orWhere('last_name', 'like', '%' . $this->search . '%')
                                    ->orWhere('document', 'like', '%' . $this->search . '%');
                            })
                            ->orderBy($this->sortBy, $this->sortDirection)->paginate($this->pagination);
            }
            else
            {
                $data= User::role('Empleado')
                            ->join('company_user', 'users.id', '=', 'company_user.user_id')
                            ->join('companies', 'company_user.company_id', '=', 'companies.id')
                            ->where('companies.id', Auth::user()->CompanyId())
                            ->select('users.*','companies.name as company_name')
                            ->orderBy($this->sortBy, $this->sortDirection)
                            ->paginate($this->pagination);
            }
        }
        return view('livewire.cv-employee.component',[
            'data' => $data
        ])
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }

}
