<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Vat extends Component
{
    public $records, $name, $value, $enabled, $dataId, $update = false, $add = false;

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

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

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

    public function mount(){

        if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
            return redirect()->to('/dashboard');

    }

    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->enabled = true;
        $this->emit('load-data-table');
    }

    public function render()
    {
        $this->records = \App\Models\Vat::select('id', 'name', 'value', 'enabled')->get();
        return view('livewire.vat');
    }

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

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

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

    public function update()
    {
        $this->validate();
        try {
            \App\Models\Vat::whereId($this->dataId)->update([
                'name' => $this->name,
                'value' => $this->value,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','IVA 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\Vat::find($id)->delete();
            session()->flash('success',"IVA eliminata");
            return redirect(request()->header('Referer'));
        }catch(\Exception $e){
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }
}
