1import{h as Y,r,j as e,D as Z,p as U,q as ee,t as se,aB as de,L as C,I as oe,B as i,e as q,S as ce,J as me,K as he,M as ue,N as V,v as D,cG as $,aD as F,b as xe,aE as O,aF as L,_ as ge,k as ae,T as z,s as te,X as l,l as B,g as pe,a2 as je,A as ve,ac as _e,P as fe,C as W,a as X,c as ke,d as Ne,aY as J,ar as ye,aN as be,ba as Q}from"./index-CobRY8C4.js";const Ce={no_rule_defined:"Ingen regel definierad för denna roll",no_sobriety_anchor:"Ingen drogfrihetsstart kunde hittas",sobriety_too_short:"För kort drogfrihetstid",discharge_not_completed:"Behandlingen avslutades inte med 'completed'",not_discharged:"Klient är inte utskriven",too_soon_after_discharge:"För kort tid sedan utskrivning",open_status_dispute:"Klientstatus är under bestridande"};function Se({open:T,onOpenChange:v,organizationId:_,initialUserId:u,initialName:f,onSuccess:t}){var P,I;Y();const[d,o]=r.useState(1),[x,g]=r.useState(u??""),[c,k]=r.useState(f??""),[m,S]=r.useState(""),[w,N]=r.useState(!1),[p,s]=r.useState("therapist"),[n,j]=r.useState(null),[y,b]=r.useState(!1),[h,E]=r.useState(""),[K,G]=r.useState(""),[M,H]=r.useState(!1),ne=()=>{o(1),j(null),E(""),G(""),S(""),u||(g(""),k(""))},R=a=>{a||ne(),v(a)},re=async()=>{if(!m.trim())return;N(!0);const{data:a,error:A}=await te.from("profiles").select("id, first_name, last_name, email").eq("email",m.trim().toLowerCase()).maybeSingle();if(N(!1),A||!a){l.error("Ingen användare hittades med den e-postadressen");return}g(a.id),k(`${a.first_name??""} ${a.last_name??""}`.trim()||a.email||a.id)},ie=async()=>{if(!x){l.error("Välj en person först");return}b(!0);const{data:a,error:A}=await B("check_role_eligibility",{p_user_id:x,p_target_role:p,p_target_org_id:_});if(b(!1),A){l.error(`Kunde inte kontrollera: ${A.message}`);return}j(a),o(2)},le=async()=>{if(h.trim().length<20){l.error("Motivering mÃ¥ste vara minst 20 tecken");return}H(!0);const{error:a}=await B("request_role_transition",{p_user_id:x,p_target_role:p,p_target_org_id:_,p_reason:h.trim(),p_supporting_notes:K.trim()||null});if(H(!1),a){l.error(`Misslyckades: ${a.message}`);return}l.success("Begäran skickad â en annan admin mÃ¥ste godkänna (4-ögonsprincip)."),t==null||t(),R(!1)};return e.jsx(Z,{open:T,onOpenChange:R,children:e.jsxs(U,{className:"max-w-xl",children:[e.jsxs(ee,{children:[e.jsx(se,{children:"Begär rolltransition"}),e.jsx(de,{children:"Begär att en före detta klient fÃ¥r en personalroll. Karenstider och regler kontrolleras automatiskt."})]}),d===1&&e.jsxs("div",{className:"space-y-4",children:[!u&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(C,{children:"Sök person via e-post"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(oe,{value:m,onChange:a=>S(a.target.value),placeholder:"[email protected]"}),e.jsx(i,{onClick:re,disabled:w||!m.trim(),children:w?e.jsx(q,{className:"h-4 w-4 animate-spin"}):"Sök"})]}),c&&e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Vald: ",e.jsx("strong",{children:c})]})]}),u&&e.jsx("div",{className:"rounded-md border p-3",children:e.jsxs("p",{className:"text-sm",children:["Person: ",e.jsx("strong",{children:f??u})]})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(C,{children:"MÃ¥lroll"}),e.jsxs(ce,{value:p,onValueChange:a=>s(a),children:[e.jsx(me,{children:e.jsx(he,{})}),e.jsxs(ue,{children:[e.jsx(V,{value:"therapist",children:"Terapeut"}),e.jsx(V,{value:"company_admin",children:"Företagsadmin"})]})]})]}),e.jsxs(D,{children:[e.jsx(i,{variant:"ghost",onClick:()=>R(!1),children:"Avbryt"}),e.jsxs(i,{onClick:ie,disabled:y||!x,children:[y&&e.jsx(q,{className:"mr-2 h-4 w-4 animate-spin"}),"Kontrollera ",e.jsx($,{className:"ml-2 h-4 w-4"})]})]})]}),d===2&&n&&e.jsxs("div",{className:"space-y-4",children:[n.eligible?e.jsxs(F,{children:[e.jsx(xe,{className:"h-4 w-4 text-emerald-600"}),e.jsx(O,{children:"Personen uppfyller karenstidskraven"}),e.jsxs(L,{children:["Krav: ",(P=n.rule_used)==null?void 0:P.min_sober_years," Ã¥rs drogfrihet,"," ",(I=n.rule_used)==null?void 0:I.min_months_since_discharge," mÃ¥nader efter utskrivning."]})]}):e.jsxs(F,{variant:"destructive",children:[e.jsx(ge,{className:"h-4 w-4"}),e.jsx(O,{children:"Personen uppfyller inte kraven"}),e.jsxs(L,{children:[e.jsx("ul",{className:"mt-2 list-disc pl-5",children:n.blockers.map(a=>e.jsx("li",{children:e.jsx(ae,{variant:"outline",children:Ce[a]??a})},a))}),n.earliest_eligible_at&&e.jsxs("p",{className:"mt-2 text-xs",children:["Tidigast eligible: ",new Date(n.earliest_eligible_at).toLocaleDateString("sv-SE")]})]})]}),e.jsxs(D,{children:[e.jsx(i,{variant:"ghost",onClick:()=>o(1),children:"Tillbaka"}),e.jsxs(i,{onClick:()=>o(3),disabled:!n.eligible,children:["Fortsätt ",e.jsx($,{className:"ml-2 h-4 w-4"})]})]})]}),d===3&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(C,{htmlFor:"motiv",children:"Motivering (minst 20 tecken)"}),e.jsx(z,{id:"motiv",value:h,onChange:a=>E(a.target.value),rows:4,placeholder:"Varför är denna person lämpl
1ig för rollen?"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[h.trim().length,"/20"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(C,{htmlFor:"notes",children:"Stödjande noteringar (valfritt)"}),e.jsx(z,{id:"notes",value:K,onChange:a=>G(a.target.value),rows:2,placeholder:"Genomförd intern utbildning, referenser m.m."})]}),e.jsx(F,{children:e.jsx(L,{className:"text-xs",children:"Begäran mÃ¥ste godkännas av en annan admin (4-ögonsprincip). Du som initierar kan inte själv godkänna."})}),e.jsxs(D,{children:[e.jsx(i,{variant:"ghost",onClick:()=>o(2),disabled:M,children:"Tillbaka"}),e.jsxs(i,{onClick:le,disabled:M||h.trim().length<20,children:[M&&e.jsx(q,{className:"mr-2 h-4 w-4 animate-spin"}),"Skicka begäran"]})]})]})]})})}const we={pending:{label:"Pending",variant:"secondary"},applied:{label:"Godkänd",variant:"default"},rejected:{label:"Avslagen",variant:"destructive"},cancelled:{label:"Avbruten",variant:"outline"}};function De(){var N,p;const T=pe(),{user:v,roles:_}=Y(),[u,f]=r.useState(!1),[t,d]=r.useState(null),[o,x]=r.useState(""),g=((N=_.find(s=>s.role==="company_admin"||s.role==="conglomerate_admin"))==null?void 0:N.organization_id)??((p=_.find(s=>s.role==="superuser"))==null?void 0:p.organization_id)??null,{data:c,isLoading:k,refetch:m}=je({queryKey:["role-transitions"],queryFn:async()=>{const{data:s,error:n}=await te.from("role_transition_requests").select("id, target_user_id, target_organization_id, status, reason, requested_by, reviewed_by, reviewed_at, applied_at, created_at, metadata").order("created_at",{ascending:!1}).limit(100);if(n)throw n;return s}}),S=async()=>{if(!t)return;if(t.mode==="reject"&&o.trim().length<5){l.error("Ange motivering till avslag");return}const{error:s}=await B("approve_role_transition",{p_request_id:t.id,p_decision:t.mode==="approve"?"approved":"rejected",p_decision_reason:o.trim()||null});if(s){l.error(s.message);return}l.success(t.mode==="approve"?"Godkänd â rollen tilldelad.":"Begäran avslagen."),d(null),x(""),m()},w=async s=>{const{error:n}=await B("cancel_role_transition",{p_request_id:s});if(n){l.error(n.message);return}l.success("Begäran avbruten"),m()};return e.jsxs("div",{className:"container max-w-5xl space-y-6 py-8",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs(i,{variant:"ghost",size:"sm",onClick:()=>T(-1),children:[e.jsx(ve,{className:"h-4 w-4 mr-1"})," Tillbaka"]}),e.jsxs("div",{children:[e.jsxs("h1",{className:"text-2xl font-bold flex items-center gap-2",children:[e.jsx(_e,{className:"h-6 w-6"})," Rolltransitions"]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Hantering av klient-till-personal-övergÃ¥ngar (4-ögonsprincip)"})]})]}),g&&e.jsxs(i,{onClick:()=>f(!0),children:[e.jsx(fe,{className:"h-4 w-4 mr-1"})," Ny begäran"]})]}),k&&e.jsx("p",{children:"Laddarâ¦"}),!k&&((c==null?void 0:c.length)??0)===0&&e.jsx(W,{children:e.jsx(X,{className:"py-12 text-center text-muted-foreground",children:"Inga rolltransitions ännu."})}),e.jsx("div",{className:"space-y-3",children:c==null?void 0:c.map(s=>{var b,h;const n=we[s.status]??{label:s.status,variant:"outline"},j=s.requested_by===(v==null?void 0:v.id),y=!j&&s.status==="pending";return e.jsxs(W,{children:[e.jsx(ke,{className:"pb-2",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(Ne,{className:"text-base",children:["MÃ¥lroll: ",e.jsx("span",{className:"font-mono",children:((b=s.metadata)==null?void 0:b.target_role)??"?"})]}),e.jsx(ae,{variant:n.variant,children:n.label})]})}),e.jsxs(X,{className:"space-y-2 text-sm",children:[e.jsxs("p",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Person:"})," ",e.jsx("span",{className:"font-mono text-xs",children:s.target_user_id})]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Motivering:"})," ",s.reason]}),((h=s.metadata)==null?void 0:h.supporting_notes)&&e.jsxs("p",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Noteringar:"})," ",s.metadata.supporting_notes]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Skapad ",J(new Date(s.created_at),"d MMM yyyy HH:mm",{locale:Q}),s.reviewed_at&&` · Beslut ${J(new Date(s.reviewed_at),"d MMM yyyy HH:mm",{locale:Q})}`]}),s.status==="pending"&&e.jsxs("div",{className:"flex gap-2 pt-2",children:[y&&e.jsxs(e.Fragment,{children:[e.jsxs(i,{size:"sm",onClick:()=>d({id:s.id,mode:"approve"}),children:[e.jsx(ye,{className:"h-4 w-4 mr-1"})," Godkänn"]}),e.jsxs(i,{size:"sm",variant:"outline",onClick:()=>d({id:s.id,mode:"reject"}),children:[e.jsx(be,{className:"h-4 w-4 mr-1"})," AvslÃ¥"]})]}),j&&e.jsx(i,{size:"sm",variant:"ghost",onClick:()=>w(s.id),children:"Avbryt"}),!y&&!j&&e.jsx("p",{className:"text-xs text-muted-foreground",children:"Väntar pÃ¥ beslut av annan admin."})]})]})]},s.id)})}),g&&e.jsx(Se,{open:u,onOpenChange:f,organizationId:g,onSuccess:m}),e.jsx(Z,{open:!!t,onOpenChange:s=>!s&&d(null),children:e.jsxs(U,{children:[e.jsx(ee,{children:e.jsx(se,{children:(t==null?void 0:t.mode)==="approve"?"Godkänn rolltransition":"AvslÃ¥ rolltransition"})}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(C,{htmlFor:"dec-reason",children:(t==null?void 0:t.mode)==="approve"?"Beslutsmotivering (valfritt)":"Motivering för avslag"}),e.jsx(z,{id:"dec-reason",value:o,onChange:s=>x(s.target.value),rows:3})]}),e.jsxs(D,{children:[e.jsx(i,{variant:"ghost",onClick:()=>d(null),children:"Avbryt"}),e.jsx(i,{variant:(t==null?void 0:t.mode)==="approve"?"default":"destructive",onClick:S,children:"Bekräfta"})]})]})})]})}export{De as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.