290 lines
11 KiB
PHP
290 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\Driver;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
class DriverManagement extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
public $filters = [
|
|
'search' => '',
|
|
'status' => '',
|
|
'license_status' => '',
|
|
'vehicle_assigned' => '',
|
|
];
|
|
|
|
public $showModal = false;
|
|
public $showPerformanceModal = false;
|
|
public $editingDriver = null;
|
|
public $selectedDriver = null;
|
|
public $performancePeriod = 30;
|
|
public $driverMetrics = [];
|
|
|
|
public $form = [
|
|
'name' => '',
|
|
'driver_id' => '',
|
|
'user_id' => '',
|
|
'phone' => '',
|
|
'email' => '',
|
|
'license_number' => '',
|
|
'license_type' => '',
|
|
'license_expiry_date' => '',
|
|
'assigned_vehicle' => '',
|
|
'vehicle_plate' => '',
|
|
'performance_score' => '',
|
|
'status' => 'active',
|
|
'is_active' => true,
|
|
'notes' => '',
|
|
];
|
|
|
|
public function mount()
|
|
{
|
|
// Initialize component without parameters
|
|
}
|
|
|
|
public function getStatsProperty()
|
|
{
|
|
return [
|
|
'total_drivers' => Driver::count(),
|
|
'active_drivers' => Driver::where('status', 'active')->count(),
|
|
'expiring_licenses' => Driver::where('license_expiry_date', '<=', now()->addDays(30))->count(),
|
|
'assigned_vehicles' => Driver::whereNotNull('assigned_vehicle')->count(),
|
|
];
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$drivers = Driver::query()
|
|
->with('user')
|
|
->when($this->filters['search'], function ($query) {
|
|
$query->where(function ($q) {
|
|
$q->where('name', 'like', '%' . $this->filters['search'] . '%')
|
|
->orWhere('driver_id', 'like', '%' . $this->filters['search'] . '%')
|
|
->orWhere('phone', 'like', '%' . $this->filters['search'] . '%');
|
|
});
|
|
})
|
|
->when($this->filters['status'], function ($query) {
|
|
$query->where('status', $this->filters['status']);
|
|
})
|
|
->when($this->filters['license_status'], function ($query) {
|
|
if ($this->filters['license_status'] === 'valid') {
|
|
$query->where('license_expiry_date', '>', now());
|
|
} elseif ($this->filters['license_status'] === 'expiring') {
|
|
$query->whereBetween('license_expiry_date', [now(), now()->addDays(30)]);
|
|
} elseif ($this->filters['license_status'] === 'expired') {
|
|
$query->where('license_expiry_date', '<', now());
|
|
}
|
|
})
|
|
->when($this->filters['vehicle_assigned'], function ($query) {
|
|
if ($this->filters['vehicle_assigned'] === 'yes') {
|
|
$query->whereNotNull('assigned_vehicle');
|
|
} elseif ($this->filters['vehicle_assigned'] === 'no') {
|
|
$query->whereNull('assigned_vehicle');
|
|
}
|
|
})
|
|
->paginate(15);
|
|
|
|
$users = \App\Models\User::select('id', 'name', 'email')
|
|
->whereDoesntHave('driver')
|
|
->orWhere('id', $this->editingDriver?->user_id)
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
return view('livewire.driver-management', [
|
|
'drivers' => $drivers,
|
|
'users' => $users,
|
|
]);
|
|
}
|
|
|
|
public function editDriver($driverId)
|
|
{
|
|
$this->editingDriver = Driver::find($driverId);
|
|
$this->form = [
|
|
'name' => $this->editingDriver->name,
|
|
'driver_id' => $this->editingDriver->driver_id,
|
|
'user_id' => $this->editingDriver->user_id,
|
|
'phone' => $this->editingDriver->phone,
|
|
'email' => $this->editingDriver->email,
|
|
'license_number' => $this->editingDriver->license_number,
|
|
'license_type' => $this->editingDriver->license_type,
|
|
'license_expiry_date' => $this->editingDriver->license_expiry_date?->format('Y-m-d'),
|
|
'assigned_vehicle' => $this->editingDriver->assigned_vehicle,
|
|
'vehicle_plate' => $this->editingDriver->vehicle_plate,
|
|
'performance_score' => $this->editingDriver->performance_score,
|
|
'status' => $this->editingDriver->status,
|
|
'is_active' => $this->editingDriver->is_active,
|
|
'notes' => $this->editingDriver->notes,
|
|
];
|
|
$this->showModal = true;
|
|
}
|
|
|
|
public function openCreateModal()
|
|
{
|
|
$this->resetForm();
|
|
$this->editingDriver = null;
|
|
$this->showModal = true;
|
|
}
|
|
|
|
private function resetForm()
|
|
{
|
|
$this->form = [
|
|
'name' => '',
|
|
'driver_id' => '',
|
|
'user_id' => '',
|
|
'phone' => '',
|
|
'email' => '',
|
|
'license_number' => '',
|
|
'license_type' => '',
|
|
'license_expiry_date' => '',
|
|
'assigned_vehicle' => '',
|
|
'vehicle_plate' => '',
|
|
'performance_score' => '',
|
|
'status' => 'active',
|
|
'is_active' => true,
|
|
'notes' => '',
|
|
];
|
|
}
|
|
|
|
public function createDriver()
|
|
{
|
|
$this->validate([
|
|
'form.name' => 'required|string|max:255',
|
|
'form.driver_id' => 'required|string|unique:drivers,driver_id',
|
|
'form.user_id' => 'nullable|exists:users,id|unique:drivers,user_id',
|
|
'form.phone' => 'nullable|string|max:20',
|
|
'form.email' => 'nullable|email|unique:drivers,email',
|
|
'form.license_number' => 'required|string|max:50',
|
|
'form.license_type' => 'nullable|string|max:50',
|
|
'form.license_expiry_date' => 'nullable|date|after:today',
|
|
'form.assigned_vehicle' => 'nullable|string|max:255',
|
|
'form.vehicle_plate' => 'nullable|string|max:20',
|
|
'form.performance_score' => 'nullable|integer|min:0|max:100',
|
|
'form.status' => 'required|in:active,inactive,suspended',
|
|
'form.is_active' => 'boolean',
|
|
'form.notes' => 'nullable|string',
|
|
]);
|
|
|
|
Driver::create([
|
|
'name' => $this->form['name'],
|
|
'driver_id' => $this->form['driver_id'],
|
|
'user_id' => $this->form['user_id'] ?: null,
|
|
'phone' => $this->form['phone'],
|
|
'email' => $this->form['email'],
|
|
'license_number' => $this->form['license_number'],
|
|
'license_type' => $this->form['license_type'],
|
|
'license_expiry_date' => $this->form['license_expiry_date'] ? \Carbon\Carbon::parse($this->form['license_expiry_date']) : null,
|
|
'assigned_vehicle' => $this->form['assigned_vehicle'],
|
|
'vehicle_plate' => $this->form['vehicle_plate'],
|
|
'performance_score' => $this->form['performance_score'] ?: null,
|
|
'status' => $this->form['status'],
|
|
'is_active' => $this->form['is_active'],
|
|
'notes' => $this->form['notes'],
|
|
]);
|
|
|
|
$this->closeModal();
|
|
session()->flash('message', 'Driver created successfully.');
|
|
}
|
|
|
|
public function updateDriver()
|
|
{
|
|
$this->validate([
|
|
'form.name' => 'required|string|max:255',
|
|
'form.driver_id' => 'required|string|unique:drivers,driver_id,' . $this->editingDriver->id,
|
|
'form.user_id' => 'nullable|exists:users,id|unique:drivers,user_id,' . $this->editingDriver->id,
|
|
'form.phone' => 'nullable|string|max:20',
|
|
'form.email' => 'nullable|email|unique:drivers,email,' . $this->editingDriver->id,
|
|
'form.license_number' => 'required|string|max:50',
|
|
'form.license_type' => 'nullable|string|max:50',
|
|
'form.license_expiry_date' => 'nullable|date',
|
|
'form.assigned_vehicle' => 'nullable|string|max:255',
|
|
'form.vehicle_plate' => 'nullable|string|max:20',
|
|
'form.performance_score' => 'nullable|integer|min:0|max:100',
|
|
'form.status' => 'required|in:active,inactive,suspended',
|
|
'form.is_active' => 'boolean',
|
|
'form.notes' => 'nullable|string',
|
|
]);
|
|
|
|
$this->editingDriver->update([
|
|
'name' => $this->form['name'],
|
|
'driver_id' => $this->form['driver_id'],
|
|
'user_id' => $this->form['user_id'] ?: null,
|
|
'phone' => $this->form['phone'],
|
|
'email' => $this->form['email'],
|
|
'license_number' => $this->form['license_number'],
|
|
'license_type' => $this->form['license_type'],
|
|
'license_expiry_date' => $this->form['license_expiry_date'] ? \Carbon\Carbon::parse($this->form['license_expiry_date']) : null,
|
|
'assigned_vehicle' => $this->form['assigned_vehicle'],
|
|
'vehicle_plate' => $this->form['vehicle_plate'],
|
|
'performance_score' => $this->form['performance_score'] ?: null,
|
|
'status' => $this->form['status'],
|
|
'is_active' => $this->form['is_active'],
|
|
'notes' => $this->form['notes'],
|
|
]);
|
|
|
|
$this->closeModal();
|
|
session()->flash('message', 'Driver updated successfully.');
|
|
}
|
|
|
|
public function deleteDriver($driverId)
|
|
{
|
|
$driver = Driver::find($driverId);
|
|
if ($driver) {
|
|
// Check if driver has any active assignments
|
|
if ($driver->assigned_vehicle) {
|
|
session()->flash('error', 'Cannot delete driver with active vehicle assignment. Please unassign vehicle first.');
|
|
return;
|
|
}
|
|
|
|
$driver->delete();
|
|
session()->flash('message', 'Driver deleted successfully.');
|
|
}
|
|
}
|
|
|
|
public function suspendDriver($driverId)
|
|
{
|
|
Driver::find($driverId)->update(['status' => 'suspended']);
|
|
session()->flash('message', 'Driver suspended successfully.');
|
|
}
|
|
|
|
public function activateDriver($driverId)
|
|
{
|
|
Driver::find($driverId)->update(['status' => 'active']);
|
|
session()->flash('message', 'Driver activated successfully.');
|
|
}
|
|
|
|
public function viewPerformance($driverId)
|
|
{
|
|
$this->selectedDriver = Driver::find($driverId);
|
|
$this->driverMetrics = [
|
|
'total_trips' => rand(50, 200),
|
|
'avg_speed' => rand(45, 65),
|
|
'violations' => rand(0, 5),
|
|
'fuel_efficiency' => rand(15, 25),
|
|
];
|
|
$this->showPerformanceModal = true;
|
|
}
|
|
|
|
public function closeModal()
|
|
{
|
|
$this->showModal = false;
|
|
$this->editingDriver = null;
|
|
$this->reset('form');
|
|
}
|
|
|
|
public function closePerformanceModal()
|
|
{
|
|
$this->showPerformanceModal = false;
|
|
$this->selectedDriver = null;
|
|
$this->driverMetrics = [];
|
|
}
|
|
|
|
public function updatingFilters()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
}
|