35567-vm/loads.php
2025-11-08 10:32:39 +00:00

127 lines
5.2 KiB
PHP

<?php
require_once 'db/config.php';
try {
$pdo = db();
// Create table if it doesn't exist
$pdo->exec("CREATE TABLE IF NOT EXISTS loads (
id INT AUTO_INCREMENT PRIMARY KEY,
origin VARCHAR(255) NOT NULL,
destination VARCHAR(255) NOT NULL,
pickup_date DATE NOT NULL,
delivery_date DATE NOT NULL,
rate DECIMAL(10, 2) NOT NULL,
status VARCHAR(50) NOT NULL DEFAULT 'Pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$origin = $_POST['origin'] ?? '';
$destination = $_POST['destination'] ?? '';
$pickup_date = $_POST['pickup_date'] ?? '';
$delivery_date = $_POST['delivery_date'] ?? '';
$rate = $_POST['rate'] ?? 0;
if ($origin && $destination && $pickup_date && $delivery_date && $rate) {
$stmt = $pdo->prepare("INSERT INTO loads (origin, destination, pickup_date, delivery_date, rate) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$origin, $destination, $pickup_date, $delivery_date, $rate]);
header("Location: loads.php"); // Redirect to avoid form resubmission
exit;
}
}
// Fetch all loads
$stmt = $pdo->query("SELECT id, origin, destination, pickup_date, delivery_date, rate, status, created_at FROM loads ORDER BY created_at DESC");
$loads = $stmt->fetchAll();
} catch (PDOException $e) {
// For development, you might want to see the error.
// In production, you'd log this and show a generic error message.
die("Database error: " . $e->getMessage());
}
include 'includes/header.php';
include 'includes/sidebar.php';
?>
<main class="main-content">
<header class="header">
<h1>Load Management</h1>
</header>
<section class="content-section">
<h2>Add New Load</h2>
<div class="card">
<form action="loads.php" method="POST" class="form-grid">
<div class="form-group">
<label for="origin">Origin</label>
<input type="text" id="origin" name="origin" required>
</div>
<div class="form-group">
<label for="destination">Destination</label>
<input type="text" id="destination" name="destination" required>
</div>
<div class="form-group">
<label for="pickup_date">Pickup Date</label>
<input type="date" id="pickup_date" name="pickup_date" required>
</div>
<div class="form-group">
<label for="delivery_date">Delivery Date</label>
<input type="date" id="delivery_date" name="delivery_date" required>
</div>
<div class="form-group">
<label for="rate">Rate ($)</label>
<input type="number" id="rate" name="rate" step="0.01" required>
</div>
<div class="form-group form-group-full">
<button type="submit" class="btn btn-primary">Create Load</button>
</div>
</form>
</div>
</section>
<section class="content-section">
<h2>Current Loads</h2>
<div class="card">
<div class="table-responsive">
<table>
<thead>
<tr>
<th>ID</th>
<th>Origin</th>
<th>Destination</th>
<th>Pickup Date</th>
<th>Delivery Date</th>
<th>Rate</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php if (empty($loads)): ?>
<tr>
<td colspan="7" style="text-align:center;">No loads found.</td>
</tr>
<?php else: ?>
<?php foreach ($loads as $load): ?>
<tr>
<td><?php echo htmlspecialchars($load['id']); ?></td>
<td><?php echo htmlspecialchars($load['origin']); ?></td>
<td><?php echo htmlspecialchars($load['destination']); ?></td>
<td><?php echo htmlspecialchars($load['pickup_date']); ?></td>
<td><?php echo htmlspecialchars($load['delivery_date']); ?></td>
<td>$<?php echo htmlspecialchars(number_format($load['rate'], 2)); ?></td>
<td><span class="status-badge status-<?php echo strtolower(htmlspecialchars($load['status'])); ?>"><?php echo htmlspecialchars($load['status']); ?></span></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</section>
</main>
<?php include 'includes/footer.php'; ?>