<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Card extends Component
{
    public $records, $name, $enabled, $use_for_user_check, $one_year_expire, $next_day_expire, $next_month_expire, $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->next_day_expire = null;
        $this->next_month_expire = null;
        $this->enabled = true;
        $this->use_for_user_check = true;
        $this->one_year_expire = false;
        $this->emit('load-data-table');
    }

    public function render()
    {
        $this->records = \App\Models\Card::get(); //orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
        return view('livewire.card');
    }

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

    public function store()
    {
        $this->validate();
        if ($this->one_year_expire)
        {
            $this->next_day_expire = 0;
            $this->next_month_expire = 0;
        }
        try {
            \App\Models\Card::create([
                'name' => $this->name,
                'next_day_expire' => $this->next_day_expire,
                'next_month_expire' => $this->next_month_expire,
                'enabled' => $this->enabled,
                'use_for_user_check' => $this->use_for_user_check,
                'one_year_expire' => $this->one_year_expire
            ]);
            session()->flash('success','Tessera creata');
            $this->resetFields();
            $this->add = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function edit($id){
        try {
            $card = \App\Models\Card::findOrFail($id);
            if( !$card) {
                session()->flash('error','Tessera non trovata');
            } else {
                $this->name = $card->name;
                $this->enabled = $card->enabled;
                $this->use_for_user_check = $card->use_for_user_check;
                $this->one_year_expire = $card->one_year_expire;
                $this->next_day_expire = $card->next_day_expire;
                $this->next_month_expire = $card->next_month_expire;
                $this->dataId = $card->id;
                $this->update = true;
                $this->add = false;
            }
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

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