<?php

namespace App\Http\Livewire;

use App\Models\Company;
use App\Models\HistoryContractsUser;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use Livewire\Component;
use App\Traits\OrderByTable;
use Barryvdh\DomPDF\Facade as PDF;


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

    public $search, $company_id, $pageTitle, $componentName, $user_id, $selected_WC_id;
    public $salary_include, $requested_to, $solicitation_date, $reviewed_by, $rejected_reason;
    public $status_work_certificate, $work_certificate_id;
    public $company_name, $company_plan_name, $company_plan_id;
    public $first_name, $last_name;

    public $breadcrumb_show;

    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($id=null, $breadcrumb_show=true)
    {
        if ($id == null) {
            return redirect()->route('dashboard');
        }

        UserAuthPermission('Company_work_certificate_view');
        $user = User::find($id);
        $this->user_id = $user->id;
        $this->first_name = $user->first_name;
        $this->last_name = $user->last_name;
        if ($user->status == 'INACTIVO'){
            return redirect()->route('dashboard');
        }
        EmployeeOtherCompany($this->user_id); //Verifica que el empleado pertenezca a la empresa
        $this->company_id = Company::find(Auth::user()->CompanyId())->id;
        $this->pageTitle = 'Certificados Laborales';
        $this->componentName = 'Solicitudes';

        $this->breadcrumb_show = $breadcrumb_show;


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

    }

    public function render()
    {
        $work_certificates_users = $this->searchList();
        $this->count_reg = $work_certificates_users->total();

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

    public function searchList(){
        if (strlen($this->search) > 0){
            $work_certificates_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(
                    '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('users.id', $this->user_id)
            ->where('companies.id', Auth::user()->CompanyId())
            ->where(function ($query) {
            $query->where('type_work_permits.name', 'like', '%' . $this->search . '%')
                    ->orWhere('work_permits.register_code', '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{
            $work_certificates_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(
                    '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('users.id', $this->user_id)
            ->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 $work_certificates_users;
    }

    public function openWC($id)
    {
        $this->work_certificate_id = $id;

        $this->emit('OpenWorkCertModal', $this->work_certificate_id);
    }

    public function downloadCertificate($id)
    {
        UserAuthPermission('Company_work_certificate_download');
        $work_certificate = WorkCertificate::find($id);
        if ($work_certificate != null){
            if ($work_certificate->status == '1'){ //Si el certificado esta aprobado
                $this->emit('downloadCertificatePDF', $id);//llama al método para descargar el certificado del controlador Company_WorkCertificateDetailController
            }
        }


    }



}
