feature: add delete parking lot with active bookings guard and image cleanup

This commit is contained in:
Ghassan Yusuf 2026-04-15 15:57:04 +03:00
parent c6d2d98cc8
commit bf82e331d1
4 changed files with 54 additions and 1 deletions

View File

@ -14,7 +14,8 @@
"Bash(git checkout:*)",
"Bash(git reset:*)",
"Bash(php -l app/Http/Controllers/Admin/ParkingLotController.php)",
"Bash(php -l app/Http/Requests/StoreParkingLotRequest.php)"
"Bash(php -l app/Http/Requests/StoreParkingLotRequest.php)",
"Bash(php -l app/Http/Controllers/Admin/BookingController.php)"
]
}
}

View File

@ -68,6 +68,29 @@ class ParkingLotController extends Controller
]);
}
public function destroy(ParkingLot $parkingLot): JsonResponse
{
// Block deletion if there are active bookings
$activeCount = $parkingLot->bookings()->where('status', 'active')->count();
if ($activeCount > 0) {
return response()->json([
'success' => false,
'message' => "لا يمكن الحذف — يوجد {$activeCount} حجز نشط في هذا الموقف",
], 422);
}
if ($parkingLot->image) {
Storage::disk('public')->delete($parkingLot->image);
}
$parkingLot->delete();
return response()->json([
'success' => true,
'message' => 'تم حذف الموقف بنجاح',
]);
}
public function toggleStatus(ParkingLot $parkingLot): JsonResponse
{
$parkingLot->update(['is_active' => !$parkingLot->is_active]);

View File

@ -143,6 +143,12 @@
title="{{ $lot->is_active ? 'تعطيل' : 'تفعيل' }}">
<i class="bi {{ $lot->is_active ? 'bi-slash-circle' : 'bi-check-circle' }}" style="font-size:.8rem;"></i>
</button>
<button class="btn btn-sm"
style="background:rgba(239,68,68,.08);color:#dc2626;border:none;border-radius:.375rem;width:30px;height:30px;padding:0;"
onclick="deleteLot({{ $lot->id }}, '{{ addslashes($lot->name) }}')"
title="حذف">
<i class="bi bi-trash3" style="font-size:.8rem;"></i>
</button>
</div>
</td>
</tr>
@ -490,6 +496,28 @@ function doSearch() {
const t = document.getElementById('searchInput').value;
window.location.href = `/admin/parking-lots?search=${encodeURIComponent(t)}`;
}
async function deleteLot(id, name) {
if (!confirm(`حذف الموقف "${name}"؟\nسيتم حذف جميع بيانات الموقف نهائياً.`)) return;
try {
const res = await fetch(`/admin/parking-lots/${id}`, {
method: 'DELETE',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
'Accept': 'application/json',
}
});
const data = await res.json();
if (data.success) {
location.reload();
} else {
alert(data.message || 'تعذّر الحذف');
}
} catch {
alert('خطأ في الاتصال');
}
}
</script>
@endpush

View File

@ -29,6 +29,7 @@ Route::prefix('admin')->middleware('admin')->name('admin.')->group(function () {
Route::post('/parking-lots', [\App\Http\Controllers\Admin\ParkingLotController::class, 'store'])->name('parking-lots.store');
Route::put('/parking-lots/{parkingLot}', [\App\Http\Controllers\Admin\ParkingLotController::class, 'update'])->name('parking-lots.update');
Route::post('/parking-lots/{parkingLot}/toggle', [\App\Http\Controllers\Admin\ParkingLotController::class, 'toggleStatus'])->name('parking-lots.toggle');
Route::delete('/parking-lots/{parkingLot}', [\App\Http\Controllers\Admin\ParkingLotController::class, 'destroy'])->name('parking-lots.destroy');
// Active Bookings
Route::get('/bookings/active', [\App\Http\Controllers\Admin\BookingController::class, 'activeIndex'])->name('bookings.active');