import { Ionicons } from "@expo/vector-icons"; import { router, useLocalSearchParams } from "expo-router"; import { useMemo, useState } from "react"; import { Alert, Image, ScrollView, View } from "react-native"; import { RxImagePager, RxMedicineLines, RxQueriedPanel, RxStatusStepper, } from "@/components/prescription"; import { EscalationBanner } from "@/components/care"; import { Badge } from "@/components/ui/Badge"; import { Button } from "@/components/ui/Button"; import { Card, InfoRow, Section } from "@/components/ui/Card"; import { DoseReminderPanel } from "@/components/ui/DoseReminderPanel"; import { AppHeader } from "@/components/ui/Header"; import { Screen } from "@/components/ui/Screen"; import { Text } from "@/components/ui/Text"; import { products } from "@/lib/data/products"; import { scheduleDoseReminder } from "@/lib/notifications"; import { routes } from "@/lib/routes"; import { useCartStore } from "@/lib/stores/cartStore"; import { useNotificationStore } from "@/lib/stores/notificationStore"; import { usePrescriptionStore } from "@/lib/stores/prescriptionStore"; import { colors, gutter } from "@/lib/theme"; const statusTone = { submitted: "neutral", under_review: "warn", approved: "brand", queried: "info", rejected: "danger", filled: "brand", } as const; export default function PrescriptionDetailScreen() { const params = useLocalSearchParams<{ id: string }>(); const id = Array.isArray(params.id) ? params.id[0] : params.id; const rx = usePrescriptionStore((s) => s.prescriptions.find((p) => p.id === id)); const allReminders = usePrescriptionStore((s) => s.reminders); const doseLogs = usePrescriptionStore((s) => s.doseLogs); const reminders = useMemo( () => allReminders.filter((r) => r.prescriptionId === id), [allReminders, id] ); const reminder = reminders[0]; const reminderId = `rem_${id}`; const recentLogs = useMemo( () => doseLogs.filter((l) => l.reminderId === reminderId).slice(0, 5), [doseLogs, reminderId] ); const markRefilled = usePrescriptionStore((s) => s.markRefilled); const updateRxStatus = usePrescriptionStore((s) => s.updateRxStatus); const replyToQuery = usePrescriptionStore((s) => s.replyToQuery); const confirmDose = usePrescriptionStore((s) => s.confirmDose); const addProduct = useCartStore((s) => s.addProduct); const pushNotification = useNotificationStore((s) => s.push); const [showSetup, setShowSetup] = useState(!reminder?.enabled); if (!rx) { return ( Prescription not found. ); } const dueIn = (() => { if (!rx.lastFilledAt) return null; const due = new Date(rx.lastFilledAt); due.setDate(due.getDate() + rx.daysSupply); return Math.ceil((due.getTime() - Date.now()) / (1000 * 60 * 60 * 24)); })(); const pendingLog = recentLogs.find((l) => l.status === "pending"); const refillSoon = dueIn !== null && dueIn <= 5; return ( } /> Pharmacist review status {rx.status === "queried" ? ( { replyToQuery(rx.id, message); pushNotification({ kind: "rx", title: "Reply sent", body: "Your clarification was sent back to the pharmacist.", href: routes.rxDetail(rx.id), }); Alert.alert("Sent", "Status moved back to under review."); }} /> ) : null} {rx.status === "rejected" ? ( Prescription rejected {rx.pharmacistMessage || "The pharmacist could not approve this script. Upload a clearer copy or visit a branch."}