1import{r as l,j as e}from"./vendor-react-DIA7vK4g.js";import{b6 as ne,b7 as O,b8 as A,bm as u,at as B,bb as j,bg as p,aS as re,a$ as le,aW as ie,bY as ce,bZ as oe,b_ as y,b$ as i,c0 as de,c1 as r,R as me,ab as H,bF as xe,bL as he,bM as ue,bN as je,bO as pe,bP as c,bz as g,cd as ye,bA as z,bB as K,bC as Q,bD as G,bE as v,by as ge,ch as V,ba as d}from"./index-DtUVjOr4.js";import{F as ve}from"./FinanceiroLayout-BFNsfPwP.js";import{P as Ne}from"./percent-SoF1nZwz.js";import{a as fe,P as be}from"./power-hfy_Hc63.js";import"./vendor-docs-BA9xu9ak.js";import"./vendor-utils-DWHjTIse.js";import"./vendor-supabase-_bOAMmkC.js";import"./useSandboxConfig-H_pzAYpM.js";import"./GlobalSearch-CILhhYPj.js";import"./command-8X5Fvwln.js";import"./repeat-sulmqLMn.js";import"./billing-formatters-uNs-HNzw.js";import"./corner-down-left-BYgMx1Bm.js";import"./clock-x8uDBefL.js";function Ee(){const P=ne(),[U,h]=l.useState(!1),[N,T]=l.useState(""),[F,L]=l.useState(""),[f,M]=l.useState("percent"),[b,q]=l.useState(""),[C,$]=l.useState(""),[m,k]=l.useState(!1),[_,R]=l.useState(""),[w,E]=l.useState(""),{data:a,isLoading:W}=O({queryKey:["discount-campaigns-all"],queryFn:async()=>{const{data:s,error:t}=await d.from("discount_campaigns").select("*").order("created_at",{ascending:!1});if(t)throw t;return await Promise.all(s.map(async x=>{const{data:n}=await d.from("customer_discounts").select("id, total_discount_amount, status").eq("campaign_id",x.id),ee=(n==null?void 0:n.length)||0,se=(n==null?void 0:n.filter(D=>D.status==="active").length)||0,ae=(n==null?void 0:n.reduce((D,te)=>D+Number(te.total_discount_amount||0),0))||0;return{...x,customerCount:ee,activeCount:se,totalVolume:ae}}))}}),{data:S}=O({queryKey:["loyalty-penalties-active"],queryFn:async()=>{const{data:s,error:t}=await d.from("loyalty_penalties").select("id, name").eq("is_active",!0).order("name");if(t)throw t;return s}}),I=A({mutationFn:async()=>{var x,n;const{data:s}=await d.auth.getSession(),t=(n=(x=s==null?void 0:s.session)==null?void 0:x.user)==null?void 0:n.id,{error:o}=await d.from("discount_campaigns").insert({name:N.trim(),description:F.trim()||null,discount_type:f,discount_value:parseFloat(b),duration_months:C?parseInt(C):null,has_loyalty:m,loyalty_months:m&&_?parseInt(_):null,loyalty_penalty_id:m&&w?w:null,created_by:t||null});if(o)throw o},onSuccess:()=>{P.invalidateQueries({queryKey:["discount-campaigns-all"]}),V({title:"Campanha criada com sucesso"}),Z()},onError:s=>V({title:"Erro",description:s.message,variant:"destructive"})}),Y=A({mutationFn:async({id:s,isActive:t})=>{const{error:o}=await d.from("discount_campaigns").update({is_active:!t}).eq("id",s);if(o)throw o},onSuccess:()=>{P.invalidateQueries({queryKey:["discount-campaigns-all"]}),V({title:"Campanha atualizada"})}});
1function Z(){T(""),L(""),M("percent"),q(""),$(""),k(!1),R(""),E(""),h(!1)}const J=(a==null?void 0:a.reduce((s,t)=>s+(t.totalVolume||0),0))||0,X=(a==null?void 0:a.filter(s=>s.is_active).length)||0;return e.jsx(ve,{children: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-2xl font-bold",children:"Campanhas de Desconto"}),e.jsx("p",{className:"text-muted-foreground text-sm",children:"Gerencie condições comerciais e cupons de desconto"})]}),e.jsxs(u,{onClick:()=>h(!0),children:[e.jsx(B,{className:"h-4 w-4 mr-1"})," Nova Campanha"]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsx(j,{children:e.jsx(p,{className:"pt-6",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(re,{className:"h-5 w-5 text-primary"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold",children:X}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Campanhas ativas"})]})]})})}),e.jsx(j,{children:e.jsx(p,{className:"pt-6",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(le,{className:"h-5 w-5 text-primary"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold",children:(a==null?void 0:a.reduce((s,t)=>s+t.activeCount,0))||0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Clientes com desconto ativo"})]})]})})}),e.jsx(j,{children:e.jsx(p,{className:"pt-6",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ie,{className:"h-5 w-5 text-amber-500"}),e.jsxs("div",{children:[e.jsxs("p",{className:"text-2xl font-bold",children:["R$ ",J.toFixed(2)]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Volume total de descontos"})]})]})})})]}),e.jsx(j,{children:e.jsx(p,{className:"p-0",children:e.jsxs(ce,{children:[e.jsx(oe,{children:e.jsxs(y,{children:[e.jsx(i,{children:"Nome"}),e.jsx(i,{children:"Tipo"}),e.jsx(i,{children:"Valor"}),e.jsx(i,{children:"Duração"}),e.jsx(i,{children:"Fidelidade"}),e.jsx(i,{children:"Clientes"}),e.jsx(i,{children:"Volume (R$)"}),e.jsx(i,{children:"Status"}),e.jsx(i,{className:"w-[80px]",children:"Ações"})]})}),e.jsx(de,{children:W?e.jsx(y,{children:e.jsx(r,{colSpan:9,className:"text-center text-muted-foreground",children:"Carregando..."})}):a!=null&&a.length?a.map(s=>e.jsxs(y,{children:[e.jsx(r,{children:e.jsxs("div",{children:[e.jsx("span",{className:"font-medium",children:s.name}),s.description&&e.jsx("p",{className:"text-xs text-muted-foreground truncate max-w-[200px]",children:s.description})]})}),e.jsx(r,{children:e.jsxs("span",{className:"flex items-center gap-1 text-sm",children:[s.discount_type==="percent"?e.jsx(Ne,{className:"h-3 w-3"}):e.jsx(me,{className:"h-3 w-3"}),s.discount_type==="percent"?"Percentual":"Valor fixo"]})}),e.jsx(r,{className:"font-mono",children:s.discount_type==="percent"?`${s.discount_value}%`:`R$ ${Number(s.discount_value).toFixed(2)}`}),e.jsx(r,{children:s.duration_months?`${s.duration_months} meses`:"Permanente"}),e.jsx(r,{children:s.has_loyalty?e.jsxs("span",{className:"flex items-center gap-1 text-sm",children:[e.jsx(H,{className:"h-3.5 w-3.5 text-amber-600"}),e.jsxs("span",{className:"font-medium",children:[s.loyalty_months||"â"," meses"]})]}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"â"})}),e.jsx(r,{children:e.jsxs("span",{className:"text-sm",children:[s.activeCount," ativo(s) / ",s.customerCount," total"]})}),e.jsxs(r,{className:"font-mono",children:["R$ ",s.totalVolume.toFixed(2)]}),e.jsx(r,{children:e.jsx(xe,{variant:"outline",className:s.is_active?"bg-green-100 text-green-700 border-green-200 dark:bg-green-950 dark:text-green-400 dark:border-green-800":"bg-gray-100 text-gray-500 border-gray-200 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-700",children:s.is_active?"Ativa":"Inativa"})}),e.jsx(r,{children:e.jsx(u,{size:"icon",variant:"ghost",className:"h-7 w-7",onClick:()=>Y.mutate({id:s.id,isActive:s.is_active}),children:s.is_active?e.jsx(fe,{className:"h-4 w-4"}):e.jsx(be,{className:"h-4 w-4"})})})]},s.id)):e.jsx(y,{children:e.jsx(r,{colSpan:9,className:"text-center text-muted-foreground",children:"Nenhuma campanha criada"})})})]})})}),e.jsx(he,{open:U,onOpenChange:h,children:e.jsxs(ue,{className:"sm:max-w-md",children:[e.jsx(je,{children:e.jsx(pe,{children:"Nova Campanha de Desconto"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Nome"}),e.jsx(g,{value:N,onChange:s=>T(s.target.value),placeholder:"Ex: Condição especial HEMOLABOR"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Descrição (opcional)"}),e.jsx(ye,{value:F,onChange:s=>L(s.target.value),rows:2})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Tipo"}),e.jsxs(z,{value:f,onValueChange:M,children:[e.jsx(K,{children:e.jsx(Q,{})}),e.jsxs(G,{children:[e.jsx(v,{value:"percent",children:"Percentual (%)"}),e.jsx(v,{value:"fixed",children:"Valor fixo (R$)"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Valor"}),e.jsx(g,{type:"number",min:0,step:f==="percent"?1:.01,value:b,onChange:s=>q(s.target.value)})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Duração do desconto (meses)"}),e.jsx(g,{type:"number",min:1,value:C,onChange:s=>$(s.target.value),placeholder:"Vazio = permanente"})]}),e.jsxs("div",{className:"space-y-3 rounded-xl border p-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ge,{id:"hasLoyalty",checked:m,onCheckedChange:s=>k(!!s)}),e.jsxs(c,{htmlFor:"hasLoyalty",className:"cursor-pointer flex items-center gap-1.5",children:[e.jsx(H,{className:"h-3.5 w-3.5"})," Campanha com fidelidade"]})]}),m&&e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Meses de fidelidade"}
1),e.jsx(g,{type:"number",min:1,value:_,onChange:s=>R(s.target.value),placeholder:"Ex: 12"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Multa de rescisão"}),e.jsxs(z,{value:w,onValueChange:E,children:[e.jsx(K,{children:e.jsx(Q,{placeholder:"Nenhuma"})}),e.jsxs(G,{children:[e.jsx(v,{value:"",children:"Nenhuma"}),S==null?void 0:S.map(s=>e.jsx(v,{value:s.id,children:s.name},s.id))]})]})]})]})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(u,{variant:"outline",onClick:()=>h(!1),children:"Cancelar"}),e.jsxs(u,{onClick:()=>I.mutate(),disabled:!N.trim()||!b||I.isPending,children:[e.jsx(B,{className:"h-4 w-4 mr-1"})," Criar Campanha"]})]})]})]})})]})})}export{Ee 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.