Mini Kabibi Habibi

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

<?php
session_start();
include 'includes/db.php';

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

$page_title = "Issuance Records";

// Filter by supply_name
$filter_supply_name = isset($_GET['supply_name']) ? trim($_GET['supply_name']) : '';

// Entries per page
$perPageOptions = [10, 20, 50, 100];
$limit = isset($_GET['limit']) && in_array((int)$_GET['limit'], $perPageOptions) ? (int)$_GET['limit'] : 10;

$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Build WHERE clause for filtering
$whereClause = '';
$params = [];

if ($filter_supply_name !== '') {
    $whereClause = 'WHERE supply_name = :supply_name';
    $params[':supply_name'] = $filter_supply_name;
}

// Count total records for pagination
$count_stmt = $pdo->prepare("SELECT COUNT(*) FROM supply_issuance $whereClause");
$count_stmt->execute($params);
$total_records = $count_stmt->fetchColumn();
$total_pages = ceil($total_records / $limit);

// Fetch paginated records
$data_sql = "SELECT * FROM supply_issuance $whereClause ORDER BY date DESC, id DESC LIMIT :limit OFFSET :offset";
$stmt = $pdo->prepare($data_sql);

// Bind dynamic params
if ($filter_supply_name !== '') {
    $stmt->bindValue(':supply_name', $filter_supply_name);
}
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get distinct supply names for dropdown
$supply_names_stmt = $pdo->query("SELECT DISTINCT supply_name FROM supply_issuance ORDER BY supply_name ASC");
$supply_names = $supply_names_stmt->fetchAll(PDO::FETCH_COLUMN);
?>


<!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;
    }

    .main-content {
      margin-left: 250px;
      padding: 40px;
      box-sizing: border-box;
    }

    h2.page-title {
    font-weight: 600;
    margin-bottom: 30px;
    }


    .table-container {
      max-height: 500px;
      overflow-y: auto;
      border: 1px solid #ddd;
    }

    table thead th {
      position: sticky;
      top: 0;
      background-color: #343a40;
      color: #fff;
      z-index: 1;
    }

    .table td, .table th {
      word-wrap: break-word;
      white-space: normal;
      word-break: break-word;
    }

    td:nth-child(9) {
      max-width: 120px; /* Smaller width = more wrapping */
      overflow-wrap: break-word;
    }

    .pagination {
      margin-top: 20px;
    }

    .filter-form {
      display: flex;
      gap: 1rem;
      flex-wrap: wrap;
      align-items: center;
      margin-bottom: 20px;
    }

    .filter-form .form-select {
      min-width: 200px;
    }
  </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"><?= htmlspecialchars($page_title) ?></h2>

  <form method="get" class="d-flex flex-wrap align-items-center gap-3 mb-3">

<!-- Records per page -->
  <div class="d-flex align-items-center">
    <label for="limit" class="me-2 mb-0">Show:</label>
    <select name="limit" id="limit" class="form-select form-select-sm" onchange="this.form.submit()">
      <?php foreach ($perPageOptions as $option): ?>
        <option value="<?= $option ?>" <?= $limit === $option ? 'selected' : '' ?>><?= $option ?></option>
      <?php endforeach; ?>
    </select>
  </div>

  <!-- Supply Name Filter -->
  <div class="d-flex align-items-center">
    <label for="supply_name" class="me-2 mb-0">Supply:</label>
    <select name="supply_name" id="supply_name" class="form-select form-select-sm" onchange="this.form.submit()">
      <option value="">All</option>
      <?php foreach ($supply_names as $name): ?>
        <option value="<?= htmlspecialchars($name) ?>" <?= ($filter_supply_name === $name) ? 'selected' : '' ?>>
          <?= htmlspecialchars($name) ?>
        </option>
      <?php endforeach; ?>
    </select>
  </div>

  <input type="hidden" name="page" value="1" />

</form>



  <?php if (count($records) === 0): ?>
    <div class="alert alert-info">No issuance records found.</div>
  <?php else: ?>
    <div class="table-container">
      <table class="table table-bordered table-striped align-middle mb-0">
        <thead>
          <tr>
            <th>Date</th>
            <th>Patient Name</th>
            <th>Age</th>
            <th>School</th>
            <th>Supply Name</th>
            <th>Quantity</th>
            <th>Released By</th>
            <th>Received By</th>
            <th>Remarks</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($records as $row): ?>
            <tr>
              <td><?= htmlspecialchars($row['date']) ?></td>
              <td><?= htmlspecialchars($row['patient_name']) ?></td>
              <td><?= htmlspecialchars($row['age']) ?></td>
              <td><?= htmlspecialchars($row['school']) ?></td>
              <td><?= htmlspecialchars($row['supply_name']) ?></td>
              <td><?= htmlspecialchars($row['quantity']) ?></td>
              <td><?= htmlspecialchars($row['released_by']) ?></td>
              <td><?= htmlspecialchars($row['received_by']) ?></td>
              <td><?= nl2br(htmlspecialchars($row['remarks'])) ?></td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>

    <!-- Pagination -->
    <nav>
  <ul class="pagination">
    <!-- Prev button -->
    <?php if ($page > 1): ?>
      <li class="page-item">
        <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $page - 1])) ?>">&laquo; Prev</a>
      </li>
    <?php else: ?>
      <li class="page-item disabled">
        <span class="page-link">&laquo; Prev</span>
      </li>
    <?php endif; ?>

    <!-- Page numbers -->
    <?php
    $range = 3;
    for ($i = max(1, $page - $range); $i <= min($total_pages, $page + $range); $i++):
      $link = '?' . http_build_query(array_merge($_GET, ['page' => $i]));
    ?>
      <li class="page-item <?= $i == $page ? 'active' : '' ?>">
        <a class="page-link" href="<?= $link ?>"><?= $i ?></a>
      </li>
    <?php endfor; ?>

    <!-- Next button -->
    <?php if ($page < $total_pages): ?>
      <li class="page-item">
        <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $page + 1])) ?>">Next &raquo;</a>
      </li>
    <?php else: ?>
      <li class="page-item disabled">
        <span class="page-link">Next &raquo;</span>
      </li>
    <?php endif; ?>
  </ul>
</nav>

  <?php endif; ?>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>