1import{a0 as K,a1 as Z,r as i,a2 as G,j as e,B as x,f as k,a3 as h,a4 as W,I as J,D as X,F as Y,G as ee,H as ae,d as L,g as T}from"./index-FWKSGZPO.js";import{C as B,c as E}from"./card-DTQVPU97.js";import{B as se}from"./badge-Cpd-8HxK.js";import{S as P,a as R,b as I,c as q,d as c}from"./select-CNpOZwnZ.js";import{R as te}from"./refresh-cw-CD2FjEhk.js";import{D as ne}from"./download-D6yxZerY.js";import"./index-BdQq_4o_.js";const w={pending:"Aguardando pagamento",paid:"Ativa",cancelled:"Cancelada",expired:"Expirada"},le={paid:"default",pending:"secondary",cancelled:"destructive",expired:"outline"},S={pix:"Pix",card:"Cartão",unknown:"Não identificada"},C={"whatsapp-bandi":"WhatsApp (Bandy)",xpark:"Site xpark",valetina:"Valetina",multipark:"Multipark",aerovalet:"Aerovalet"},m=o=>o?new Date(o).toLocaleDateString("pt-BR",{timeZone:"America/Sao_Paulo"}):"-",v=o=>o?new Date(o).toLocaleString("pt-BR",{timeZone:"America/Sao_Paulo"}):"-";function pe(){const o=K(),{isAdmin:F}=Z(),[N,V]=i.useState(""),[g,M]=i.useState("all"),[j,O]=i.useState("all"),[s,_]=i.useState(null),[y,A]=i.useState(null),{data:d,isLoading:z,isFetching:D,refetch:U}=G({queryKey:["assinaturas"],queryFn:async()=>{const{data:a,error:t}=await L.from("assinaturas").select("*").order("created_at",{ascending:!1}).limit(1e3);if(t)throw t;return a??[]}}),f=i.useMemo(()=>{const a=N.trim().toLowerCase(),t=a.replace(/\D/g,"");return(d??[]).filter(l=>g!=="all"&&(l.status??"")!==g||j!=="all"&&(l.plan_slug??"")!==j?!1:!!(!a||[l.customer_name,l.customer_email,l.plan_name,l.mp_payment_id].filter(Boolean).join(" ").toLowerCase().includes(a)||t.length>=4&&(l.customer_phone??"").replace(/\D/g,"").includes(t)))},[d,N,g,j]),u=i.useMemo(()=>{const a=d??[],t=a.filter(r=>r.status==="paid"),l=t.reduce((r,p)=>r+Number(p.amount||0),0);return{total:a.length,ativas:t.length,pendentes:a.filter(r=>r.status==="pending").length,canceladas:a.filter(r=>r.status==="cancelled").length,receita:l,ticket:t.length?l/t.length:0}},[d]),H=i.useMemo(()=>{const a=new Map;for(const t of d??[])t.plan_slug&&a.set(t.plan_slug,t.plan_name??t.plan_slug);return Array.from(a.entries())},[d]),b=async(a,t)=>{A(a.id);const{error:l}=await L.from("assinaturas").update({status:t}).eq("id",a.id);if(A(null),l){T.error("Não foi possÃvel atualizar",{description:l.message});return}T.success(`Assinatura marcada como ${w[t]??t}`),_(null),o.invalidateQueries({queryKey:["assinaturas"]})},Q=()=>{const a=["Criada em","Cliente","E-mail","Telefone","Plano","Viagens/ano","Valor mensal","Total do ano","Pagamento","Status","Origem","InÃcio","Fim","Pago em","ID Mercado Pago"],t=f.map(n=>[v(n.created_at),n.customer_name??"",n.customer_email??"",n.customer_phone??"",n.plan_name??"",n.plan_freq??"",Number(n.monthly_price||0).toFixed(2),Number(n.amount||0).toFixed(2),S[n.payment_method??""]??n.payment_method??"",w[n.status??""]??n.status??"",C[n.source??""]??n.source??"",m(n.period_start),m(n.period_end),v(n.paid_at),n.mp_payment_id??""].map($=>`"${String($).replace(/"/g,'""')}"`).join(";")),l=new Blob(["\uFEFF"+[a.join(";"),...t].join(` 2`)],{type:"text/csv;charset=utf-8;"}),r=URL.createObjectURL(l),p=document.createElement("a");p.href=r,p.download=`assinaturas-${new Date().toISOString().slice(0,10)}.csv`,p.click(),URL.revokeObjectURL(r)};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Assinaturas"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Controle dos planos contratados: quem assinou, qual plano, quanto pagou e por onde veio."})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(x,{variant:"outline",size:"sm",onClick:()=>U(),disabled:D,children:[D?e.jsx(k,{className:"h-4 w-4 animate-spin mr-2"}):e.jsx(te,{className:"h-4 w-4 mr-2"}),"Atualizar"]}),e.jsxs(x,{variant:"outline",size:"sm",onClick:Q,disabled:!f.length,children:[e.jsx(ne,{className:"h-4 w-4 mr-2"}),"Exportar"]})]})]}),e.jsx("div",{className:"grid gap-3 sm:grid-cols-2 lg:grid-cols-5",children:[{label:"Ativas",value:String(u.ativas)},{label:"Aguardando pagamento",value:String(u.pendentes)},{label:"Canceladas",value:String(u.canceladas)},{label:"Receita de assinaturas",value:h(u.receita)},{label:"Ticket médio",value:h(u.ticket)}].map(a=>e.jsx(B,{children:e.jsxs(E,{className:"p-4",children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-muted-foreground",children:a.label}),e.jsx("div",{className:"mt-1 text-xl font-semibold",children:a.value})]})},a.label))}),e.jsx(B,{children:e.jsxs(E,{className:"p-4 space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap gap-3",children:[e.jsxs("div",{className:"relative min-w-[240px] flex-1",children:[e.jsx(W,{className:"absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(J,{className:"pl-9",placeholder:"Buscar por nome, e-mail, telefone ou ID do pagamento",value:N,onChange:a=>V(a.target.value)})]}),e.jsxs(P,{value:g,onValueChange:M,children:[e.jsx(R,{className:"w-[200px]",children:e.jsx(I,{placeholder:"Status"})}),e.jsxs(q,{children:[e.jsx(c,{value:"all",children:"Todos os status"}),e.jsx(c,{value:"paid",children:"Ativa"}),e.jsx(c,{value:"pending",children:"Aguardando pagamento"}),e.jsx(c,{value:"cancelled",children:"Cancelada"}),e.jsx(c,{value:"expired",children:"Expirada"})]})]}),e.jsxs(P,{value:j,onValueChange:O,children:[e.jsx(R,{className:"w-[200px]",children:e.jsx(I,{placeholder:"Plano"})}),e.jsxs(q,{children:[e.jsx(c,{value:"all",children:"Todos os planos"}),H.map(([a,t])=>e.jsx(c,{value:a,children:t},a))]})]})]}),z?e.jsx("div",{className:"flex justify-center py-12",children:e.jsx(k,{className:"h-5 w-5 animate-spin"})}):f.length?e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"text-muted-foreground",children:e.jsxs("tr",{className:"border-b",children:[e.jsx("th",{className:"py-2 pr-3 text-left font-medium",children:"Criada em"}),e.jsx("th",{className:"py-2 pr-3 text-left font-medium",children:"Cliente"}),e.jsx("th",{className:"py-2 pr-3 text-left font-medium",children:"Plano"}),e.jsx("th",{className:"py-2 pr-3 text-right font-medium",children:"Total do ano"}),e.jsx("th",{className:"py-2 pr-3 text-left font-medium",children:"Pagamento"}),e.jsx("th",{className:"py-2 pr-3 text-left font-medium",children:"Origem"}),e.jsx("th",{className:"py-2 pr-3 text-left font-medium",children:"Vigência"}),e.jsx("th",{className:"py-2 pr-3 text-left font-medium",children:"Status"})]})}),e.jsx("tbody",{children:f.map(a=>e.jsxs("tr",{className:"border-b cursor-pointer hover:bg-muted/40",onClick:()=>_(a),children:[e.jsx("td",{className:"py-2 pr-3 whitespace-nowrap",children:v(a.created_at)}),e.jsxs("td",{className:"py-2 pr-3",children:[e.jsx("div",{className:"font-medium",children:a.customer_name||"Sem nome"}),e.jsx("div",{className:"text-[11px] text-muted-foreground",children:a.customer_email||a.customer_phone||"-"})]}),e.jsxs("td",{className:"py-2 pr-3",children:[e.jsx("div",{children:a.plan_name||a.plan_slug||"-"}),e.jsx("div",{className:"text-[11px] text-muted-foreground",children:a.plan_freq||"-"})]}),e.jsx("td",{className:"py-2 pr-3 text-right font-medium",children:h(Number(a.amount||0))}),e.jsx("td",{className:"py-2 pr-3",children:S[a.payment_method??""]??a.payment_method??"-"}),e.jsx("td",{className:"py-2 pr-3",children:C[a.source??""]??a.source??"-"}),e.jsxs("td",{className:"py-2 pr-3 whitespace-nowrap",children:[m(a.period_start)," a ",m(a.period_end)]}),e.jsx("td",{className:"py-2 pr-3",children:e.jsx(se,{variant:le[a.status??""]??"outline",children:w[a.status??""]??a.status??"-"})})]},a.id))})]})}):e.jsx("p",{className:"py-12 text-center text-sm text-muted-foreground",children:"Nenhuma assinatura encontrada com esses filtros."})]})}),e.jsx(X,{open:!!s,onOpenChange:a=>!a&&_(null),children:e.jsxs(Y,{className:"max-w-lg",children:[e.jsx(ee,{children:e.jsx(ae,{children:"Detalhes da assinatura"})}),s&&e.jsxs("div",{className:"space-y-4 text-sm",children:[e.jsx("div",{className:"grid grid-cols-2 gap-3",children:[["Cliente",s.customer_name||"-"],["E-mail",s.customer_email||"-"],["Telefone",s.customer_phone||"-"],["Plano",s.plan_name||s.plan_
2slug||"-"],["Viagens por ano",s.plan_freq||"-"],["Valor mensal",h(Number(s.monthly_price||0))],["Total do ano",h(Number(s.amount||0))],["Forma de pagamento",S[s.payment_method??""]??s.payment_method??"-"],["Origem",C[s.source??""]??s.source??"-"],["InÃcio",m(s.period_start)],["Fim",m(s.period_end)],["Pago em",v(s.paid_at)],["ID Mercado Pago",s.mp_payment_id||"-"],["Reserva vinculada",s.booking_id||"-"]].map(([a,t])=>e.jsxs("div",{children:[e.jsx("div",{className:"text-[11px] uppercase tracking-wide text-muted-foreground",children:a}),e.jsx("div",{className:"break-words",children:t})]},a))}),F&&e.jsxs("div",{className:"flex flex-wrap gap-2 border-t pt-4",children:[s.status!=="paid"&&e.jsx(x,{size:"sm",disabled:y===s.id,onClick:()=>b(s,"paid"),children:"Marcar como ativa"}),s.status!=="cancelled"&&e.jsx(x,{size:"sm",variant:"destructive",disabled:y===s.id,onClick:()=>b(s,"cancelled"),children:"Cancelar assinatura"}),s.status!=="pending"&&e.jsx(x,{size:"sm",variant:"outline",disabled:y===s.id,onClick:()=>b(s,"pending"),children:"Voltar para pendente"})]})]})]})})]})}export{pe 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.