diff --git a/src/app/page.tsx b/src/app/page.tsx
index a4805bd..6a359fb 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -349,6 +349,127 @@ export default function Home() {
setActiveTenantId(tenantId);
setPortalMode('clinic');
setClinicTab('calendar');
+
+ // Automatically sync active patient, note, and superbills for the selected clinic
+ const tenantPatients = patients.filter((p) => p.tenantId === tenantId);
+ if (tenantPatients.length > 0) {
+ setActivePatient(tenantPatients[0]);
+ const matchingSoap =
+ soapNotes.find((s) => s.patientId === tenantPatients[0].id) ||
+ soapNotes.find((s) => s.tenantId === tenantId);
+ setActiveSoapNote(matchingSoap);
+ }
+
+ const tenantSuperbills = superbills.filter((sb) => sb.tenantId === tenantId);
+ if (tenantSuperbills.length > 0) {
+ setActiveSuperbill(tenantSuperbills[0]);
+ }
+ };
+
+ const handleQuickScheduleAppointment = (newAptData: any) => {
+ clinicalAudio.playSuccess();
+ const newApt: Appointment = {
+ id: `apt-${Date.now()}`,
+ ...newAptData,
+ };
+
+ const existingPatient = patients.find((p) => p.id === newAptData.patientId);
+ if (!existingPatient) {
+ const nameParts = (newAptData.patientName || 'New Patient').split(' ');
+ const firstName = nameParts[0] || 'Walk-In';
+ const lastName = nameParts.slice(1).join(' ') || 'Patient';
+ const newPatient: Patient = {
+ id: newAptData.patientId,
+ tenantId: activeTenant.id,
+ firstName,
+ lastName,
+ email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}@example.com`,
+ phone: newAptData.patientPhone || '(555) 000-0000',
+ dob: '1990-01-01',
+ gender: 'Undisclosed',
+ address: activeTenant.cityStateZip,
+ insuranceName: 'Direct / Stripe Connect',
+ insuranceId: 'SELF-PAY',
+ chiefComplaint: newAptData.notes || newAptData.serviceType,
+ daysSinceLastVisit: 0,
+ lastVisitDate: newAptData.date,
+ nextAppointmentDate: newAptData.date,
+ status: 'active',
+ carePlan: {
+ title: 'Acute Relief & Biomechanical Alignment',
+ totalVisits: 6,
+ completedVisits: 1,
+ frequency: '1-2x / week',
+ targetCondition: newAptData.serviceType,
+ startDate: newAptData.date,
+ status: 'on_track',
+ },
+ };
+ setPatients((prev) => [newPatient, ...prev]);
+ setActivePatient(newPatient);
+ } else {
+ setActivePatient(existingPatient);
+ }
+
+ setAppointments((prev) => [newApt, ...prev]);
+ addToast(
+ 'success',
+ 'Encounter Scheduled',
+ `${newApt.patientName} scheduled for ${newApt.time} (${newApt.serviceType})`
+ );
+ };
+
+ const handleEndTelehealthCall = (consultData: {
+ subjective: string;
+ objective: string;
+ assessment: string;
+ plan: string;
+ duration: string;
+ }) => {
+ clinicalAudio.playSuccess();
+ const today = new Date().toISOString().substring(0, 10);
+ const updatedNote: SoapNote = {
+ id: activeSoapNote?.id || `soap-${Date.now()}`,
+ tenantId: activeTenant.id,
+ patientId: activePatient.id,
+ patientName: `${activePatient.firstName} ${activePatient.lastName}`,
+ providerId: activeProvider.id,
+ providerName: activeProvider.name,
+ date: today,
+ status: 'draft',
+ discipline: 'chiropractic',
+ vasScore: 4,
+ subjective: consultData.subjective,
+ objective: consultData.objective,
+ assessment: consultData.assessment,
+ plan: `${consultData.plan}\n\n[Telehealth Video Consult • Duration: ${consultData.duration} • Encrypted WebRTC Session]`,
+ spinalAdjustments: activeSoapNote?.spinalAdjustments || [],
+ icd10Codes: activeSoapNote?.icd10Codes || [
+ { code: 'M99.01', description: 'Segmental and somatic dysfunction of cervical region' },
+ ],
+ cptCodes: [
+ { code: '99203', description: 'Office/Telehealth Outpatient Visit, 30 min', fee: 110 },
+ { code: '97110', description: 'Therapeutic Exercises & Ergonomic Protocol', fee: 45 },
+ ],
+ };
+
+ setSoapNotes((prev) => {
+ const idx = prev.findIndex((s) => s.patientId === activePatient.id);
+ if (idx >= 0) {
+ const copy = [...prev];
+ copy[idx] = updatedNote;
+ return copy;
+ }
+ return [updatedNote, ...prev];
+ });
+
+ setActiveSoapNote(updatedNote);
+ setClinicTab('charting');
+ addToast(
+ 'success',
+ 'Telehealth Note Synced to Chart',
+ `Clinical findings for ${activePatient.firstName} ${activePatient.lastName} transferred to SOAP editor (${consultData.duration})`
+ );
};
const handleUpdateProductStock = (productId: string, newStock: number) => {
@@ -464,7 +585,7 @@ export default function Home() {