<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Nation extends Component
{
    public $records, $code, $name, $enabled, $isItaly, $dataId, $update = false, $add = false;

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

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

    public $sortField ='name';
    public $sortAsc = true;

    public function sortBy($field)
    {
        if($this->sortField === $field)
        {
            $this->sortAsc = ! $this->sortAsc;
        } else {
            $this->sortAsc = true;
        }

        $this->sortField = $field;
    }

    public function resetFields(){
        $this->name = '';
        $this->code = '';
        $this->enabled = true;
        $this->isItaly = false;
        $this->emit('load-data-table');
    }

    public function render()
    {

        $this->records = \App\Models\Nation::select('id', 'name', 'code', 'enabled')->get();//->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
        return view('livewire.nation');
    }

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

    public function store()
    {
        $this->validate();
        try {
            \App\Models\Nation::create([
                'name' => $this->name,
                'code' => $this->code,
                //'enabled' => $this->enabled,
                //'is_italy' => $this->isItaly,
            ]);
            session()->flash('success','Nazione creata');
            $this->resetFields();
            $this->add = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function edit($id){
        try {
            $nation = \App\Models\Nation::findOrFail($id);
            if( !$nation) {
                session()->flash('error','Nazione non trovata');
            } else {
                $this->name = $nation->name;
                $this->code = $nation->code;
                $this->enabled = $nation->enabled;
                $this->isItaly = $nation->is_italy;
                $this->dataId = $nation->id;
                $this->update = true;
                $this->add = false;
            }
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function update()
    {
        $this->validate();
        try {
            \App\Models\Nation::whereId($this->dataId)->update([
                'name' => $this->name,
                'code' => $this->code,
                'enabled' => $this->enabled,
                'is_italy' => $this->isItaly,
            ]);
            session()->flash('success','Nazione aggiornata');
            $this->resetFields();
            $this->update = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

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

    public function delete($id)
    {
        try{
            \App\Models\Nation::find($id)->delete();
            session()->flash('success',"Nazione eliminata");
        }catch(\Exception $e){
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }
}
