import{c as P,r as n,j as e,L as G,B as j,l as Q,z as X,A as Y,T as v,S as J,d as Z,E as ee,F as se}from"./index-TNj5auBr.js";import{P as ae,B as te}from"./PageHeader-B9Ytwo9b.js";import{E as ie}from"./EmptyState-DGz1kDMS.js";import{T as ne}from"./TabLabelCount-NU3aDS1r.js";import{t as B,b as re}from"./format-BZvJYrzU.js";import{D as le,L as oe,F as T,a as de,P as ce}from"./plus-CZCr3OEY.js";import{B as F}from"./Banner-DVJsik2E.js";import{T as g}from"./TextInput-DL-5979h.js";import{S as pe}from"./Selector-D-0RMSQ0.js";import{a as R,p as N,T as xe}from"./Table-SizHLk8D.js";import{M as he}from"./mail-DG37riix.js";import{P as me,A as ue}from"./phone-BqZPAT1U.js";import{I}from"./IconButton-Dlo2bZzs.js";import{S as fe}from"./square-pen-BItzjjNj.js";import{T as ye}from"./trash-2-Mcf_1vVz.js";import{C as ge}from"./Card-Cvh0wiAW.js";import{C as je}from"./contact-CqGb35TO.js";import"./HStack-tSPMb9QU.js";/** * @license lucide-react v1.22.0 - ISC * * This source code is licensed under the ISC license. * See the LICENSE file in the root directory of this source tree. */const ve=[["path",{d:"M3 14h3a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-7a9 9 0 0 1 18 0v7a2 2 0 0 1-2 2h-1a2 2 0 0 1-2-2v-3a2 2 0 0 1 2-2h3",key:"1xhozi"}]],be=P("headphones",ve);/** * @license lucide-react v1.22.0 - ISC * * This source code is licensed under the ISC license. * See the LICENSE file in the root directory of this source tree. */const Ce=[["path",{d:"M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z",key:"oel41y"}],["path",{d:"M12 8v4",key:"1got3b"}],["path",{d:"M12 16h.01",key:"1drbdi"}]],Se=P("shield-alert",Ce);/** * @license lucide-react v1.22.0 - ISC * * This source code is licensed under the ISC license. * See the LICENSE file in the root directory of this source tree. */const we=[["path",{d:"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2",key:"975kel"}],["circle",{cx:"12",cy:"7",r:"4",key:"17ys0d"}]],ke=P("user",we),Te=["admin","rep","manager"],_={name:"",email:"",phone:"",role:"rep",pin:"",password:""},ze=(a,l)=>l&&!a.includes(l)?[l,...a]:a,Ee=(a,l)=>ze(a,l).map(o=>({value:o,label:o.charAt(0).toUpperCase()+o.slice(1)}));function De({open:a,mode:l,initial:o,onClose:u,onSaved:b}){const x=l==="edit",[t,C]=n.useState(_),[h,S]=n.useState(!1),[p,d]=n.useState(null),r=c=>m=>C(f=>({...f,[c]:m}));n.useEffect(()=>{a&&(C({..._,...o||{}}),d(null))},[a,o]);const z=async()=>{if(!t.name.trim()){d("Name is required.");return}if(!t.email.trim()){d("Email is required.");return}if(!x&&!t.password.trim()){d("Password is required.");return}S(!0),d(null);const c={first_name:t.name.trim(),email:t.email.trim(),phone:t.phone,role:t.role,...t.password?{password:t.password}:{},...t.pin?{pin:String(t.pin)}:{}};try{x?await X(o.id,c):await Y(c),b()}catch(m){d(m.message||"Failed to save user")}finally{S(!1)}};return e.jsx(le,{isOpen:a,onOpenChange:c=>{c||u()},width:600,purpose:"form",children:e.jsx(G,{header:e.jsx(de,{title:x?"Edit Team User":"Add Team User",onOpenChange:c=>{c||u()}}),content:e.jsx(Q,{children:e.jsxs(T,{children:[p&&e.jsx(F,{status:"error",title:p}),e.jsxs(T,{direction:"horizontal",children:[e.jsx(g,{label:"Name",isRequired:!0,value:t.name,onChange:r("name")}),e.jsx(g,{label:"Email",type:"email",isRequired:!0,value:t.email,onChange:r("email")})]}),e.jsxs(T,{direction:"horizontal",children:[e.jsx(g,{label:"Phone",value:t.phone,onChange:r("phone")}),e.jsx(pe,{label:"Role",options:Ee(Te,t.role),value:t.role,onChange:r("role")})]}),e.jsxs(T,{direction:"horizontal",children:[e.jsx(g,{label:"PIN",value:t.pin,onChange:r("pin")}),e.jsx(g,{label:x?"New Password (optional)":"Password",isRequired:!x,type:"password",value:t.password,onChange:r("password")})]})]})}),footer:e.jsx(oe,{hasDivider:!0,children:e.jsxs("div",{style:{display:"flex",justifyContent:"flex-end",gap:"12px"},children:[e.jsx(j,{label:"Cancel",variant:"ghost",onClick:u,isDisabled:h}),e.jsx(j,{label:x?"Save Changes":"Create User",variant:"primary",onClick:z,isDisabled:h,isLoading:h})]})})})})}const Le={admin:{color:"primary",icon:Se},manager:{color:"success",icon:se},rep:{color:"info",icon:je},support:{color:"warning",icon:be}},Me=a=>Le[String(a||"").toLowerCase()]||{color:"secondary",icon:ke};function Re({name:a,email:l}){const o=l?l.split("@")[0]:"";return e.jsxs("div",{style:{minWidth:0},children:[e.jsx("div",{style:{fontWeight:600,color:"#1e293b",fontSize:"0.8rem",lineHeight:1.2},children:a}),o&&e.jsxs("div",{style:{fontSize:"0.68rem",color:"#64748b"},children:["@",o]})]})}const Ne={primary:"red",success:"success",info:"info",warning:"warning",secondary:"neutral"};function Pe({role:a}){const{color:l}=Me(a);return e.jsx(te,{variant:Ne[l]||"neutral",label:B(a)})}function Ze(){var A;const[a,l]=n.useState([]),[o,u]=n.useState(!0),[b,x]=n.useState(null),[t,C]=n.useState(0),[h,S]=n.useState(""),[p,d]=n.useState(0),[r,z]=n.useState(10),[c,m]=n.useState({open:!1,mode:"add",initial:null}),[f,E]=n.useState(null),[O,U]=n.useState(!1),w=n.useRef(0),k=()=>{const s=++w.current;u(!0),x(null),Z().then(i=>{w.current===s&&l(i.map(re))}).catch(i=>{w.current===s&&x(i.message||"Failed to load team users")}).finally(()=>{w.current===s&&u(!1)})};n.useEffect(()=>{k()},[]);const D=n.useMemo(()=>{const s=[];return a.forEach(i=>{s.includes(i.role)||s.push(i.role)}),[{key:"all",label:"All"},...s.map(i=>({key:i,label:B(i)}))]},[a]),L=((A=D[Math.min(t,D.length-1)])==null?void 0:A.key)||"all",q=n.useMemo(()=>{const s={all:a.length};return a.forEach(i=>{s[i.role]=(s[i.role]||0)+1}),s},[a]),y=n.useMemo(()=>a.filter(s=>{const i=L==="all"||s.role===L,K=!h||[s.name,s.email,s.phone,s.role].join(" ").toLowerCase().includes(h.toLowerCase());return i&&K}),[a,L,h]),M=y.slice(p*r,p*r+r),$=n.useMemo(()=>M.map((s,i)=>({...s,_rowNumber:p*r+i+1})),[M,p,r]),H=n.useMemo(()=>[{key:"_rowNumber",header:e.jsx("div",{style:{paddingLeft:"24px",paddingTop:"12px"},children:"S.No"}),width:R(72),renderCell:s=>e.jsx("div",{style:{paddingLeft:"24px"},children:e.jsx(v,{type:"supporting",color:"secondary",children:s._rowNumber})})},{key:"name",header:e.jsx("div",{style:{padding:"12px 16px 0"},children:"User"}),width:N(2),renderCell:s=>e.jsx("div",{style:{padding:"10px 16px"},children:e.jsx(Re,{name:s.name,email:s.email})})},{key:"email",header:e.jsx("div",{style:{padding:"12px 16px 0"},children:"Email"}),width:N(2),renderCell:s=>e.jsx("div",{style:{padding:"10px 16px"},children:s.email?e.jsxs("a",{href:`mailto:${s.email}`,style:{display:"inline-flex",alignItems:"center",gap:"6px",textDecoration:"none"},className:"link-hover",children:[e.jsx(he,{size:14,style:{color:"#94a3b8"}}),e.jsx(v,{type:"body",size:"sm",children:s.email})]}):e.jsx(v,{type:"supporting",color:"disabled",children:"—"})})},{key:"phone",header:e.jsx("div",{style:{padding:"12px 16px 0"},children:"Phone"}),width:N(1),renderCell:s=>e.jsx("div",{style:{padding:"10px 16px"},children:s.phone?e.jsxs("a",{href:`tel:${s.phone}`,style:{display:"inline-flex",alignItems:"center",gap:"6px",textDecoration:"none"},className:"link-hover",children:[e.jsx(me,{size:14,style:{color:"#94a3b8"}}),e.jsx(v,{type:"body",size:"sm",children:s.phone})]}):e.jsx(v,{type:"supporting",color:"disabled",children:"—"})})},{key:"role",header:e.jsx("div",{style:{padding:"12px 16px 0"},children:"Role"}),width:R(140),renderCell:s=>e.jsx("div",{style:{padding:"10px 16px"},children:e.jsx(Pe,{role:s.role})})},{key:"actions",header:e.jsx("div",{style:{paddingRight:"24px",paddingTop:"12px"},children:"Actions"}),width:R(120),align:"end",renderCell:s=>e.jsxs("div",{style:{display:"inline-flex",gap:"8px",paddingRight:"24px"},children:[e.jsx(I,{label:`Edit ${s.name}`,tooltip:"Edit user",variant:"ghost",size:"sm",icon:e.jsx(fe,{size:16}),onClick:()=>m({open:!0,mode:"edit",initial:s})}),e.jsx(I,{label:`Delete ${s.name}`,tooltip:"Delete user",variant:"ghost",size:"sm",icon:e.jsx(ye,{size:16,style:{color:"#dc2626"}}),onClick:()=>E(s)})]})}],[]),W=()=>{m({open:!1,mode:"add",initial:null}),k()},V=async()=>{U(!0);try{await ee(f.id),E(null),k()}catch(s){x(s.message||"Failed to delete user")}finally{U(!1)}};return e.jsxs("div",{style:{fontFamily:"system-ui, sans-serif"},children:[e.jsx(ae,{title:"Team Users",action:e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"12px",flexWrap:"nowrap"},children:[e.jsx("div",{style:{flex:"0 1 320px",minWidth:"200px"},children:e.jsx(g,{label:"Search team users",isLabelHidden:!0,placeholder:"Search team users…",startIcon:e.jsx(J,{size:16}),hasClear:!0,value:h,onChange:s=>{S(s),d(0)}})}),e.jsx(j,{variant:"primary",icon:e.jsx(ce,{size:14}),onClick:()=>m({open:!0,mode:"add",initial:null}),label:"Add User",style:{flexShrink:0}})]})}),e.jsxs(ge,{style:{padding:"0",borderRadius:"16px",border:"1px solid rgba(5, 54, 89, 0.08)",backgroundColor:"#ffffff",overflow:"hidden",boxShadow:"0 10px 30px -5px rgba(0,0,0,0.02), 0 4px 12px -3px rgba(0,0,0,0.02)"},children:[e.jsx("div",{style:{display:"flex",padding:"0 24px",borderBottom:"1px solid rgba(5, 54, 89, 0.06)",overflowX:"auto",gap:"24px"},children:D.map((s,i)=>e.jsx("div",{onClick:()=>{C(i),d(0)},style:{padding:"16px 0",cursor:"pointer",borderBottom:t===i?"2px solid #C01227":"2px solid transparent",color:t===i?"#C01227":"#64748b",fontWeight:t===i?600:500,fontSize:"0.875rem",transition:"all 0.2s"},children:e.jsx(ne,{label:s.label,count:q[s.key]||0,active:t===i})},s.key))}),b&&e.jsx("div",{style:{padding:"20px 24px 0"},children:e.jsx(F,{status:"error",title:b,endContent:e.jsx(j,{label:"Retry",variant:"ghost",size:"sm",onClick:k})})}),o?e.jsx("div",{style:{display:"flex",justifyContent:"center",padding:"48px 0"},children:e.jsx("div",{className:"spinner",style:{width:"32px",height:"32px",border:"3px solid #f1f5f9",borderTop:"3px solid #C01227",borderRadius:"50%",animation:"spin 1s linear infinite"}})}):M.length===0?e.jsx(ie,{title:"No team users found",caption:"Try a different role or search term, or add a user."}):e.jsx("div",{style:{paddingTop:"8px",paddingBottom:"8px"},children:e.jsx(xe,{data:$,columns:H,idKey:"id",density:"compact",dividers:"rows",hasHover:!0,isStriped:!0,textOverflow:"truncate"})}),e.jsxs("div",{style:{display:"flex",justifyContent:"flex-end",alignItems:"center",padding:"12px 24px",borderTop:"1px solid #e2e8f0",gap:"20px",flexWrap:"wrap"},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",fontSize:"0.875rem",color:"#475569"},children:[e.jsx("span",{children:"Rows per page:"}),e.jsx("select",{value:r,onChange:s=>{z(Number(s.target.value)),d(0)},className:"rpp-select",children:[5,10,25].map(s=>e.jsx("option",{value:s,children:s},s))})]}),e.jsxs("span",{style:{fontSize:"0.875rem",color:"#475569"},children:[y.length===0?"0-0":`${p*r+1}-${Math.min((p+1)*r,y.length)}`," of ",y.length]}),e.jsxs("div",{style:{display:"flex",gap:"8px"},children:[e.jsx(j,{label:"Prev",variant:"secondary",size:"sm",onClick:()=>d(s=>Math.max(0,s-1)),isDisabled:p===0}),e.jsx(j,{label:"Next",variant:"secondary",size:"sm",onClick:()=>d(s=>Math.min(Math.ceil(y.length/r)-1,s+1)),isDisabled:p>=Math.ceil(y.length/r)-1})]})]})]}),e.jsx(De,{open:c.open,mode:c.mode,initial:c.initial,onClose:()=>m({open:!1,mode:"add",initial:null}),onSaved:W}),e.jsx(ue,{isOpen:!!f,onOpenChange:s=>{s||E(null)},title:"Delete user?",description:f?`This will permanently remove ${f.name} from the PostgreSQL database. This cannot be undone.`:"",actionLabel:"Delete",onAction:V,isActionLoading:O}),e.jsx("style",{children:` .link-hover:hover { text-decoration: underline; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } `})]})}export{Ze as default};