1import{o as D,r as m,x as E,m as g,j as e,Z as T,y as R,t as x,I as F}from"./index-DpfGtWPm.js";import{C as n,a as c,b as h,d as u,c as L}from"./card-nihJjM9B.js";import{I as $}from"./input-BKxE2JZ7.js";import{T as A}from"./tag-DhV0v16o.js";import{S as P}from"./search-DHHS73mc.js";import{T as I}from"./trending-up-NKgnhIob.js";function G(){const[o,j]=D(),[t,b]=m.useState([]),[C,f]=m.useState(!0),[d,v]=m.useState(o.get("code")||"");m.useEffect(()=>{y()},[o]);const y=async()=>{f(!0);try{let s=E.from("promo_tracking").select("*").order("created_at",{ascending:!1});const r=o.get("code");r&&(s=s.ilike("promo_code",r));const{data:p,error:l}=await s;if(l)throw l;b(p||[])}catch(s){console.error("Error loading sales:",s),g.error("Erreur lors du chargement des ventes")}finally{f(!1)}},N=()=>{d.trim()?j({code:d.trim()}):j({})},S=()=>{if(t.length===0){g.error("Aucune donnée à exporter");return}const s=["Date","Email","Montant","Commission","Taux"],r=t.map(a=>[new Date(a.created_at).toLocaleDateString("fr-FR"),a.buyer_email,`${a.sale_amount.toFixed(2)}â¬`,`${a.commission_amount.toFixed(2)}â¬`,`${a.commission_rate}%`]),p=[s.join(","),...r.map(a=>a.join(","))].join(` 2`),l=new Blob([p],{type:"text/csv;charset=utf-8;"}),i=document.createElement("a"),k=URL.createObjectURL(l);i.setAttribute("href",k),i.setAttribute("download",`promo_${d||"all"}_${new Date().toISOString().split("T")[0]}.csv`),i.style.visibility="hidden",document.body.appendChild(i),i.click(),document.body.removeChild(i),g.success("Export CSV téléchargé")},w=t.reduce((s,r)=>s+r.sale_amount,0),_=t.reduce((s,r)=>s+r.commission_amount,0);return e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(T,{}),e.jsx("main",{className:"container mx-auto px-4 py-8 mt-20",children:e.jsxs("div",{className:"max-w-6xl mx-auto",children:[e.jsxs("div",{className:"flex items-center justify-between mb-8",children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Tracking Codes Promo"}),e.jsx(R,{to:"/admin/promo-codes",children:e.jsxs(x,{variant:"outline",children:[e.jsx(A,{className:"w-4 h-4 mr-2"}),"Gérer les codes promo"]})})]}),e.jsx(n,{className:"mb-6",children:e.jsx(c,{className:"pt-6",children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx($,{placeholder:"Rechercher un code promo (ex: MARC20)",value:d,onChange:s=>v(s.target.value),onKeyDown:s=>s.key==="Enter"&&N()}),e.jsxs(x,{onClick:N,children:[e.jsx(P,{className:"w-4 h-4 mr-2"}),"Rechercher"]}),o.get("code")&&e.jsx(x,{variant:"outline",onClick:()=>{v(""),j({})},children:"Réinitialiser"})]})})}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6 mb-6",children:[e.jsxs(n,{children:[e.jsx(h,{className:"pb-3",children:e.jsx(u,{children:"Nombre de ventes"})}),e.jsx(c,{children:e.jsx("div",{className:"text-3xl font-bold",children:t.length})})]}),e.jsxs(n,{children:[e.jsx(h,{className:"pb-3",children:e.jsx(u,{children:"Chiffre d'affaires total"})}),e.jsx(c,{children:e.jsxs("div",{className:"text-3xl font-bold text-primary",children:[w.toFixed(2),"â¬"]})})]}),e.jsxs(n,{children:[e.jsx(h,{className:"pb-3",children:e.jsx(u,{children:"Commission à verser"})}),e.jsx(c,{children:e.jsxs("div",{className:"text-3xl font-bold text-green-600",children:[_.toFixed(2),"â¬"]})})]})]}),e.jsxs(n,{children:[e.jsx(h,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(L,{children:"Détail des ventes"}),e.jsx(u,{children:o.get("code")?`Code promo : ${o.get("code")}`:"Toutes les ventes avec code promo"})]}),t.length>0&&e.jsxs(x,{onClick:S,variant:"outline",children:[e.jsx(F,{className:"w-4 h-4 mr-2"}),"Exporter CSV"]})]})}),e.jsx(c,{children:C?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"Chargement..."}):t.length===0?e.jsxs("div",{className:"text-center py-12",children:[e.jsx(I,{className:"w-12 h-12 mx-auto mb-4 text-muted-foreground opacity-50"}),e.jsxs("p",{className:"text-muted-foreground",children:["Aucune vente trouvée",o.get("code")&&" pour ce code promo"]})]}):e.jsx("div",{className:"space-y-3",children:t.map(s=>e.jsx("div",{className:"border rounded-lg p-4 hover:border-primary/50 transition-colors",children:e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-2",children:[e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-medium text-muted-foreground",children:"Email"}),e.jsx("div",{className:"font-medium",children:s.buyer_email})]}),e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-medium text-muted-foreground",children:"Montant"}),e.jsxs("div",{className:"font-bold text-primary",children:[s.sale_amount.toFixed(2),"â¬"]})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"text-sm font-medium text-muted-foreground",children:["Commission (",s.commission_rate,"%)"]}),e.jsxs("div",{className:"font-bold text-green-600",children:[s.commission_amount.toFixed(2),"â¬"]})]}),e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-medium text-muted-foreground",children:"Date"}),e.jsx("div",{children:new Date(s.created_at).toLocaleDateString("fr-FR",{day:"2-digit",month:"2-digit",year:"numeric",hour:"2-digit",minute:"2-digit"})})]})]})},s.id))})})]})]})})]})}export{G 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.