CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Models\EmployeeType;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class EmployeeTypeLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_name="Employee Types";
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $emp_type_name,$emp_type_shrt_name,$status,$created_by,$modified_by;
public $modelid;
public $showmodal=false;
public $successmsg="The Employee Type data has been saved successfully.";
public $updatemsg="The Employee Type data has been updated successfully.";
public $deletemsg="The Employee Type data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Employee Type name has already exists in our database. Please Try Another Name";
public $errormsg="Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules=[
'emp_type_name'=>'required|max:255',
'emp_type_shrt_name'=>'required|max:255',
];
public $updatedrules=[
'emp_type_name'=>'required|max:255',
'emp_type_shrt_name'=>'required|max:255',
'status'=>'required',
];
public $messages=[
'emp_type_name.required'=>'This field is required',
'emp_type_name.max'=>'This field allows maximum 255 character',
'emp_type_shrt_name.required'=>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->resetValidation();
$this->showmodal=true;
//$this->dispatchBrowserEvent('livewire:load');
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
}
public function updateclick($id){
$this->modelid=$id;
$this->loadData($this->modelid);
$this->showmodal=true;
}
public function deleteclick($id){
$this->modelid=$id;
$this->dispatchBrowserEvent('confirmdelete',[
'message'=>'Are you sure want to delete this data ?',
'funcname'=>'delete'
]);
}
public function cleanVar(){
$this->modelid=null;
$this->emp_type_name=null;
$this->emp_type_shrt_name=null;
$this->status=null;
$this->showmodal=false;
}
public function loadData($id){
$response=EmployeeType::find($id);
if($response){
$this->emp_type_name=$response['emp_type_name'];
$this->emp_type_shrt_name=$response['emp_type_shrt_name'];
$this->status=$response['status'];
}
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=EmployeeType::where('emp_type_name',$this->emp_type_name)->latest()->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else{
$response=EmployeeType::create([
'emp_type_name'=>$this->emp_type_name,
'emp_type_shrt_name'=>$this->emp_type_shrt_name,
'created_by'=>Auth::user()->name,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->successmsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function update(){
$this->validate($this->updatedrules,$this->messages);
$response=EmployeeType::find($this->modelid);
if($response){
$response->update([
'emp_type_name'=>$this->emp_type_name,
'emp_type_shrt_name'=>$this->emp_type_shrt_name,
'status'=>$this->status,
'modified_by'=>Auth::user()->name,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=EmployeeType::find($this->modelid);
if($response){
if($response->delete()){
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->deletemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function read(){
$depts=EmployeeType::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $depts;
}
public function render()
{
$datatable=$this->read();
return view('livewire.hrms.employee-type-livewire',compact('datatable'));
}
}