<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Reference;

class CV_ReferencesController extends Component
{
    public $name,$selected_RF_id,$company,$title_position,$phone;
    public $activeButtonSaveUpdate;

    public function mount($user_sel_id, $activeButtonSaveUpdate)
    {
        $this->user_sel_id = $user_sel_id;
        $this->componentName = 'Referencias';
        $this->activeButtonSaveUpdate = $activeButtonSaveUpdate;
    }
    public function render()
    {
        $references = Reference::where('user_id', $this->user_sel_id)->get();
        return view('livewire.cv-employee.detail.partials.references.component', compact('references'));
    }

    public function resetUI()
    {
        $this->name = '';
        $this->company = '';
        $this->title_position = '';
        $this->phone = '';
        $this->resetValidation();
    }


    public function openAddRF()
    {
        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_RF_id = 0;
        $this->emit('show-modal-RF','Abrir Modal');
    }

    public function StoreRF()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-LG', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'name' => 'required|max:100',
            'company' => 'required|max:100',
            'title_position' => 'required|max:100',
            'phone' => "required|min:7max:15",
        ];

        $messages = [
            'name.required' => 'El campo es obligatorio',
            'name.max' => 'El campo debe tener máximo 100 caracteres',
            'company.required' => 'El campo es obligatorio',
            'company.max' => 'El campo debe tener máximo 100 caracteres',
            'title_position.required' => 'El campo es obligatorio',
            'title_position.max' => 'El campo debe tener máximo 100 caracteres',
            'phone.required' => 'Ingrega el número de Telefono',
            'phone.min' => 'El Teléfono debe tener como mínimo 7 cáracteres',
            'phone.max' => 'El Teléfono debe tener máximo 15 cáracteres',
        ];

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

        Reference::create([
            'user_id' => $this->user_sel_id,
            'name' => $this->name,
            'company' => $this->company,
            'title_position' => $this->title_position,
            'phone' => $this->phone,
        ]);

        $this->resetUI();
        $this->emit('item-added-RF', 'Referencia registrada');
    }

    public function EditRF(Reference $references)
    {
        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_RF_id = $references->id;
        $this->name = $references->name;
        $this->company = $references->company;
        $this->title_position = $references->title_position;
        $this->phone = $references->phone;
        $this->emit('show-modal-RF', 'Abrir Modal');
    }

    public function UpdateRF()
    {
        if (EmployeeOtherCompany($this->user_sel_id, true)[0] == NULL)
        {
            $this->emit('item-error-LG', EmployeeOtherCompany($this->user_sel_id, true)[1]);
            return false;
        }
        $rules = [
            'name' => 'required|max:100',
            'company' => 'required|max:100',
            'title_position' => 'required|max:100',
            'phone' => "required|min:7max:15",
        ];

        $messages = [
            'name.required' => 'El campo es obligatorio',
            'name.max' => 'El campo debe tener máximo 100 caracteres',
            'company.required' => 'El campo es obligatorio',
            'company.max' => 'El campo debe tener máximo 100 caracteres',
            'title_position.required' => 'El campo es obligatorio',
            'title_position.max' => 'El campo debe tener máximo 100 caracteres',
            'phone.required' => 'Ingrega el número de Telefono',
            'phone.min' => 'El Teléfono debe tener como mínimo 7 cáracteres',
            'phone.max' => 'El Teléfono debe tener máximo 15 cáracteres',
        ];


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

        $references = Reference::find($this->selected_RF_id);

        $references->update([
            'user_id' => $this->user_sel_id,
            'name' => $this->name,
            'company' => $this->company,
            'title_position' => $this->title_position,
            'phone' => $this->phone,
        ]);

        $this->resetUI();
        $this->emit('item-added-RF', 'Referencia registrada');

    }

    protected $listeners = ['deleteRowRF' => 'DestroyRF'];

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

}
