import{u as C,r,j as e,L as k,B as c,s as m,t as n}from"./index-B8oXSmui.js";import{I as N}from"./input-BeAjoI93.js";import{C as d}from"./card-BGuHQar-.js";import{A}from"./arrow-left-CdXF6txo.js";import{P as L}from"./plus-i7MGezIG.js";import{U as I}from"./upload-B3BNuGJr.js";import{T as S}from"./trash-2-ByzjCUYQ.js";function R(){const{user:t}=C(),[o,v]=r.useState([]),[u,x]=r.useState(""),[p,h]=r.useState(""),[f,j]=r.useState(""),i=async()=>{const{data:s}=await m.from("recipients").select("id,email,name").order("created_at",{ascending:!1});v(s??[])};r.useEffect(()=>{t&&i()},[t]);const b=async s=>{if(s.preventDefault(),!t)return;const{error:l}=await m.from("recipients").insert({user_id:t.id,email:u.trim().toLowerCase(),name:p||null});if(l)return n.error(l.message);x(""),h(""),i()},w=async()=>{if(!t)return;const s=f.split(/[\s,;\n]+/).map(a=>a.trim().toLowerCase()).filter(a=>/.+@.+\..+/.test(a));if(s.length===0)return n.error("Aucun email valide détecté");const l=s.map(a=>({user_id:t.id,email:a})),{error:g}=await m.from("recipients").upsert(l,{onConflict:"user_id,email",ignoreDuplicates:!0});if(g)return n.error(g.message);n.success(`${s.length} email(s) ajouté(s)`),j(""),i()},y=async s=>{await m.from("recipients").delete().eq("id",s),i()};return e.jsxs("div",{className:"mx-auto max-w-4xl p-6 md:p-10",children:[e.jsxs(k,{to:"/app",className:"mb-4 inline-flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground",children:[e.jsx(A,{className:"h-4 w-4"})," Retour"]}),e.jsx("h1",{className:"mb-1 text-3xl font-bold",children:"Destinataires"}),e.jsx("p",{className:"mb-8 text-sm text-muted-foreground",children:"Gérez la liste des emails qui recevront vos communications."}),e.jsxs("div",{className:"grid gap-6 md:grid-cols-2",children:[e.jsxs(d,{className:"p-5",children:[e.jsx("h3",{className:"mb-3 font-semibold",children:"Ajouter un email"}),e.jsxs("form",{onSubmit:b,className:"space-y-3",children:[e.jsx(N,{type:"email",placeholder:"email@exemple.com",required:!0,value:u,onChange:s=>x(s.target.value)}),e.jsx(N,{placeholder:"Nom (optionnel)",value:p,onChange:s=>h(s.target.value)}),e.jsxs(c,{type:"submit",className:"w-full",children:[e.jsx(L,{className:"h-4 w-4"})," Ajouter"]})]})]}),e.jsxs(d,{className:"p-5",children:[e.jsx("h3",{className:"mb-3 font-semibold",children:"Importer en masse"}),e.jsx("textarea",{className:"min-h-24 w-full rounded-md border bg-background p-3 text-sm",placeholder:"Collez vos emails séparés par des virgules, espaces ou retours à la ligne",value:f,onChange:s=>j(s.target.value)}),e.jsxs(c,{onClick:w,variant:"secondary",className:"mt-3 w-full",children:[e.jsx(I,{className:"h-4 w-4"})," Importer"]})]})]}),e.jsxs(d,{className:"mt-6 p-5",children:[e.jsxs("h3",{className:"mb-3 font-semibold",children:["Liste (",o.length,")"]}),o.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"Aucun destinataire pour le moment."}):e.jsx("div",{className:"divide-y",children:o.map(s=>e.jsxs("div",{className:"flex items-center justify-between py-2.5",children:[e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-medium",children:s.email}),s.name&&e.jsx("div",{className:"text-xs text-muted-foreground",children:s.name})]}),e.jsx(c,{size:"sm",variant:"ghost",onClick:()=>y(s.id),children:e.jsx(S,{className:"h-4 w-4 text-destructive"})})]},s.id))})]})]})}export{R as component};
