<?php
namespace App\Http\Livewire;
use Livewire\Component;
class Categories extends Component
{
    public $level_1 = [];
    public $level_2 = [];
    public $level_3 = [];

    public $level_1_id = 0;
    public $level_2_id = 0;
    public $level_3_id = 0;

    public function render()
    {
        $this->emit('hideMsg');
        $reset = false;
        if ($this->level_1_id > 0)
        {
            $this->level_2 = \App\Models\Category::where('parent_id', $this->level_1_id)->orderBy('name')->get();
            if (sizeof($this->level_2) == 0)
            {
                $this->emit('storeCategoryWithID', $this->level_1_id);
                $reset = true;
            }
        }
        if ($this->level_2_id > 0)
        {
            $this->level_3 = \App\Models\Category::where('parent_id', $this->level_2_id)->orderBy('name')->get();
            if (sizeof($this->level_3) == 0)
            {
                $this->emit('storeCategoryWithID', $this->level_2_id);
                $reset = true;
            }
        }
        if ($this->level_3_id > 0)
        {
            $this->emit('storeCategoryWithID', $this->level_3_id);
            $reset = true;
        }
        $this->level_1 = \App\Models\Category::where('parent_id', null)->orderBy('name')->get();
        if ($reset)
        {
            $this->level_1_id = 0;
            $this->level_2_id = 0;
            $this->level_3_id = 0;
            $this->level_2 = [];
            $this->level_3 = [];
        }

        return view('livewire.categories');
    }
}
