<?php

namespace App\Http\Livewire;

use Livewire\Component;

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

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

    protected $messages = [
        'name.required' => 'Il nome è 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->visibility = 'IN';
        $this->enabled = true;
        $this->emit('load-data-table');
    }

    public function render()
    {
        //$this->records = \App\Models\Bank::select('id', 'name', 'enabled')->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
        $this->records = \App\Models\Bank::select('id', 'name', 'visibility', 'enabled')->get();
        return view('livewire.bank');
    }

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

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

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

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