1import{a2 as J,a3 as Q,a4 as V,e as W,r as n,s as o,z as X,j as a,C as g,t as l}from"./index-pTF10v3b.js";import{B as c}from"./button-BdNloxPj.js";import{I}from"./input-Bpai2mtc.js";import{L as z}from"./label-B_Ipra5m.js";import{B as E}from"./badge-BC0DLmfc.js";import{u as Y}from"./useServerFn-BBwoKgJP.js";import{U as Z}from"./users-BKgaEEbK.js";import{P as ee}from"./plus-DhJ_V-oW.js";import{C as ae}from"./circle-check-BZ19kOEN.js";import{C as se}from"./copy-C6QbhjYd.js";import{L as ie}from"./loader-circle-DjtKoGrj.js";import{M as te}from"./mail-1qLww3hn.js";import{R as ne}from"./refresh-cw-cy4onpR7.js";import{c as re}from"./createLucideIcon-BFnnMaHU.js";import{T as oe}from"./trash-2-DFpJEYvo.js";import"./index-DexvSvjY.js";import"./index-C8y9Pifk.js";import"./index-wAw2rTQC.js";const le=[["path",{d:"M12 2v10",key:"mnfbl"}],["path",{d:"M18.4 6.6a9 9 0 1 1-12.77.04",key:"obofu9"}]],ce=re("power",le),me=J({method:"POST"}).middleware([Q]).handler(V("c2207b1794151b6bd958680924373c5c35b9efc6238ccd7d5bc042ca1dea6178"));function ze(){const{user:d,ready:v}=W(),L=Y(me),[r,R]=n.useState(null),[D,q]=n.useState(""),[y,A]=n.useState([]),[U,j]=n.useState({}),[P,w]=n.useState({}),[u,N]=n.useState(""),[p,_]=n.useState(""),[T,b]=n.useState(!1),[C,k]=n.useState(null);n.useEffect(()=>{if(!v||!d)return;let e=!1;return(async()=>{const{data:s}=await o.from("companies").select("id, name").eq("owner_id",d.id).limit(1);if(e)return;const i=s?.[0]?.id??null;R(i),q(s?.[0]?.name??""),i&&await m(i)})(),()=>{e=!0}},[v,d]);async function m(e){const{data:s}=await o.from("employees").select("*").eq("company_id",e).order("invited_at",{ascending:!1});A(s??[]);const i=(s??[]).map(t=>t.id);if(i.length===0){j({}),w({});return}const{data:f}=await o.from("training_progress").select("employee_id, status").in("employee_id",i),x={};for(const t of f??[])t.status==="completed"&&t.employee_id&&(x[t.employee_id]=(x[t.employee_id]??0)+1);j(x);const{data:H}=await o.from("training_certifications").select("employee_id, passed").in("employee_id",i),h={};for(const t of H??[])t.passed&&t.employee_id&&(h[t.employee_id]=(h[t.employee_id]??0)+1);w(h)}async function $(e){if(e.preventDefault(),!r||!u.trim()||!p.trim())return;b(!0);const{error:s}=await o.from("employees").insert({company_id:r,full_name:u.trim(),email:p.trim()});if(b(!1),s){l.error(s.message);return}N(""),_(""),l.success("Dipendente aggiunto"),await m(r)}async function F(e){if(!r)return;const{error:s}=await o.from("employees").update({access_token:crypto.randomUUID()}).eq("id",e.id);if(s)return l.error(s.message);l.success("Link rigenerato (il vecchio non funziona più)"),await m(r)}async function M(e){r&&(await o.from("employees").update({active:!e.active}).eq("id",e.id),await m(r))}async function B(e){r&&confirm(`Rimuovere ${e.full_name}? I suoi progressi verranno cancellati.`)&&(await o.from("employees").delete().eq("id",e.id),await m(r))}function S(e){return`${window.location.origin}/formazione/d/${e.access_token}`}async function O(e){await navigator.clipboard.writeText(S(e)),l.success("Link copiato")}async function K(e){k(e.id);try{await L({data:{recipientEmail:e.email,idempotencyKey:`training-invite-${e.id}-${e.access_token}`,templateData:{fullName:e.full_name,companyName:D||void 0,courseUrl:S(e)}}}),l.success(`Email inviata a ${e.email}`)}catch(s){l.error(s?.message||"Invio email fallito")}finally{k(null)}}const G=X.reduce((e,s)=>e+s.chapters.reduce((i,f)=>i+f.lessons.length,0),0);return a.jsxs("div",{className:"p-6 md:p-10 max-w-5xl mx-auto",children:[a.jsxs("header",{className:"mb-8",children:[a.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[a.jsx("div",{className:"w-10 h-10 rounded-lg bg-primary/10 grid place-items-center",children:a.jsx(Z,{className:"w-5 h-5 text-primary"})}),a.jsx("h1",{className:"text-3xl font-semibold tracking-tight",children:"Dipendenti in formazione"})]}),a.jsx("p",{className:"text-muted-foreground max-w-2xl",children:"Aggiungi i dipendenti della tua azienda e invia loro il link personale. Non serve password: il link è univoco e revocabile in qualsiasi momento."})]}),a.jsx(g,{className:"p-5 mb-8",children:a.jsxs("form",{onSubmit:$,className:"grid md:grid-cols-[1fr_1fr_auto] gap-3 items-end",children:[a.jsxs("div",{children:[a.jsx(z,{htmlFor:"emp-name",children:"Nome e cognome"}),a.jsx(I,{id:"emp-name",value:u,onChange:e=>N(e.target.value),placeholder:"Mario Rossi"})]}),a.jsxs("div",{children:[a.jsx(z,{htmlFor:"emp-email",children:"Email"}),a.jsx(I,{id:"emp-email",type:"email",value:p,onChange:e=>_(e.target.value),placeholder:"[email protected]"})]}),a.jsxs(c,{type:"submit",disabled:T,children:[a.jsx(ee,{className:"w-4 h-4"}
1)," Aggiungi"]})]})}),a.jsxs("div",{className:"space-y-3",children:[y.length===0&&a.jsx(g,{className:"p-8 text-center text-muted-foreground",children:"Nessun dipendente ancora. Aggiungine uno qui sopra per iniziare."}),y.map(e=>{const s=U[e.id]??0,i=P[e.id]??0;return a.jsx(g,{className:"p-5",children:a.jsxs("div",{className:"flex items-start justify-between gap-4 flex-wrap",children:[a.jsxs("div",{className:"min-w-0 flex-1",children:[a.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[a.jsx("h3",{className:"font-semibold text-lg",children:e.full_name}),e.active?a.jsx(E,{variant:"secondary",children:"Attivo"}):a.jsx(E,{variant:"outline",className:"text-muted-foreground",children:"Disattivato"})]}),a.jsx("p",{className:"text-sm text-muted-foreground truncate",children:e.email}),a.jsxs("div",{className:"flex flex-wrap gap-4 mt-2 text-xs text-muted-foreground",children:[a.jsxs("span",{children:[s,"/",G," lezioni"]}),a.jsxs("span",{className:"inline-flex items-center gap-1",children:[a.jsx(ae,{className:"w-3 h-3 text-emerald-500"})," ",i," certificati"]}),e.last_seen_at&&a.jsxs("span",{children:["Ultima attività : ",new Date(e.last_seen_at).toLocaleDateString("it-IT")]})]})]}),a.jsxs("div",{className:"flex flex-wrap gap-2",children:[a.jsxs(c,{size:"sm",variant:"outline",onClick:()=>O(e),children:[a.jsx(se,{className:"w-4 h-4"})," Copia link"]}),a.jsxs(c,{size:"sm",variant:"outline",onClick:()=>K(e),disabled:C===e.id||!e.active,children:[C===e.id?a.jsx(ie,{className:"w-4 h-4 animate-spin"}):a.jsx(te,{className:"w-4 h-4"})," Invia per email"]}),a.jsx(c,{size:"sm",variant:"ghost",onClick:()=>F(e),title:"Rigenera link (revoca il precedente)",children:a.jsx(ne,{className:"w-4 h-4"})}),a.jsx(c,{size:"sm",variant:"ghost",onClick:()=>M(e),title:e.active?"Disattiva":"Riattiva",children:a.jsx(ce,{className:"w-4 h-4"})}),a.jsx(c,{size:"sm",variant:"ghost",onClick:()=>B(e),title:"Rimuovi",children:a.jsx(oe,{className:"w-4 h-4 text-destructive"})})]})]})},e.id)})]})]})}export{ze as component};
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.