<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Training;

class CV_TrainingsController extends Component
{
    public $componentName, $selected_TR_id, $year, $name, $time, $company, $place;
    public $activeButtonSaveUpdate;



    public function mount($user_sel_id, $activeButtonSaveUpdate)
    {
        $this->user_sel_id = $user_sel_id;
        $this->componentName = 'Capacitaciones';
        $this->activeButtonSaveUpdate = $activeButtonSaveUpdate;
    }

    public function render()
    {
        $trainings = Training::where('user_id', $this->user_sel_id)->get();
        return view('livewire.cv-employee.detail.partials.trainings.component', compact('trainings'));
    }

    public function resetUI()
    {
        $this->year = '';
        $this->name = '';
        $this->time = '';
        $this->company = '';
        $this->place = '';
        $this->resetValidation();
    }
    public function openAddTR()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-TR', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $this->selected_TR_id = 0;
        $this->emit('show-modal-TR','Abrir Modal');
    }
    public function StoreTR()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-TR', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'year' => 'required|not_in:Seleccionar',
            'name' => 'required|max:99',
            'time' => 'required|max:99',
            'company' => 'required|max:99',
            'place' => 'required|max:99',
        ];

        $messages = [
            'year.required' => 'El campo es obligatorio',
            'year.not_in' => 'Selecciona un año diferente a Seleccionar',
            'name.required' => 'El campo es obligatorio',
            'name.max' => 'El campo debe tener máximo 99 caracteres',
            'time.required' => 'El campo es obligatorio',
            'time.max' => 'El campo debe tener máximo 99 caracteres',
            'company.required' => 'El campo es obligatorio',
            'company.max' => 'El campo debe tener máximo 99 caracteres',
            'place.required' => 'El campo es obligatorio',
            'place.max' => 'El campo debe tener máximo 99 caracteres',
        ];

        $this->validate($rules, $messages);

        $trainings = Training::create([
            'user_id' => $this->user_sel_id,
            'year' => $this->year,
            'name' => $this->name,
            'time' => $this->time,
            'company' => $this->company,
            'place' => $this->place,
        ]);

        $this->resetUI();
        $this->emit('item-added-TR', 'Capacitación registrada');
    }

    public function EditTR(Training $training)
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-TR', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $this->selected_TR_id = $training->id;
        $this->year = $training->year;
        $this->name = $training->name;
        $this->time = $training->time;
        $this->company = $training->company;
        $this->place = $training->place;
        $this->emit('show-modal-TR', 'Abrir Modal');
    }
    public function UpdateTR()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-TR', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'year' => 'required|not_in:Seleccionar',
            'name' => 'required|max:99',
            'time' => 'required|max:99',
            'company' => 'required|max:99',
            'place' => 'required|max:99',
        ];

        $messages = [
            'year.required' => 'El campo es obligatorio',
            'year.not_in' => 'Selecciona un año diferente a Seleccionar',
            'name.required' => 'El campo es obligatorio',
            'name.max' => 'El campo debe tener máximo 99 caracteres',
            'time.required' => 'El campo es obligatorio',
            'time.max' => 'El campo debe tener máximo 99 caracteres',
            'company.required' => 'El campo es obligatorio',
            'company.max' => 'El campo debe tener máximo 99 caracteres',
            'place.required' => 'El campo es obligatorio',
            'place.max' => 'El campo debe tener máximo 99 caracteres',
        ];

        $this->validate($rules, $messages);

        $trainings = Training::find($this->selected_TR_id);

        $trainings->update([
            'user_id' => $this->user_sel_id,
            'year' => $this->year,
            'name' => $this->name,
            'time' => $this->time,
            'company' => $this->company,
            'place' => $this->place,
        ]);

        $this->resetUI();
        $this->emit('item-updated-TR', 'Capacitación actualizada');

    }
    protected $listeners = ['deleteRowTR' => 'DestroyTR'];

    public function DestroyTR($id)
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-TR', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        Training::find($id)->delete();
        $this->emit('item-deleted-TR', 'Se eliminó la Capacitación con éxito');
    }

}
