<?php

namespace App\Http\Livewire;
use Livewire\Component;

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

    public $nations = array();

    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;
    }

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

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

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

    public function mount()
    {
        $this->nations = \App\Models\Nation::select('id', 'name')->get();
    }

    public function render()
    {
        $this->records = \App\Models\Province::with('nation')->get();
        foreach($this->records as $r)
        {
            $r->nation = $r->nation->name;
        }
        /*if ($this->sortAsc)
            $this->records = $this->records->sortBy($this->sortField);
        else
            $this->records = $this->records->sortByDesc($this->sortField);*/

        return view('livewire.province');
    }

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

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

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

    public function update()
    {
        $this->validate();
        try {
            \App\Models\Province::whereId($this->dataId)->update([
                'name' => $this->name,
                'code' => $this->code,
                'nation_id' => $this->nation_id,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','Provincia 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\Province::find($id)->delete();
            session()->flash('success',"Provincia eliminata");
        }catch(\Exception $e){
            session()->flash('error','Errore (' . $e->getMessage() . ')');
        }
    }
}
