import{b as A,r as a,d as p,j as s,m as r,T as L,o as U,B as C,p as B,C as H,R as F,l as q,q as z,s as G}from"./index-B92aEPQH.js";import{c as O,u as Q,D as W,g as J,a as K}from"./DataTable-bL18i-bk.js";import{P as V}from"./PageBreadcrumb-Dsn9CkpH.js";import{D as X}from"./DeleteConfirmationModal-D9TSHum9.js";import{T as Y}from"./TablePagination-CZupPUhN.js";import{C as Z,b as _,c as ee}from"./Card-DPmHd5BI.js";import"./Table-BCHyhTAY.js";import"./PageMetaData-DFmRpQLs.js";import"./Modal-D4T7V4-p.js";function de(){const{can:l}=A(),[S,v]=a.useState([]),[o,P]=a.useState(0),[t,n]=a.useState(0),[c]=a.useState(10),[g,w]=a.useState(""),[x,k]=a.useState(""),[d,I]=a.useState(""),[m,T]=a.useState(""),[u,y]=a.useState(!1),[f,E]=a.useState({genders:[],lungConditions:[]}),[b,h]=a.useState(null),i=O(),j=async()=>{const e=new URLSearchParams({search:x,page:String(t+1),limit:String(c)});d&&e.set("gender",d),m&&e.set("lungCondition",m),u&&e.set("unassigned","true");const N=await p(`/api/patients?${e}`);v(N.items),P(N.total)};a.useEffect(()=>{p("/api/patients/filters").then(E).catch(()=>{})},[]),a.useEffect(()=>{j().catch(()=>{})},[t,x,d,m,u]);const M=a.useMemo(()=>[i.accessor("sourcePatientId",{header:"Patient ID"}),i.accessor("name",{header:"Name",cell:({row:e})=>s.jsx(r,{to:`/patients/${e.original.id}`,className:"link-reset fw-semibold",children:e.original.name})}),i.accessor("mobile",{header:"Mobile"}),i.accessor("age",{header:"Age"}),i.accessor("gender",{header:"Gender"}),i.accessor("lungCondition",{header:"Lung condition"}),i.accessor(e=>e.assignedEducator?`${e.assignedEducator.firstName} ${e.assignedEducator.lastName}`:"Unassigned",{id:"educator",header:"PRT"}),{header:"Actions",cell:({row:e})=>s.jsxs("div",{className:"d-flex gap-1",children:[l("onboarding","add")&&e.original.sourcePatientId?s.jsx(r,{className:"btn btn-success btn-sm",to:`/onboarding/new?patientId=${encodeURIComponent(e.original.sourcePatientId)}`,children:"Onboard"}):null,s.jsx(r,{className:"btn btn-default btn-icon btn-sm",to:`/patients/${e.original.id}`,children:s.jsx(L,{className:"fs-lg"})}),l("patients","edit")?s.jsx(r,{className:"btn btn-default btn-icon btn-sm",to:`/patients/${e.original.id}/edit`,children:s.jsx(U,{className:"fs-lg"})}):null,l("patients","delete")?s.jsx(C,{variant:"default",size:"sm",className:"btn-icon",onClick:()=>h(e.original.id),children:s.jsx(B,{className:"fs-lg"})}):null]})}],[l]),R=Q({data:S,columns:M,getCoreRowModel:K(),getSortedRowModel:J()}),D=o===0?0:t*c+1,$=Math.min((t+1)*c,o);return s.jsxs(H,{fluid:!0,children:[s.jsx(V,{title:"Master Patients"}),s.jsx(F,{children:s.jsx(q,{children:s.jsxs(Z,{children:[s.jsx(_,{className:"border-light",children:s.jsxs("div",{className:"d-flex flex-nowrap gap-2 align-items-center w-100",children:[s.jsxs("form",{className:"d-flex flex-nowrap gap-2 align-items-center flex-grow-1 min-w-0",onSubmit:e=>{e.preventDefault(),n(0),k(g)},children:[s.jsxs("div",{className:"app-search flex-shrink-0",style:{width:220},children:[s.jsx("input",{className:"form-control",placeholder:"Search name, mobile, ID...",value:g,onChange:e=>w(e.target.value)}),s.jsx(z,{className:"app-search-icon text-muted"})]}),s.jsxs("select",{className:"form-select flex-shrink-0",style:{width:130},value:d,onChange:e=>{n(0),I(e.target.value)},children:[s.jsx("option",{value:"",children:"All genders"}),f.genders.map(e=>s.jsx("option",{value:e,children:e},e))]}),s.jsxs("select",{className:"form-select flex-shrink-0",style:{width:150,maxWidth:150},value:m,onChange:e=>{n(0),T(e.target.value)},children:[s.jsx("option",{value:"",children:"All conditions"}),f.lungConditions.map(e=>s.jsx("option",{value:e,children:e},e))]}),s.jsxs("div",{className:"form-check mb-0 text-nowrap flex-shrink-0",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:"unassigned",checked:u,onChange:e=>{n(0),y(e.target.checked)}}),s.jsx("label",{className:"form-check-label",htmlFor:"unassigned",children:"Unassigned"})]}),s.jsx(C,{type:"submit",variant:"light",className:"flex-shrink-0",children:"Search"})]}),s.jsxs("div",{className:"d-flex gap-2 flex-shrink-0 ms-auto",children:[l("patients","import")?s.jsx(r,{className:"btn btn-outline-primary",to:"/patients/import",children:"Import"}):null,l("patients","add")?s.jsxs(r,{className:"btn btn-success",to:"/patients/new",children:[s.jsx(G,{className:"me-1"})," Add master patient"]}):null]})]})}),s.jsx(W,{table:R,emptyMessage:"No master patients found"}),o>0&&s.jsx(ee,{className:"border-0",children:s.jsx(Y,{totalItems:o,start:D,end:$,itemsName:"master patients",showInfo:!0,previousPage:()=>n(e=>Math.max(0,e-1)),canPreviousPage:t>0,pageCount:Math.ceil(o/c),pageIndex:t,setPageIndex:n,nextPage:()=>n(e=>e+1),canNextPage:(t+1)*ch(null),itemName:"master patient",selectedCount:1,onConfirm:async()=>{await p(`/api/patients/${b}`,{method:"DELETE"}),h(null),await j()}})]})}export{de as default};