239 lines
11 KiB
PHP
239 lines
11 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$notification = null;
|
|
|
|
// Handle Add, Edit, Delete actions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$db = db();
|
|
// Add Fuel Type
|
|
if (isset($_POST['add_fuel_type'])) {
|
|
$name = trim($_POST['name']);
|
|
if (!empty($name)) {
|
|
try {
|
|
$stmt = $db->prepare("INSERT INTO fuel_types (name) VALUES (?)");
|
|
$stmt->execute([$name]);
|
|
$_SESSION['notification'] = ['text' => 'Fuel type added successfully!', 'type' => 'success'];
|
|
} catch (PDOException $e) {
|
|
$_SESSION['notification'] = ['text' => 'Error adding fuel type: ' . $e->getMessage(), 'type' => 'danger'];
|
|
}
|
|
} else {
|
|
$_SESSION['notification'] = ['text' => 'Fuel type name is required.', 'type' => 'warning'];
|
|
}
|
|
}
|
|
// Edit Fuel Type
|
|
elseif (isset($_POST['edit_fuel_type'])) {
|
|
$id = $_POST['fuel_type_id'];
|
|
$name = trim($_POST['name']);
|
|
if (!empty($name) && !empty($id)) {
|
|
try {
|
|
$stmt = $db->prepare("UPDATE fuel_types SET name = ? WHERE id = ?");
|
|
$stmt->execute([$name, $id]);
|
|
$_SESSION['notification'] = ['text' => 'Fuel type updated successfully!', 'type' => 'success'];
|
|
} catch (PDOException $e) {
|
|
$_SESSION['notification'] = ['text' => 'Error updating fuel type: ' . $e->getMessage(), 'type' => 'danger'];
|
|
}
|
|
} else {
|
|
$_SESSION['notification'] = ['text' => 'Fuel type name and ID are required.', 'type' => 'warning'];
|
|
}
|
|
}
|
|
header("Location: fuel_types.php");
|
|
exit;
|
|
}
|
|
|
|
// Delete Fuel Type
|
|
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->prepare("SELECT COUNT(*) FROM tanks WHERE fuel_type_id = ?");
|
|
$stmt->execute([$_GET['id']]);
|
|
if ($stmt->fetchColumn() > 0) {
|
|
$_SESSION['notification'] = ['text' => 'Cannot delete. This fuel type is in use by one or more tanks.', 'type' => 'warning'];
|
|
} else {
|
|
$stmt = $db->prepare("DELETE FROM fuel_types WHERE id = ?");
|
|
$stmt->execute([$_GET['id']]);
|
|
$_SESSION['notification'] = ['text' => 'Fuel type deleted successfully!', 'type' => 'success'];
|
|
}
|
|
} catch (PDOException $e) {
|
|
$_SESSION['notification'] = ['text' => 'Error deleting fuel type: ' . $e->getMessage(), 'type' => 'danger'];
|
|
}
|
|
header("Location: fuel_types.php");
|
|
exit;
|
|
}
|
|
|
|
if (isset($_SESSION['notification'])) {
|
|
$notification = $_SESSION['notification'];
|
|
unset($_SESSION['notification']);
|
|
}
|
|
|
|
$fuel_types = db()->query("SELECT * FROM fuel_types ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);
|
|
$page_title = "Fuel Type Management";
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($page_title) ?> - Petrol Pump Management</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.3/font/bootstrap-icons.min.css">
|
|
</head>
|
|
<body>
|
|
<?php include 'includes/header.php'; ?>
|
|
|
|
<div class="container mt-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h1><i class="bi bi-fuel-pump"></i> <?= htmlspecialchars($page_title) ?></h1>
|
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addFuelTypeModal">
|
|
<i class="bi bi-plus-circle"></i> Add New Fuel Type
|
|
</button>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Name</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($fuel_types as $type): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($type['id']) ?></td>
|
|
<td><?= htmlspecialchars($type['name']) ?></td>
|
|
<td>
|
|
<button type="button" class="btn btn-sm btn-outline-secondary edit-btn"
|
|
data-bs-toggle="modal" data-bs-target="#editFuelTypeModal"
|
|
data-id="<?= $type['id'] ?>"
|
|
data-name="<?= htmlspecialchars($type['name']) ?>">
|
|
<i class="bi bi-pencil"></i>
|
|
</button>
|
|
<button type="button" class="btn btn-sm btn-outline-danger delete-btn"
|
|
data-bs-toggle="modal" data-bs-target="#deleteConfirmModal"
|
|
data-id="<?= $type['id'] ?>">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Fuel Type Modal -->
|
|
<div class="modal fade" id="addFuelTypeModal" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="POST">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Add Fuel Type</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Fuel Type Name*</label>
|
|
<input type="text" class="form-control" name="name" required>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" name="add_fuel_type" class="btn btn-primary">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Fuel Type Modal -->
|
|
<div class="modal fade" id="editFuelTypeModal" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="POST">
|
|
<input type="hidden" name="fuel_type_id" id="edit_fuel_type_id">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Edit Fuel Type</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label for="edit_name" class="form-label">Fuel Type Name*</label>
|
|
<input type="text" class="form-control" id="edit_name" name="name" required>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" name="edit_fuel_type" class="btn btn-primary">Save Changes</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Delete Confirmation Modal -->
|
|
<div class="modal fade" id="deleteConfirmModal" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Confirm Deletion</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">Are you sure you want to delete this fuel type?</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<a href="#" id="delete-confirm-btn" class="btn btn-danger">Delete</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Toast Notification -->
|
|
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
|
|
<div id="notificationToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="toast-header"></div>
|
|
<div class="toast-body"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const notification = <?php echo json_encode($notification); ?>;
|
|
if (notification) {
|
|
const toastEl = document.getElementById('notificationToast');
|
|
const toastHeader = toastEl.querySelector('.toast-header');
|
|
const toastBody = toastEl.querySelector('.toast-body');
|
|
|
|
toastHeader.innerHTML = `<strong class="me-auto">Notification</strong><button type="button" class="btn-close" data-bs-dismiss="toast"></button>`;
|
|
toastBody.textContent = notification.text;
|
|
toastEl.classList.remove('bg-success', 'bg-danger', 'bg-warning');
|
|
toastEl.classList.add('bg-' + notification.type, 'text-white');
|
|
|
|
new bootstrap.Toast(toastEl).show();
|
|
}
|
|
|
|
const editModal = document.getElementById('editFuelTypeModal');
|
|
editModal.addEventListener('show.bs.modal', function (event) {
|
|
const button = event.relatedTarget;
|
|
const id = button.getAttribute('data-id');
|
|
const name = button.getAttribute('data-name');
|
|
editModal.querySelector('#edit_fuel_type_id').value = id;
|
|
editModal.querySelector('#edit_name').value = name;
|
|
});
|
|
|
|
const deleteModal = document.getElementById('deleteConfirmModal');
|
|
deleteModal.addEventListener('show.bs.modal', function (event) {
|
|
const button = event.relatedTarget;
|
|
const id = button.getAttribute('data-id');
|
|
deleteModal.querySelector('#delete-confirm-btn').href = 'fuel_types.php?action=delete&id=' + id;
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|