<?php

namespace App\Http\Livewire;

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

class Company_WorkCertificatesController extends Component
{
    use WithPagination;
    use WithFileUploads;
    use OrderByTable;

    public $search, $company_id, $pageTitle, $componentName, $count_reg, $work_certificate_id;
    public $selected_id, $salary_include, $requested_to, $solicitation_date, $reviewed_by, $status_work_certificate;

    public $breadcrumb_show = true;

    private $pagination = 10;

    protected $paginationTheme = 'bootstrap';

    protected $listeners = [
        'reload-list-work-certificates' => 'searchList'// emit del componente WorkPermitDetailController para refrescar la lista de permisos laborales
    ];

    public function mount()
    {
        UserAuthPermission('Company_work_certificate_index');
        $this->company_id = Company::find(Auth::user()->CompanyId())->id;
        $this->pageTitle = 'Listado';
        $this->componentName = 'Permisos laborales';
        $this->company_name = Company::find($this->company_id )->name;
        $this->company_plan_name = Company::find($this->company_id )->plan->name;

        $this->sortBy= 'work_certificates.created_at';
    }

    public function render()
    {

        $users = $this->searchList();
        $this->count_reg = $users->total();

        return view('livewire.company.work-certificates.component',compact('users'))
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }

    public function searchList(){
        if (strlen($this->search) > 0)
            {

                $users= User::role('Empleado')
                            ->join('work_certificates', 'users.id', '=', 'work_certificates.user_id')
                            ->join('company_user', 'users.id', '=', 'company_user.user_id')
                            ->join('companies', 'company_user.company_id', '=', 'companies.id')
                            ->select(
                                    'users.*', 'company_user.job_position_id',
                                    'work_certificates.id as work_certificate_id',
                                    'work_certificates.solicitation_date', 'work_certificates.salary_include',
                                    'work_certificates.requested_to', 'work_certificates.reviewed_by', 'work_certificates.status as status_work_certificate',
                                    )
                            ->where('work_certificates.company_id', Auth::user()->CompanyId())
                            ->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 . '%');
                            })
                            ->where(function ($query) {
                                if ($this->status_work_certificate != null) {
                                    $query->where('work_certificates.status', $this->status_work_certificate);
                                }
                            })
                            ->orderBy($this->sortBy, $this->sortDirection)->paginate($this->pagination);
            }
        else
        {
            $users = User::role('Empleado')
                            ->join('work_certificates', 'users.id', '=', 'work_certificates.user_id')
                            ->join('company_user', 'users.id', '=', 'company_user.user_id')
                            ->join('companies', 'company_user.company_id', '=', 'companies.id')
                            ->select(
                                    'users.*', 'company_user.job_position_id',
                                    'work_certificates.id as work_certificate_id',
                                    'work_certificates.solicitation_date', 'work_certificates.salary_include',
                                    'work_certificates.requested_to', 'work_certificates.reviewed_by', 'work_certificates.status as status_work_certificate',
                                    )
                            ->where('work_certificates.company_id', Auth::user()->CompanyId())
                            ->where('companies.id', Auth::user()->CompanyId())
                            ->where(function ($query) {
                                if ($this->status_work_certificate != null) {
                                    $query->where('work_certificates.status', $this->status_work_certificate);
                                }
                            })
                            ->orderBy($this->sortBy, $this->sortDirection)->paginate($this->pagination);
        }

        return $users;

    }

    public function resetUI()
    {
        $this->search = '';
        $this->resetValidation();
        $this->resetPage();
    }


    public function openWC($id)
    {
        $this->work_certificate_id = $id;
        $this->emit('OpenWorkCertModal', $this->work_certificate_id);
    }

    public function jobPositionName($id)
    {
        $jobPosition = DB::table('job_positions')
            ->where('id', $id)
            ->get()->first();
        return $jobPosition->name;
    }


}
