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

333 lines
16 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 Bunk
if (isset($_POST['add_bunk'])) {
$name = trim($_POST['name']);
$owner = trim($_POST['owner']);
$contact = trim($_POST['contact']);
$location = trim($_POST['location']);
if (!empty($name)) {
try {
$stmt = $db->prepare("INSERT INTO bunks (name, owner, contact, location) VALUES (?, ?, ?, ?)");
$stmt->execute([$name, $owner, $contact, $location]);
$_SESSION['notification'] = ['text' => 'Bunk added successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error adding bunk: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'Bunk name is required.', 'type' => 'warning'];
}
}
// Edit Bunk
elseif (isset($_POST['edit_bunk'])) {
$id = $_POST['bunk_id'];
$name = trim($_POST['name']);
$owner = trim($_POST['owner']);
$contact = trim($_POST['contact']);
$location = trim($_POST['location']);
if (!empty($name) && !empty($id)) {
try {
$stmt = $db->prepare("UPDATE bunks SET name = ?, owner = ?, contact = ?, location = ? WHERE id = ?");
$stmt->execute([$name, $owner, $contact, $location, $id]);
$_SESSION['notification'] = ['text' => 'Bunk updated successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error updating bunk: ' . $e->getMessage(), 'type' => 'danger'];
}
} else {
$_SESSION['notification'] = ['text' => 'Bunk name and ID are required.', 'type' => 'warning'];
}
}
header("Location: bunks.php");
exit;
}
// Delete Bunk
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
try {
$db = db();
$stmt = $db->prepare("DELETE FROM bunks WHERE id = ?");
$stmt->execute([$_GET['id']]);
$_SESSION['notification'] = ['text' => 'Bunk deleted successfully!', 'type' => 'success'];
} catch (PDOException $e) {
$_SESSION['notification'] = ['text' => 'Error deleting bunk: ' . $e->getMessage(), 'type' => 'danger'];
}
header("Location: bunks.php");
exit;
}
if (isset($_SESSION['notification'])) {
$notification = $_SESSION['notification'];
unset($_SESSION['notification']);
}
try {
$db = db();
$stmt = $db->query("SELECT id, name, owner, contact, location, created_at FROM bunks ORDER BY created_at DESC");
$bunks = $stmt->fetchAll();
} catch (PDOException $e) {
$bunks = [];
$notification = ['text' => 'Error fetching bunks: ' . $e->getMessage(), 'type' => 'danger'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bunk Management</title>
<meta name="description" content="Manage your fuel bunks efficiently. Built with Flatlogic Generator.">
<meta name="keywords" content="fuel bunk management, petrol pump software, inventory, sales tracking, flatlogic">
<meta property="og:title" content="Bunk Management">
<meta property="og:description" content="Manage your fuel bunks efficiently. Built with Flatlogic Generator.">
<meta property="og:image" content="">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
<style>
body {
background-color: #f8f9fa;
}
.navbar {
background: linear-gradient(to right, #0d6efd, #0dcaf0);
}
.toast-container {
z-index: 1080;
}
</style>
</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 class="h2">Bunk Management</h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addBunkModal">
<i class="bi bi-plus-circle me-1"></i> Add New Bunk
</button>
</div>
<div class="card shadow-sm">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th>Name</th>
<th>Owner</th>
<th>Contact</th>
<th>Location</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($bunks)): ?>
<tr>
<td colspan="6" class="text-center text-muted">No bunks found. Add one to get started!</td>
</tr>
<?php else: ?>
<?php foreach ($bunks as $bunk): ?>
<tr>
<td><?= htmlspecialchars($bunk['name']) ?></td>
<td><?= htmlspecialchars($bunk['owner']) ?></td>
<td><?= htmlspecialchars($bunk['contact']) ?></td>
<td><?= htmlspecialchars($bunk['location']) ?></td>
<td><?= date('d M, Y', strtotime($bunk['created_at'])) ?></td>
<td>
<button type="button" class="btn btn-sm btn-outline-secondary edit-btn"
data-bs-toggle="modal" data-bs-target="#editBunkModal"
data-id="<?= $bunk['id'] ?>"
data-name="<?= htmlspecialchars($bunk['name']) ?>"
data-owner="<?= htmlspecialchars($bunk['owner']) ?>"
data-contact="<?= htmlspecialchars($bunk['contact']) ?>"
data-location="<?= htmlspecialchars($bunk['location']) ?>">
<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="<?= $bunk['id'] ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Add Bunk Modal -->
<div class="modal fade" id="addBunkModal" tabindex="-1" aria-labelledby="addBunkModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="bunks.php" method="POST">
<div class="modal-header">
<h5 class="modal-title" id="addBunkModalLabel">Add New Bunk</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="name" class="form-label">Bunk Name*</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="owner" class="form-label">Owner</label>
<input type="text" class="form-control" id="owner" name="owner">
</div>
<div class="mb-3">
<label for="contact" class="form-label">Contact</label>
<input type="text" class="form-control" id="contact" name="contact">
</div>
<div class="mb-3">
<label for="location" class="form-label">Location</label>
<textarea class="form-control" id="location" name="location" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="add_bunk" class="btn btn-primary">Save Bunk</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Bunk Modal -->
<div class="modal fade" id="editBunkModal" tabindex="-1" aria-labelledby="editBunkModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="bunks.php" method="POST">
<input type="hidden" name="bunk_id" id="edit_bunk_id">
<div class="modal-header">
<h5 class="modal-title" id="editBunkModalLabel">Edit Bunk</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="edit_name" class="form-label">Bunk Name*</label>
<input type="text" class="form-control" id="edit_name" name="name" required>
</div>
<div class="mb-3">
<label for="edit_owner" class="form-label">Owner</label>
<input type="text" class="form-control" id="edit_owner" name="owner">
</div>
<div class="mb-3">
<label for="edit_contact" class="form-label">Contact</label>
<input type="text" class="form-control" id="edit_contact" name="contact">
</div>
<div class="mb-3">
<label for="edit_location" class="form-label">Location</label>
<textarea class="form-control" id="edit_location" name="location" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="edit_bunk" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-labelledby="deleteConfirmModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteConfirmModalLabel">Confirm Deletion</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure you want to delete this bunk? This action cannot be undone.
</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 toast-container">
<div id="notificationToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Notification</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</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 () {
// Toast notification
<?php if ($notification): ?>
const toastEl = document.getElementById('notificationToast');
const toastBody = toastEl.querySelector('.toast-body');
toastEl.classList.remove('bg-success', 'bg-danger', 'bg-warning');
toastEl.classList.add('bg-<?= $notification['type'] ?>', 'text-white');
toastBody.textContent = '<?= addslashes(htmlspecialchars($notification['text'])) ?>';
const toast = new bootstrap.Toast(toastEl);
toast.show();
<?php endif; ?>
// Edit modal handler
const editBunkModal = document.getElementById('editBunkModal');
editBunkModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
const name = button.getAttribute('data-name');
const owner = button.getAttribute('data-owner');
const contact = button.getAttribute('data-contact');
const location = button.getAttribute('data-location');
const modalTitle = editBunkModal.querySelector('.modal-title');
const modalBodyInputId = editBunkModal.querySelector('#edit_bunk_id');
const modalBodyInputName = editBunkModal.querySelector('#edit_name');
const modalBodyInputOwner = editBunkModal.querySelector('#edit_owner');
const modalBodyInputContact = editBunkModal.querySelector('#edit_contact');
const modalBodyInputLocation = editBunkModal.querySelector('#edit_location');
modalTitle.textContent = 'Edit Bunk: ' + name;
modalBodyInputId.value = id;
modalBodyInputName.value = name;
modalBodyInputOwner.value = owner;
modalBodyInputContact.value = contact;
modalBodyInputLocation.value = location;
});
// Delete confirmation handler
const deleteConfirmModal = document.getElementById('deleteConfirmModal');
deleteConfirmModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
const deleteBtn = deleteConfirmModal.querySelector('#delete-confirm-btn');
deleteBtn.href = 'bunks.php?action=delete&id=' + id;
});
});
</script>
</body>
</html>