Mini Kabibi Habibi
<?php
session_start();
include 'includes/db.php';
// Access control
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
header("Location: login.php");
exit();
}
$page_title = "Edit Medical Supply";
// Validate and fetch existing record
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid supply ID.");
}
$id = (int) $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM medical_supplies WHERE id = ?");
$stmt->execute([$id]);
$supply = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$supply) {
die("Supply not found.");
}
$success_message = '';
$error_message = '';
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$item = trim($_POST['item']);
$description = trim($_POST['description']);
$unit = trim($_POST['unit']);
$date_received = $_POST['date_received'];
$expiration_date = $_POST['expiration_date'];
$quantity = intval($_POST['quantity']);
$issued = intval($_POST['issued']);
$balance = intval($_POST['balance']);
if ($item && $unit && $date_received && $expiration_date && $quantity >= 0 && $issued >= 0 && $balance >= 0) {
$updateStmt = $pdo->prepare("
UPDATE medical_supplies SET
item = :item,
description = :description,
unit = :unit,
date_received = :date_received,
expiration_date = :expiration_date,
quantity = :quantity,
issued = :issued,
balance = :balance
WHERE id = :id
");
$updateStmt->execute([
':item' => $item,
':description' => $description,
':unit' => $unit,
':date_received' => $date_received,
':expiration_date' => $expiration_date,
':quantity' => $quantity,
':issued' => $issued,
':balance' => $balance,
':id' => $id
]);
$success_message = "Supply successfully updated.";
// Optional redirect:
// header("Location: current_stock.php");
// exit();
} else {
$error_message = "Please fill out all required fields correctly.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($page_title) ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<style>
body {
margin: 0;
background-color: #f8f9fa;
}
.sidebar {
position: fixed;
top: 0;
left: 0;
width: 250px;
height: 100vh;
background-color: #2c3e50;
color: white;
overflow-y: auto;
}
.sidebar a {
color: #ccc;
text-decoration: none;
padding: 15px;
display: block;
transition: 0.3s;
}
.sidebar a:hover {
background-color: #34495e;
color: #fff;
}
.sidebar .collapse a {
font-size: 0.95rem;
padding-left: 30px;
}
.main-content {
margin-left: 250px;
padding: 40px;
box-sizing: border-box;
min-height: 100vh;
}
h2.page-title {
font-weight: 600;
margin-bottom: 30px;
}
</style>
</head>
<body>
<?php include 'sidebar.php'; ?> <!-- Sidebar included here -->
<div class="main-content">
<h2 class="page-title">Edit Medical Supply</h2>
<?php if ($success_message): ?>
<div class="alert alert-success"><?= htmlspecialchars($success_message) ?></div>
<?php elseif ($error_message): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error_message) ?></div>
<?php endif; ?>
<!-- Modal Triggered Automatically on Page Load -->
<div class="modal fade" id="editSupplyModal" tabindex="-1" aria-labelledby="editSupplyModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<form method="POST" class="needs-validation" novalidate>
<div class="modal-header">
<h5 class="modal-title" id="editSupplyModalLabel">Edit Medical Supply</h5>
<a href="current_stock.php" class="btn-close" aria-label="Close"></a>
</div>
<div class="modal-body">
<div class="row g-3">
<div class="col-md-4">
<label for="item" class="form-label">Item <span class="text-danger">*</span></label>
<input type="text" id="item" name="item" class="form-control" value="<?= htmlspecialchars($supply['item']) ?>" required>
<div class="invalid-feedback">Please enter the item name.</div>
</div>
<div class="col-md-4">
<label for="description" class="form-label">Description</label>
<input type="text" id="description" name="description" class="form-control" value="<?= htmlspecialchars($supply['description']) ?>">
</div>
<div class="col-md-4">
<label for="unit" class="form-label">Unit <span class="text-danger">*</span></label>
<input type="text" id="unit" name="unit" class="form-control" value="<?= htmlspecialchars($supply['unit']) ?>" required>
<div class="invalid-feedback">Please specify the unit.</div>
</div>
<div class="col-md-4">
<label for="date_received" class="form-label">Date Received <span class="text-danger">*</span></label>
<input type="date" id="date_received" name="date_received" class="form-control" value="<?= htmlspecialchars($supply['date_received']) ?>" required>
<div class="invalid-feedback">Please select the date received.</div>
</div>
<div class="col-md-4">
<label for="expiration_date" class="form-label">Expiration Date <span class="text-danger">*</span></label>
<input type="date" id="expiration_date" name="expiration_date" class="form-control" value="<?= htmlspecialchars($supply['expiration_date']) ?>" required>
<div class="invalid-feedback">Please select the expiration date.</div>
</div>
<div class="col-md-4">
<label for="quantity" class="form-label">Quantity <span class="text-danger">*</span></label>
<input type="number" id="quantity" name="quantity" class="form-control" value="<?= htmlspecialchars($supply['quantity']) ?>" required min="0">
<div class="invalid-feedback">Please enter the quantity (minimum 0).</div>
</div>
<div class="col-md-4">
<label for="issued" class="form-label">Issued</label>
<input type="number" id="issued" name="issued" class="form-control" value="<?= htmlspecialchars($supply['issued']) ?>" min="0">
</div>
<div class="col-md-4">
<label for="balance" class="form-label">Balance</label>
<input type="number" id="balance" name="balance" class="form-control" value="<?= htmlspecialchars($supply['balance']) ?>" min="0">
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Update Supply</button>
<a href="current_stock.php" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
// Bootstrap 5 client-side validation
(() => {
'use strict'
const forms = document.querySelectorAll('.needs-validation')
Array.from(forms).forEach(form => {
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
</script>
<script>
document.addEventListener("DOMContentLoaded", function () {
var editModal = new bootstrap.Modal(document.getElementById('editSupplyModal'));
editModal.show();
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>