CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Models\AttendanceType;
use Livewire\Component;
use Livewire\WithPagination;
class AttendanceTypeLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $attendance_type,$short_name,$status;
public $modelid;
public $showmodal=false;
public $successmsg="The Attendance Type data has been saved successfully.";
public $updatemsg="The Attendance Type data has been updated successfully.";
public $deletemsg="The Attendance Type data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Attendance 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=[
'attendance_type'=>'required|max:25',
'short_name'=>'required|max:25',
];
public $updatedrules=[
'attendance_type'=>'required|max:25',
'short_name'=>'required|max:25',
'status'=>'required',
];
public $messages=[
'attendance_type.required'=>'This field is required',
'attendance_type.max'=>'This field allows maximum 25 character',
'short_name.required'=>'This field is required',
'short_name.max'=>'This field allows maximum 25 character',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->resetValidation();
$this->showmodal=true;
}
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->attendance_type=null;
$this->short_name=null;
$this->status=null;
$this->showmodal=false;
}
public function loadData($id){
$response=AttendanceType::find($id);
if($response){
$this->attendance_type=$response['attendance_type'];
$this->short_name=$response['short_name'];
$this->status=$response['status'];
}
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=AttendanceType::where('attendance_type',$this->attendance_type)->latest()->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else {
$response = AttendanceType::create([
'attendance_type' => $this->attendance_type,
'short_name' => $this->short_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=AttendanceType::find($this->modelid);
if($response){
$response->update([
'attendance_type'=>$this->attendance_type,
'short_name'=>$this->short_name,
'status'=>$this->status,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=AttendanceType::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(){
$attendance_types=AttendanceType::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $attendance_types;
}
public function render()
{
$datatable=$this->read();
return view('livewire.hrms.attendance-type-livewire',compact('datatable'));
}
}