import { useState } from "react"; import { useAuth } from "@clerk/react"; import { confirmVisit } from "../lib/api"; export default function ConfirmVisitModal({ record, onClose, onConfirmed, onSaveSuccess }) { const { getToken } = useAuth(); const [visitDate, setVisitDate] = useState(""); const [error, setError] = useState(""); const [saving, setSaving] = useState(false); const [step, setStep] = useState("entry"); // "entry" | "confirm" const today = new Date().toISOString().split("T")[0]; const minDate = "2020-01-01"; // Real shipment date from the backend record. The legacy reconstruction from // coverage_end_date is a fallback for records loaded before last_shipment_date // existed; it never fabricates today's date. function computeShipmentDate() { if (record.last_shipment_date) return record.last_shipment_date; if (record.coverage_end_date && record.days_until_coverage_end !== undefined) { const coverageEnd = new Date(record.coverage_end_date); const shipmentMs = coverageEnd.getTime() - record.days_until_coverage_end * 86400000; return new Date(shipmentMs).toISOString().split("T")[0]; } return null; } function handleValidateAndAdvance() { setError(""); if (!visitDate) { setError("Please enter a visit date."); return; } if (visitDate > today) { setError("Visit date cannot be in the future."); return; } // Validate: visit must be within 6 months (183 days, matching the backend) // before the order/shipment date const shipmentDateStr = computeShipmentDate(); if (!shipmentDateStr) { setError("This record has no shipment date on file. Re-import the CSV and try again."); return; } const shipmentDateObj = new Date(shipmentDateStr + "T00:00:00"); const visitDateObj = new Date(visitDate + "T00:00:00"); const sixMonthsBeforeShipment = new Date(shipmentDateObj.getTime() - 183 * 86400000); if (visitDateObj < sixMonthsBeforeShipment) { setError("Visit date must be within 6 months of the order date."); return; } setStep("confirm"); } async function handleConfirmAndSave() { setSaving(true); try { const token = await getToken().catch(() => null); const updated = await confirmVisit({ patient_id: record.patient_id, confirmed_date: visitDate, shipment_date: computeShipmentDate(), payer: record.payer, device_type: record.device_type, quantity: record.quantity || 1, component: record.component || "sensor", // Echo doc status fields so the backend recompute preserves // checklist state instead of regressing it to defaults csv_visit_date: record.csv_visit_date || null, csv_swo_status: record.csv_swo_status || null, csv_pecos_verified: record.csv_pecos_verified || null, csv_pa_status: record.csv_pa_status || null, csv_diagnosis_on_file: record.csv_diagnosis_on_file || null, csv_transfer_from: record.csv_transfer_from || null, order_number: record.order_number || null, hcpcs: record.hcpcs || null, // P6 nested view: plan type + the patient's echoed lines so the // response carries the re-rolled patient (visit fans out to all // lines). Legacy records lack these — behavior unchanged. ...(record.plan_type ? { plan_type: record.plan_type } : {}), ...(record._echo_lines ? { lines: record._echo_lines } : {}), }, token); onConfirmed(record.patient_id, updated); onSaveSuccess?.({ patient_id: record.patient_id }); onClose(); } catch (e) { setError(e.message || "Failed to save. Please try again."); setStep("entry"); } finally { setSaving(false); } } const formattedVisitDate = visitDate ? new Date(visitDate + "T00:00:00").toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric" }) : ""; return (