import{aP as N,l as q,R as n,bV as c,o as h,es as k,aL as d,bT as i,j as e,E as B,cn as Y,bE as D,dH as H,a9 as g,aQ as M}from"./app-BgLa5dit.js";import{S as V}from"./BSa28HTr.chunk.js";import{C as x}from"./q-IBKcuF.chunk.js";import"./EZim8aq-.chunk.js";import"./DUpYPoLs.chunk.js";import"./BD9MrUJP.chunk.js";import"./s6cGcwlA.chunk.js";import"./Zwjmkasg.chunk.js";import"./4qGb1BIY.chunk.js";import"./DnXDEnAR.chunk.js";import"./DpUpv6rY.chunk.js";import"./CG7zbly5.chunk.js";import"./m4aqaK_l.chunk.js";import"./MEkA24R1.chunk.js";import"./C53kfXmd.chunk.js";function oe(t){const y=N(),S=q(),[a,U]=n.useState(null),[f,C]=n.useState(null),[b,v]=n.useState(!1),[j,R]=n.useState(""),[u,T]=n.useState(c.User),l=h(x.getUser(t.customerId,t.userId)),G=h(l.hasError&&k.user(t.userId)),r=l.hasError?G:l,m=h(a&&d.getUserType(r.data)===i.Learner&&x.yearGroups(a.id));function E(s){U(s)}function I(){if(d.getUserType(r.data)===i.Learner&&!f){R("Must select a year level for students");return}v(!0);const s={userId:r.data.id,masterRole:u};d.getUserType(r.data)===i.Learner&&(s.userYearGroupId=f.id);const o={addUserRequests:[s]};M(y,x.addUserToCustomer(a.id,o,A,P))}function A(){S.success("Successfully added user"),t.closePopup()}function P(){t.closePopup(),S.error("Could not add user. This may be due to Online being enabled for the selected customer.")}function F(s){const o=s.currentTarget.value,L=m.data.find(w=>w.id.toString()===o.toString());C(L)}const p=!!r.data;return e.jsxs(B,{title:"Add user to customer",closePopup:t.closePopup,showLoadingSpinner:!p,children:[p&&!a&&e.jsxs(e.Fragment,{children:[e.jsx("label",{children:"Select a customer:"}),e.jsx(Y,{placeholder:"Search for a customer",onSuggestionSelected:E,getRemoteUrl:s=>V.searchCustomer(s),activationLength:2,getSuggestionComponent:s=>e.jsxs("div",{children:[s.name," (",s.crmId,")"]}),formatRemoteSuggestions:s=>s.data,autoFocusInput:!0,debounceTime:200,getEmptyStateComponent:()=>e.jsx("div",{children:"No customers found."})})]}),p&&a&&e.jsxs(e.Fragment,{children:[e.jsxs("p",{children:["Do you want to add ",e.jsxs("strong",{children:[r.data.name," ",r.data.surname]}),"  (",r.data.username,") to customer ",e.jsx("strong",{children:a.name})," (",a.crmId,")?"]}),d.getUserType(r.data)===i.Staff&&e.jsxs(e.Fragment,{children:[e.jsx("label",{children:"User role: "}),e.jsxs("div",{className:"d-flex align-items-center mb-2",children:[e.jsx(D,{onClick:()=>T(u===c.Admin?c.User:c.Admin),on:u===c.Admin}),e.jsx("span",{className:"fw-semibold ms-2",children:"Administrator"})]})]}),m.data&&e.jsxs("div",{className:"my-2 form-group",children:[e.jsx("p",{children:"In this year level:"}),e.jsxs("select",{name:"yearGroup","aria-errormessage":"yearGroupError",className:`form-control ${j?"is-invalid":""}`,onChange:F,children:[e.jsx("option",{className:"text-secondary",value:"",children:"Select a year level"}),m.data.map((s,o)=>e.jsx("option",{value:s.id,children:s.name},o))]}),e.jsx(H,{name:"yearGroup",children:j})]}),e.jsxs(g,{children:[e.jsx(g.Cancel,{onClick:t.closePopup}),e.jsx(g.Submit,{variant:"danger",onClick:I,text:"Add",showSpinner:b})]})]})]})}export{oe as AddUserToCustomerView};
//# sourceMappingURL=DQh-kRPO.chunk.js.map
