import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Search, Edit, Eye, ChevronLeft, ChevronRight, Settings, } from "lucide-react"; import { useAuth } from "@/hooks/use-auth"; import { cn } from "@/lib/utils"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Patient } from "@repo/db/types"; import { formatDateToHumanReadable } from "@/utils/dateUtils"; export default function ReportsPage() { const { user } = useAuth(); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [searchTerm, setSearchTerm] = useState(""); const [searchField, setSearchField] = useState("all"); const [currentPage, setCurrentPage] = useState(1); const itemsPerPage = 5; // Fetch patients const { data: patients = [], isLoading: isLoadingPatients } = useQuery< Patient[] >({ queryKey: ["/api/patients"], enabled: !!user, }); // Filter patients based on search const filteredPatients = patients.filter((patient) => { if (!searchTerm) return true; const searchLower = searchTerm.toLowerCase(); const fullName = `${patient.firstName} ${patient.lastName}`.toLowerCase(); const patientId = `PID-${patient?.id?.toString().padStart(4, "0")}`; switch (searchField) { case "name": return fullName.includes(searchLower); case "id": return patientId.toLowerCase().includes(searchLower); case "phone": return patient.phone?.toLowerCase().includes(searchLower) || false; case "all": default: return ( fullName.includes(searchLower) || patientId.toLowerCase().includes(searchLower) || patient.phone?.toLowerCase().includes(searchLower) || patient.email?.toLowerCase().includes(searchLower) || false ); } }); // Pagination const totalPages = Math.ceil(filteredPatients.length / itemsPerPage); const startIndex = (currentPage - 1) * itemsPerPage; const endIndex = startIndex + itemsPerPage; const currentPatients = filteredPatients.slice(startIndex, endIndex); const toggleMobileMenu = () => { setIsMobileMenuOpen(!isMobileMenuOpen); }; const getPatientInitials = (firstName: string, lastName: string) => { return `${firstName.charAt(0)}${lastName.charAt(0)}`.toUpperCase(); }; return (
View and manage all patient information