1import{r as n,a as I,s as h,j as e,D as H,d as $,e as O,f as G,H as Z,L as m,I as y,ad as J,h as K,b as E,S as q,p as Q,q as W,Z as X,_ as Y,$ as ee,a0 as A,a2 as ae,B as se}from"./index-Dlc_Xpwz.js";import{C as te,a as ie,b as le}from"./card-Dd3MFBnH.js";import{T as re,a as ne,b as M,c as k,d as ce,e as _}from"./table-CfjA1Sts.js";import{S,a as b,b as L,c as D,d as j}from"./select-BON4HBU3.js";import{P as oe}from"./plus-BlO_r7Lv.js";import{F as de}from"./filter-DpDNL9DI.js";import{E as ue}from"./ellipsis-vertical-D-T28OrO.js";import{C as me}from"./copy-Df6KpYCa.js";import{P as he}from"./pen-DTZ3FdV-.js";import{p as xe}from"./pt-BR-DVv9S9L3.js";import"./chevron-down-C0Nn0zRl.js";import"./chevron-up-BjviA4Ic.js";const pe=()=>{const[d,v]=n.useState([]),[c,N]=n.useState(!0),{toast:r}=I(),u=async()=>{try{N(!0);const{data:o,error:l}=await h.from("commercial_subscription_links").select(` 2 *, 3 plan:plans(name), 4 package:commercial_packages(name) 5 `).order("created_at",{ascending:!1});if(l)throw l;v(o)}catch(o){r({title:"Erro ao buscar links",description:o.message,variant:"destructive"})}finally{N(!1)}},w=async(o,l)=>{try{const{error:p}=await h.from("commercial_subscription_links").update({status:l}).eq("id",o);if(p)throw p;r({title:"Status atualizado",description:`Link atualizado para ${l}.`}),await u()}catch(p){r({title:"Erro ao atualizar status",description:p.message,variant:"destructive"})}},x=async o=>{try{const{error:l}=await h.from("commercial_subscription_links").delete().eq("id",o);if(l)throw l;r({title:"Link excluÃdo",description:"O link foi removido com sucesso."}),await u()}catch(l){r({title:"Erro ao excluir link",description:l.message,variant:"destructive"})}};return n.useEffect(()=>{u()},[]),{links:d,loading:c,fetchLinks:u,updateLinkStatus:w,deleteLink:x}},B=d=>d.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g,"").replace(/[^a-z0-9]+/g,"-").replace(/(^-|-$)+/g,"");function ge({isOpen:d,onClose:v,linkId:c,onSuccess:N}){const[r,u]=n.useState(!1),[w,x]=n.useState(!1),[o,l]=n.useState([]),[p,T]=n.useState([]),[P,z]=n.useState([]),{toast:C}=I(),[t,s]=n.useState({name_internal:"",slug:"",product_type:"package",plan_id:null,module_key:null,package_id:null,status:"ativo",usage_limit:null,utm_source:"",utm_medium:"",utm_campaign:""});n.useEffect(()=>{d&&(async()=>{x(!0);try{const[i,R,F]=await Promise.all([h.from("plans").select("id, name").eq("is_active",!0),h.from("commercial_modules").select("module_key, name").eq("is_active",!0),h.from("commercial_packages").select("id, name").eq("status","ativo")]);if(l(i.data||[]),T(R.data||[]),z(F.data||[]),c){const{data:U,error:V}=await h.from("commercial_subscription_links").select("*").eq("id",c).single();if(V)throw V;s(U)}}catch(i){C({title:"Erro ao buscar dados",description:i.message,variant:"destructive"})}finally{x(!1)}})()},[d,c]);const f=a=>{s(i=>({...i,name_internal:a,slug:i.slug===B(i.name_internal||"")?B(a):i.slug}))},g=async()=>{try{if(u(!0),!t.name_internal)throw new Error("Informe o nome interno.");if(!t.slug)throw new Error("Informe o slug.");const a={name_internal:t.name_internal,slug:t.slug,product_type:t.product_type,status:t.status,usage_limit:t.usage_limit,utm_source:t.utm_source,utm_medium:t.utm_medium,utm_campaign:t.utm_campaign,valid_from:t.valid_from,valid_until:t.valid_until,plan_id:t.product_type==="plan"?t.plan_id:null,module_key:t.product_type==="module"?t.module_key:null,package_id:t.product_type==="package"?t.package_id:null};if(c){const{error:i}=await h.from("commercial_subscription_links").update(a).eq("id",c);if(i)throw i}else{const{error:i}=await h.from("commercial_subscription_links").insert(a);if(i)throw i}C({title:"Sucesso",description:"Link salvo com sucesso."}),N(),v()}catch(a){C({title:"Erro ao salvar",description:a.message,variant:"destructive"})}finally{u(!1)}};return e.jsx(H,{open:d,onOpenChange:a=>!a&&v(),children:e.jsxs($,{className:"max-w-2xl max-h-[90vh] overflow-hidden flex flex-col p-0",children:[e.jsx(O,{className:"p-6 pb-0",children:e.jsx(G,{children:c?"Editar Link":"Novo Link de Assinatura"})}),e.jsx(Z,{className:"flex-1 p-6 pt-2",children:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Nome Interno"}),e.jsx(y,{value:t.name_internal,onChange:a=>f(a.target.value),placeholder:"Ex: Campanha Black Friday"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Slug Personalizado"}),e.jsx(y,{value:t.slug,onChange:a=>s(i=>({...i,slug:a.target.value}))})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Tipo de Produto"}),e.jsxs(S,{value:t.product_type,onValueChange:a=>s(i=>({...i,product_type:a})),children:[e.jsx(b,{children:e.jsx(L,{})}),e.jsxs(D,{children:[e.jsx(j,{value:"plan",children:"Plano"}),e.jsx(j,{value:"module",children:"Módulo"}),e.jsx(j,{value:"package",children:"Pacote"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Produto Vinculado"}),t.product_type==="plan"&&e.jsxs(S,{value:t.plan_id||"",onValueChange:a=>s(i=>({...i,plan_id:a})),children:[e.jsx(b,{children:e.jsx(L,{placeholder:"Selecione o plano"})}),e.jsx(D,{children:o.map(a=>e.jsx(j,{value:a.id,children:a.name},a.id))})]}),t.product_type==="module"&&e.jsxs(S,{value:t.module_key||"",onValueChange:a=>s(i=>({...i,module_key:a})),children:[e.jsx(b,{children:e.jsx(L,{placeholder:"Selecione o módulo"})}),e.jsx(D,{children:p.map(a=>e.jsx(j,{value:a.module_key,children:a.name},a.module_key))})]}),t.product_type==="package"&&e.jsxs(S,{value:t.package_id||"",onValueChange:a=>s(i=>({...i,package_id:a})),children:[e.jsx(b,{children:e.jsx(L,{placeholder:"Selecione o pacote"})}),e.jsx(D,{children:P.map(a=>e.jsx(j,{value:a.id,children:a.name},a.id))})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Limite de Usos (Opcional)"}),e.jsx(y,{type:"number",value:t.usage_limit||"",onChange:a=>s(i=>({...i,usage_limit:a.target.value?Number(a.target.value):null}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Status"}),e.jsxs(S,{value:t.status,onValueChange:a=>s(i=>({...i,status:a})),children:[e.jsx(b,{children:e.jsx(L,{})}),e.jsxs(D,{children:[e.jsx(j,{value:"ativo",children:"Ativo"}),e.jsx(j,{value:"inativo",children:"Inativo"})]})]})]})]}),e.jsx(J,{}),e.jsx("h4",{className:"text-sm font-semibold",children:"Parâmetros de Rastreamento (UTMs)"}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Source"}),e.jsx(y,{value:t.utm_source||"",onChange:a=>s(i=>({...i,utm_source:a.target.value})),placeholder:"google, facebook"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Medium"}),e.jsx(y,{value:t.utm_medium||"",onChange:a=>s(i=>({...i,utm_medium:a.target.value})),placeholder:"cpc, organic"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Campaign"}),e.jsx(y,{value:t.utm_campaign||"",onChange:a=>s(i=>({...i,utm_campaign:a.target.value})),placeholder:"black_friday"})]})]})]})}),e.jsxs(K,{className:"p-6 border-t",children:[e.jsx(E,{variant:"ghost",onClick:v,disabled:r,children:"Cancelar"}),e.jsx(E,{onClick:g,disabled:r||w,children:r?"Salvando...":c?"Atualizar Link":"Criar Link"})]})]})})}function De(){const{links:d,loading:v,deleteLink:c,fetchLinks:N}=pe(),[r,u]=n.useState(""),[w,x]=n.useState(!1),[o,l]=n.useState(null),{toast:p}=I(),T=d.filter(s=>s.name_internal.toLowerCase().includes(r.toLowerCase())||s.slug.toLowerCase().includes(r.toLowerCase())),P=s=>{const f={ativo:"success",inativo:"warning",expirado:"destructive",limite_atingido:"outline"},g={ativo:"Ativo",inativo:"Inativo",expirado:"Expirado",limite_atingido:"Limite Atingido"};return e.jsx(se,{variant:f[s]||"default",children:g[s]||s})},z=s=>{const g=`${window.location.hostname==="localhost"||window.location.hostname.includes("lovable.app")?window.location.origin:"https://gfcon.com.br"}/oferta/${s}`;navigator.clipboard.writeText(g),p({title:"Link copiado",description:"A URL da oferta foi copiada para a área de transferência."})},C=s=>{l(s),x(!0)},t=()=>{l(null),x(!0)};return v?e.jsxs("div",{className:"space-y-6",children:[e.jsx(q,{className:"h-40 w-full"}),e.jsx(q,{className:"h-60 w-full"})]}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col md:flex-row md:items-center justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-3xl font-bold tracking-tight",children:"Links de Assinatura"}),e.jsx("p",{className:"text-muted-foreground",children:"Gere links especiais para campanhas e ofertas exclusivas."})]}),e.jsxs(E,{onClick:t,children:[e.jsx(oe,{className:"mr-2 h-4 w-4"}),"Novo Link"]})]}),e.jsxs(te,{children:[e.jsx(ie,{children:e.jsxs("div",{className:"flex flex-col md:flex-row md:items-center justify-between gap-4",children:[e.jsxs("div",{className:"relative w-full md:w-96",children:[e.jsx(Q,{className:"absolute left-2 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(y,{placeholder:"Buscar links...",className:"pl-8",value:r,onChange:s=>u(s.target.value)})]}),e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(E,{variant:"outline",size:"icon",children:e.jsx(de,{className:"h-4 w-4"})})})]})}),e.jsx(le,{children:e.jsxs(re,{children:[e.jsx(ne,{children:e.jsxs(M,{children:[e.jsx(k,{children:"Nome Interno / Slug"}),e.jsx(k,{children:"Tipo / Produto"}),e.jsx(k,{children:"Status"}),e.jsx(k,{children:"Uso / Limite"}),e.jsx(k,{children:"Validade"}),e.jsx(k,{className:"text-right",children:"Ações"})]})}),e.jsx(ce,{children:T.length===0?e.jsx(M,{children:e.jsx(_,{colSpan:6,className:"text-center py-10 text-muted-foreground",children:"Nenhum link encontrado."})}):T.map(s=>{var f,g;return e.jsxs(M,{children:[e.jsxs(_,{children:[e.jsx("div",{className:"font-medium",children:s.name_internal}),e.jsx("div",{className:"text-xs text-muted-foreground",children:s.slug})]}),e.jsxs(_,{children:[e.jsx("div",{className:"capitalize font-medium text-xs",children:s.product_type}),e.jsx("div",{className:"text-xs text-muted-foreground",children:((f=s.plan)==null?void 0:f.name)||((g=s.package)==null?void 0:g.name)||s.module_key||"N/A"})]}),e.jsx(_,{children:P(s.status)}),e.jsx(_,{children:e.jsxs("div",{className:"text-xs",children:[s.usage_count," / ",s.usage_limit||"â"]})}),e.jsx(_,{children:s.valid_until?e.jsx("div",{className:"text-xs text-muted-foreground",children:W(new Date(s.valid_until),"dd/MM/yy",{locale:xe})}):e.jsx("div",{className:"text-xs text-muted-foreground",children:"Ilimitado"})}),e.jsx(_,{className:"text-right",children:e.jsxs(X,{children:[e.jsx(Y,{asChild:!0,children:e.jsx(E,{variant:"ghost",size:"icon",children:e.jsx(ue,{className:"h-4 w-4"})})}
5),e.jsxs(ee,{align:"end",children:[e.jsxs(A,{onClick:()=>z(s.slug),children:[e.jsx(me,{className:"mr-2 h-4 w-4"})," Copiar Link"]}),e.jsxs(A,{onClick:()=>C(s.id),children:[e.jsx(he,{className:"mr-2 h-4 w-4"})," Editar"]}),e.jsxs(A,{className:"text-destructive",onClick:()=>c(s.id),children:[e.jsx(ae,{className:"mr-2 h-4 w-4"})," Excluir"]})]})]})})]},s.id)})})]})})]}),w&&e.jsx(ge,{isOpen:w,onClose:()=>x(!1),linkId:o,onSuccess:()=>N()})]})}export{De 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.