CasperSecurity
<?php
namespace App\Http\Livewire\Tendermanagement;
use App\Models\TenderVeriety;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Auth;
class TenderVerietyLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_names="Tender Categories";
public $page_name="Tender Category";
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $variety_name,$status;
public $modelid;
public $showmodal=false;
public $rules=[
'variety_name'=>'required|max:255'
];
public $updatedrules=[
'variety_name'=>'required|max:255'
];
public $messages=[
'variety_name.required'=>'This field is required',
'variety_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=TenderVeriety::find($id);
if($data){
$this->variety_name=$data->variety_name;
$this->status=$data->status;
}
//dd($this->lat);
}
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->resetValidation();
$this->showmodal=true;
$this->cleanVar();
//$this->dispatchBrowserEvent('livewire:load');
}
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->variety_name=null;
//$this->showmodal=false;
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=TenderVeriety::where('variety_name',$this->variety_name)->latest()->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'Sorry !!! Veriety name has already exists in our database. Please Try Another Name'
]);
}else{
$response=TenderVeriety::create([
'variety_name'=>$this->variety_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=TenderVeriety::find($this->modelid);
if($response){
$res=TenderVeriety::where('variety_name',$this->variety_name)->where('id','!=',$response->id)->latest()->first();
if($res){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'Sorry !!! Veriety name has already exists in our database. Please Try Another Name'
]);
}
else{
$response->update([
'variety_name'=>$this->variety_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 !!! Verity name has already exists in our database. Please Try Another Name'
]);
}
}
}
}
public function delete(){
$response=TenderVeriety::find($this->modelid);
if($response){
$response->delete();
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>'The data has been deleted successfully'
]);
}
}
public function read(){
$tender_veriety=TenderVeriety::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $tender_veriety;
}
public function render()
{
$verieties=$this->read();
return view('livewire.tendermanagement.tender-veriety-livewire',compact('verieties'));
}
}