120 lines
5.1 KiB
PHP
120 lines
5.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/header.php';
|
|
|
|
$pdo = db();
|
|
$message = '';
|
|
|
|
// Handle form submission for adding a new tank
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_tank'])) {
|
|
$bunk_id = $_POST['bunk_id'];
|
|
$fuel_type_id = $_POST['fuel_type_id'];
|
|
$name = trim($_POST['name']);
|
|
$capacity = $_POST['capacity'];
|
|
|
|
if (!empty($bunk_id) && !empty($fuel_type_id) && !empty($name) && !empty($capacity)) {
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO tanks (bunk_id, fuel_type_id, name, capacity) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$bunk_id, $fuel_type_id, $name, $capacity]);
|
|
$message = '<div class="alert alert-success">Tank added successfully!</div>';
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
} else {
|
|
$message = '<div class="alert alert-warning">Please fill in all fields.</div>';
|
|
}
|
|
}
|
|
|
|
// Fetch related data for forms and listing
|
|
$bunks = $pdo->query("SELECT id, name FROM bunks ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
|
|
$fuel_types = $pdo->query("SELECT id, name FROM fuel_types ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Fetch all tanks with their related bunk and fuel type names
|
|
$stmt = $pdo->query("
|
|
SELECT
|
|
t.id,
|
|
t.name AS tank_name,
|
|
t.capacity,
|
|
b.name AS bunk_name,
|
|
ft.name AS fuel_type_name
|
|
FROM tanks t
|
|
JOIN bunks b ON t.bunk_id = b.id
|
|
JOIN fuel_types ft ON t.fuel_type_id = ft.id
|
|
ORDER BY b.name, t.name
|
|
");
|
|
$tanks = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
?>
|
|
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<h2>Manage Tanks</h2>
|
|
<p>List of all fuel tanks, associated with a bunk and fuel type.</p>
|
|
|
|
<table class="table table-striped table-hover">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th>Tank Name</th>
|
|
<th>Bunk</th>
|
|
<th>Fuel Type</th>
|
|
<th>Capacity (L)</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($tanks)): ?>
|
|
<tr>
|
|
<td colspan="4" class="text-center">No tanks found. Add one to get started.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($tanks as $tank): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($tank['tank_name']); ?></td>
|
|
<td><?php echo htmlspecialchars($tank['bunk_name']); ?></td>
|
|
<td><?php echo htmlspecialchars($tank['fuel_type_name']); ?></td>
|
|
<td><?php echo htmlspecialchars(number_format($tank['capacity'])); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Add New Tank</h5>
|
|
<?php echo $message; ?>
|
|
<form action="tanks.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Tank Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required placeholder="e.g., Tank 1 - MS">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="bunk_id" class="form-label">Bunk</label>
|
|
<select class="form-select" id="bunk_id" name="bunk_id" required>
|
|
<option value="">Select a Bunk</option>
|
|
<?php foreach ($bunks as $bunk): ?>
|
|
<option value="<?php echo $bunk['id']; ?>"><?php echo htmlspecialchars($bunk['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="fuel_type_id" class="form-label">Fuel Type</label>
|
|
<select class="form-select" id="fuel_type_id" name="fuel_type_id" required>
|
|
<option value="">Select a Fuel Type</option>
|
|
<?php foreach ($fuel_types as $fuel_type): ?>
|
|
<option value="<?php echo $fuel_type['id']; ?>"><?php echo htmlspecialchars($fuel_type['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="capacity" class="form-label">Capacity (in Liters)</label>
|
|
<input type="number" class="form-control" id="capacity" name="capacity" required placeholder="e.g., 20000">
|
|
</div>
|
|
<button type="submit" name="add_tank" class="btn btn-primary">Add Tank</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|