1import{r as _,j as e}from"./vendor-react-DIA7vK4g.js";import{P as Q,C as H}from"./ParceiroAdminLayout-DR3iAduS.js";import{C as K}from"./ComercialPageHeader-Dx0iWF8w.js";import{b5 as R,b6 as U,b7 as G,b8 as C,bL as J,cg as X,bm as l,at as y,bM as Y,bN as Z,bO as W,bP as t,bz as d,bA as w,bB as S,bC as T,bD as D,bE as P,c7 as E,aa as ee,bb as se,bg as ae,bi as ie,bY as oe,bZ as ne,b_ as M,b$ as m,c0 as te,c1 as x,s as de,O as le,bF as k,aq as re,aV as ce,aS as me,b9 as r,ba as u}from"./index-DtUVjOr4.js";import{f as q}from"./simulator-pricing-C_AkzkqV.js";import"./vendor-docs-BA9xu9ak.js";import"./GlobalSearch-CILhhYPj.js";import"./command-8X5Fvwln.js";import"./useSandboxConfig-H_pzAYpM.js";import"./repeat-sulmqLMn.js";import"./billing-formatters-uNs-HNzw.js";import"./vendor-utils-DWHjTIse.js";import"./corner-down-left-BYgMx1Bm.js";import"./clock-x8uDBefL.js";import"./eye-DZWf3ZF4.js";import"./chevrons-up-down-D6Nd5u2s.js";import"./PageHeader-DbudfU8v.js";import"./vendor-supabase-_bOAMmkC.js";const F=[{value:"nenhum",label:"Nenhum"},{value:"percentual",label:"Percentual (%)"},{value:"fixo",label:"Valor Fixo (R$)"}];function Ee(){const{user:p}=R(),v=U(),[I,h]=_.useState(!1),[c,b]=_.useState(null),[O,f]=_.useState(null),[i,o]=_.useState({codigo:"",descricao:"",desconto_mensalidade_tipo:"nenhum",desconto_mensalidade_valor:0,desconto_mensalidade_meses:0,desconto_treinamento_tipo:"nenhum",desconto_treinamento_valor:0,ativo:!0,validade_inicio:"",validade_fim:"",uso_maximo:""}),{data:j,isLoading:V}=G({queryKey:["sim-cupons-admin"],queryFn:async()=>{const{data:s,error:a}=await u.from("sim_cupons").select("*").order("created_at",{ascending:!1});if(a)throw a;return s}}),g=C({mutationFn:async s=>
1{const a={codigo:s.codigo.toUpperCase().trim(),descricao:s.descricao||null,desconto_mensalidade_tipo:s.desconto_mensalidade_tipo,desconto_mensalidade_valor:s.desconto_mensalidade_valor,desconto_mensalidade_meses:s.desconto_mensalidade_meses,desconto_treinamento_tipo:s.desconto_treinamento_tipo,desconto_treinamento_valor:s.desconto_treinamento_valor,ativo:s.ativo,validade_inicio:s.validade_inicio||null,validade_fim:s.validade_fim||null,uso_maximo:s.uso_maximo?parseInt(s.uso_maximo):null,created_by:p==null?void 0:p.id};if(c){const{error:n}=await u.from("sim_cupons").update(a).eq("id",c.id);if(n)throw n}else{const{error:n}=await u.from("sim_cupons").insert(a);if(n)throw n}},onSuccess:()=>{v.invalidateQueries({queryKey:["sim-cupons-admin"]}),r.success(c?"Cupom atualizado!":"Cupom criado!"),N()},onError:s=>{var a;(a=s.message)!=null&&a.includes("unique")?r.error("Já existe um cupom com este código"):r.error("Erro ao salvar cupom")}}),$=C({mutationFn:async s=>{const{error:a}=await u.from("sim_cupons").delete().eq("id",s);if(a)throw a},onSuccess:()=>{v.invalidateQueries({queryKey:["sim-cupons-admin"]}),r.success("Cupom excluÃdo!")},onError:()=>{r.error("Erro ao excluir cupom")}}),z=C({mutationFn:async({id:s,ativo:a})=>{const{error:n}=await u.from("sim_cupons").update({ativo:a}).eq("id",s);if(n)throw n},onSuccess:()=>{v.invalidateQueries({queryKey:["sim-cupons-admin"]})}}),N=()=>{h(!1),b(null),o({codigo:"",descricao:"",desconto_mensalidade_tipo:"nenhum",desconto_mensalidade_valor:0,desconto_mensalidade_meses:0,desconto_treinamento_tipo:"nenhum",desconto_treinamento_valor:0,ativo:!0,validade_inicio:"",validade_fim:"",uso_maximo:""})},A=s=>{var a;
1b(s),o({codigo:s.codigo,descricao:s.descricao||"",desconto_mensalidade_tipo:s.desconto_mensalidade_tipo,desconto_mensalidade_valor:s.desconto_mensalidade_valor,desconto_mensalidade_meses:s.desconto_mensalidade_meses,desconto_treinamento_tipo:s.desconto_treinamento_tipo,desconto_treinamento_valor:s.desconto_treinamento_valor,ativo:s.ativo,validade_inicio:s.validade_inicio||"",validade_fim:s.validade_fim||"",uso_maximo:((a=s.uso_maximo)==null?void 0:a.toString())||""}),h(!0)},B=async(s,a)=>{await navigator.clipboard.writeText(s),f(a),r.success("Código copiado!"),setTimeout(()=>f(null),2e3)},L=s=>{if(s.preventDefault(),!i.codigo.trim()){r.error("Código é obrigatório");return}g.mutate(i)};return e.jsx(Q,{children:e.jsxs(H,{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(K,{className:"mb-0",title:"Gestão de Cupons",subtitle:"Gerencie cupons de desconto para o simulador"}),e.jsxs(J,{open:I,onOpenChange:h,children:[e.jsx(X,{asChild:!0,children:e.jsxs(l,{children:[e.jsx(y,{className:"h-4 w-4 mr-2"}),"Novo Cupom"]})}),e.jsxs(Y,{className:"max-w-lg",children:[e.jsx(Z,{children:e.jsx(W,{children:c?"Editar Cupom":"Novo Cupom"})}),e.jsxs("form",{onSubmit:L,className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"col-span-2",children:[e.jsx(t,{children:"Código"}),e.jsx(d,{value:i.codigo,onChange:s=>o(a=>({...a,codigo:s.target.value.toUpperCase()})),placeholder:"EX: PROMO20",className:"uppercase"})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(t,{children:"Descrição (opcional)"}),e.jsx(d,{value:i.descricao,onChange:s=>o(a=>({...a,descricao:s.target.value})),placeholder:"Descrição do cupom"})]})]}),e.jsxs("div",{className:"space-y-3 p-3 bg-muted/50 rounded-lg",children:[e.jsx("p",{className:"text-sm font-medium",children:"Desconto na Mensalidade"}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(t,{className:"text-xs",children:"Tipo"}),e.jsxs(w,{value:i.desconto_mensalidade_tipo,onValueChange:s=>o(a=>({...a,desconto_mensalidade_tipo:s})),children:[e.jsx(S,{children:e.jsx(T,{})}),e.jsx(D,{children:F.map(s=>e.jsx(P,{value:s.value,children:s.label},s.value))})]})]}),e.jsxs("div",{children:[e.jsx(t,{className:"text-xs",children:"Valor"}),e.jsx(d,{type:"number",value:i.desconto_mensalidade_valor,onChange:s=>o(a=>({...a,desconto_mensalidade_valor:parseFloat(s.target.value)||0})),disabled:i.desconto_mensalidade_tipo==="nenhum"})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(t,{className:"text-xs",children:"Meses de desconto"}),e.jsx(d,{type:"text",inputMode:"numeric",value:i.desconto_mensalidade_meses,onChange:s=>{const a=s.target.value;(a===""||/^\d*$/.test(a))&&o(n=>({...n,desconto_mensalidade_meses:a}))},onBlur:s=>{const a=parseInt(s.target.value,10);o(n=>({...n,desconto_mensalidade_meses:isNaN(a)?0:Math.max(0,a)}))},disabled:i.desconto_mensalidade_tipo==="nenhum",placeholder:"0 = permanente"})]})]})]}),e.jsxs("div",{className:"space-y-3 p-3 bg-muted/50 rounded-lg",children:[e.jsx("p",{className:"text-sm font-medium",children:"Desconto no Treinamento"}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(t,{className:"text-xs",children:"Tipo"}),e.jsxs(w,{value:i.desconto_treinamento_tipo,onValueChange:s=>o(a=>({...a,desconto_treinamento_tipo:s})),children:[e.jsx(S,{children:e.jsx(T,{})}),e.jsx(D,{children:F.map(s=>e.jsx(P,{value:s.value,children:s.label},s.value))})]})]}),e.jsxs("div",{children:[e.jsx(t,{className:"text-xs",children:"Valor"}),e.jsx(d,{type:"number",value:i.desconto_treinamento_valor,onChange:s=>o(a=>({...a,desconto_treinamento_valor:parseFloat(s.target.value)||0})),disabled:i.desconto_treinamento_tipo==="nenhum"})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(t,{children:"Validade InÃcio"}),e.jsx(d,{type:"date",value:i.validade_inicio,onChange:s=>o(a=>({...a,validade_inicio:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(t,{children:"Validade Fim"}
1),e.jsx(d,{type:"date",value:i.validade_fim,onChange:s=>o(a=>({...a,validade_fim:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(t,{children:"Uso Máximo"}),e.jsx(d,{type:"number",value:i.uso_maximo,onChange:s=>o(a=>({...a,uso_maximo:s.target.value})),placeholder:"Ilimitado"})]}),e.jsxs("div",{className:"flex items-center gap-2 pt-6",children:[e.jsx(E,{checked:i.ativo,onCheckedChange:s=>o(a=>({...a,ativo:s}))}),e.jsx(t,{children:"Ativo"})]})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-4",children:[e.jsx(l,{type:"button",variant:"outline",onClick:N,children:"Cancelar"}),e.jsxs(l,{type:"submit",disabled:g.isPending,children:[g.isPending&&e.jsx(ee,{className:"h-4 w-4 mr-2 animate-spin"}),c?"Salvar":"Criar"]})]})]})]})]})]}),e.jsx(se,{children:e.jsx(ae,{className:"p-0",children:V?e.jsx("div",{className:"p-4 space-y-4",children:[1,2,3].map(s=>e.jsx(ie,{className:"h-12 w-full"},s))}):j&&j.length>0?e.jsxs(oe,{children:[e.jsx(ne,{children:e.jsxs(M,{children:[e.jsx(m,{children:"Código"}),e.jsx(m,{children:"Desconto"}),e.jsx(m,{children:"Uso"}),e.jsx(m,{children:"Status"}),e.jsx(m,{className:"text-right",children:"Ações"})]})}),e.jsx(te,{children:j.map(s=>e.jsxs(M,{children:[e.jsxs(x,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("code",{className:"font-mono font-semibold",children:s.codigo}),e.jsx(l,{variant:"ghost",size:"icon",className:"h-6 w-6",onClick:()=>B(s.codigo,s.id),children:O===s.id?e.jsx(de,{className:"h-3 w-3 text-success"}):e.jsx(le,{className:"h-3 w-3"})})]}),s.descricao&&e.jsx("p",{className:"text-xs text-muted-foreground",children:s.descricao})]}),e.jsx(x,{children:e.jsxs("div",{className:"space-y-1",children:[s.desconto_mensalidade_tipo!=="nenhum"&&e.jsxs(k,{variant:"secondary",className:"text-xs",children:["Mens: ",s.desconto_mensalidade_tipo==="percentual"?`${s.desconto_mensalidade_valor}%`:q(s.desconto_mensalidade_valor),s.desconto_mensalidade_meses>0&&` (${s.desconto_mensalidade_meses}m)`]}),s.desconto_treinamento_tipo!=="nenhum"&&e.jsxs(k,{variant:"outline",className:"text-xs",children:["Trein: ",s.desconto_treinamento_tipo==="percentual"?`${s.desconto_treinamento_valor}%`:q(s.desconto_treinamento_valor)]})]})}),e.jsx(x,{children:e.jsxs("span",{className:"text-sm",children:[s.uso_atual,s.uso_maximo&&` / ${s.uso_maximo}`]})}),e.jsx(x,{children:e.jsx(E,{checked:s.ativo,onCheckedChange:a=>z.mutate({id:s.id,ativo:a})})}),e.jsx(x,{className:"text-right",children:e.jsxs("div",{className:"flex justify-end gap-1",children:[e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>A(s),children:e.jsx(re,{className:"h-4 w-4"})}),e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>{confirm("Excluir este cupom?")&&$.mutate(s.id)},children:e.jsx(ce,{className:"h-4 w-4 text-destructive"})})]})})]},s.id))})]}):e.jsxs("div",{className:"p-8 text-center",children:[e.jsx(me,{className:"h-12 w-12 mx-auto text-muted-foreground/50 mb-4"}),e.jsx("p",{className:"text-muted-foreground",children:"Nenhum cupom cadastrado"}),e.jsxs(l,{className:"mt-4",onClick:()=>h(!0),children:[e.jsx(y,{className:"h-4 w-4 mr-2"}),"Criar Primeiro Cupom"]})]})})})]})})}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.