import{t as re,D as ie,v as le,r as c,d as f,j as e,l as i,C as oe,R as P,B as I,m as H}from"./index-DGCPJq4z.js";import{O as C}from"./OptionsSelect-Mr65YEpO.js";import{P as de}from"./PageBreadcrumb-zFeQijFR.js";import{S as ce}from"./SlotSelect-Bk2AJPwg.js";import{u as me,W as ue}from"./useSettings-YCBBpGwE.js";import{a as he}from"./dates-DzZ1gfzQ.js";import{m as K,i as ge}from"./mobile-CfiPsqNQ.js";import{F as o}from"./FormLabel-DoSLr8ng.js";import{F as h}from"./FormControl-DGdX8GC2.js";import{C as pe,a as xe}from"./Card-D1j94NWu.js";import{F as je}from"./FormCheck-tmQhdIFS.js";import"./PageMetaData-B-bWCH8Q.js";import"./FormContext-DDFZ16IF.js";const E={name:"",mobile:"",age:"",gender:"",sessionLanguage:"",cohort:"",slot:"",preferredWeekday:"",diseaseType:"",paymentStatus:"Unpaid",sessionsPaid:30,patientStatus:"active",whatsappOptOut:!1};function _(r){return he(r)}function fe(r){const g=[_(r.startDate),r.slot,r.language,r.diseaseType].filter(Boolean).join(" · ");return g?`${r.name} (${g})`:r.name}function x(r){return r==null||r===""?"—":r}function V(r){return{...E,name:r.name||"",mobile:r.mobile||"",age:r.age??"",gender:r.gender||"",sessionLanguage:r.sessionLanguage||"",slot:r.slot||""}}function Ee(){const{id:r}=re(),[O]=ie(),g=(O.get("patientId")||"").trim(),l=!!r,z=le(),[Q,M]=c.useState(E),[X,L]=c.useState(E),[W,T]=c.useState(""),[t,S]=c.useState(null),[A,v]=c.useState(""),[B,y]=c.useState(!1),[w,$]=c.useState([]),[q,k]=c.useState(null),[p,Z]=c.useState(!1),[R,m]=c.useState(""),[N,G]=c.useState(!1),{settings:b}=me(),ee=b?.cohortStartWeekdays||[1,2],a=t?X:Q,d=(s,n)=>{(t?L:M)(D=>({...D,[s]:n}))},F=!a.cohort,U=String(t?.importedLanguage||t?.sessionLanguage||"").trim(),se=l||!t||!U,ae=l||!String(t?.slot||"").trim(),te=l||!t;c.useEffect(()=>{if(l||!g)return;let s=!0;return T(g),m(""),v(""),y(!0),f(`/api/onboarding/patient-lookup?patientId=${encodeURIComponent(g)}`).then(n=>{s&&(k(null),n.found&&n.patient?(S(n.patient),L(V(n.patient))):(S(null),v("No patient found for this ID.")))}).catch(n=>{s&&m(n.message)}).finally(()=>{s&&y(!1)}),()=>{s=!1}},[l,g]),c.useEffect(()=>{l&&f(`/api/onboarding/${r}`).then(s=>{Z(s.reviewStatus==="confirmed"),M({name:s.name||"",mobile:s.mobile||"",age:s.age??"",gender:s.gender||"",sessionLanguage:s.sessionLanguage||"",cohort:s.cohort?.id||"",slot:s.slot||"",preferredWeekday:s.preferredWeekday??"",diseaseType:s.diseaseType||"",paymentStatus:s.paymentStatus||"Unpaid",sessionsPaid:s.sessionsPaid??30,patientStatus:s.patientStatus||"active",whatsappOptOut:!!s.whatsappOptOut})}).catch(s=>m(s.message))},[r,l]),c.useEffect(()=>{const s=new URLSearchParams;a.sessionLanguage&&s.set("language",a.sessionLanguage),a.cohort&&s.set("include",a.cohort);let n=!0;return f(`/api/onboarding/available-cohorts?${s}`).then(j=>{n&&$(j.items||[])}).catch(()=>{n&&$([])}),()=>{n=!1}},[a.sessionLanguage,a.cohort]);const u=c.useMemo(()=>w.find(s=>s.id===a.cohort)||null,[w,a.cohort]),ne=async()=>{const s=W.trim();if(!s){m("Enter a patient ID.");return}m(""),v(""),y(!0);try{const n=await f(`/api/onboarding/patient-lookup?patientId=${encodeURIComponent(s)}`);k(null),n.found&&n.patient?(S(n.patient),L(V(n.patient))):(S(null),v("No patient found for this ID."))}catch(n){m(n.message)}finally{y(!1)}},J=async s=>{if(s.preventDefault(),m(""),v(""),!p&&F&&(!a.sessionLanguage||!a.slot||a.preferredWeekday===""||!a.diseaseType)){m("Without a cohort, language, slot, preferred weekday, and category are required.");return}if(!String(t?.mobile||"").trim()&&!ge(a.mobile)){m("Mobile must be 10 digits.");return}G(!0);const n=new FormData;Object.entries(a).forEach(([j,D])=>n.append(j,D??"")),!l&&t?.sourcePatientId&&n.append("sourcePatientId",t.sourcePatientId),q&&n.append("screenshot",q);try{l?await f(`/api/onboarding/${r}`,{method:"PATCH",body:n}):await f("/api/onboarding",{method:"POST",body:n}),z("/onboarding")}catch(j){m(j.message)}finally{G(!1)}},Y=e.jsxs(e.Fragment,{children:[se?e.jsxs(i,{md:6,children:[e.jsxs(o,{children:["Language ",F?e.jsx("span",{className:"text-danger",children:"*"}):null]}),e.jsx(C,{value:a.sessionLanguage,onChange:s=>d("sessionLanguage",s),options:b?.languages,disabled:p}),t&&!U?e.jsx("div",{className:"form-text",children:"This patient has no language on the master record. Select one to continue."}):null]}):null,e.jsxs(i,{md:4,children:[e.jsx(o,{children:"Payment status"}),e.jsx(C,{value:a.paymentStatus,onChange:s=>d("paymentStatus",s),options:b?.paymentStatuses,required:!0})]}),e.jsxs(i,{md:4,children:[e.jsx(o,{children:"Sessions paid"}),e.jsx(h,{type:"number",min:0,value:a.sessionsPaid,onChange:s=>d("sessionsPaid",s.target.value===""?"":Number(s.target.value)),placeholder:"30"}),e.jsx("div",{className:"form-text",children:"Defaults to 30 if left blank. Raise this after more payment to clear the red limit."})]}),e.jsxs(i,{md:4,children:[e.jsx(o,{children:"Patient status"}),e.jsxs("select",{className:"form-select",value:a.patientStatus,onChange:s=>d("patientStatus",s.target.value),children:[e.jsx("option",{value:"active",children:"Active"}),e.jsx("option",{value:"deactivated",children:"Deactivated"})]}),e.jsx("div",{className:"form-text",children:"Deactivated keeps the patient on the cohort. Only an active patient can be moved."})]}),F?e.jsxs(e.Fragment,{children:[ae?e.jsxs(i,{md:4,children:[e.jsxs(o,{children:["Slot ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(ce,{value:a.slot,onChange:s=>d("slot",s),required:!0,disabled:p}),t?.importedSlot?e.jsxs("div",{className:"form-text",children:["Imported sessions use ",t.importedSlot,". Select the matching slot."]}):null]}):null,e.jsxs(i,{md:4,children:[e.jsxs(o,{children:["Preferred start weekday ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsxs("select",{className:"form-select",value:a.preferredWeekday,onChange:s=>d("preferredWeekday",s.target.value),required:!0,disabled:p,children:[e.jsx("option",{value:"",children:"Select weekday"}),ee.map(s=>e.jsx("option",{value:s,children:ue[s]},s))]})]}),e.jsxs(i,{md:4,children:[e.jsxs(o,{children:["Category ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(C,{value:a.diseaseType,onChange:s=>d("diseaseType",s),options:b?.diseaseTypes,disabled:p})]})]}):null,e.jsxs(i,{md:6,children:[e.jsx(o,{children:"Payment screenshot"}),e.jsx(h,{type:"file",accept:".jpg,.jpeg,.png,.webp,image/jpeg,image/jpg,image/png,image/webp",onChange:s=>k(s.target.files?.[0]||null)}),e.jsxs("div",{className:"form-text",children:[l?"Leave empty to keep the current screenshot. ":"","JPG, JPEG, PNG, or WebP. Max 5MB."]})]})]});return e.jsxs(oe,{fluid:!0,children:[e.jsx(de,{title:l?"Edit patient":"Add patient",subtitle:"Patients"}),e.jsx(pe,{children:e.jsxs(xe,{children:[p?e.jsx("div",{className:"alert alert-info",children:"This patient is confirmed. You can update contact and clinical details. Language, slot, weekday, category, and cohort stay as they are — use Move on the patient list to change those."}):null,R?e.jsx("div",{className:"alert alert-danger",children:R}):null,l?null:e.jsx("form",{onSubmit:s=>{s.preventDefault(),ne()},children:e.jsx(P,{className:"g-3",children:e.jsxs(i,{md:6,children:[e.jsx(o,{children:"Patient ID"}),e.jsxs("div",{className:"d-flex gap-2",children:[e.jsx(h,{value:W,onChange:s=>T(s.target.value),placeholder:"Enter patient ID"}),e.jsx(I,{type:"submit",variant:"primary",disabled:B,children:B?"Searching...":"Search"})]})]})})}),!l&&A?e.jsx("div",{className:"alert alert-danger mt-3 mb-0",children:A}):null,!l&&t?e.jsxs("form",{onSubmit:J,className:"mt-4",children:[e.jsxs("div",{className:"border rounded p-3 bg-light mb-3",children:[e.jsx("div",{className:"fw-semibold mb-2",children:"Patient found"}),e.jsxs(P,{className:"g-2",children:[e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Patient ID"}),e.jsx("div",{children:x(t.sourcePatientId)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Name"}),e.jsx("div",{children:x(t.name)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Mobile"}),e.jsx("div",{children:x(t.mobile)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Age"}),e.jsx("div",{children:x(t.age)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Gender"}),e.jsx("div",{children:x(t.gender)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Language"}),e.jsx("div",{children:x(t.importedLanguage||t.sessionLanguage)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Slot"}),e.jsx("div",{children:x(t.importedSlot||t.slot)})]})]})]}),e.jsxs(P,{className:"g-3",children:[String(t.name||"").trim()?null:e.jsxs(i,{md:6,children:[e.jsxs(o,{children:["Name ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(h,{value:a.name,onChange:s=>d("name",s.target.value),required:!0})]}),String(t.mobile||"").trim()?null:e.jsxs(i,{md:6,children:[e.jsxs(o,{children:["Mobile ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(h,{inputMode:"numeric",maxLength:10,value:a.mobile,onChange:s=>d("mobile",K(s.target.value)),required:!0})]}),Y]}),e.jsxs("div",{className:"d-flex gap-2 mt-4",children:[e.jsx(I,{type:"submit",disabled:N,children:N?"Saving...":"Add patient"}),e.jsx(H,{className:"btn btn-light",to:"/onboarding",children:"Cancel"})]})]}):null,te?e.jsxs(e.Fragment,{children:[l?null:e.jsxs("div",{className:"d-flex align-items-center my-4",children:[e.jsx("hr",{className:"flex-grow-1 m-0"}),e.jsx("span",{className:"px-3 text-muted fw-semibold",children:"OR"}),e.jsx("hr",{className:"flex-grow-1 m-0"})]}),e.jsxs("form",{onSubmit:J,children:[e.jsxs(P,{className:"g-3",children:[e.jsxs(i,{md:6,children:[e.jsxs(o,{children:["Name ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(h,{value:a.name,onChange:s=>d("name",s.target.value),required:!0})]}),e.jsxs(i,{md:6,children:[e.jsxs(o,{children:["Mobile ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(h,{inputMode:"numeric",maxLength:10,value:a.mobile,onChange:s=>d("mobile",K(s.target.value)),required:!0})]}),e.jsxs(i,{md:3,children:[e.jsx(o,{children:"Age"}),e.jsx(h,{type:"number",min:0,value:a.age,onChange:s=>d("age",s.target.value)})]}),e.jsxs(i,{md:3,children:[e.jsx(o,{children:"Gender"}),e.jsx(C,{value:a.gender,onChange:s=>d("gender",s),options:b?.genders})]}),e.jsxs(i,{md:8,children:[e.jsx(o,{children:"Future cohort (optional)"}),e.jsxs("select",{className:"form-select",value:a.cohort,onChange:s=>d("cohort",s.target.value),disabled:p,children:[e.jsx("option",{value:"",children:"Leave unassigned for matching"}),w.map(s=>e.jsx("option",{value:s.id,children:fe(s)},s.id))]}),u?e.jsxs("div",{className:"form-text",children:["Starts ",_(u.startDate)," · ",u.slot,u.language?` · ${u.language}`:"",u.virtualPrt?` · ${u.virtualPrt.firstName} ${u.virtualPrt.lastName}`:""]}):e.jsx("div",{className:"form-text",children:"Choose a future cohort, or leave empty and set language, slot, weekday, and category for the matching pool."})]}),e.jsx(i,{xs:12,children:e.jsx(je,{checked:!!a.whatsappOptOut,onChange:s=>d("whatsappOptOut",s.target.checked),label:"Do not add this patient to the cohort WhatsApp group"})}),Y]}),e.jsxs("div",{className:"d-flex gap-2 mt-4",children:[e.jsx(I,{type:"submit",disabled:N,children:N?"Saving...":l?"Save changes":"Add patient"}),e.jsx(H,{className:"btn btn-light",to:"/onboarding",children:"Cancel"})]})]})]}):null]})})]})}export{Ee as default};