<?php

namespace App\Http\Livewire;
use App\Models\User;
use App\Models\Language;
use App\Models\LevelLanguage;
use App\Models\LanguageUser;

use Livewire\Component;

class CV_LanguageUserController extends Component
{
    public $languageuser, $language_id, $langueg, $selected_LG_id, $languages, $level_id;
    public $activeButtonSaveUpdate;

    public function mount($user_sel_id, $activeButtonSaveUpdate)
    {
        $this->user_sel_id = $user_sel_id;
        $this->componentName = 'Idiomas';
        $this->languages = Language::select('*')->orderBy('name', 'asc')->get();
        $user = User::find($this->user_sel_id);
        $this->activeButtonSaveUpdate = $activeButtonSaveUpdate;
    }

    public function resetUI()
    {
        $this->language_id = '';
        $this->level_id = '';
        $this->resetValidation();
    }

    public function render()
    {
        $languages = Language::all();
        $levels = LevelLanguage::all();
        $languageusers = LanguageUser::join('languages', 'language_users.language_id', '=', 'languages.id')
            ->join('level_languages','language_users.level_id','=','level_languages.id')
            ->select('language_users.*', 'languages.name as language_name', 'level_languages.name as level_name')
            ->where('language_users.user_id', $this->user_sel_id)
            ->orderBy('language_users.id', 'desc')
            ->get();

        return view('livewire.cv-employee.detail.partials.languages.component', compact('languages','languageusers','levels'));
    }


    public function openAddLG()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-LG', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $this->selected_LG_id = 0;
        $this->emit('show-modal-LG','Abrir Modal');
    }

    public function StoreLG()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-LG', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'language_id' => 'required|not_in:Seleccionar|unique:language_users,language_id,NULL,id,user_id,'.$this->user_sel_id,
            'level_id' => 'required|not_in:Seleccionar',
        ];
        $messages = [
            'language_id.required' => 'El campo es obligatorio',
            'language_id.not_in' => 'Selecciona un nivel diferente a Seleccionar',
            'language_id.unique' => 'El idioma ya esta registrado',
            'level_id.required' => 'El campo es obligatorio',
            'level_id.not_in' => 'Selecciona un nivel diferente a Seleccionar',
        ];
        $this->validate($rules, $messages);
        LanguageUser::create([
            'user_id' => $this->user_sel_id,
            'language_id' => $this->language_id,
            'level_id' => $this->level_id,
        ]);
        $this->resetUI();
        $this->emit('item-added-LG', 'Idioma registrado');
    }

    public function EditLG(LanguageUser $languageusers)
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-LG', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $this->selected_LG_id = $languageusers->id;
        $this->language_id = $languageusers->language_id;
        $this->level_id = $languageusers->level_id;
        $this->emit('show-modal-LG', 'Abrir Modal');
    }

    public function UpdateLG()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-LG', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'language_id' => 'required|not_in:Seleccionar|unique:language_users,language_id,'.$this->selected_LG_id.',id,user_id,'.$this->user_sel_id,
            'level_id' => 'required|not_in:Seleccionar',
        ];
        $messages = [
            'language_id.required' => 'El campo es obligatorio',
            'language_id.not_in' => 'Selecciona un nivel diferente a Seleccionar',
            'language_id.unique' => 'El idioma ya esta registrado',
            'level_id.required' => 'El campo es obligatorio',
            'level_id.not_in' => 'Selecciona un nivel diferente a Seleccionar',
        ];
        $this->validate($rules, $messages);
        $languageusers = LanguageUser::find($this->selected_LG_id);
        $languageusers->update([
            'user_id' => $this->user_sel_id,
            'language_id' => $this->language_id,
            'level_id' => $this->level_id,
        ]);

        $this->resetUI();
        $this->emit('item-updated-LG', 'Idioma Actualizado');

    }

    protected $listeners = ['deleteRowLG' => 'DestroyLG'];

    public function DestroyLG($id)
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-LG', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        LanguageUser::find($id)->delete();
        $this->emit('item-deleted-LG', 'Se eliminó el idioma con éxito');
    }

}
