Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/clinic/
Upload File :
Current File : C:/xampp/htdocs/clinic/add_supplies.php

<?php
session_start();
include 'includes/db.php'; // Adjust path if needed

// Access control
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: login.php");
    exit();
}

$page_title = "Add Medical Supplies";

$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 = 0;
    $balance = $quantity;

    if ($item && $unit && $date_received && $expiration_date && $quantity > 0) {
        $stmt = $pdo->prepare("
            INSERT INTO medical_supplies 
            (item, description, unit, date_received, expiration_date, quantity, issued, balance)
            VALUES 
            (:item, :description, :unit, :date_received, :expiration_date, :quantity, :issued, :balance)
        ");
        $stmt->execute([
            ':item' => $item,
            ':description' => $description,
            ':unit' => $unit,
            ':date_received' => $date_received,
            ':expiration_date' => $expiration_date,
            ':quantity' => $quantity,
            ':issued' => $issued,
            ':balance' => $balance
        ]);

        $success_message = "Supply successfully added.";
    } else {
        $error_message = "Please fill out all required fields.";
    }
}
?>

<!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>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
</head>
<body>

<?php include 'sidebar.php'; ?>

<div class="main-content">
  <h2 class="page-title">Add Medical Supplies</h2>

  <!-- Alert messages -->
  <?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; ?>

  <!-- Button to trigger modal -->
  <button type="button" class="btn btn-success mb-4" data-bs-toggle="modal" data-bs-target="#addSupplyModal">
  Add Here
  </button>


  <!-- Modal -->
  <div class="modal fade" id="addSupplyModal" tabindex="-1" aria-labelledby="addSupplyModalLabel" 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="addSupplyModalLabel">Add Medical Supply</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>

          <div class="modal-body">
            <div class="row g-3">
              <div class="col-md-4 mb-3">
                <label for="item" class="form-label">Item <span class="text-danger">*</span></label>
                <input type="text" id="item" name="item" class="form-control" required placeholder="Item name" value="<?= htmlspecialchars($_POST['item'] ?? '') ?>">
                <div class="invalid-feedback">Please enter the item name.</div>
              </div>

              <div class="col-md-4 mb-3">
                <label for="unit" class="form-label">Description <span class="text-danger">*</span></label>
                <input type="text" id="description" name="description" class="form-control" required placeholder="Item description" value="<?= htmlspecialchars($_POST['description'] ?? '') ?>">
                <div class="invalid-feedback">Please specify the desciption.</div>
              </div>

              <div class="col-md-4 mb-3">
                <label for="unit" class="form-label">Unit <span class="text-danger">*</span></label>
                <input type="text" id="unit" name="unit" class="form-control" required placeholder="pcs, bottle, etc." value="<?= htmlspecialchars($_POST['unit'] ?? '') ?>">
                <div class="invalid-feedback">Please specify the unit.</div>
              </div>

              <div class="col-md-4 mb-3">
                <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" required value="<?= htmlspecialchars($_POST['date_received'] ?? '') ?>">
                <div class="invalid-feedback">Please select the date received.</div>
              </div>

              <div class="col-md-4 mb-3">
                <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" required value="<?= htmlspecialchars($_POST['expiration_date'] ?? '') ?>">
                <div class="invalid-feedback">Please select the expiration date.</div>
              </div>

              <div class="col-md-4 mb-3">
                <label for="quantity" class="form-label">Quantity <span class="text-danger">*</span></label>
                <input type="number" id="quantity" name="quantity" class="form-control" required min="1" placeholder="0" value="<?= htmlspecialchars($_POST['quantity'] ?? '') ?>">
                <div class="invalid-feedback">Please enter the quantity (minimum 1).</div>
              </div>
            </div>
          </div>

          <div class="modal-footer">
            <button type="submit" class="btn btn-success">Add Supply</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<!-- Bootstrap JS and form validation -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
(() => {
  '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>

<!-- Auto-open modal if there's an error -->
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST' && $error_message): ?>
<script>
  document.addEventListener("DOMContentLoaded", function() {
    var myModal = new bootstrap.Modal(document.getElementById('addSupplyModal'));
    myModal.show();
  });
</script>
<?php endif; ?>
</body>
</html>