<?php

namespace App\Http\Livewire;

use Livewire\Component;

use Livewire\WithPagination;

class ModelloVeicolo extends Component
{

    use WithPagination;

    protected $paginationTheme = 'bootstrap';

    public $title = 'Modello veicolo';

    public $marche = [];

    public $search, $search_marca_id, $name, $marca_id, $dataId, $update = false, $add = false;

    protected $rules = [
        'name' => 'required'
    ];

    protected $messages = [
        'name.required' => 'Il nome è obbligatorio'
    ];

    public function resetFields(){
        $this->name = '';
        $this->marca_id = null;
    }

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function render()
    {
        $this->marche = \App\Models\MarcaVeicolo::orderBy('name')->get();
        $rows = \App\Models\ModelloVeicolo::where('name', 'like', '%'.$this->search.'%');
        $rows = $rows->orderBy('name')->paginate(10);
        return view('livewire.modello-veicolo', ['records' => $rows]);
    }

    public function add()
    {
        $this->resetFields();
        $this->add = true;
        $this->update = false;
    }

    public function store()
    {
        $this->validate();
        try {
            $m = \App\Models\ModelloVeicolo::create([
                'name' => $this->name
            ]);
            \App\Models\MarcaModelloVeicolo::create([
                'marca_id' => $this->marca_id,
                'modello_id' => $m->id
            ]);
            session()->flash('success','Record creato');
            $this->resetFields();
            $this->add = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore in fase di salvataggio');
        }
    }

    public function edit($id){
        try {
            $record = \App\Models\ModelloVeicolo::findOrFail($id);
            if( !$record) {
                session()->flash('error','Record non trovato');
            } else {
                $this->name = $record->name;
                $this->dataId = $record->id;
                $this->update = true;
                $this->add = false;
                $this->marca_id = \App\Models\MarcaModelloVeicolo::where('modello_id', $this->dataId)->first()->marca_id;
            }
        } catch (\Exception $ex) {
            session()->flash('error','Errore');
        }
    }

    public function update()
    {
        $this->validate();
        try {
            \App\Models\ModelloVeicolo::whereId($this->dataId)->update([
                'name' => $this->name
            ]);
            \App\Models\MarcaModelloVeicolo::where('modello_id', $this->dataId)->delete();
            \App\Models\MarcaModelloVeicolo::create([
                'marca_id' => $this->marca_id,
                'modello_id' => $this->dataId
            ]);
            session()->flash('success','Record aggiornato');
            $this->resetFields();
            $this->update = false;
        } catch (\Exception $ex) {
            session()->flash('success','Errore');
        }
    }

    public function cancel()
    {
        $this->add = false;
        $this->update = false;
        $this->resetFields();
    }

    public function delete($id)
    {
        try{
            \App\Models\ModelloVeicolo::find($id)->delete();
            session()->flash('success',"Record eliminato");
        }catch(\Exception $e){
            session()->flash('error',"Errore");
        }
    }
}
