<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Category;
use App\Models\Department;
use App\Models\Category as CategoryModel;
use Livewire\Attributes\On;

class Categories extends Component
{
	protected $listeners=['openAddModal','editCategory'];
	
	// add the properties for input fields
	public $CategoryID;
    public $CategoryName;
	public $DepartmentID;
	public $Description;
	public $departments;
	
	protected $rules = [
		'CategoryName' => 'required',
		'DepartmentID' => 'required',
		'Description' => 'required',
    ];
	
	protected $messages = [
        'CategoryName.required' => 'Category is required',
		'DepartmentID.required' => 'Department is required'
    ];
	
    public function render()
    {
		$this->departments= Department::orderBy('DepartmentName','asc')->get();
        return view('livewire.categories');
    }
	
	// add new category
    public function saveCategory()
    {
        // validate the data
        $this->validate();
		
		if(CategoryModel::where('CategoryName', $this->CategoryName)->exists()){
			//toast
			$this->dispatch('alert', [
				'type' => 'error',
				'message' => "Category already exists!",
			]);
			return;
		}

		$category = new CategoryModel();
		$category->CategoryName = $this->CategoryName;
		$category->DepartmentID = $this->DepartmentID;
		$category->Description = $this->Description;
		$category->save();
		
		$lastCategoryId = $category->CategoryID;
		
		$this->resetFields();
		
		//event
        $this->dispatch('closeModal', 'addModal');
		
		$this->dispatch('refreshDatatable');
		
		//toast
        $this->dispatch('alert', [
            'type' => 'success',
            'message' => "Category Added Successfully!",
        ]);
    }
	
	//edit category
    public function editCategory($id)
    {
		$category = CategoryModel::find($id);
		
		if($category){
			$this->CategoryID = $category->CategoryID;
			$this->CategoryName=$category->CategoryName;
			$this->DepartmentID = $category->DepartmentID;
			$this->Description=$category->Description;
			$this->resetValidation();
			$this->dispatch('openModal', 'editModal');
		}
		else{
            return redirect()->back();
        }
		
    }
	
	public function updateCategory()
    {
        $this->validate();

		$category = CategoryModel::find($this->CategoryID);
		$category->CategoryName=$this->CategoryName;
		$category->DepartmentID=$this->DepartmentID;
		$category->Description=$this->Description;
		$category->save();
		
        $this->resetFields();
		
		$this->dispatch('refreshDatatable');
		
		//event
        $this->dispatch('closeModal', 'editModal');
		$this->dispatch('updatedItem');
		
		//toast
        $this->dispatch('alert', [
            'type' => 'success',
            'message' => "Category Updated Successfully!",
        ]);
    }
	
	public function resetFields()
    {
        $this->resetValidation();
        $this->CategoryName = '';
		$this->DepartmentID = '';
		$this->Description = '';
    }
	
	public function openAddModal()
    {
        $this->resetValidation();
        $this->CategoryName = '';
		$this->DepartmentID = '';
		$this->Description = '';
		$this->dispatch('openModal', 'addModal');
    }
	
	#[On('category-deleted')]
    public function deleteCategory($id)
    {
		$category=CategoryModel::find($id);
		$category->delete();
		
		$this->dispatch('refreshDatatable');
		
		//toast
		$this->dispatch('alert', [
			'type' => 'success',
			'message' => "Category deleted Successfully!",
		]);

    }
	
}
