1import{r as j,af as F,j as e,B as c,I as o,s as m,b as l}from"./index-B6Gxx4GM.js";import{u as D}from"./useQuery-C3bj8Vn9.js";import{u as p}from"./useMutation-r1Y5CKuG.js";import{P as M,T as k}from"./textarea-BcRGzi4J.js";import{C as g,a as I,b as L,d as v}from"./card-DarvT0nx.js";import{D as Q,e as A,a as K,b as O,c as P}from"./dialog-CppMw5qz.js";import{L as n}from"./label-BERruQyk.js";import{S as B}from"./switch-DbzZusa5.js";import{M as N}from"./mail-B7eLQSox.js";import{S as H}from"./square-pen-L-a7STHH.js";import{T as W}from"./trash-2-Beq5DyJU.js";import"./index-CThjfcc1.js";function ae(){const[b,i]=j.useState(!1),[d,f]=j.useState(null),[a,r]=j.useState({name:"",subject:"",content:"",delay_days:0,sequence_order:1,active:!0}),u=F(),{data:x,isLoading:q}=D({queryKey:["email-templates"],queryFn:async()=>{const{data:s,error:t}=await m.from("email_templates").select("*").order("sequence_order",{ascending:!0});if(t)throw t;return s}}),C=p({mutationFn:async s=>{const{error:t}=await m.from("email_templates").insert([s]);if(t)throw t},onSuccess:()=>{u.invalidateQueries({queryKey:["email-templates"]}),l.success("Email template created successfully"),h(),i(!1)},onError:s=>{l.error("Failed to create template: "+s.message)}}),w=p({mutationFn:async({id:s,template:t})=>{const{error:y}=await m.from("email_templates").update(t).eq("id",s);if(y)throw y},onSuccess:()=>{u.invalidateQueries({queryKey:["email-templates"]}),l.success("Email template updated successfully"),h(),i(!1)},onError:s=>{l.error("Failed to update template: "+s.message)}}),_=p({mutationFn:async s=>{const{error:t}=await m.from("email_templates").delete().eq("id",s);if(t)throw t},onSuccess:()=>{u.invalidateQueries({queryKey:["email-templates"]}),l.success("Email template deleted successfully")},onError:s=>{l.error("Failed to delete template: "+s.message)}}),h=()=>{r({name:"",subject:"",content:"",delay_days:0,sequence_order:1,active:!0}),f(null)},E=s=>{f(s),r({name:s.name,subject:s.subject,content:s.content,delay_days:s.delay_days,sequence_order:s.sequence_order,active:s.active}),i(!0)},S=s=>{s.preventDefault(),d?w.mutate({id:d.id,template:a}):C.mutate(a)},T=s=>{confirm("Are you sure you want to delete this template?")&&_.mutate(s)};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Email Templates"}),e.jsx("p",{className:"text-muted-foreground mt-1",children:"Create automated follow-up sequences for eBook leads"})]}),e.jsxs(Q,{open:b,onOpenChange:s=>{i(s),s||h()},children:[e.jsx(A,{asChild:!0,children:e.jsxs(c,{children:[e.jsx(M,{className:"mr-2 h-4 w-4"}),"New Template"]})}),e.jsxs(K,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(O,{children:e.jsx(P,{children:d?"Edit Template":"Create Email Template"})}),e.jsxs("form",{onSubmit:S,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(n,{htmlFor:"name",children:"Template Name"}),e.jsx(o,{id:"name",value:a.name,onChange:s=>r({...a,name:s.target.value}),placeholder:"e.g., Welcome Email",required:!0})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(n,{htmlFor:"delay_days",children:"Delay (days after download)"}),e.jsx(o,{id:"delay_days",type:"number",min:"0",value:a.delay_days,onChange:s=>r({...a,delay_days:parseInt(s.target.value)}),required:!0})]}),e.jsxs("div",{children:[e.jsx(n,{htmlFor:"sequence_order",children:"Sequence Order"}),e.jsx(o,{id:"sequence_order",type:"number",min:"1",value:a.sequence_order,onChange:s=>r({...a,sequence_order:parseInt(s.target.value)}),required:!0})]})]}),e.jsxs("div",{children:[e.jsx(n,{htmlFor:"subject",children:"Email Subject"}),e.jsx(o,{id:"subject",value:a.subject,onChange:s=>r({...a,subject:s.target.value}),placeholder:"e.g., Welcome to Web3 Mastery!",required:!0})]}),e.jsxs("div",{children:[e.jsx(n,{htmlFor:"content",children:"Email Content (HTML supported)"}),e.jsx(k,{id:"content",value:a.content,onChange:s=>r({...a,content:s.target.value}),placeholder:"Use {{name}} and {{email}} as variables...",rows:12,className:"font-mono text-sm",required:!0}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Available variables: ","{","{","name","}","}",", ","{","{","email","}","}"]})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(B,{id:"active",checked:a.active,onCheckedChange:s=>r({...a,active:s})}),e.jsx(n,{htmlFor:"active",children:"Active"})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(c,{type:"button",variant:"outline",onClick:()=>i(!1),children:"Cancel"}),e.jsxs(c,{type:"submit",children:[d?"Update":"Create"," Template"]})]})]})]})]})]}),q?e.jsx("div",{className:"text-center py-8",children:"Loading templates..."}):x&&x.length>0?e.jsx("div",{className:"grid gap-4",children:x.map(s=>e.jsxs(g,{children:[e.jsx(I,{children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(N,{className:"h-5 w-5 text-muted-foreground"}),e.jsxs("div",{children:[e.jsx(L,{className:"text-lg",children:s.name}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:["Sequence #",s.sequence_order," ⢠Sends after"," ",s.delay_days," day",s.delay_days!==1?"s":""]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:`px-2 py-1 text-xs rounded-full ${s.active?"bg-green-100 text-green-700":"bg-gray-100 text-gray-700"}`,children:s.active?"Active":"Inactive"}),e.jsx(c,{variant:"ghost",size:"sm",onClick:()=>E(s),children:e.jsx(H,{className:"h-4 w-4"})}),e.jsx(c,{variant:"ghost",size:"sm",onClick:()=>T(s.id),children:e.jsx(W,{className:"h-4 w-4 text-destructive"})})]})]})}),e.jsx(v,{children:e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-sm font-medium",children:"Subject:"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:s.subject})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-sm font-medium",children:"Preview:"}),e.jsx("p",{className:"text-sm text-muted-foreground line-clamp-3",children:s.content.replace(/<[^>]*>/g,"")})]})]})})]},s.id))}):e.jsx(g,{children:e.jsx(v,{className:"py-8",children:e.jsxs("div",{className:"text-center",children:[e.jsx(N,{className:"h-12 w-12 text-muted-foreground mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-semibold mb-2",children:"No email templates yet"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"Create your first automated follow-up sequence"})]})})})]})}export{ae 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.