<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Department;
use App\Models\Department as DepartmentModel;
use Livewire\Attributes\On;

class Departments extends Component
{
	protected $listeners=['editDepartment','openAddModal'];
	
	// add the properties for input fields
	public $DepartmentID;
    public $DepartmentName;
	public $Description;
	
	protected $rules = [
        'DepartmentName' => 'required',
		'Description' => 'required',
    ];
	
	protected $messages = [
        'DepartmentName.required' => 'Department name is required'
    ];
	
    public function render()
    {
        return view('livewire.departments');
    }
	
	// add new department
    public function saveDepartment()
    {
        // validate the data
        $this->validate();
		
		if(DepartmentModel::where('DepartmentName', $this->DepartmentName)->exists()){
			//toast
			$this->dispatch('alert', [
				'type' => 'error',
				'message' => "Department already exists!",
			]);
			return;
		}

		$department=DepartmentModel::create([
            'DepartmentName' => $this->DepartmentName,
			'Description' => $this->Description,
        ]);
		
		$this->resetFields();
		
		$this->dispatch('refreshDatatable');
		
		//event
        $this->dispatch('closeModal', 'addModal');
		
		//toast
        $this->dispatch('alert', [
            'type' => 'success',
            'message' => "Department Added Successfully!",
        ]);
    }
	
	//edit zone
    public function editDepartment($id)
    {
		$department = DepartmentModel::find($id);
		
		if($department){
			$this->DepartmentID = $department->DepartmentID;
			$this->DepartmentName=$department->DepartmentName;
			$this->Description=$department->Description;
			$this->resetValidation();
			$this->dispatch('openModal', 'editModal');
		}
		else{
            return redirect()->back();
        }
		
    }
	
	public function updateDepartment()
    {
        $this->validate();

		$department = DepartmentModel::where('DepartmentID',$this->DepartmentID)->first();
		$department->DepartmentName=$this->DepartmentName;
		$department->Description=$this->Description;
		$department->save();
		
        $this->resetFields();
		
		$this->dispatch('refreshDatatable');
		
		//event
        $this->dispatch('closeModal', 'editModal');
		$this->dispatch('updatedItem');
		
		//toast
        $this->dispatch('alert', [
            'type' => 'success',
            'message' => "Department Updated Successfully!",
        ]);
    }
	
	public function resetFields()
    {
        $this->resetValidation();
        $this->DepartmentName = '';
		$this->Description = '';
    }
	
	public function openAddModal()
    {
        $this->resetValidation();
        $this->DepartmentName = '';
		$this->Description = '';
		$this->dispatch('openModal', 'addModal');
    }
	
	#[On('department-deleted')]
    public function deleteDepartment($id)
    {
		$department=DepartmentModel::find($id);
		$department->delete();
		
		$this->dispatch('refreshDatatable');
		
		//toast
		$this->dispatch('alert', [
			'type' => 'success',
			'message' => "Department deleted Successfully!",
		]);

    }
	
}
