PageSourceSearch

https://www.xpark.ai/assets/AdminCoupons-CvbmxWbE.js

js xpark.ai collected 2026-10-05 14:02:35 UTC 10,235 bytes, 1 lines download raw bytes

1import{r as n,j as e,f as q,p as M,B as j,ab as G,a8 as D,D as k,F as R,G as B,H as E,I as r,$ as H,d as c,g as x}from"./index-FWKSGZPO.js";import{S as J,a as K,b as Q,c as W,d as V}from"./select-CNpOZwnZ.js";import{P as X}from"./pencil-BFL_pWo_.js";import"./index-BdQq_4o_.js";const z={code:"",description:"",discount_type:"percent",discount_value:"10",active:!0,valid_from:"",valid_until:"",max_uses:"",max_uses_per_email:"",min_days:"",airports:"",categories:""},p=g=>new Intl.NumberFormat("pt-BR",{style:"currency",currency:"BRL"}).format(g);function te(){const[g,N]=n.useState(!0),[b,O]=n.useState([]),[v,F]=n.useState({}),[u,C]=n.useState(null),[L,m]=n.useState(!1),[t,i]=n.useState(z),[h,y]=n.useState(null),[S,A]=n.useState([]);async function _(){N(!0);const{data:s}=await c.from("coupons").select("*").order("created_at",{ascending:!1});O(s??[]);const{data:l}=await c.from("coupon_redemptions").select("coupon_id, discount_amount"),o={};(l??[]).forEach(d=>{const f=o[d.coupon_id]??{uses:0,discount:0};f.uses+=1,f.discount+=Number(d.discount_amount)||0,o[d.coupon_id]=f}),F(o),N(!1)}n.useEffect(()=>{_()},[]);function I(){C(null),i(z),m(!0)}function U(s){var a,l,o;C(s),i({code:s.code,description:s.description??"",discount_type:s.discount_type,discount_value:String(s.discount_value),active:s.active,valid_from:s.valid_from?s.valid_from.slice(0,10):"",valid_until:s.valid_until?s.valid_until.slice(0,10):"",max_uses:((a=s.max_uses)==null?void 0:a.toString())??"",max_uses_per_email:((l=s.max_uses_per_email)==null?void 0:l.toString())??"",min_days:((o=s.min_days)==null?void 0:o.toString())??"",airports:(s.airports??[]).join(", "),categories:(s.categories??[]).join(", ")}),m(!0)}async function P(){const s=t.code.trim().toUpperCase();if(!s)return x.error("Código obrigatório");const a=Number(t.discount_value);if(!(a>0))return x.error("Valor deve ser maior que zero");const l={code:s,description:t.description.trim()||null,discount_type:t.discount_type,discount_value:a,active:t.active,valid_from:t.valid_from?new Date(t.valid_from).toISOString():null,valid_until:t.valid_until?new Date(t.valid_until+"T23:59:59").toISOString():null,max_uses:t.max_uses?Number(t.max_uses):null,max_uses_per_email:t.max_uses_per_email?Number(t.max_uses_per_email):null,min_days:t.min_days?Number(t.min_days):null,airports:t.airports?t.airports.split(",").map(d=>d.trim().toUpperCase()).filter(Boolean):null,categories:t.categories?t.categories.split(",").map(d=>d.trim().toLowerCase()).filter(Boolean):null},{error:o}=u?await c.from("coupons").update(l).eq("id",u.id):await c.from("coupons").insert(l);if(o)return x.error(o.message);x.success(u?"Cupom atualizado":"Cupom criado"),m(!1),_()}async function T(s){const{error:a}=await c.from("coupons").update({active:!s.active}).eq("id",s.id);if(a)return x.error(a.message);_()}async function $(s){y(s);const{data:a}=await c.from("coupon_redemptions").select("*").eq("coupon_id",s.id).order("redeemed_at",{ascending:!1});A(a??[])}const w=n.useMemo(()=>{let s=0,a=0;return Object.values(v).forEach(l=>{s+=l.uses,a+=l.discount}),{uses:s,discount:a}},[v]);return g?e.jsx("div",{className:"flex items-center justify-center py-20",children:e.jsx(q,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-2xl font-semibold flex items-center gap-2",children:[e.jsx(M,{className:"h-6 w-6"})," Cupons de desconto"]}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[w.uses," utilizações · ",p(w.discount)," em descontos concedidos"]})]}),e.jsxs(j,{onClick:I,children:[e.jsx(G,{className:"h-4 w-4 mr-2"})," Novo cupom"]})]}),e.jsx("div",{className:"rounded-lg border bg-card overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-muted/50 text-left",children:e.jsxs("tr",{children:[e.jsx("th",{className:"p-3",children:"Código"}),e.jsx("th",{className:"p-3",children:"Descrição"}),e.jsx("th",{className:"p-3",children:"Desconto"}),e.jsx("th",{className:"p-3",children:"Validade"}),e.jsx("th",{className:"p-3",children:"Usos"}),e.jsx("th",{className:"p-3",children:"Ativo"}),e.jsx("th",{className:"p-3 text-right",children:"Ações"})]})}),e.jsxs("tbody",{children:[b.map(s=>{const a=v[s.id]??{uses:0,discount:0};return e.jsxs("tr",{className:"border-t",children:[e.jsx("td",{className:"p-3 font-mono font-semibold",children:s.code}),e.jsx("td",{className:"p-3 text-muted-foreground",children:s.description??"—"}),e.jsx("td",{className:"p-3",children:s.discount_type==="percent"?`${s.discount_value}%`:p(Number(s.discount_value))}),e.jsx("td",{className:"p-3 text-xs text-muted-foreground",children:s.valid_until?new Date(s.valid_until).toLocaleDateString("pt-BR"):"sem expiração"}),e.jsx("td",{className:"p-3",children:e.jsxs("button",{className:"underline hover:text-primary",onClick:()=>$(s),children:[a.uses,s.max_uses?` / ${s.max_uses}`:""]})}),e.jsx("td",{className:"p-3",children:e.jsx(D,{checked:s.active,onCheckedChange:()=>T(s)})}),e.jsx("td",{className:"p-3 text-right",children:e.jsx(j,{variant:"ghost",size:"sm",onClick:()=>U(s),children:e.jsx(X,{className:"h-4 w-4"})})})]},s.id)}),b.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"p-6 text-center text-muted-foreground",children:"Nenhum cupom cadastrado."})})]})]})}),e.jsx(k,{open:L,onOpenChange:m,children:e.jsxs(R,{className:"max-w-lg",children:[e.jsx(B,{children:e.jsx(E,{children:u?"Editar cupom":"Novo cupom"})}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Código"}),e.jsx(r,{value:t.code,onChange:s=>i({...t,code:s.target.value.toUpperCase()}),className:"uppercase"})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Descrição"}),e.jsx(r,{value:t.description,onChange:s=>i({...t,description:s.target.value})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Tipo"}),e.jsxs(J,{value:t.discount_type,onValueChange:s=>i({...t,discount_type:s}),children:[e.jsx(K,{children:e.jsx(Q,{})}),e.jsxs(W,{children:[e.jsx(V,{value:"percent",children:"Percentual (%)"}),e.jsx(V,{value:"fixed",children:"Valor fixo (R$)"})]})]})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"text-xs text-muted-foreground",children:["Valor (",t.discount_type==="percent"?"%":"R$",")"]}),e.jsx(r,{type:"number",min:"0.01",step:"0.01",value:t.discount_value,onChange:s=>i({...t,discount_value:s.target.value})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Válido de"}),e.jsx(r,{type:"date",value:t.valid_from,onChange:s=>i({...t,valid_from:s.target.value})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Válido até"}
1),e.jsx(r,{type:"date",value:t.valid_until,onChange:s=>i({...t,valid_until:s.target.value})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Máx. usos total"}),e.jsx(r,{type:"number",min:"0",value:t.max_uses,onChange:s=>i({...t,max_uses:s.target.value}),placeholder:"ilimitado"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Máx. usos por email"}),e.jsx(r,{type:"number",min:"0",value:t.max_uses_per_email,onChange:s=>i({...t,max_uses_per_email:s.target.value}),placeholder:"ilimitado"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Estadia mínima (diárias)"}),e.jsx(r,{type:"number",min:"0",value:t.min_days,onChange:s=>i({...t,min_days:s.target.value}),placeholder:"sem mínimo"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Aeroportos (vírgula)"}),e.jsx(r,{value:t.airports,onChange:s=>i({...t,airports:s.target.value}),placeholder:"GRU, VCP (vazio = todos)"})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Categorias (vírgula)"}),e.jsx(r,{value:t.categories,onChange:s=>i({...t,categories:s.target.value}),placeholder:"descoberta, coberta, valet (vazio = todas)"})]}),e.jsxs("div",{className:"col-span-2 flex items-center gap-2",children:[e.jsx(D,{checked:t.active,onCheckedChange:s=>i({...t,active:s})}),e.jsx("span",{className:"text-sm",children:"Ativo"})]})]}
1),e.jsxs(H,{children:[e.jsx(j,{variant:"ghost",onClick:()=>m(!1),children:"Cancelar"}),e.jsx(j,{onClick:P,children:u?"Salvar":"Criar"})]})]})}),e.jsx(k,{open:!!h,onOpenChange:s=>!s&&y(null),children:e.jsxs(R,{className:"max-w-3xl",children:[e.jsx(B,{children:e.jsxs(E,{children:["Utilizações · ",h==null?void 0:h.code]})}),e.jsx("div",{className:"max-h-[60vh] overflow-y-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-muted/50 text-left sticky top-0",children:e.jsxs("tr",{children:[e.jsx("th",{className:"p-2",children:"Data"}),e.jsx("th",{className:"p-2",children:"Email"}),e.jsx("th",{className:"p-2",children:"Booking"}),e.jsx("th",{className:"p-2 text-right",children:"Original"}),e.jsx("th",{className:"p-2 text-right",children:"Desconto"}),e.jsx("th",{className:"p-2 text-right",children:"Final"})]})}),e.jsxs("tbody",{children:[S.map(s=>e.jsxs("tr",{className:"border-t",children:[e.jsx("td",{className:"p-2 text-xs",children:new Date(s.redeemed_at).toLocaleString("pt-BR")}),e.jsx("td",{className:"p-2 text-xs",children:s.customer_email??"—"}),e.jsx("td",{className:"p-2 text-xs font-mono",children:s.booking_id?s.booking_id.slice(0,8):"—"}),e.jsx("td",{className:"p-2 text-right",children:p(Number(s.original_amount))}),e.jsxs("td",{className:"p-2 text-right text-emerald-600",children:["- ",p(Number(s.discount_amount))]}),e.jsx("td",{className:"p-2 text-right font-medium",children:p(Number(s.final_amount))})]},s.id)),S.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"p-6 text-center text-muted-foreground",children:"Nenhuma utilização registrada."})})]})]})})]})})]})}export{te 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.