35567-vm/drivers.php
Flatlogic Bot f9a46874cd v2
2025-11-08 10:23:58 +00:00

132 lines
5.0 KiB
PHP

<?php
require_once 'db/config.php';
$page_title = "Drivers";
// Handle form submission for adding a new driver
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_driver'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$status = trim($_POST['status']);
if (!empty($name) && !empty($email) && !empty($phone) && !empty($status)) {
try {
$pdo = db();
$sql = "INSERT INTO drivers (name, email, phone, status) VALUES (:name, :email, :phone, :status)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['name' => $name, 'email' => $email, 'phone' => $phone, 'status' => $status]);
// Redirect to avoid form resubmission
header("Location: drivers.php");
exit;
} catch (PDOException $e) {
$error_message = "Database error: " . $e->getMessage();
}
}
}
// Create drivers table if it doesn't exist
try {
$pdo = db();
$pdo->exec("CREATE TABLE IF NOT EXISTS drivers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(50) NOT NULL,
status VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
} catch (PDOException $e) {
die("Could not create table: " . $e->getMessage());
}
// Fetch all drivers
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name, email, phone, status, created_at FROM drivers ORDER BY created_at DESC");
$drivers = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$error_message = "Database error: " . $e->getMessage();
}
include 'includes/header.php';
?>
<?php include 'includes/sidebar.php'; ?>
<main class="main-content">
<header class="header">
<h1>Manage Drivers</h1>
</header>
<section class="card">
<h2>Add New Driver</h2>
<form action="drivers.php" method="POST" class="form-grid">
<div class="form-group">
<label for="name">Full Name</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="tel" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="status">Status</label>
<select id="status" name="status" required>
<option value="Available" selected>Available</option>
<option value="On-duty">On-duty</option>
<option value="Off-duty">Off-duty</option>
<option value="Maintenance">Maintenance</option>
</select>
</div>
<div class="form-group form-group-full">
<button type="submit" name="add_driver" class="button button-primary">Add Driver</button>
</div>
</form>
</section>
<section class="card">
<h2>All Drivers</h2>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<div class="table-responsive">
<table class="data-table">
<thead>
<tr>
<th>Name</th>
<th>Contact</th>
<th>Status</th>
<th>Joined</th>
</tr>
</thead>
<tbody>
<?php if (empty($drivers)): ?>
<tr>
<td colspan="4">No drivers found.</td>
</tr>
<?php else: ?>
<?php foreach ($drivers as $driver): ?>
<tr>
<td><?php echo htmlspecialchars($driver['name']); ?></td>
<td>
<a href="mailto:<?php echo htmlspecialchars($driver['email']); ?>"><?php echo htmlspecialchars($driver['email']); ?></a><br>
<a href="tel:<?php echo htmlspecialchars($driver['phone']); ?>"><?php echo htmlspecialchars($driver['phone']); ?></a>
</td>
<td><span class="status-badge status-<?php echo strtolower(htmlspecialchars($driver['status'])); ?>"><?php echo htmlspecialchars($driver['status']); ?></span></td>
<td><?php echo date("M d, Y", strtotime($driver['created_at'])); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</section>
</main>
<?php include 'includes/footer.php'; ?>