<?php

namespace App\Http\Livewire;

use App\Models\Skill;
use Livewire\Component;

class CV_SkillsController extends Component
{
    public $componentName, $selected_SK_id, $info;
    public $activeButtonSaveUpdate;

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

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

    public function resetUI()
    {
        $this->info = '';
        $this->resetValidation();
    }
    public function openAddSK()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-SK', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $this->selected_SK_id = 0;
        $this->emit('show-modal-SK','Abrir Modal');
    }
    public function StoreSK()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-SK', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'info' => 'required|max:500',
        ];

        $messages = [
            'info.required' => 'El campo es obligatorio',
            'info.max' => 'El campo debe tener máximo 500 caracteres',
        ];

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

        Skill::create([
            'user_id' => $this->user_sel_id,
            'info' => $this->info,
        ]);

        $this->resetUI();
        $this->emit('item-added-SK', 'Habilidad registrada');
    }

    public function EditSK(Skill $skill)
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-SK', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $this->selected_SK_id = $skill->id;
        $this->info = $skill->info;
        $this->emit('show-modal-SK', 'Abrir Modal');
    }

    public function UpdateSK()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-SK', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'info' => 'required|max:500',
        ];

        $messages = [
            'info.required' => 'El campo es obligatorio',
        ];

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

        $skills = Skill::find($this->selected_SK_id);

        $skills->update([
            'user_id' => $this->user_sel_id,
            'info' => $this->info,
        ]);

        $this->resetUI();
        $this->emit('item-updated-SK', 'Habilidad actualizada');

    }
    protected $listeners = ['deleteRowSK' => 'DestroySK'];

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


}
