1import{c as Z,ac as ee,r as x,j as e,a1 as z,C as v,e as b,a_ as $,T as se,F as ae,b as te,d as le,B as M,bj as re,I as ne,al as O,be as g,o,by as ie,s as ce,bn as p}from"./index-CSZt44S3.js";import{u as de,g as y}from"./useAbandonedCarts-CTSrgy7Z.js";import{S as B,a as G,b as K,c as Q,d as u}from"./select-BsvOSc3O.js";import{T as oe,a as me,b as X,c as h,d as xe,e as j}from"./table-CLOWLIsS.js";import{D as ue,a as he,b as je,c as ge}from"./dialog-6oDXanGQ.js";import{g as pe}from"./admin-session-CN1mboOa.js";import{T as Ne}from"./triangle-alert-D00aK9Na.js";import{R as fe}from"./refresh-cw-r8zJjR9h.js";import{E as ve}from"./eye-mHpaLqZM.js";import{M as J}from"./mail-uAoZXq07.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const U=Z("UserX",[["path",{d:"M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2",key:"1yyitq"}],["circle",{cx:"9",cy:"7",r:"4",key:"nufk8"}],["line",{x1:"17",x2:"22",y1:"8",y2:"13",key:"3nzzx3"}],["line",{x1:"22",x2:"17",y1:"8",y2:"13",key:"1swrse"}]]),w=t=>new Intl.NumberFormat("it-IT",{style:"currency",currency:"EUR"}).format(t);function be(){const{toast:t}=ee(),{carts:_,loading:C,stats:c,refetch:N}=de(),[l,f]=x.useState(""),[k,W]=x.useState("all"),[S,Y]=x.useState("all"),[A,R]=x.useState(null),[q,V]=x.useState(!1),E=s=>!!s.external_user_id||!!s.user_id,L=async s=>{if(!s.customer_email){t({variant:"destructive",title:"Impossibile inviare",description:"Email non disponibile per questo utente."});return}V(!0);try{const a=await pe(),i=y(s),{error:n}=await ce.functions.invoke("send-abandoned-cart-email",{body:{external_token:a,customerEmail:s.customer_email,customerName:s.customer_name||"Cliente",cartItems:i.map(r=>({name:r.name,sku:r.sku,quantity:r.quantity,price:r.unit_price,image_url:r.image_url})),totalValue:Number(s.total_value)}});if(n)throw n;t({title:"Email inviata! âï¸",description:`Email di recupero inviata a ${s.customer_email}`})}catch(a){console.error("Recovery email error:",a),t({variant:"destructive",title:"Errore invio email",description:(a==null?void 0:a.message)||"Errore sconosciuto"})}finally{V(!1)}},D=x.useMemo(()=>_.filter(s=>{var i,n,r;const a=E(s);if(k==="user"&&!a||k==="guest"&&a||S==="abandoned"&&s.recovered_at||S==="recovered"&&!s.recovered_at)return!1;if(l){const d=l.toLowerCase(),T=y(s).some(m=>{var P,F;return((P=m==null?void 0:m.name)==null?void 0:P.toLowerCase().includes(d))||((F=m==null?void 0:m.sku)==null?void 0:F.toLowerCase().includes(d))}),I=((i=s.customer_name)==null?void 0:i.toLowerCase().includes(d))||((n=s.customer_email)==null?void 0:n.toLowerCase().includes(d));return T||I||((r=s.guest_id)==null?void 0:r.toLowerCase().includes(d))}return!0}),[_,l,k,S]);return C?e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:Array.from({length:4}).map((s,a)=>e.jsx(z,{className:"h-24"},a))}),e.jsx(z,{className:"h-96"})]}):e.jsxs("div",{className:"space-y-4 sm:space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[e.jsx(v,{children:e.jsxs(b,{className:"p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground mb-1",children:[e.jsx($,{className:"h-4 w-4"}),e.jsx("span",{className:"text-xs font-medium",children:"Totale carrelli"})]}),e.jsx("p",{className:"text-2xl font-bold",children:c.total})]})}),e.jsx(v,{children:e.jsxs(b,{className:"p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground mb-1",children:[e.jsx(se,{className:"h-4 w-4"}),e.jsx("span",{className:"text-xs font-medium",children:"Valore totale"})]}),e.jsx("p",{className:"text-2xl font-bold",children:w(c.totalValue)})]})}),e.jsx(v,{children:e.jsxs(b,{className:"p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground mb-1",children:[e.jsx(Ne,{className:"h-4 w-4"}),e.jsx("span",{className:"text-xs font-medium",children:"Abbandonati (30gg)"})]}),e.jsx("p",{className:"text-2xl font-bold",children:c.abandonedLast30})]})}),e.jsx(v,{children:e.jsxs(b,{className:"p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-green-600 mb-1",children:[e.jsx(ae,{className:"h-4 w-4"}),e.jsx("span",{className:"text-xs font-medium",children:"Recuperati"})]}),e.jsx("p",{className:"text-2xl font-bold text-green-600",children:c.recovered})]})})]}),e.jsxs(v,{children:[e.jsxs(te,{className:"pb-3",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3",children:[e.jsx(le,{className:"text-lg",children:"Carrelli Abbandonati"}),e.jsxs(M,{variant:"outline",size:"sm",onClick:N,children:[e.jsx(fe,{className:"h-4 w-4 mr-1"}
6)," Aggiorna"]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row gap-2 mt-2",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(re,{className:"absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(ne,{placeholder:"Cerca cliente, prodotto, SKU, email...",value:l,onChange:s=>f(s.target.value),className:"pl-8 h-9"})]}),e.jsxs(B,{value:k,onValueChange:s=>W(s),children:[e.jsx(G,{className:"w-full sm:w-[150px] h-9",children:e.jsx(K,{})}),e.jsxs(Q,{children:[e.jsx(u,{value:"all",children:"Tutti"}),e.jsx(u,{value:"user",children:"Registrati"}),e.jsx(u,{value:"guest",children:"Guest"})]})]}),e.jsxs(B,{value:S,onValueChange:s=>Y(s),children:[e.jsx(G,{className:"w-full sm:w-[150px] h-9",children:e.jsx(K,{})}),e.jsxs(Q,{children:[e.jsx(u,{value:"all",children:"Tutti gli stati"}),e.jsx(u,{value:"abandoned",children:"Abbandonati"}),e.jsx(u,{value:"recovered",children:"Recuperati"})]})]})]})]}),e.jsx(b,{className:"px-0 pb-0",children:D.length===0?e.jsxs("div",{className:"text-center py-16 px-4",children:[e.jsx($,{className:"h-12 w-12 mx-auto text-muted-foreground/30 mb-3"}),e.jsx("p",{className:"text-muted-foreground font-medium",children:"Nessun carrello trovato"})]}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"hidden md:block overflow-x-auto",children:e.jsxs(oe,{children:[e.jsx(me,{children:e.jsxs(X,{children:[e.jsx(h,{children:"Cliente"}),e.jsx(h,{children:"Data"}),e.jsx(h,{children:"Prodotti"}),e.jsx(h,{className:"text-right",children:"Valore"}),e.jsx(h,{children:"Stato"}),e.jsx(h,{className:"w-[80px]"})]})}),e.jsx(xe,{children:D.map(s=>{var n,r,d,H,T;const a=y(s),i=E(s);return e.jsxs(X,{className:"cursor-pointer hover:bg-muted/50",onClick:()=>R(s),children:[e.jsx(j,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[i?e.jsx("div",{className:"h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center shrink-0",children:e.jsx(O,{className:"h-4 w-4 text-primary"})}):e.jsx("div",{className:"h-8 w-8 rounded-full bg-muted flex items-center justify-center shrink-0",children:e.jsx(U,{className:"h-4 w-4 text-muted-foreground"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:s.customer_name||(i?"Utente registrato":"Guest")}),s.customer_email?e.jsx("p",{className:"text-xs text-muted-foreground truncate",children:s.customer_email}):e.jsx("p",{className:"text-[11px] text-muted-foreground truncate",children:i?"Email non disponibile":`ID: ${(n=s.guest_id)==null?void 0:n.slice(0,8)}...`})]})]})}),e.jsxs(j,{children:[e.jsx("div",{className:"text-sm",children:g(new Date(s.created_at),"d MMM yy HH:mm",{locale:p})}),e.jsxs("div",{className:"text-[11px] text-muted-foreground",children:["Agg. ",g(new Date(s.updated_at),"d MMM HH:mm",{locale:p})]})]}),e.jsx(j,{children:e.jsx("div",{className:"max-w-[280px]",children:a.length>0?e.jsxs("div",{className:"flex items-center gap-2",children:[((r=a[0])==null?void 0:r.image_url)&&e.jsx("img",{src:a[0].image_url,alt:"",className:"h-8 w-8 rounded object-contain shrink-0"}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:((d=a[0])==null?void 0:d.name)||"Prodotto"}),e.jsxs("p",{className:"text-[11px] text-muted-foreground",children:[((H=a[0])==null?void 0:H.sku)&&`SKU: ${a[0].sku}`,((T=a[0])==null?void 0:T.quantity)&&` · Qtà : ${a[0].quantity}`,a.length>1&&` · +${a.length-1} altri`]})]})]}):e.jsx("span",{className:"text-muted-foreground text-sm",children:"â"})})}),e.jsx(j,{className:"text-right font-semibold text-sm",children:w(Number(s.total_value))}),e.jsx(j,{children:s.recovered_at?e.jsx(o,{className:"bg-green-100 text-green-700 border-green-200",children:"Recuperato"}):e.jsx(o,{variant:"destructive",className:"bg-orange-100 text-orange-700 border-orange-200",children:"Abbandonato"})}),e.jsx(j,{children:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(M,{variant:"ghost",size:"icon",className:"h-8 w-8",children:e.jsx(ve,{className:"h-4 w-4"})}),s.customer_email&&!s.recovered_at&&e.jsx(M,{variant:"ghost",size:"icon",className:"h-8 w-8",title:"Invia email di re
6cupero",disabled:q,onClick:I=>{I.stopPropagation(),L(s)},children:e.jsx(J,{className:"h-4 w-4"})})]})})]},s.id)})})]})}),e.jsx("div",{className:"md:hidden px-3 pb-3 space-y-2",children:D.map(s=>{var n,r;const a=y(s),i=E(s);return e.jsx("div",{className:"p-3 border rounded-lg active:bg-muted/50 transition-colors",onClick:()=>R(s),children:e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[i?e.jsxs(o,{variant:"outline",className:"gap-1 text-[10px]",children:[e.jsx(ie,{className:"h-3 w-3"})," ",s.customer_name||"Registrato"]}):e.jsxs(o,{variant:"secondary",className:"gap-1 text-[10px]",children:[e.jsx(U,{className:"h-3 w-3"})," Guest"]}),s.recovered_at?e.jsx(o,{className:"bg-green-100 text-green-700 border-green-200 text-[10px]",children:"Recuperato"}):e.jsx(o,{variant:"destructive",className:"bg-orange-100 text-orange-700 border-orange-200 text-[10px]",children:"Abbandonato"})]}),s.customer_email&&e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:s.customer_email}),a.length>0&&e.jsxs("div",{className:"flex items-center gap-2",children:[((n=a[0])==null?void 0:n.image_url)&&e.jsx("img",{src:a[0].image_url,alt:"",className:"h-8 w-8 rounded object-contain shrink-0"}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:((r=a[0])==null?void 0:r.name)||"Prodotto"}),a.length>1&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["+",a.length-1," altri"]})]})]})]}),e.jsxs("div",{className:"text-right shrink-0",children:[e.jsx("p",{className:"font-semibold text-sm",children:w(Number(s.total_value))}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:g(new Date(s.created_at),"d MMM yy",{locale:p})})]})]})},s.id)})})]})})]}),e.jsx(ue,{open:!!A,onOpenChange:()=>R(null),children:e.jsxs(he,{className:"max-w-lg max-h-[90vh] overflow-y-auto mx-2",children:[e.jsx(je,{children:e.jsx(ge,{children:"Dettaglio Carrello Abbandonato"})}),A&&e.jsx(ye,{cart:A,onSendEmail:L,sendingEmail:q})]})})]})}function ye({cart:t,onSendEmail:_,sendingEmail:C}){const c=y(t),N=!!t.external_user_id||!!t.user_id;return e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"p-3 rounded-lg bg-muted/50 space-y-1",children:e.jsxs("div",{className:"flex items-center gap-2",children:[N?e.jsx("div",{className:"h-10 w-10 rounded-full bg-primary/10 flex items-center justify-center shrink-0",children:e.jsx(O,{className:"h-5 w-5 text-primary"})}):e.jsx("div",{className:"h-10 w-10 rounded-full bg-muted flex items-center justify-center shrink-0",children:e.jsx(U,{className:"h-5 w-5 text-muted-foreground"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-semibold text-sm",children:t.customer_name||(N?"Utente registrato":"Visitatore anonimo")}),t.customer_email&&e.jsx("p",{className:"text-xs text-muted-foreground",children:t.customer_email})]}),N&&e.jsx(o,{variant:"outline",className:"ml-auto shrink-0 text-[10px]",children:"Registrato"})]})}),e.jsxs("div",{className:"grid grid-cols-2 gap-3 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground text-xs",children:"Stato"}),e.jsx("p",{className:"font-medium",children:t.recovered_at?"Recuperato":"Abbandonato"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground text-xs",children:"Articoli"}),e.jsxs("p",{className:"font-medium",children:[c.reduce((l,f)=>l+(f.quantity||1),0)," pz (",c.length," prodotti)"]})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground text-xs",children:"Creato"}),e.jsx("p",{className:"font-medium",children:g(new Date(t.created_at),"d MMM yyyy HH:mm",{locale:p})})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground text-xs",children:"Aggiornato"}),e.jsx("p",{className:"font-medium",children:g(new Date(t.updated_at),"d MMM yyyy HH:mm",{locale:p})})]}),t.recovered_at&&e.jsxs("div",{className:"col-span-2",children:[e.jsx("span",{className:"text-muted-foreground text-xs",children:"Recuperato il"}
6),e.jsx("p",{className:"font-medium",children:g(new Date(t.recovered_at),"d MMM yyyy HH:mm",{locale:p})})]})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold text-sm mb-2",children:"Prodotti nel carrello"}),e.jsxs("div",{className:"space-y-2",children:[c.map((l,f)=>e.jsxs("div",{className:"flex items-center gap-3 p-2.5 border rounded-lg",children:[l.image_url&&e.jsx("img",{src:l.image_url,alt:"",className:"h-12 w-12 object-contain rounded shrink-0"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:l.name||"Prodotto"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["SKU: ",l.sku||"â"," · Qtà : ",l.quantity||1]})]}),e.jsx("span",{className:"text-sm font-semibold shrink-0",children:l.unit_price?w(Number(l.unit_price)*(l.quantity||1)):"â"})]},f)),c.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground py-2",children:"Nessun prodotto trovato nei dati del carrello"})]})]}),e.jsxs("div",{className:"flex justify-between items-center pt-2 border-t",children:[e.jsx("span",{className:"font-medium",children:"Totale carrello"}),e.jsx("span",{className:"text-lg font-bold",children:w(Number(t.total_value))})]}),t.customer_email&&!t.recovered_at&&e.jsxs(M,{className:"w-full",onClick:()=>_(t),disabled:C,children:[e.jsx(J,{className:"h-4 w-4 mr-2"}),C?"Invio in corso...":`Invia Email di Recupero a ${t.customer_email}`]})]})}function De(){return e.jsx(be,{})}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.