NexLab_System / templates /patients.html
alisaleem97's picture
Upload folder using huggingface_hub
6ea020b verified
Raw History Blame Contribute Delete
17.4 kB
<!-- templates/patients.html -->
{% extends "base.html" %}
{% block title %}Patient Management - NexLab{% endblock %}
{% block page_title %}Patient Management{% endblock %}
{% block content %}
<div class="bg-white rounded-xl shadow-sm border border-slate-200">
<!-- Header removed per request -->
<!-- Filters -->
<div class="p-6 border-b border-slate-200 bg-slate-50">
<form method="GET" action="/patients" class="grid grid-cols-1 md:grid-cols-5 gap-4">
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Search</label>
<input type="text" name="search" value="{{ search }}" placeholder="Name, ID, or Phone"
class="w-full border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-500 focus:outline-none">
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">From Date (DD-MM-YYYY)</label>
<input type="date" name="from_date" value="{{ from_date }}"
class="w-full border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-500 focus:outline-none">
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">To Date (DD-MM-YYYY)</label>
<input type="date" name="to_date" value="{{ to_date }}"
class="w-full border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-500 focus:outline-none">
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Test</label>
<select name="test_filter" class="w-full border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-500 focus:outline-none">
<option value="">All Tests...</option>
{% for test in tests %}
<option value="{{ test.id }}" {% if test_filter == test.id|string %}selected{% endif %}>
{{ test.test_name }}
</option>
{% endfor %}
</select>
</div>
<div class="flex items-end">
<button type="submit" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-2 rounded-lg text-sm font-medium transition duration-200">
🔍 Filter
</button>
</div>
</form>
</div>
<!-- Success/Error Messages -->
{% if message_success %}
<div class="p-4 bg-green-50 border-b border-green-200 text-green-800 text-sm">
✅ {{ message_success }}
</div>
{% endif %}
{% if message_error %}
<div class="p-4 bg-red-50 border-b border-red-200 text-red-800 text-sm">
❌ {{ message_error }}
</div>
{% endif %}
<!-- Patient Table -->
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-slate-200">
<thead class="bg-slate-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Patient ID</th>
<th class="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Name</th>
<th class="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Gender</th>
<th class="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Age</th>
<th class="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Phone</th>
<th class="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Registered</th>
<th class="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-slate-200">
{% for item in patient_visits %}
<tr class="hover:bg-slate-50">
<td class="px-6 py-4 whitespace-nowrap text-sm font-mono text-slate-600">{{ item.patient.patient_id }}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-slate-800">{{ item.patient.full_name }}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm">
<span class="px-2 py-1 rounded-full text-xs {{ 'bg-blue-100 text-blue-800' if item.patient.gender == 'male' else 'bg-pink-100 text-pink-800' }}">
{{ item.patient.gender|capitalize }}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
{{ item.patient.age }} {{ item.patient.age_unit or 'year' }}{{ 's' if item.patient.age and item.patient.age > 1 else '' }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
{{ item.patient.phone_key }} {{ item.patient.phone_number }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-slate-500">
{{ item.visit_date.strftime('%Y-%m-%d %H:%M') if item.visit_date else item.patient.created_at.strftime('%Y-%m-%d %H:%M') }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<div class="flex gap-2">
<a href="/patients/edit/{{ item.patient.patient_id }}?visit_id={{ item.visit_id }}"
class="text-indigo-600 hover:text-indigo-900" title="Edit">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
</svg>
</a>
<button onclick="printBarcode('{{ item.patient.patient_id }}', '{{ item.visit_id }}')"
class="text-blue-600 hover:text-blue-900" title="Print Barcode">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v1m6 11h2m-6 0h-2v4m0-11v3m0 0h.01M12 12h4.01M16 20h4M4 12h4m12 0h.01M5 8h2a1 1 0 001-1V5a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1zm12 0h2a1 1 0 001-1V5a1 1 0 00-1-1h-2a1 1 0 00-1 1v2a1 1 0 001 1zM5 20h2a1 1 0 001-1v-2a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1z"></path>
</svg>
</button>
<button onclick="printReceipt('{{ item.patient.patient_id }}', '{{ item.visit_id }}')"
class="text-purple-600 hover:text-purple-900" title="Print Receipt">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
</button>
<button onclick="openDeleteModal('{{ item.patient.patient_id }}', '{{ item.patient.full_name }}')"
class="text-red-600 hover:text-red-900" title="Delete">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
</svg>
</button>
</div>
</td>
</tr>
{% else %}
<tr>
<td colspan="7" class="px-6 py-8 text-center text-slate-500">
No patients found. <a href="/patient-registration" class="text-indigo-600 hover:text-indigo-900">Register a new patient</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- Footer with Pagination -->
<div class="p-4 border-t border-slate-200 bg-slate-50">
<div class="flex flex-col sm:flex-row items-center justify-between gap-3">
<p class="text-sm text-slate-600">
Showing <strong>{{ patient_visits|length }}</strong> of <strong>{{ total_count }}</strong> visit(s)
{% if search or test_filter %}
| <a href="/patients" class="text-indigo-600 hover:text-indigo-900">Clear filters</a>
{% endif %}
</p>
{% if total_pages > 1 %}
<nav class="flex items-center gap-1">
{% if page > 1 %}
<a href="/patients?page={{ page - 1 }}&search={{ search }}&from_date={{ from_date }}&to_date={{ to_date }}&test_filter={{ test_filter }}"
class="px-3 py-1.5 text-sm font-medium text-slate-600 bg-white border border-slate-300 rounded-lg hover:bg-slate-100 transition">
← Prev
</a>
{% endif %}
{% for p in range(1, total_pages + 1) %}
{% if p == page %}
<span class="px-3 py-1.5 text-sm font-bold text-white bg-indigo-600 rounded-lg">{{ p }}</span>
{% elif p == 1 or p == total_pages or (p >= page - 2 and p <= page + 2) %}
<a href="/patients?page={{ p }}&search={{ search }}&from_date={{ from_date }}&to_date={{ to_date }}&test_filter={{ test_filter }}"
class="px-3 py-1.5 text-sm font-medium text-slate-600 bg-white border border-slate-300 rounded-lg hover:bg-slate-100 transition">
{{ p }}
</a>
{% elif p == page - 3 or p == page + 3 %}
<span class="px-2 text-slate-400">…</span>
{% endif %}
{% endfor %}
{% if page < total_pages %}
<a href="/patients?page={{ page + 1 }}&search={{ search }}&from_date={{ from_date }}&to_date={{ to_date }}&test_filter={{ test_filter }}"
class="px-3 py-1.5 text-sm font-medium text-slate-600 bg-white border border-slate-300 rounded-lg hover:bg-slate-100 transition">
Next →
</a>
{% endif %}
</nav>
{% endif %}
</div>
</div>
</div>
<!-- Delete Reason Modal -->
<div id="deleteModal" class="fixed inset-0 z-50 hidden items-center justify-center bg-black/50 backdrop-blur-sm">
<div class="bg-white rounded-2xl shadow-2xl w-full max-w-md mx-4 overflow-hidden">
<div class="bg-red-50 px-6 py-4 border-b border-red-100">
<h3 class="text-lg font-semibold text-red-800 flex items-center gap-2">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z"></path>
</svg>
Delete Patient
</h3>
<p class="text-sm text-red-600 mt-1">Patient: <strong id="deletePatientName"></strong></p>
</div>
<form id="deleteForm" method="POST" action="">
<div class="px-6 py-5">
<p id="deleteResultWarning" class="hidden mb-4 p-3 rounded-lg text-sm font-semibold bg-yellow-100 text-yellow-800 outline outline-1 outline-yellow-300"></p>
<label class="block text-sm font-medium text-slate-700 mb-2">Deletion Reason <span class="text-red-500">*</span></label>
<textarea id="deleteReason" name="deleted_reason" rows="3" required
class="w-full border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-red-500 focus:border-red-500 focus:outline-none resize-none"
placeholder="Please enter the reason for deleting this patient..."></textarea>
<p id="deleteReasonError" class="text-xs text-red-500 mt-1 hidden">Deletion reason is required.</p>
</div>
<div class="px-6 py-4 bg-slate-50 flex justify-end gap-3 border-t border-slate-200">
<button type="button" onclick="closeDeleteModal()"
class="px-4 py-2 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50 transition">
Cancel
</button>
<button type="submit" id="deleteConfirmBtn"
class="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded-lg hover:bg-red-700 transition disabled:opacity-50 disabled:cursor-not-allowed" disabled>
Delete Patient
</button>
</div>
</form>
</div>
</div>
<!-- Hidden Print Container -->
<div id="printContainer" style="display: none;"></div>
<script>
function formatPrice(amount) {
return Math.round(amount).toLocaleString('en-US');
}
// Delete Modal Functions
async function openDeleteModal(patientId, patientName) {
document.getElementById('deletePatientName').textContent = patientName;
document.getElementById('deleteForm').action = `/patients/delete/${patientId}`;
document.getElementById('deleteReason').value = '';
document.getElementById('deleteConfirmBtn').disabled = true;
document.getElementById('deleteReasonError').classList.add('hidden');
const warningEl = document.getElementById('deleteResultWarning');
if (warningEl) warningEl.classList.add('hidden');
// Check if patient has results
try {
const resp = await fetch(`/api/patients/${patientId}/has-resulted-tests`);
const data = await resp.json();
if (data.success && data.has_results && warningEl) {
warningEl.textContent = '⚠️ WARNING: This patient has at least one test with a clinical result. Deleting the patient will archive their clinical history.';
warningEl.classList.remove('hidden');
}
} catch(e) {
console.error("Failed to check patient results", e);
}
const modal = document.getElementById('deleteModal');
modal.classList.remove('hidden');
modal.classList.add('flex');
document.getElementById('deleteReason').focus();
}
function closeDeleteModal() {
const modal = document.getElementById('deleteModal');
modal.classList.add('hidden');
modal.classList.remove('flex');
}
// Enable/disable confirm button based on reason input
document.getElementById('deleteReason').addEventListener('input', function() {
const btn = document.getElementById('deleteConfirmBtn');
const error = document.getElementById('deleteReasonError');
if (this.value.trim().length > 0) {
btn.disabled = false;
error.classList.add('hidden');
} else {
btn.disabled = true;
}
});
// Validate on submit
document.getElementById('deleteForm').addEventListener('submit', function(e) {
const reason = document.getElementById('deleteReason').value.trim();
if (!reason) {
e.preventDefault();
document.getElementById('deleteReasonError').classList.remove('hidden');
document.getElementById('deleteReason').focus();
}
});
// Close modal on backdrop click
document.getElementById('deleteModal').addEventListener('click', function(e) {
if (e.target === this) closeDeleteModal();
});
// Close modal on Escape key
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') closeDeleteModal();
});
// Print Barcode Function - Opens proper barcode page
async function printBarcode(patientId, visitId) {
let url = '/print-barcode/' + patientId;
if (visitId) url += '?visit_id=' + encodeURIComponent(visitId);
window.open(url, '_blank');
}
// Print Receipt Function - Opens proper receipt page
async function printReceipt(patientId, visitId) {
let url = '/print-receipt/' + patientId;
if (visitId) url += '?visit_id=' + encodeURIComponent(visitId);
window.open(url, '_blank');
}
</script>
<script src="/static/vendor/bwip-js/bwip-js.min.js"></script>
<script src="/static/vendor/bwip-js/barcode-helper.js"></script>
{% endblock %}