<?php

namespace App\Http\Livewire;

use App\Models\Company;
use App\Models\HistoryContractsUser;
use App\Models\User;
use App\Models\WorkCertificate;
use App\Models\WorkPermit;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Barryvdh\DomPDF\Facade as PDF;
use Luecano\NumeroALetras\NumeroALetras;

class Company_WorkCertificateDetailController extends Component
{
    public $company_id, $first_name, $last_name, $employee_id;
    public $salary_include, $requested_to, $solicitation_date, $reviewed_by, $status_work_certificate, $rejected_reason;

    public $breadcrumb_show = true;

    public function mount($work_certificate_id=null)// el nombre del parametro debe coincidir con el nombre de la variable que se le pasa al componente
    {
        $this->work_certificate_id = $work_certificate_id;
        $this->status = '0';
    }

    public function render()
    {
        return view('livewire.company.employee.detail.work-certificates.detail.component');
    }

    public function resetUIWC()
    {
        //$this->id_work_permit = 0;
        $this->resetValidation();
    }

    protected $listeners = [
        'OpenWorkCertModal' => 'OpenWorkCertificate',
        'WorkCertificateAction',
    ];

    public function OpenWorkCertificate($work_certificate_id) // el nombre del parámetro debe coincidir con el nombre de la variable que se le pasa al componente
    {
        UserAuthPermission('Company_work_certificate_view');
    //$this->work_permit_modal = $id;
        debug('-----------modal---------');
        debug($work_certificate_id);
        if ($work_certificate_id == null){
            return redirect()->route('certificados-laborales');
        }
        $this->work_certificate_id = $work_certificate_id;
        $work_certificate = WorkCertificate::find($this->work_certificate_id);
        $user = User::find($work_certificate->user_id);
        $this->employee_id = $user->id;
        $this->first_name = $user->first_name;
        $this->last_name = $user->last_name;

        $this->solicitation_date = $work_certificate->solicitation_date;
        $this->requested_to = Request_to_Default($work_certificate->requested_to);
        $this->salary_include = $work_certificate->salary_include;
        $this->reviewed_by = $work_certificate->reviewed_by;
        $this->status = $work_certificate->status;
        $this->rejected_reason = $work_certificate->rejected_reason;

        $this->emit('show-modal-work-certificate','Abrir Modal');
    }



    public function WorkCertificateAction($id, $opc, $reason=null){
        switch ($opc) {
            case '1':
                $this->status = '1';
                $this->rejected_reason = null;
                $message = 'El certificado laboral se ha aprobado';
                $body ='Se ha aprobado el certificado laboral, para mas información consulte el módulo de certificados laborales.';
                break;
            case '2':
                $this->status = '2';
                $this->rejected_reason = $reason;
                $message = 'El certificado laboral se ha rechazado';
                $body= 'El certificado laboral ha sido rechazado, para mas información consulte el módulo de certificados laborales.';
                break;
        }

        $work_certificate = WorkCertificate::find($id);
        $work_certificate->Update(
            [
                'status' => $this->status,
                'reviewed_by' => Auth::user()->id,
                'rejected_reason' => $reason,
            ]
        );

        $message = NotifyMessageCustom(
            $title = $message,
            $body = $body,
            $url = route('empleado-certificados-laborales'),
            $button_text = 'Ver certificados laborales',
            $via_from = 'Company',
            $user_to_id = $work_certificate->user_id,
            $user_from_id = Auth::user()->id
        );

        $this->emit('reload-list-work-certificates','reload-modal');
        //$this->emit('work-certificate-updated', $message);
    }

}
