import{t as E,v as P,b as w,r as c,d as h,j as s,C as I,R as S,l as t,B as T,m as F}from"./index-DGCPJq4z.js";import{O as C}from"./OptionsSelect-Mr65YEpO.js";import{P as L}from"./PageBreadcrumb-zFeQijFR.js";import{u as A}from"./useSettings-YCBBpGwE.js";import{m as H,i as B}from"./mobile-CfiPsqNQ.js";import{C as D,a as M}from"./Card-D1j94NWu.js";import{F as a}from"./FormLabel-DoSLr8ng.js";import{F as o}from"./FormControl-DGdX8GC2.js";import"./PageMetaData-B-bWCH8Q.js";import"./FormContext-DDFZ16IF.js";const $={sourcePatientId:"",name:"",mobile:"",age:"",gender:"",lungCondition:"",otherHealthCondition:"",language:"",consentType:"",isNewOrOld:"",doctor:{sourceId:"",name:"",uin:""},assignedEducator:""};function W(){const{id:r}=E(),d=!!r,j=P(),{can:b,permissions:f}=w(),[n,u]=c.useState($),[N,O]=c.useState([]),[p,m]=c.useState(""),x=b("patients","edit")&&f.scopes?.patients!=="assigned",{settings:v}=A();c.useEffect(()=>{x&&h("/api/users/educators").then(e=>O(e.items||[])).catch(()=>{})},[x]),c.useEffect(()=>{d&&h(`/api/patients/${r}`).then(e=>{u({sourcePatientId:e.sourcePatientId||"",name:e.name||"",mobile:e.mobile||"",age:e.age??"",gender:e.gender||"",lungCondition:e.lungCondition||"",otherHealthCondition:e.otherHealthCondition||"",language:e.sessionLanguage||e.language||"",consentType:e.consentType||"",isNewOrOld:e.isNewOrOld||"",doctor:{sourceId:e.doctor?.sourceId||"",name:e.doctor?.name||"",uin:e.doctor?.uin||""},assignedEducator:e.assignedEducator?.id||""})}).catch(e=>m(e.message))},[r,d]);const i=(e,g)=>u(l=>({...l,[e]:g})),y=async e=>{if(e.preventDefault(),m(""),String(n.mobile||"").trim()&&!B(n.mobile)){m("Mobile must be 10 digits.");return}const g={...n,age:n.age===""?null:Number(n.age),assignedEducator:n.assignedEducator||null,sessionLanguage:n.language};try{if(d)await h(`/api/patients/${r}`,{method:"PATCH",body:g}),j(`/patients/${r}`);else{const l=await h("/api/patients",{method:"POST",body:g});j(`/patients/${l.id}`)}}catch(l){m(l.message)}};return s.jsxs(I,{fluid:!0,children:[s.jsx(L,{title:d?"Edit master patient":"Add master patient",subtitle:"Master Patients"}),s.jsx(D,{children:s.jsxs(M,{children:[p?s.jsx("div",{className:"alert alert-danger",children:p}):null,s.jsxs("form",{onSubmit:y,children:[s.jsxs(S,{className:"g-3",children:[s.jsxs(t,{md:4,children:[s.jsx(a,{children:"Patient ID"}),s.jsx(o,{value:n.sourcePatientId,onChange:e=>i("sourcePatientId",e.target.value),disabled:d,placeholder:"Optional — auto if blank"})]}),s.jsxs(t,{md:4,children:[s.jsx(a,{children:"Name"}),s.jsx(o,{value:n.name,onChange:e=>i("name",e.target.value),required:!0})]}),s.jsxs(t,{md:4,children:[s.jsx(a,{children:"Mobile"}),s.jsx(o,{inputMode:"numeric",maxLength:10,value:n.mobile,onChange:e=>i("mobile",H(e.target.value))})]}),s.jsxs(t,{md:3,children:[s.jsx(a,{children:"Age"}),s.jsx(o,{type:"number",value:n.age,onChange:e=>i("age",e.target.value)})]}),s.jsxs(t,{md:3,children:[s.jsx(a,{children:"Gender"}),s.jsx(C,{value:n.gender,onChange:e=>i("gender",e),options:v?.genders})]}),s.jsxs(t,{md:3,children:[s.jsx(a,{children:"Language"}),s.jsx(C,{value:n.language,onChange:e=>i("language",e),options:v?.languages})]}),s.jsxs(t,{md:3,children:[s.jsx(a,{children:"New / old"}),s.jsxs("select",{className:"form-select",value:n.isNewOrOld,onChange:e=>i("isNewOrOld",e.target.value),children:[s.jsx("option",{value:"",children:"Select"}),s.jsx("option",{children:"New"}),s.jsx("option",{children:"Old"})]})]}),s.jsxs(t,{md:6,children:[s.jsx(a,{children:"Lung condition"}),s.jsx(o,{value:n.lungCondition,onChange:e=>i("lungCondition",e.target.value)})]}),s.jsxs(t,{md:6,children:[s.jsx(a,{children:"Other health condition"}),s.jsx(o,{value:n.otherHealthCondition,onChange:e=>i("otherHealthCondition",e.target.value)})]}),s.jsxs(t,{md:4,children:[s.jsx(a,{children:"Consent type"}),s.jsx(o,{value:n.consentType,onChange:e=>i("consentType",e.target.value)})]}),s.jsxs(t,{md:4,children:[s.jsx(a,{children:"Doctor name"}),s.jsx(o,{value:n.doctor.name,onChange:e=>u({...n,doctor:{...n.doctor,name:e.target.value}})})]}),s.jsxs(t,{md:4,children:[s.jsx(a,{children:"Doctor UIN"}),s.jsx(o,{value:n.doctor.uin,onChange:e=>u({...n,doctor:{...n.doctor,uin:e.target.value}})})]}),x?s.jsxs(t,{md:6,children:[s.jsx(a,{children:"Assigned PRT"}),s.jsxs("select",{className:"form-select",value:n.assignedEducator,onChange:e=>i("assignedEducator",e.target.value),children:[s.jsx("option",{value:"",children:"Unassigned"}),N.map(e=>s.jsxs("option",{value:e.id,children:[e.firstName," ",e.lastName]},e.id))]})]}):null]}),s.jsxs("div",{className:"d-flex gap-2 mt-4",children:[s.jsx(T,{type:"submit",children:"Save"}),s.jsx(F,{className:"btn btn-light",to:d?`/patients/${r}`:"/patients",children:"Cancel"})]})]})]})})]})}export{W as default};