CasperSecurity
<?php
namespace App\Http\Livewire\Transportmanagement;
use App\Models\ManageVehicle;
use App\Models\VehicleBreakdown;
use Livewire\Component;
use Livewire\WithPagination;
class VehicleBreakdownLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $vehicle_id,$vehicle_breakdown_date,$vehicle_breakdown_place,$vehicle_breakdown_description;
protected $listeners = ['delete'];
public $modelid;
public $showmodal = false;
public $status = true;
public $successmsg="Vehicle Breakdown has been created successfully.";
public $deletemsg="Vehicle Breakdown has been deleted successfully";
public $updatemsg="Vehicle Breakdown has been updated successfully";
public $errormsg="Oops !!! Something went wrong.";
protected $rules = [
'vehicle_id'=>'required',
'vehicle_breakdown_date'=>'required',
'vehicle_breakdown_place'=>'required',
'vehicle_breakdown_description'=>'required',
];
protected $updatedrules = [
'vehicle_id'=>'required',
'vehicle_breakdown_date'=>'required',
'vehicle_breakdown_place'=>'required',
'vehicle_breakdown_description'=>'required',
'status' => 'nullable',
];
protected $messages = [
'vehicle_breakdown_date.required'=>'The Vehicle Breakdown date cannot be empty.',
'vehicle_breakdown_place.required' => 'The Vehicle Breakdown Place cannot be empty.',
'vehicle_breakdown_description.required' => 'The Vehicle Breakdown Description cannot be empty.',
'status.required' => 'The status cannot be empty.',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->cleanVar();
$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->vehicle_id=null;
$this->vehicle_breakdown_date=null;
$this->vehicle_breakdown_place=null;
$this->vehicle_breakdown_description=null;
$this->status=null;
}
public function loadData($id){
$response = VehicleBreakdown::find($id);
if ($response) {
$this->vehicle_id = $response['vehicle_id'];
$this->vehicle_breakdown_date = $response['vehicle_breakdown_date'];
$this->vehicle_breakdown_place = $response['vehicle_breakdown_place'];
$this->vehicle_breakdown_description = $response['vehicle_breakdown_description'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = VehicleBreakdown::where('vehicle_breakdown_date', $this->vehicle_breakdown_date)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
} else {
$response = VehicleBreakdown::create([
'vehicle_id' => $this->vehicle_id,
'vehicle_breakdown_date' => $this->vehicle_breakdown_date,
'vehicle_breakdown_place' => $this->vehicle_breakdown_place,
'vehicle_breakdown_description' => $this->vehicle_breakdown_description,
]);
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 = VehicleBreakdown::find($this->modelid);
if ($response) {
$response->update([
'vehicle_id' => $this->vehicle_id,
'vehicle_breakdown_date' => $this->vehicle_breakdown_date,
'vehicle_breakdown_place' => $this->vehicle_breakdown_place,
'vehicle_breakdown_description' => $this->vehicle_breakdown_description,
'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 = VehicleBreakdown::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()
{
$vehicle_breakdowns = VehicleBreakdown::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $vehicle_breakdowns;
}
public function render()
{
$datatable=[];
$datatable=$this->read();
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$vehicle = ManageVehicle::where('id',$data->vehicle_id)->first();
//dd($holiday);
//$datatable[$key]['holiday_type'] = $holiday ? $holiday->holiday_type:'N/A';
if($vehicle){
$datatable[$key]['vehicle_registration_no'] = $vehicle->vehicle_registration_no;
}
else{
$datatable[$key]['vehicle_registration_no'] ="NA";
}
}
}
$vehicles = ManageVehicle::where('status', 1)->get();
return view('livewire.transportmanagement.vehicle-breakdown-livewire',compact('datatable','vehicles'));
}
}