1import{j as e}from"./vendor-editor-BX0sR0z-.js";import{r as o}from"./vendor-react-BPk9u-w4.js";import{d as se,u as ae,m as B,h as le}from"./index-s_BTi5XF.js";import{u as ie}from"./useLeads-DVM3rTeR.js";import{u as ne}from"./useAccounts-DxENSI2p.js";import{u as oe}from"./useAdAuditLogs-BJvMVNgf.js";import{toast as v}from"./vendor-sonner-DPZ7yCFm.js";import{P as re}from"./PipelineBoard-D-1fsHv2.js";import{P as u}from"./PipelinePageShell-Cn_44rbF.js";import{P as ce,a as de}from"./PipelineSearchBar-DruuWJGT.js";import{a as xe,P as me,b as pe}from"./PipelineFilterMenu-BZUt_2jN.js";import{u as ue}from"./ConfirmProvider-DKdfbp7i.js";import{u as A,s as y}from"./usePersistedState-Cld4OPMb.js";import{u as he}from"./useRealtimePipeline-JlzHtAnI.js";import{u as be}from"./usePipelineSearch-DEHzh-zP.js";import{L as je,ae as _,bp as V,i as fe,N as h,a as ge,el as Ne,Y as q,aG as ve,X as ye,ax as we,aA as Ce,aC as ke}from"./vendor-icons-XRoHuOlC.js";import{A as Se}from"./index-T0bE9RkD.js";import"./vendor-charts-Cf-ruFca.js";import"./vendor-ui-utils-Bbg69IUi.js";import"./vendor-graph-Df3mHyA7.js";import"./vendor-supabase-UBwEW4nm.js";import"./vendor-helmet-C74EPpLp.js";import"./core.esm-26oV3C8c.js";import"./sortable.esm-jm1pLohw.js";import"./FiltroDeDatas-DFtmXfcQ.js";import"./useMediaQuery-DmKoq-cZ.js";import"./periodo-D0dm4jd0.js";import"./createReactComponent-LJ3Rzzl2.js";import"./IconCalendar-B44dlgS6.js";import"./IconChevronDown-BcsKoyPw.js";import"./IconCheck-2awuSAfX.js";import"./IconChevronLeft-CWWwj-5H.js";import"./IconChevronRight-_i4BDM-V.js";import"./useEscapeKey-BwRSuhVP.js";import"./useDebounce-KXyFDGIX.js";const d={new:{label:"Novo",dot:"bg-blue-500",bg:"bg-blue-50/30"},contacted:{label:"Contatado",dot:"bg-amber-500",bg:"bg-amber-50/30"},qualified:{label:"Qualificado",dot:"bg-purple-500",bg:"bg-purple-50/30"},won:{label:"Fechado",dot:"bg-emerald-500",bg:"bg-emerald-50/30"},lost:{label:"Perdido",dot:"bg-slate-400",bg:"bg-slate-50/50"}},Le=["new","contacted","qualified","won","lost"],ct=()=>{var R;const{currentPortal:r}=se(),{profile:w}=ae(),z=w==null?void 0:w.organization_id,{leads:c,loading:T,updateLeadStatus:b,refresh:C}=ie(r.id),k=o.useCallback(()=>C(),[C]);he({table:"ad_leads",filter:`portal_id=eq.${r.id}`,channelKey:`ad_leads-${r.id}`,onInsert:k,onUpdate:k,onDelete:k});const{upsertAccount:G}=ne(r.id,void 0,z),{logActivity:K}=oe(r.id,z),[Q,M,U]=be(),[j,P]=A(y("pipeline:ads-leads:filters",r.id),{}),[l,x]=o.useState(null),[m,D]=A(y("pipeline:ads-leads:view",r.id),"list"),[$,O]=A(y("pipeline:ads-leads:density",r.id),"comfortable"),[X,S]=o.useState(new Set),Y=ue(),E=async(t,a)=>{for(const s of a)await b(s.id,t);S(new Set),v.success(`${a.length} lead(s) movido(s)`)},J=async t=>{if(!await Y({title:"Excluir leads",message:`Excluir ${t.length} lead(s)?`,variant:"danger",confirmLabel:"Excluir tudo"}))return;const s=t.map(n=>n.id),{error:i}=await le.from("ad_leads").delete().in("id",s);if(i){v.error("Erro ao excluir");return}S(new Set),v.success(`${s.length} lead(s) excluÃdo(s)`),C()},W=[{label:"Qualificar",icon:e.jsx(h,{size:12}),onClick:t=>E("qualified",t)},{label:"Marcar Perdido",onClick:t=>E("lost",t)},{label:"Excluir",onClick:J,variant:"danger"}],f=o.useMemo(()=>{const t=c.filter(n=>n.status==="new").length,a=c.filter(n=>n.status==="contacted"||n.status==="qualified").length,s=c.filter(n=>n.status==="won").length,i=c.filter(n=>n.status==="lost").length;return{newCount:t,negotiating:a,won:s,lost:i,total:c.length}},[c]),g=o.useMemo(()=>[{id:"status",label:"Status",type:"multiselect",derive:t=>{const a=new Map;return t.forEach(s=>a.set(s.status,(a.get(s.status)||0)+1)),Array.from(a.entries()).map(([s,i])=>{var n,F;return{value:s,label:((n=d[s])==null?void 0:n.label)??s,count:i,dotClass:(F=d[s])==null?void 0:F.dot}})}},{id:"company",label:"Empresa",type:"multiselect",derive:t=>{const a=new Map;return t.forEach(s=>{const i=s.company||"__none__";a.set(i,(a.get(i)||0)+1)}),Array.from(a.entries()).sort((s,i)=>i[1]-s[1]).slice(0,20).map(([s,i])=>({value:s,label:s==="__none__"?"Sem empresa":s,count:i}))}}],[]),I=o.useMemo(()=>({status:t=>t.status,company:t=>t.company||"__none__"}),[]),p=o.useMemo(()=>{const t=M.toLowerCase(),a=xe(c,g,j,I);return t?a.filter(s=>s.name.toLowerCase().includes(t)||s.email.toLowerCase().includes(t)||(s.company||"").toLowerCase().includes(t)):a},[c,M,g,j,I]),L=async t=>{await G({name:t.company||t.name,status:"active",type:"direct",portal_id:r.id}
1)&&(await K({action:"Lead Convertido em Conta",entity_type:"lead",entity_id:t.id,details:{company:t.company||t.name}}),await b(t.id,"won",`Convertido em conta: ${new Date().toLocaleDateString()}`),v.success("Lead convertido em cliente!"),x(null))},N=o.useCallback(async(t,a)=>{await b(t,a)},[b]),Z=o.useCallback(t=>t.status,[]),H=o.useCallback(t=>t.id,[]),ee=o.useCallback((t,a)=>N(t,a),[N]),te=o.useMemo(()=>Le.map(t=>({id:t,label:d[t].label,dotClass:d[t].dot})),[]);return e.jsxs(u,{title:"Contatos & Leads",subtitle:"Pipeline comercial de anunciantes interessados.",density:m==="kanban"?$:void 0,onDensityChange:m==="kanban"?O:void 0,metrics:e.jsxs(e.Fragment,{children:[e.jsx(u.Metric,{label:"Total",value:f.total}),e.jsx(u.Metric,{label:"Novos",value:f.newCount,dot:"bg-blue-500"}),e.jsx(u.Metric,{label:"Negociando",value:f.negotiating,dot:"bg-amber-500"}),e.jsx(u.Metric,{label:"Fechados",value:f.won,dot:"bg-emerald-500",valueClassName:"text-emerald-600"})]}),actions:e.jsxs("div",{className:"flex items-center gap-1.5 border-r border-slate-200 pr-3 mr-1",children:[e.jsx("button",{onClick:()=>D("list"),className:`p-1.5 rounded-md transition-colors ${m==="list"?"bg-slate-100 text-slate-800":"text-slate-400 hover:bg-slate-50"}`,title:"Visão de tabela","aria-label":"Visão de tabela",children:e.jsx(Ce,{size:14})}),e.jsx("button",{onClick:()=>D("kanban"),className:`p-1.5 rounded-md transition-colors ${m==="kanban"?"bg-slate-100 text-slate-800":"text-slate-400 hover:bg-slate-50"}`,title:"Visão de pipeline","aria-label":"Visão de pipeline",children:e.jsx(ke,{size:14})})]}),toolbar:e.jsx(ce,{viewMode:m==="list"?"list":"kanban",search:e.jsx(de,{value:Q,onChange:U,placeholder:"Buscar por nome, email ou empresa...",ariaLabel:"Buscar por nome, email ou empresa",totalMatches:p.length,totalItems:c.length,recentKey:y("pipeline:ads-leads:recent-search",r.id)}),filters:e.jsx(pe,{filters:g,state:j,onChange:P,items:c}),filterChips:e.jsx(me,{filters:g,state:j,onChange:P,items:c})}),toolbarHint:`${p.length} leads`,children:[T?e.jsxs("div",{className:"h-full flex flex-col items-center justify-center gap-3",children:[e.jsx(je,{className:"w-5 h-5 text-slate-300 animate-spin"}),e.jsx("p",{className:"text-[13px] text-slate-400",children:"Carregando leads..."})]}):m==="list"?e.jsx("div",{className:"flex-1 overflow-y-auto",children:e.jsxs("table",{className:"w-full text-left border-collapse",children:[e.jsx("thead",{className:"sticky top-0 z-10 bg-slate-50/80 backdrop-blur-sm",children:e.jsxs("tr",{className:"border-b border-slate-200/80",children:[e.jsx("th",{className:"px-8 py-2.5 text-[12px] font-medium text-slate-400",children:"Contato"}),e.jsx("th",{className:"px-4 py-2.5 text-[12px] font-medium text-slate-400",children:"Empresa"}),e.jsx("th",{className:"px-4 py-2.5 text-[12px] font-medium text-slate-400",children:"Assunto"}),e.jsx("th",{className:"px-4 py-2.5 text-[12px] font-medium text-slate-400",children:"Data"}),e.jsx("th",{className:"px-4 py-2.5 text-[12px] font-medium text-slate-400 text-center",children:"Status"}),e.jsx("th",{className:"px-8 py-2.5 text-[12px] font-medium text-slate-400 text-right",children:"Ações"})]})}),e.jsx("tbody",{className:"text-[13px]",children:p.length>0?p.map(t=>{var s;const a=d[t.status]||d.new;return e.jsxs("tr",{onClick:()=>x(t),className:"border-b border-slate-100 hover:bg-slate-50/50 transition-colors group cursor-pointer",children:[e.jsx("td",{className:"px-8 py-3",children:e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-medium text-slate-800 truncate",children:t.name}),e.jsxs("div",{className:"flex items-center gap-3 text-[11px] text-slate-400 mt-0.5",children:[e.jsxs("span",{className:"flex items-center gap-1 truncate",children:[e.jsx(_,{size:10,className:"flex-shrink-0"})," ",t.email]}),t.phone&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(V,{size:10,className:"flex-shrink-0"})," ",t.phone]})]})]})}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:"text-[13px] text-slate-600",children:t.company||e.jsx("span",{className:"text-slate-300",children:" -"})})}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:`text-[12px] px-2 py-0.5 rounded font-medium truncate max-w-[180px] inline-block ${(s=t.subject)!=null&&s.includes("[RESERVA]")?"bg-indigo-50 text-indigo-600":"text-slate-500"}`,children:t.subject||"Dúvida Geral"})}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:"text-[12px] text-slate-400 tabular-nums",children:new Date(t.created_at).toLocaleDateString("pt-BR")})}),e.jsx("td",{className:"px-4 py-3 text-center",onClick:i=>i.stopPropagation(),children:e.jsxs("div",{className:"relative inline-block",children:[e.jsxs("select",{value:t.status,onChange:i=>N(t.id,i.target.value),className:"appearance-none text-[12px] font-medium pl-4 pr-6 py-1 rounded-full border border-slate-200 bg-white text-slate-600 focus:outline-none focus:border-slate-400 cursor-pointer transition-colors",children:[e.jsx("option",{value:"new",children:"Novo"}),e.jsx("option",{value:"contacted",children:"Contatado"}
1),e.jsx("option",{value:"qualified",children:"Qualificado"}),e.jsx("option",{value:"won",children:"Fechado"}),e.jsx("option",{value:"lost",children:"Perdido"})]}),e.jsx("span",{className:`absolute left-1.5 top-1/2 -translate-y-1/2 w-1.5 h-1.5 rounded-full ${a.dot}`}),e.jsx(fe,{size:10,className:"absolute right-1.5 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none"})]})}),e.jsx("td",{className:"px-8 py-3 text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1 opacity-0 group-hover:opacity-100 transition-opacity",children:[t.status!=="won"&&e.jsx("button",{onClick:i=>{i.stopPropagation(),L(t)},className:"p-1.5 text-slate-400 hover:text-emerald-600 rounded-lg hover:bg-emerald-50 transition-colors",title:"Converter em Conta",children:e.jsx(h,{size:14})}),e.jsx(ge,{size:14,className:"text-slate-300"})]})})]},t.id)}):e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"px-8 py-16 text-center text-[13px] text-slate-400",children:"Nenhum lead encontrado."})})})]})}):e.jsx(re,{portalId:r.id,columns:te,items:p,density:$,maxVisiblePerColumn:30,selectable:!0,selectedItems:X,onSelectionChange:S,bulkActions:W,getColumnId:Z,getItemId:H,onMove:ee,dragHandle:"grip",emptyColumnNode:e.jsxs("div",{className:"h-28 border border-dashed border-slate-200 rounded-lg flex flex-col items-center justify-center gap-2 text-slate-300",children:[e.jsx(ve,{size:18}),e.jsx("p",{className:"text-[10px] font-medium text-center px-4",children:"Sem leads"})]}),renderCard:(t,{isDragging:a,dragHandleProps:s})=>{var i;return e.jsxs("div",{onClick:()=>x(t),className:`bg-white rounded-lg border border-slate-200/80 p-3.5 cursor-pointer group transition-all hover:border-slate-300 hover:shadow-sm ${a?"shadow-lg border-blue-300":""}`,children:[e.jsxs("div",{className:"flex items-start gap-2 mb-2",children:[e.jsx("div",{ref:s==null?void 0:s.ref,...(s==null?void 0:s.listeners)??{},onClick:n=>n.stopPropagation(),className:"mt-0.5 text-slate-200 opacity-0 group-hover:opacity-100 transition-opacity flex-shrink-0 cursor-grab touch-none","aria-label":"Arrastar",children:e.jsx(Ne,{size:12})}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"text-[13px] font-medium text-slate-800 leading-snug truncate",children:t.name}),e.jsxs("p",{className:"text-[11px] text-slate-400 truncate mt-0.5 flex items-center gap-1",children:[e.jsx(_,{size:9,className:"flex-shrink-0"})," ",t.email]})]})]}),t.company&&e.jsxs("p",{className:"text-[11px] text-slate-500 truncate flex items-center gap-1 mb-1.5",children:[e.jsx(q,{size:9,className:"flex-shrink-0 text-slate-300"})," ",t.company]}),t.subject&&e.jsx("span",{className:`text-[10px] px-1.5 py-0.5 rounded font-medium truncate max-w-full inline-block ${(i=t.subject)!=null&&i.includes("[RESERVA]")?"bg-indigo-50 text-indigo-600":"bg-slate-50 text-slate-500"}`,children:t.subject}),e.jsxs("div",{className:"flex items-center justify-between mt-2.5 pt-2 border-t border-slate-100/80",children:[e.jsx("span",{className:"text-[10px] text-slate-300 tabular-nums",children:new Date(t.created_at).toLocaleDateString("pt-BR")}),t.status!=="won"&&e.jsx("button",{onClick:n=>{n.stopPropagation(),L(t)},className:"p-1 text-slate-300 hover:text-emerald-600 rounded opacity-0 group-hover:opacity-100 transition-all hover:bg-emerald-50",title:"Converter em Conta",children:e.jsx(h,{size:11})})]})]})}}),e.jsx(Se,{children:l&&e.jsxs(e.Fragment,{children:[e.jsx(B.div,{initial:{opacity:0}
1,animate:{opacity:1},exit:{opacity:0},onClick:()=>x(null),className:"fixed inset-0 z-40 bg-slate-900/20 backdrop-blur-[2px]"}),e.jsxs(B.div,{initial:{x:"100%"},animate:{x:0},exit:{x:"100%"},transition:{type:"spring",damping:30,stiffness:300},className:"fixed right-0 top-0 bottom-0 z-50 w-full max-w-md bg-white border-l border-slate-200 shadow-2xl flex flex-col",children:[e.jsxs("div",{className:"px-6 py-5 border-b border-slate-200/80 flex items-start justify-between flex-shrink-0",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h3",{className:"text-[15px] font-semibold text-slate-900 truncate",children:l.name}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("span",{className:`w-1.5 h-1.5 rounded-full ${(d[l.status]||d.new).dot}`}),e.jsx("span",{className:"text-[11px] text-slate-500 font-medium",children:(d[l.status]||d.new).label})]})]}),e.jsx("p",{className:"text-[12px] text-slate-400 mt-0.5",children:l.company||"Pessoa FÃsica"})]}),e.jsx("button",{onClick:()=>x(null),className:"p-1.5 text-slate-400 hover:text-slate-700 hover:bg-slate-100 rounded-lg transition-colors",children:e.jsx(ye,{size:16})})]}),e.jsxs("div",{className:"flex-1 overflow-y-auto p-6 space-y-5",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx("h4",{className:"text-[12px] font-semibold text-slate-500",children:"Informações de contato"}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-3 text-[13px]",children:[e.jsx(_,{size:14,className:"text-slate-300 flex-shrink-0"}),e.jsx("a",{href:`mailto:${l.email}`,className:"text-slate-700 hover:text-slate-900 hover:underline break-all",children:l.email})]}),l.phone&&e.jsxs("div",{className:"flex items-center gap-3 text-[13px]",children:[e.jsx(V,{size:14,className:"text-slate-300 flex-shrink-0"}),e.jsx("a",{href:`tel:${l.phone}`,className:"text-slate-700 hover:text-slate-900 hover:underline",children:l.phone})]}),l.company&&e.jsxs("div",{className:"flex items-center gap-3 text-[13px]",children:[e.jsx(q,{size:14,className:"text-slate-300 flex-shrink-0"}),e.jsx("span",{className:"text-slate-700",children:l.company})]}),e.jsxs("div",{className:"flex items-center gap-3 text-[13px]",children:[e.jsx(we,{size:14,className:"text-slate-300 flex-shrink-0"}),e.jsxs("span",{className:"text-slate-500 tabular-nums",children:[new Date(l.created_at).toLocaleDateString("pt-BR")," à s"," ",new Date(l.created_at).toLocaleTimeString("pt-BR",{hour:"2-digit",minute:"2-digit"})]})]})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("h4",{className:"text-[12px] font-semibold text-slate-500",children:"Assunto"}),e.jsx("span",{className:`text-[13px] font-medium inline-block px-2.5 py-1 rounded ${(R=l.subject)!=null&&R.includes("[RESERVA]")?"bg-indigo-50 text-indigo-600":"bg-slate-50 text-slate-600"}`,children:l.subject||"Dúvida Geral"})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("h4",{className:"text-[12px] font-semibold text-slate-500",children:"Mensagem"}),e.jsx("div",{className:"text-[13px] text-slate-700 whitespace-pre-wrap leading-relaxed bg-slate-50 p-4 rounded-lg border border-slate-200/80",children:l.message?l.message.split(" | ").map((t,a)=>{const s=t.split(": ");return e.jsxs("div",{className:"mb-1.5 last:mb-0",children:[e.jsxs("span",{className:"font-semibold text-slate-800",children:[s[0],":"]})," ",e.jsx("span",{className:"text-slate-600",children:s.slice(1).join(": ")})]},a)}):e.jsx("span",{className:"text-slate-400 italic",children:"Sem mensagem."})})]}),l.notes&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("h4",{className:"text-[12px] font-semibold text-slate-500",children:"Notas internas"}),e.jsx("p",{className:"text-[13px] text-slate-600 bg-amber-50/60 p-3 rounded-lg border border-amber-100",children:l.notes})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("h4",{className:"text-[12px] font-semibold text-slate-500",children:"Alterar status"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:Object.entries(d).map(([t,{label:a,dot:s}])=>e.jsxs("button",{onClick:()=>{N(l.id,t),x({...l,status:t})},className:`px-3 py-1.5 rounded-lg text-[12px] font-medium border transition-colors flex items-center gap-1.5 ${l.status===t?"bg-slate-900 text-white border-slate-900":"bg-white text-slate-500 border-slate-200 hover:border-slate-300"}`,children:[e.jsx("span",{className:`w-1.5 h-1.5 rounded-full ${l.status===t?"bg-white":s}`}),a]},t))})]})]}),e.jsxs("div",{className:"px-6 py-4 border-t border-slate-200/80 flex items-center justify-between flex-shrink-0",children:[l.status!=="won"?e.jsxs("button",{onClick:()=>L(l),className:"px-4 py-1.5 bg-emerald-600 text-white rounded-lg text-[13px] font-medium hover:bg-emerald-700 transition-colors flex items-center gap-1.5",children:[e.jsx(h,{size:14}),"Converter em Conta"]}):e.jsxs("span",{className:"text-[12px] text-emerald-600 font-medium flex items-center gap-1.5",children:[e.jsx(h,{size:14})," Convertido em cliente"]}),e.jsx("button",{onClick:()=>x(null),className:"px-3 py-1.5 text-[13px] text-slate-500 font-medium hover:text-slate-700 transition-colors",children:"Fechar"})]})]})]})})]})};export{ct as AdsLeads};
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.