Mini Kabibi Habibi

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

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

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

$page_title = "All Records";

// Fetch all records with consultation info including physical examination & assessment
$stmt = $pdo->prepare("
    SELECT 
        l.id,
        l.patient_name,
        l.client_type,
        l.school,
        l.age,
        l.sex,
        CASE WHEN hr.log_id IS NULL THEN 'Pending' ELSE 'Completed' END AS status,
        hr.date AS consultation_date,
        hr.chief_complaint,
        hr.findings_bp,
        hr.findings_cr,
        hr.findings_rr,
        hr.findings_o2sat,
        hr.findings_temp,
        hr.physical_examination,
        hr.diagnosis,
        hr.treatment,
        l.log_date,
        l.address,
        l.contact_number,
        l.signature,
        l.attended_by
    FROM logs l
    LEFT JOIN health_records hr ON hr.log_id = l.id
    ORDER BY l.id DESC
");
$stmt->execute();
$all_records = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Separate all logged patients
$stmt2 = $pdo->prepare("SELECT * FROM logs ORDER BY id DESC");
$stmt2->execute();
$all_logged_patients = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>

<!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" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
    <style>
        body {
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            background-color: #f8f9fa;
        }
        .sidebar {
            width: 250px;
            background-color: #2c3e50;
            color: white;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
        }
        .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;
            flex: 1;
        }
        h2.page-title {
            font-weight: 600;
            margin-bottom: 30px;
        }
    </style>
</head>
<body>

<!-- Sidebar -->
<?php include 'sidebar.php'; ?>

<!-- Main content -->
<div class="main-content">
    <h2>All Records</h2>

    <!-- Bootstrap Tabs -->
    <ul class="nav nav-tabs" id="allRecordsTab" role="tablist">
        <li class="nav-item" role="presentation">
            <button class="nav-link active" id="logged-patients-tab" data-bs-toggle="tab" data-bs-target="#logged-patients" type="button" role="tab" aria-controls="logged-patients" aria-selected="true">
                All Logged Patients
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link" id="health-records-tab" data-bs-toggle="tab" data-bs-target="#health-records" type="button" role="tab" aria-controls="health-records" aria-selected="false">
                All Health Records
            </button>
        </li>
    </ul>

    <div class="tab-content p-3 border border-top-0" id="allRecordsTabContent">
        
        <!-- All Logged Patients Tab -->
        <div class="tab-pane fade show active" id="logged-patients" role="tabpanel" aria-labelledby="logged-patients-tab">
            <h5 class="mb-3">All Logged Patients</h5>
            <a href="export_logged_patients_pdf.php" class="btn btn-success mb-3 ms-2">Download PDF</a>

            <?php if (count($all_logged_patients) > 0): ?>
                <div class="table-responsive">
                    <div class="row mb-3">
                        <div class="col-md-2">
                            <label for="filterMonth" class="form-label">Filter by Month</label>
                            <select id="filterMonth" class="form-select">
                                <option value="">All</option>
                                <?php for ($m = 1; $m <= 12; $m++): ?>
                                    <option value="<?= str_pad($m, 2, '0', STR_PAD_LEFT) ?>">
                                        <?= date("F", mktime(0, 0, 0, $m, 1)) ?>
                                    </option>
                                <?php endfor; ?>
                            </select>
                        </div>

                        <div class="col-md-2">
                            <label for="filterYear" class="form-label">Filter by Year</label>
                            <select id="filterYear" class="form-select">
                                <option value="">All</option>
                                <?php
                                    $currentYear = date('Y');
                                    for ($y = $currentYear; $y >= 2020; $y--) {
                                        echo "<option value=\"$y\">$y</option>";
                                    }
                                ?>
                            </select>
                        </div>

                        <div class="col-md-3">
                            <label for="filterType" class="form-label">Filter by Type</label>
                            <select id="filterType" class="form-select">
                                <option value="">All</option>
                                <option value="learners">Learners</option>
                                <option value="teaching">Teaching</option>
                                <option value="non-teaching">Non-Teaching</option>
                            </select>
                        </div>

                        <div class="col-md-3">
                            <label for="filterSex" class="form-label">Filter by Sex</label>
                            <select id="filterSex" class="form-select">
                                <option value="">All</option>
                                <option value="male">Male</option>
                                <option value="female">Female</option>
                            </select>
                        </div>

                        <!-- ✅ Reset Filters Button -->
                        <div class="col-md-2 d-flex align-items-end">
                            <button id="resetLoggedFilters" class="btn btn-secondary w-100">Reset Filters</button>
                        </div>
                    </div>

                    <table class="table table-bordered table-striped align-middle">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Name</th>
                                <th>Type</th>
                                <th>School</th>
                                <th>Age</th>
                                <th>Sex</th>
                                <th>Address</th>
                                <th>Contact</th>
                                <th>Signature</th>
                                <th>Attended by</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($all_logged_patients as $patient): ?>
                                <tr>
                                    <td><?= (new DateTime($patient['log_date']))->format('m/d/Y') ?></td>
                                    <td><?= htmlspecialchars($patient['patient_name']) ?></td>
                                    <td><?= htmlspecialchars($patient['client_type']) ?></td>
                                    <td><?= htmlspecialchars($patient['school']) ?></td>
                                    <td><?= htmlspecialchars($patient['age']) ?></td>
                                    <td><?= htmlspecialchars($patient['sex']) ?></td>
                                    <td><?= htmlspecialchars($patient['address']) ?></td>
                                    <td><?= htmlspecialchars($patient['contact_number']) ?></td>
                                    <td>
                                        <?php 
                                            $signature = $patient['signature'] ?? '';
                                            if ($signature !== '') {
                                                if (strpos($signature, 'data:image') === 0) {
                                                    echo '<img src="' . $signature . '" alt="Signature" width="100" style="object-fit: contain; max-height: 80px;">';
                                                } else {
                                                    $signaturePath = 'signatures/' . $signature;
                                                    if (file_exists($signaturePath)) {
                                                        echo '<img src="' . htmlspecialchars($signaturePath) . '" alt="Signature" width="100" style="object-fit: contain; max-height: 80px;">';
                                                    } else {
                                                        echo '<span class="text-danger">Signature not found</span>';
                                                    }
                                                }
                                            } else {
                                                echo '<span class="text-muted">No Signature</span>';
                                            }
                                        ?>
                                    </td>
                                    <td><?= htmlspecialchars($patient['attended_by']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <div class="alert alert-info">No logged patients found.</div>
            <?php endif; ?>
        </div>

        <!-- All Health Records Tab -->
        <div class="tab-pane fade" id="health-records" role="tabpanel" aria-labelledby="health-records-tab">
            <h5 class="mb-3">All Completed Health Records</h5>
            <a href="export_health_records.php" class="btn btn-success mb-3">Download CSV</a>

            <?php
                $completed_records = array_filter($all_records, fn($r) => $r['status'] === 'Completed');
            ?>
            <?php if (count($completed_records) > 0): ?>
                <div class="table-responsive">
                    <div class="row mb-3">
                        <div class="col-md-2">
                            <label for="filterHealthMonth" class="form-label">Filter by Month</label>
                            <select id="filterHealthMonth" class="form-select">
                                <option value="">All</option>
                                <?php for ($m = 1; $m <= 12; $m++): ?>
                                    <option value="<?= str_pad($m, 2, '0', STR_PAD_LEFT) ?>">
                                        <?= date("F", mktime(0, 0, 0, $m, 1)) ?>
                                    </option>
                                <?php endfor; ?>
                            </select>
                        </div>

                        <div class="col-md-2">
                            <label for="filterHealthYear" class="form-label">Filter by Year</label>
                            <select id="filterHealthYear" class="form-select">
                                <option value="">All</option>
                                <?php
                                    for ($y = date('Y'); $y >= 2020; $y--) {
                                        echo "<option value=\"$y\">$y</option>";
                                    }
                                ?>
                            </select>
                        </div>

                        <div class="col-md-3">
                            <label for="searchName" class="form-label">Search by Name</label>
                            <input type="text" id="searchName" class="form-control" placeholder="Enter patient name..." />
                        </div>

                        <div class="col-md-3">
                            <label for="searchTreatment" class="form-label">Search by Treatment</label>
                            <input type="text" id="searchTreatment" class="form-control" placeholder="Enter treatment details..." />
                        </div>

                        <!-- ✅ Reset Filters Button -->
                        <div class="col-md-2 d-flex align-items-end">
                            <button id="resetHealthFilters" class="btn btn-secondary w-100">Reset Filters</button>
                        </div>
                    </div>

                    <table class="table table-bordered table-striped align-middle">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Patient Name</th>
                                <th>School</th>
                                <th>Age</th>
                                <th>Chief Complaint</th>
                                <th>Findings</th>
                                <th>Treatment</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($completed_records as $record): ?>
                                <tr>
                                    <td><?= htmlspecialchars(date('m/d/Y', strtotime($record['consultation_date']))) ?></td>
                                    <td><?= htmlspecialchars($record['patient_name']) ?></td>
                                    <td><?= htmlspecialchars($record['school']) ?></td>
                                    <td><?= htmlspecialchars($record['age']) ?></td>
                                    <td><?= nl2br(htmlspecialchars($record['chief_complaint'])) ?></td>
                                    <td>
                                        <strong>BP:</strong> <?= htmlspecialchars($record['findings_bp']) ?><br>
                                        <strong>CR:</strong> <?= htmlspecialchars($record['findings_cr']) ?><br>
                                        <strong>RR:</strong> <?= htmlspecialchars($record['findings_rr']) ?><br>
                                        <strong>O₂ Sat:</strong> <?= htmlspecialchars($record['findings_o2sat']) ?><br>
                                        <strong>Temp:</strong> <?= htmlspecialchars($record['findings_temp']) ?><br>
                                        <strong>Physical Examination:</strong> <?= nl2br(htmlspecialchars($record['physical_examination'])) ?><br>
                                        <strong>Diagnosis:</strong> <?= nl2br(htmlspecialchars($record['diagnosis'])) ?>
                                    </td>
                                    <td><?= nl2br(htmlspecialchars($record['treatment'])) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <div class="alert alert-info">No completed health records found.</div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- ✅ JS for Logged Patients Filters -->
<script>
document.addEventListener("DOMContentLoaded", function () {
    const filterMonth = document.getElementById("filterMonth");
    const filterYear = document.getElementById("filterYear");
    const filterType = document.getElementById("filterType");
    const filterSex = document.getElementById("filterSex");
    const resetButton = document.getElementById("resetLoggedFilters");
    const tableRows = document.querySelectorAll("#logged-patients table tbody tr");

    function filterTable() {
        const mVal = filterMonth.value, yVal = filterYear.value;
        const tVal = filterType.value.toLowerCase(), sVal = filterSex.value.toLowerCase();

        tableRows.forEach(row => {
            const [m, d, y] = row.cells[0].textContent.trim().split('/');
            const rowMonth = m.padStart(2, '0'), rowYear = y;
            const rowType = row.cells[2].textContent.toLowerCase().trim();
            const rowSex = row.cells[5].textContent.toLowerCase().trim();

            const show =
                (!mVal || rowMonth === mVal) &&
                (!yVal || rowYear === yVal) &&
                (!tVal || rowType === tVal) &&
                (!sVal || rowSex === sVal);

            row.style.display = show ? '' : 'none';
        });
    }

    [filterMonth, filterYear, filterType, filterSex].forEach(el => {
        el.addEventListener("change", filterTable);
    });

    resetButton.addEventListener("click", () => {
        filterMonth.value = "";
        filterYear.value = "";
        filterType.value = "";
        filterSex.value = "";
        filterTable();
    });
});
</script>

<!-- ✅ JS for Health Records Filters -->
<script>
document.addEventListener('DOMContentLoaded', function() {
    const monthFilter = document.getElementById('filterHealthMonth');
    const yearFilter = document.getElementById('filterHealthYear');
    const nameSearch = document.getElementById('searchName');
    const treatmentSearch = document.getElementById('searchTreatment');
    const resetButton = document.getElementById('resetHealthFilters');
    const tableRows = document.querySelectorAll('#health-records table tbody tr');

    function filterTable() {
        const month = monthFilter.value;
        const year = yearFilter.value;
        const name = nameSearch.value.toLowerCase();
        const treatment = treatmentSearch.value.toLowerCase();

        tableRows.forEach(row => {
            const dateText = row.cells[0].textContent.trim();
            const [m, d, y] = dateText.split('/');
            const rowMonth = m.padStart(2, '0');
            const rowYear = y;
            const rowName = row.cells[1].textContent.toLowerCase();
            const rowTreatment = row.cells[6].textContent.toLowerCase();

            let visible = true;
            if (month && rowMonth !== month) visible = false;
            if (year && rowYear !== year) visible = false;
            if (name && !rowName.includes(name)) visible = false;
            if (treatment && !rowTreatment.includes(treatment)) visible = false;

            row.style.display = visible ? '' : 'none';
        });
    }

    [monthFilter, yearFilter, nameSearch, treatmentSearch].forEach(el => {
        el.addEventListener('input', filterTable);
        el.addEventListener('change', filterTable);
    });

    resetButton.addEventListener('click', () => {
        monthFilter.value = '';
        yearFilter.value = '';
        nameSearch.value = '';
        treatmentSearch.value = '';
        filterTable();
    });
});
</script>

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