<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Http\Middleware\TenantMiddleware;
use Illuminate\Support\Facades\Auth;

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

    public $parent = '';

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

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

    public function boot()
    {
        app(TenantMiddleware::class)->setupTenantConnection();
    }
    public function mount(){

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

    }

    public function resetFields(){
        $this->name = '';
        $this->parent_id = null;
        $this->parent = '';
        $this->enabled = true;
    }

    public function render()
    {
        $this->records = \App\Models\Category::where('parent_id', null)->get();
        return view('livewire.category');
    }

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

    public function addLevel($parent_id)
    {
        $this->resetFields();
        $this->parent_id = $parent_id;
        $this->parent = \App\Models\Category::findOrFail($parent_id)->name;
        $this->add = true;
        $this->update = false;
    }

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

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

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