124 lines
6.1 KiB
PHP
124 lines
6.1 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
|
|
header("Location: ../login.php");
|
|
exit();
|
|
}
|
|
|
|
require_once '../db/config.php';
|
|
|
|
$pdo = db();
|
|
|
|
// Handle booking status change
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$bookingId = filter_input(INPUT_POST, 'booking_id', FILTER_VALIDATE_INT);
|
|
$carId = filter_input(INPUT_POST, 'car_id', FILTER_VALIDATE_INT);
|
|
|
|
if ($bookingId && $carId) {
|
|
$pdo->beginTransaction();
|
|
try {
|
|
if (isset($_POST['approve'])) {
|
|
// Set booking to approved and car to sold
|
|
$pdo->prepare("UPDATE bookings SET status = 'approved' WHERE id = ?")->execute([$bookingId]);
|
|
$pdo->prepare("UPDATE cars SET status = 'sold' WHERE id = ?")->execute([$carId]);
|
|
} elseif (isset($_POST['cancel'])) {
|
|
// Set booking to cancelled and car back to for sale (approved)
|
|
$pdo->prepare("UPDATE bookings SET status = 'cancelled' WHERE id = ?")->execute([$bookingId]);
|
|
$pdo->prepare("UPDATE cars SET status = 'approved' WHERE id = ?")->execute([$carId]);
|
|
}
|
|
$pdo->commit();
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
error_log("Booking status update failed: " . $e->getMessage());
|
|
}
|
|
}
|
|
header("Location: bookings.php");
|
|
exit();
|
|
}
|
|
|
|
// Fetch bookings with user and car details
|
|
$bookings = $pdo->query("
|
|
SELECT b.id, b.status, b.booking_date, u.username, u.email, c.make, c.model, c.id as car_id
|
|
FROM bookings b
|
|
JOIN users u ON b.user_id = u.id
|
|
JOIN cars c ON b.car_id = c.id
|
|
ORDER BY b.booking_date DESC
|
|
")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$projectName = 'Manage Bookings';
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($projectName) ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
|
|
<link rel="stylesheet" href="../assets/css/custom.css?v=<?= time() ?>">
|
|
</head>
|
|
<body>
|
|
<div class="admin-wrapper">
|
|
<?php include 'partials/sidebar.php'; ?>
|
|
<main class="admin-main-content">
|
|
<div class="container-fluid">
|
|
<div class="d-flex justify-content-between align-items-center pt-3 pb-2 mb-3 border-bottom">
|
|
<h1 class="h2">Manage Bookings</h1>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Customer</th>
|
|
<th>Car</th>
|
|
<th>Booking Date</th>
|
|
<th>Status</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($bookings)): ?>
|
|
<tr><td colspan="5" class="text-center">No bookings found.</td></tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($bookings as $booking): ?>
|
|
<tr>
|
|
<td>
|
|
<div><b><?= htmlspecialchars($booking['username']) ?></b></div>
|
|
<small class="text-muted"><?= htmlspecialchars($booking['email']) ?></small>
|
|
</td>
|
|
<td><?= htmlspecialchars($booking['make'] . ' ' . $booking['model']) ?></td>
|
|
<td><?= date("M d, Y, g:i A", strtotime($booking['booking_date'])) ?></td>
|
|
<td>
|
|
<span class="badge rounded-pill bg-<?= str_replace(['approved', 'pending', 'cancelled'], ['success', 'warning', 'danger'], $booking['status']) ?>">
|
|
<?= htmlspecialchars(ucfirst($booking['status'])) ?>
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<?php if ($booking['status'] === 'pending'): ?>
|
|
<form method="POST" class="d-inline-flex gap-2" onsubmit="return confirm('Are you sure?');">
|
|
<input type="hidden" name="booking_id" value="<?= $booking['id'] ?>">
|
|
<input type="hidden" name="car_id" value="<?= $booking['car_id'] ?>">
|
|
<button type="submit" name="approve" class="btn btn-sm btn-success"><i class="bi bi-check-circle me-1"></i>Approve</button>
|
|
<button type="submit" name="cancel" class="btn btn-sm btn-danger"><i class="bi bi-x-circle me-1"></i>Cancel</button>
|
|
</form>
|
|
<?php else: ?>
|
|
<span class="text-muted">No actions</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|