CasperSecurity
<?php
namespace App\Http\Livewire\Tendermanagement;
use App\Models\ClientCategory;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Auth;
class ClientCategoryLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_names="Client Categories";
public $page_name="Client Category";
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $category_name,$status;
public $modelid;
public $showmodal=false;
public $rules=[
'category_name'=>'required|max:255'
];
public $updatedrules=[
'category_name'=>'required|max:255'
];
public $messages=[
'category_name.required'=>'This field is required',
'category_name.max'=>'This field allows maximum 255 character',
];
protected $listeners = ['delete'];
public function updateclick($id){
$this->modelid=$id;
// $this->resetValidation();
$this->showmodal=true;
$this->loadData($id);
}
public function loadData($id){
$data=ClientCategory::find($id);
if($data){
$this->category_name=$data->category_name;
$this->status=$data->status;
}
//dd($this->lat);
}
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->resetValidation();
$this->showmodal=true;
//$this->dispatchBrowserEvent('livewire:load');
$this->cleanVar();
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
}
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->category_name=null;
// $this->showmodal=false;
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=ClientCategory::where('category_name',$this->category_name)->latest()->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'Sorry !!! Category name has already exists in our database. Please Try Another Name'
]);
}else{
$response=ClientCategory::create([
'category_name'=>$this->category_name,
'created_by'=>Auth::user()->id,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
//emotify('success', 'The Department data has been saved successfully');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>'The data has been saved successfully'
]);
}
}
//Str::kebab($this->dept_name)
}
public function update()
{
$this->validate($this->updatedrules,$this->messages);
$response=ClientCategory::find($this->modelid);
if($response){
$res=ClientCategory::where('category_name',$this->category_name)->where('id', '!=', $response->id)->latest()->first();
if($res){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'Sorry !!! Category name has already exists in our database. Please Try Another Name'
]);
}
else{
$response->update([
'category_name'=>$this->category_name,
'status'=>$this->status,
'modified_by'=>Auth::user()->id,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
//emotify('success', 'The Department data has been saved successfully');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>'The data has been updated successfully'
]);
}
else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'Sorry !!! Something Went Wrong'
]);
}
}
}
}
public function delete(){
$response=ClientCategory::find($this->modelid);
if($response){
$response->delete();
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>'The data has been deleted successfully'
]);
}
}
public function read(){
$client_category=ClientCategory::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $client_category;
}
public function render()
{
$categories=$this->read();
return view('livewire.tendermanagement.client-category-livewire',compact('categories'));
}
}