<?php

namespace App\Http\Livewire;

use App\Models\Company;
use App\Models\User;
use App\Models\WorkPermit;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;


class Company_WorkPermitDetailController extends Component
{
    public $company_id, $first_name, $last_name, $employee_id;
    public $work_permit_id, $register_code, $solicitation_date, $start_date, $end_date, $observations, $status, $rejected_reason, $reviewed_by;
    public $support_document, $support_document_view, $status_support_document, $rejected_reason_support_document;
    public $type_work_permit_name, $support_required;
    public $breadcrumb_show = true;

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

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

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

    protected $listeners = [
        'OpenWorkPermitModal' => 'OpenWorkPermit',
        'SupportDocumentAction',
        'WorkPermitAction',
    ];


    public function OpenWorkPermit($work_permit_id) // el nombre del parámetro debe coincidir con el nombre de la variable que se le pasa al componente
    {
        UserAuthPermission('Company_work_permit_view');
    //$this->work_permit_modal = $id;
        debug('-----------modal---------');
        debug($work_permit_id);
        if ($work_permit_id == null){
            return redirect()->route('permisos-laborares');
        }
        $this->work_permit_id = $work_permit_id;
        $work_permit = WorkPermit::find($this->work_permit_id);
        $user = User::find($work_permit->user_id);
        $this->employee_id = $user->id;
        $this->first_name = $user->first_name;
        $this->last_name = $user->last_name;

        $this->register_code = $work_permit->register_code;
        $this->solicitation_date = $work_permit->solicitation_date;
        $this->start_date = $work_permit->start_date;
        $this->end_date = $work_permit->end_date;
        $this->observations = $work_permit->observations;
        $this->status = $work_permit->status;
        $this->rejected_reason = $work_permit->rejected_reason;
        $this->support_document = $work_permit->support_document;
        $this->status_support_document = $work_permit->status_support_document;
        $this->rejected_reason_support_document = $work_permit->rejected_reason_support_document;
        $this->type_work_permit_name = $work_permit->type_work_permit_name;
        $this->support_required = $work_permit->support_required;
        $this->reviewed_by = $work_permit->reviewed_by;
        $this->emit('show-modal-work-permit','Abrir Modal');
    }

    public function SupportDocumentAction($id, $opc, $reason=null)
    {
        switch ($opc) {
            case '1':
                $this->status_support_document = '1';
                $this->rejected_reason_support_document = null;
                $title = 'El documento de soporte ha sido aceptado';
                break;
            case '2':
                $this->status_support_document = '2';
                $this->rejected_reason_support_document = $reason;
                $title = 'El documento de soporte ha sido rechazado';
                break;
            case '3':
                $this->status_support_document = '3';
                $this->rejected_reason_support_document = $reason;
                $title = 'Se requiere corrección en el documento de soporte';
                break;
        }

        $work_permit = WorkPermit::find($id);
        $work_permit->Update(
            [
                'status_support_document' => $this->status_support_document,
                'rejected_reason_support_document' => $this->rejected_reason_support_document,
            ]
        );

        $message = NotifyMessageCustom(
            $title = $title,
            $body = 'Para más información, ingrese a la plataforma.',
            $url = route('empleado-permisos-laborales'),
            $button_text = 'Ver permisos laborales',
            $via_from = 'Company',
            $user_to_id = $work_permit->user_id,
            $user_from_id = Auth::user()->id
        );

        $this->emit('reload-list-work-permits','reload-modal');
    }

    public function WorkPermitAction($id, $opc, $reason=null){
        switch ($opc) {
            case '1':
                $this->status = '1';
                $this->rejected_reason = null;
                $message = 'El permiso laboral se ha aprobado';
                break;
            case '2':
                $this->status = '2';
                $this->rejected_reason = $reason;
                $message = 'El permiso laboral se ha rechazado';
                break;
        }

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

        $message = NotifyMessageCustom(
            $title = $message,
            $body = 'Para más información, ingrese a la plataforma',
            $url = route('empleado-permisos-laborales'),
            $button_text = 'Ver permisos laborales',
            $via_from = 'Company',
            $user_to_id = $work_permit->user_id,
            $user_from_id = Auth::user()->id
        );

        $this->emit('reload-list-work-permits','reload-modal');
        $this->emit('work-permit-updated', $message);
    }



}
