<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Motivation extends Component
{
    public $records, $name, $type, $enabled, $show_in_member_presences, $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->type = '';
        $this->enabled = true;
        $this->emit('load-data-table');
    }

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

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

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

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

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