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

257 lines
12 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 Pump
if (isset($_POST['add_pump'])) {
$name = trim($_POST['name']);
$bunk_id = $_POST['bunk_id'];
if (!empty($name) && !empty($bunk_id)) {
try {
$stmt = $db->prepare("INSERT INTO pumps (name, bunk_id) VALUES (?, ?)");
$stmt->execute([$name, $bunk_id]);
$_SESSION['notification'] = ['text' => 'Pump added successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error adding pump: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'Pump name and bunk are required.', 'type' => 'warning'];
}
}
// Edit Pump
elseif (isset($_POST['edit_pump'])) {
$id = $_POST['pump_id'];
$name = trim($_POST['name']);
$bunk_id = $_POST['bunk_id'];
if (!empty($name) && !empty($bunk_id) && !empty($id)) {
try {
$stmt = $db->prepare("UPDATE pumps SET name = ?, bunk_id = ? WHERE id = ?");
$stmt->execute([$name, $bunk_id, $id]);
$_SESSION['notification'] = ['text' => 'Pump updated successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error updating pump: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'Pump name, bunk and ID are required.', 'type' => 'warning'];
}
}
header("Location: pumps.php");
exit;
}
// Soft Delete Pump
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
try {
$db = db();
$stmt = $db->prepare("UPDATE pumps SET deleted_at = NOW() WHERE id = ?");
$stmt->execute([$_GET['id']]);
$_SESSION['notification'] = ['text' => 'Pump deleted successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error deleting pump: ' . $e->getMessage(), 'type' => 'danger'];
}
header("Location: pumps.php");
exit;
}
if (isset($_SESSION['notification'])) {
$notification = $_SESSION['notification'];
unset($_SESSION['notification']);
}
$db = db();
$pumps = $db->query("SELECT p.*, b.name as bunk_name FROM pumps p JOIN bunks b ON p.bunk_id = b.id WHERE p.deleted_at IS NULL ORDER BY p.created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
$bunks = $db->query("SELECT * FROM bunks WHERE deleted_at IS NULL")->fetchAll(PDO::FETCH_ASSOC);
$page_title = "Pump 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-ev-station-fill"></i> <?= htmlspecialchars($page_title) ?></h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPumpModal">
<i class="bi bi-plus-circle"></i> Add New Pump
</button>
</div>
<div class="card">
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Name</th>
<th>Bunk</th>
<th>Created At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($pumps as $pump): ?>
<tr>
<td><?= htmlspecialchars($pump['name']) ?></td>
<td><?= htmlspecialchars($pump['bunk_name']) ?></td>
<td><?= date('d M, Y', strtotime($pump['created_at'])) ?></td>
<td>
<button type="button" class="btn btn-sm btn-outline-primary edit-btn"
data-bs-toggle="modal" data-bs-target="#editPumpModal"
data-id="<?= $pump['id'] ?>"
data-name="<?= htmlspecialchars($pump['name']) ?>"
data-bunk-id="<?= $pump['bunk_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="<?= $pump['id'] ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Add Pump Modal -->
<div class="modal fade" id="addPumpModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Add New Pump</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">Pump Name*</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="mb-3">
<label class="form-label">Bunk*</label>
<select class="form-select" name="bunk_id" required>
<option value="">Select Bunk</option>
<?php foreach ($bunks as $bunk): ?>
<option value="<?= $bunk['id'] ?>"><?= htmlspecialchars($bunk['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_pump" class="btn btn-primary">Save Pump</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Pump Modal -->
<div class="modal fade" id="editPumpModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<input type="hidden" name="pump_id" id="edit_pump_id">
<div class="modal-header">
<h5 class="modal-title">Edit Pump</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">Pump Name*</label>
<input type="text" class="form-control" id="edit_name" name="name" required>
</div>
<div class="mb-3">
<label class="form-label">Bunk*</label>
<select class="form-select" id="edit_bunk_id" name="bunk_id" required>
<?php foreach ($bunks as $bunk): ?>
<option value="<?= $bunk['id'] ?>"><?= htmlspecialchars($bunk['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_pump" 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 pump?</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('editPumpModal');
editModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
editModal.querySelector('#edit_pump_id').value = id;
editModal.querySelector('#edit_name').value = button.getAttribute('data-name');
editModal.querySelector('#edit_bunk_id').value = button.getAttribute('data-bunk-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 = 'pumps.php?action=delete&id=' + id;
});
});
</script>
</body>
</html>