1import{e as R,r as $,f as k,u as A,A as de,w as ce,j as e,t as c}from"./index-ypY3K6D-.js";import{r as t,bL as me,z as ue,k as K,a as D,y as xe,x as pe,a4 as ge,m as fe,a8 as he}from"./icons-Dlea1YgH.js";const je=R({method:"GET"}).middleware([$]).handler(k("14ec7dce7d81f0f755c67a3db05eff6c9fc236c422a5aa3f2e8ba05cef58cf54")),be=R({method:"POST"}).middleware([$]).handler(k("9bbd823856f6751d1e157b69de09bcdb258b32d5454c1a8f3285939dcdd26c36")),Ne=R({method:"POST"}).middleware([$]).handler(k("9f2498548d6b3998b77be54ad70d0948dc0cdb89230a643ac06bdd32a0413b99")),ve=R({method:"POST"}).middleware([$]).handler(k("0cab12b6293f5940cf91b699897e7305248bdf03145794a25bdff06f9596754f")),ye=[{key:"media_mensal_cents",label:"Média/mês ativo"},{key:"saldo_cents",label:"Saldo parado"},{key:"recarregou_cents",label:"Recarregou (PIX)"},{key:"dias_parado",label:"Dias parado"},{key:"ultimo_contato",label:"Ãltimo contato"}],f=s=>(s/100).toLocaleString("pt-BR",{style:"currency",currency:"BRL"}),l="w-full rounded-xl border border-border bg-card/60 p-2.5 text-sm text-foreground outline-none focus:border-emerald/40 focus:ring-2 focus:ring-emerald/20";function Se(){const s=A(je),m=A(be),q=A(Ne),h=A(ve),F=de(),{data:i,isLoading:X,refetch:z,isFetching:J}=ce({queryKey:["admin","recuperacao"],queryFn:()=>s()}),[u,j]=t.useState("");t.useEffect(()=>{i?.template&&!u&&j(i.template)},[i?.template]);const[x,U]=t.useState("nunca"),[b,H]=t.useState("14"),[N,Y]=t.useState(""),[v,Z]=t.useState(""),[M,ee]=t.useState(!1),[p,ae]=t.useState("media_mensal_cents"),[I,te]=t.useState({}),[y,_]=t.useState(null),[w,se]=t.useState("Reconquistar clientes que recarregaram, consumiram saldo e pararam de usar. Quero entender o motivo, me colocar à disposição e gerar conexão pessoal."),[L,re]=t.useState("Próximo, humano, sem parecer marketing massivo"),[B,oe]=t.useState(""),[E,ne]=t.useState("Gustavo â Consulta.ai"),[Q,V]=t.useState(!1);async function le(){if(!w.trim()){c.error("Descreva o objetivo da mensagem.");return}V(!0);try{const a=await h({data:{objetivo:w.trim(),tom:L.trim()||void 0,oferta:B.trim()||void 0,assinatura:E.trim()||void 0}});j(a.template),c.success("Mensagem gerada! Revise antes de usar.")}catch(a){c.error(a?.message??"Falha ao gerar mensagem")}finally{V(!1)}}const P=t.useMemo(()=>{const a=b.trim()===""?null:Number(b),r=N.trim()===""?null:Number(N),d=v.trim()===""?null:Math.round(Number(v.replace(",","."))*100),T=(i?.clients??[]).filter(o=>{if(o.nao_contatar&&!M||x==="nunca"&&o.ultimo_contato||x==="contatado"&&!o.ultimo_contato||d!==null&&o.recarregou_cents<d)return!1;if(!o.ultimo_contato){const S=o.dias_parado??1/0;if(a!==null&&S<a||r!==null&&S>r)return!1}return!0}),C=o=>p==="ultimo_contato"?o.ultimo_contato?new Date(o.ultimo_contato.em).getTime():-1/0:p==="dias_parado"?o.dias_parado??1/0:o[p];return T.sort((o,S)=>C(S)-C(o))},[i?.clients,x,b,N,v,M,p]);function W(a){return u.replaceAll("{primeiro_nome}",a.primeiro_nome).replaceAll("{empresa}",a.empresa).replaceAll("{saldo}",(a.saldo_cents/100).toLocaleString("pt-BR",{minimumFractionDigits:2,maximumFractionDigits:2}))}async function ie(a){_(a.client_id);try{const r=await m({data:{client_id:a.client_id,message:W(a)}});c.success(r.already?`${a.empresa}: contato deste mês já estava registrado.`:`Contato com ${a.empresa} registrado.`),await F.invalidateQueries({queryKey:["admin","recuperacao"]})}catch(r){c.error(r?.message??"Falha ao registrar contato")}finally{_(null)}}async function G(a){const r=!a.nao_contatar;if(!(r&&!window.confirm(`Marcar ${a.empresa} como "não contatar"? Ele sai da lista.`))){_(a.client_id);try{await q({data:{client_id:a.client_id,on:r}}),c.success(r?`${a.empresa} não será mais listado.`:`${a.empresa} voltou para a lista.`),await F.invalidateQueries({queryKey:["admin","recuperacao"]})}catch(d){c.error(d?.message??"Falha ao salvar")}finally{_(null)}}}const n=i?.resumo,O=i?.janela_dias??14;return e.jsxs("div",{className:"mx-auto max-w-6xl space-y-6",children:[e.jsxs("header",{className:"relative overflow-hidden rounded-3xl mesh-ambient glass-strong p-6 md:p-8",children:[e.jsxs("p",{className:"eyebrow text-muted-foreground inline-flex items-center gap-2",children:[e.jsx(me,{className:"h-3 w-3"})," Recuperação"]}),e.jsx("h1",{className:"mt-2 text-3xl font-bold tracking-tight text-foreground",children:"Recuperação de clientes"}),e.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:"Clientes que pagaram e usaram. Defina o público, comece pelos mais relevantes e fale com cada um pelo seu WhatsApp. O sistema registra o contato e mede quem voltou."})]}),e.jsxs("section",{className:"rounded-2xl glass-strong p-5 space-y-3",children:[e.jsxs("h2",{className:"inline-flex items-center gap-2 text-sm font-semibold text-foreground",children:[e.jsx(ue,{className:"h-4 w-4 text-emerald"})," Resultado dos contatos · últimos ",i?.placar_dias??90," dias"]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3 md:grid-cols-5",children:[e.jsx(g,{titulo:"Contatados",valor:n?String(n.contatados):"â"}),e.jsx(g,{titulo:"Voltaram",valor:n?String(n.voltaram):"â"}),e.jsx(g,{titulo:`Aguardando (${O}d)`,valor:n?String(n.aguardando):"â"}),e.jsx(g,{titulo:"Taxa de volta",valor:n?.taxaPct!=null?`${n.taxaPct.toLocaleStr
1ing("pt-BR")}%`:"â"}),e.jsx(g,{titulo:"Recarregado após contato",valor:n?f(n.recargaCents):"â"})]}),e.jsxs("p",{className:"text-[11px] text-muted-foreground",children:["Voltou após o contato = consulta cobrada ou recarga PIX em até ",O," dias depois do contato. Mede o que aconteceu depois, não prova que foi por causa do contato. A taxa só conta contatos com a janela já fechada."]})]}),e.jsxs("section",{className:"rounded-2xl glass-strong p-5 space-y-4",children:[e.jsxs("div",{children:[e.jsxs("h2",{className:"inline-flex items-center gap-2 text-sm font-semibold text-foreground",children:[e.jsx(K,{className:"h-4 w-4 text-emerald"})," Criar mensagem com IA"]}),e.jsxs("p",{className:"mt-1 text-xs text-muted-foreground",children:["A IA escreve um rascunho com as variáveis ",e.jsx("code",{children:"{primeiro_nome}"}),", ",e.jsx("code",{children:"{empresa}"}),","," ",e.jsx("code",{children:"{saldo}"}),". Você revisa antes de usar."]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[e.jsxs("label",{className:"md:col-span-2 text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Objetivo da mensagem"}),e.jsx("textarea",{value:w,onChange:a=>se(a.target.value),rows:3,className:l})]}),e.jsxs("label",{className:"text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Tom / estilo"}),e.jsx("input",{value:L,onChange:a=>re(a.target.value),className:l})]}),e.jsxs("label",{className:"text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Oferta / benefÃcio (opcional)"}),e.jsx("input",{value:B,onChange:a=>oe(a.target.value),placeholder:"Só o que você vai cumprir",className:l})]}),e.jsxs("label",{className:"md:col-span-2 text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Assinatura"}),e.jsx("input",{value:E,onChange:a=>ne(a.target.value),className:l})]})]}),e.jsxs("button",{onClick:le,disabled:Q||!w.trim(),className:"inline-flex items-center gap-2 rounded-full bg-emerald px-4 py-2 text-sm font-semibold text-white hover:bg-emerald/90 disabled:opacity-50",children:[Q?e.jsx(D,{className:"h-4 w-4 animate-spin"}):e.jsx(K,{className:"h-4 w-4"}),u.trim()?"Regerar mensagem":"Gerar mensagem"]})]}),e.jsxs("section",{className:"rounded-2xl glass-strong p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-2",children:[e.jsx("h2",{className:"text-sm font-semibold text-foreground",children:"Mensagem"}),e.jsx("button",{onClick:()=>j(i?.template??""),className:"rounded-full border border-border px-3 py-1 text-xs text-muted-foreground hover:text-foreground",children:"Restaurar padrão"})]}),e.jsx("textarea",{value:u,onChange:a=>j(a.target.value),rows:10,className:`${l} font-mono`})]}),e.jsxs("section",{className:"rounded-2xl glass-strong p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-2",children:[e.jsxs("h2",{className:"inline-flex items-center gap-2 text-sm font-semibold text-foreground",children:[e.jsx(xe,{className:"h-4 w-4 text-emerald"})," Público"]}),e.jsxs("button",{onClick:()=>z(),className:"rounded-full border border-border px-3 py-1 text-xs text-muted-foreground hover:text-foreground inline-flex items-center gap-1",children:[e.jsx(pe,{className:`h-3 w-3 ${J?"animate-spin":""}`})," Atualizar"]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3 md:grid-cols-5",children:[e.jsxs("label",{className:"text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Situação"}),e.jsxs("select",{value:x,onChange:a=>U(a.target.value),className:l,children:[e.jsx("option",{value:"nunca",children:"Nunca contatados"}),e.jsx("option",{value:"contatado",children:"Já contatados"}),e.jsx("option",{value:"todos",children:"Todos"})]})]}),e.jsxs("label",{className:"text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Parado há (mÃn. dias)"}),e.jsx("input",{type:"number",min:0,value:b,onChange:a=>H(a.target.value),className:l})]}),e.jsxs("label",{className:"text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Parado há (máx. dias)"}),e.jsx("input",{type:"number",min:0,value:N,onChange:a=>Y(a.target.value),placeholder:"sem limite",className:l})]}),e.jsxs("label",{className:"text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Recarregou (mÃn. R$)"}
1),e.jsx("input",{value:v,onChange:a=>Z(a.target.value),placeholder:"0,00",className:l})]}),e.jsxs("label",{className:"text-xs text-muted-foreground space-y-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Ordenar por"}),e.jsx("select",{value:p,onChange:a=>ae(a.target.value),className:l,children:ye.map(a=>e.jsx("option",{value:a.key,children:a.label},a.key))})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-2 text-xs text-muted-foreground",children:[e.jsxs("span",{children:[e.jsx("strong",{className:"text-foreground",children:P.length})," no público",x!=="nunca"&&" · dias parado só recorta quem ainda não foi contatado"]}),e.jsxs("label",{className:"inline-flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",checked:M,onChange:a=>ee(a.target.checked)}),'Mostrar "não contatar"']})]})]}),X?e.jsx("div",{className:"flex h-48 items-center justify-center",children:e.jsx(D,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):e.jsx("section",{className:"rounded-2xl glass-strong p-2 overflow-hidden",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"text-xs text-muted-foreground",children:e.jsxs("tr",{className:"border-b border-border",children:[e.jsx("th",{className:"px-3 py-2 text-left font-medium",children:"Empresa"}),e.jsx("th",{className:"px-3 py-2 text-right font-medium",title:"Consumo ÷ meses entre a primeira e a última consulta",children:"Média/mês ativo"}),e.jsx("th",{className:"px-3 py-2 text-right font-medium",children:"Recarregou (PIX)"}),e.jsx("th",{className:"px-3 py-2 text-right font-medium",children:"Saldo"}),e.jsx("th",{className:"px-3 py-2 text-right font-medium",children:"Parado"}),e.jsx("th",{className:"px-3 py-2 text-left font-medium",children:"Contato"}),e.jsx("th",{className:"px-3 py-2 text-right font-medium",children:"Ação"})]})}),e.jsxs("tbody",{children:[P.map(a=>{const r=W(a),d=!!a.wa_phone&&!a.nao_contatar&&!!u.trim();return e.jsxs("tr",{className:"border-b border-border/40 last:border-0",children:[e.jsxs("td",{className:"px-3 py-2 align-top",children:[e.jsx("div",{className:"font-medium text-foreground",children:a.empresa}),e.jsxs("div",{className:"text-[11px] text-muted-foreground",children:[a.qtd_consultas," consultas"]}),e.jsxs("details",{className:"mt-1",children:[e.jsx("summary",{className:"cursor-pointer text-[11px] text-muted-foreground hover:text-foreground",children:"ver mensagem"}),e.jsx("pre",{className:"mt-1 max-w-md whitespace-pre-wrap rounded-md bg-muted/40 p-2 text-[11px] text-foreground",children:r})]})]}),e.jsx("td",{className:"px-3 py-2 align-top text-right tabular-nums",children:f(a.media_mensal_cents)}),e.jsx("td",{className:"px-3 py-2 align-top text-right tabular-nums",children:f(a.recarregou_cents)}),e.jsx("td",{className:"px-3 py-2 align-top text-right tabular-nums",children:f(a.saldo_cents)}),e.jsx("td",{className:"px-3 py-2 align-top text-right text-muted-foreground",children:a.dias_parado!=null?`${a.dias_parado}d`:"â"}),e.jsx("td",{className:"px-3 py-2 align-top",children:e.jsx(_e,{c:a,janela:O})}),e.jsx("td",{className:"px-3 py-2 align-top",children:e.jsx("div",{className:"flex flex-col items-end gap-1",children:a.nao_contatar?e.jsx("button",{onClick:()=>G(a),disabled:y===a.client_id,className:"rounded-full border border-border px-3 py-1 text-xs text-muted-foreground hover:text-foreground disabled:opacity-40",children:"Voltar à lista"}):e.jsxs(e.Fragment,{children:[e.jsxs("a",{href:d?`https://wa.me/${a.wa_phone}?text=${encodeURIComponent(r)}`:void 0,target:"_blank",rel:"noreferrer","aria-disabled":!d,onClick:T=>{if(!d){T.preventDefault();return}te(C=>({...C,[a.client_id]:!0}))},className:`inline-flex items-center gap-1 rounded-full bg-emerald px-3 py-1 text-xs font-semibold text-white hover:bg-emerald/90 ${d?"":"pointer-events-none opacity-40"}`,children:[e.jsx(ge,{className:"h-3 w-3"})," ",a.wa_phone?"Abrir WhatsApp":"Sem WhatsApp"]}),e.jsxs("button",{onClick:()=>ie(a),disabled:!I[a.client_id]||y===a.client_id,title:I[a.client_id]?"":"Abra o WhatsApp primeiro",className:"inline-flex items-center gap-1 rounded-full border border-border px-3 py-1 text-xs font-semibold text-foreground hover:bg-muted disabled:opacity-40",children:[y===a.client_id?e.jsx(D,{className:"h-3 w-3 animate-spin"}):e.jsx(fe,{className:"h-3 w-3"}),"Marquei contato"]}),e.jsxs("button",{onClick:()=>G(a),disabled:y===a.client_id,className:"inline-flex items-center gap-1 text-[11px] text-muted-foreground hover:text-destructive disabled:opacity-40",children:[e.jsx(he,{className:"h-3 w-3"})," Não contatar"]})]})})})]},a.client_id)}),!P.length&&e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"p-6 text-center text-muted-foreground",children:"Nenhum cliente neste público."})})]})]})})})]})}function g({titulo:s,valor:m}){return e.jsxs("div",{className:"rounded-xl bg-muted/40 p-3",children:[e.jsx("div",{className:"text-[11px] font-medium uppercase tracking-wider text-muted-foreground",children:s}),e.jsx("div",{className:"mt-1 text-xl font-bold tabular-nums text-foreground",children:m})]})}function _e({c:s,janela:m}){if(s.nao_contatar)return e.jsx("span",{className:"rounded-full bg-destructive/15 px-2 py-0.5 text-[11px] font-medium text-destructive",children:"Não contatar"});if(!s.ultimo_contato)return e.jsx("span",{className:"text-[11px] text-muted-foreground",children:"Nunca contatado"});const q=new Date(s.ultimo_contato.em).toLocaleDateString("pt-BR"),h=s.resultado==="voltou"?{label:`Voltou em ${s.dias_ate_voltar}d${s.recarga_apos_contato_cents?` · ${f(s.recarga_apos_contato_cents)}`:""}`,cls:"bg-emerald/15 text-emerald"}:s.resultado==="nao_voltou"?{label:`Não voltou em ${m}d`,cls:"bg-muted text-muted-foreground"}:{label:"Aguardando",cls:"bg-amber-500/15 text-amber-600"};return e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"text-[11px] text-muted-foreground",children:[q," · ",s.ultimo_contato.canal==="pessoal"?"pessoal":"número do sistema"]}),e.jsx("span",{className:`inline-flex rounded-full px-2 py-0.5 text-[11px] font-medium ${h.cls}`,children:h.label})]})}
1export{Se as component};
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.