35005-vm/nozzles.php
Flatlogic Bot d9ae8f552d v3
2025-10-16 12:45:17 +00:00

281 lines
14 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 Nozzle
if (isset($_POST['add_nozzle'])) {
$name = trim($_POST['name']);
$pump_id = $_POST['pump_id'];
$tank_id = $_POST['tank_id'];
if (!empty($name) && !empty($pump_id) && !empty($tank_id)) {
try {
$stmt = $db->prepare("INSERT INTO nozzles (name, pump_id, tank_id) VALUES (?, ?, ?)");
$stmt->execute([$name, $pump_id, $tank_id]);
$_SESSION['notification'] = ['text' => 'Nozzle added successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error adding nozzle: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'All fields are required.', 'type' => 'warning'];
}
}
// Edit Nozzle
elseif (isset($_POST['edit_nozzle'])) {
$id = $_POST['nozzle_id'];
$name = trim($_POST['name']);
$pump_id = $_POST['pump_id'];
$tank_id = $_POST['tank_id'];
if (!empty($name) && !empty($pump_id) && !empty($tank_id) && !empty($id)) {
try {
$stmt = $db->prepare("UPDATE nozzles SET name = ?, pump_id = ?, tank_id = ? WHERE id = ?");
$stmt->execute([$name, $pump_id, $tank_id, $id]);
$_SESSION['notification'] = ['text' => 'Nozzle updated successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error updating nozzle: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'All fields and ID are required.', 'type' => 'warning'];
}
}
header("Location: nozzles.php");
exit;
}
// Soft Delete Nozzle
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
try {
$db = db();
$stmt = $db->prepare("UPDATE nozzles SET deleted_at = NOW() WHERE id = ?");
$stmt->execute([$_GET['id']]);
$_SESSION['notification'] = ['text' => 'Nozzle deleted successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error deleting nozzle: ' . $e->getMessage(), 'type' => 'danger'];
}
header("Location: nozzles.php");
exit;
}
if (isset($_SESSION['notification'])) {
$notification = $_SESSION['notification'];
unset($_SESSION['notification']);
}
$db = db();
$nozzles = $db->query("SELECT n.*, p.name as pump_name, t.name as tank_name FROM nozzles n JOIN pumps p ON n.pump_id = p.id JOIN tanks t ON n.tank_id = t.id WHERE n.deleted_at IS NULL ORDER BY n.created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
$pumps = $db->query("SELECT * FROM pumps WHERE deleted_at IS NULL")->fetchAll(PDO::FETCH_ASSOC);
$tanks = $db->query("SELECT * FROM tanks")->fetchAll(PDO::FETCH_ASSOC);
$page_title = "Nozzle 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-cone-striped"></i> <?= htmlspecialchars($page_title) ?></h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addNozzleModal">
<i class="bi bi-plus-circle"></i> Add New Nozzle
</button>
</div>
<div class="card">
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Name</th>
<th>Pump</th>
<th>Tank</th>
<th>Created At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($nozzles as $nozzle): ?>
<tr>
<td><?= htmlspecialchars($nozzle['name']) ?></td>
<td><?= htmlspecialchars($nozzle['pump_name']) ?></td>
<td><?= htmlspecialchars($nozzle['tank_name']) ?></td>
<td><?= date('d M, Y', strtotime($nozzle['created_at'])) ?></td>
<td>
<button type="button" class="btn btn-sm btn-outline-primary edit-btn"
data-bs-toggle="modal" data-bs-target="#editNozzleModal"
data-id="<?= $nozzle['id'] ?>"
data-name="<?= htmlspecialchars($nozzle['name']) ?>"
data-pump-id="<?= $nozzle['pump_id'] ?>"
data-tank-id="<?= $nozzle['tank_id'] ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button type="button" class="btn btn-sm btn-outline-danger delete-btn"
data-bs-toggle="modal" data-bs-target="#deleteConfirmModal"
data-id="<?= $nozzle['id'] ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Add Nozzle Modal -->
<div class="modal fade" id="addNozzleModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Add New Nozzle</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Nozzle Name*</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="mb-3">
<label class="form-label">Pump*</label>
<select class="form-select" name="pump_id" required>
<option value="">Select Pump</option>
<?php foreach ($pumps as $pump): ?>
<option value="<?= $pump['id'] ?>"><?= htmlspecialchars($pump['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Tank*</label>
<select class="form-select" name="tank_id" required>
<option value="">Select Tank</option>
<?php foreach ($tanks as $tank): ?>
<option value="<?= $tank['id'] ?>"><?= htmlspecialchars($tank['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="add_nozzle" class="btn btn-primary">Save Nozzle</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Nozzle Modal -->
<div class="modal fade" id="editNozzleModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<input type="hidden" name="nozzle_id" id="edit_nozzle_id">
<div class="modal-header">
<h5 class="modal-title">Edit Nozzle</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Nozzle Name*</label>
<input type="text" class="form-control" id="edit_name" name="name" required>
</div>
<div class="mb-3">
<label class="form-label">Pump*</label>
<select class="form-select" id="edit_pump_id" name="pump_id" required>
<?php foreach ($pumps as $pump): ?>
<option value="<?= $pump['id'] ?>"><?= htmlspecialchars($pump['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Tank*</label>
<select class="form-select" id="edit_tank_id" name="tank_id" required>
<?php foreach ($tanks as $tank): ?>
<option value="<?= $tank['id'] ?>"><?= htmlspecialchars($tank['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="edit_nozzle" 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 nozzle?</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('editNozzleModal');
editModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
editModal.querySelector('#edit_nozzle_id').value = id;
editModal.querySelector('#edit_name').value = button.getAttribute('data-name');
editModal.querySelector('#edit_pump_id').value = button.getAttribute('data-pump-id');
editModal.querySelector('#edit_tank_id').value = button.getAttribute('data-tank-id');
});
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 = 'nozzles.php?action=delete&id=' + id;
});
});
</script>
</body>
</html>