import{r,ba as o,B as j}from"./createLucideIcon-BcINIXoS.js";import{C,R as z}from"./Container-DAwH21q9.js";import{I as b}from"./index-ChG3lseq.js";import{F as L}from"./Table-Zgkg3btb.js";import"./InnerHeader-BcvyF-FL.js";import"../assets/admin-D0g83suT.js";import"./Input-gYODSeX_.js";import"./EyeOutlined-B40DaD3_.js";import"./SearchOutlined-B5_bI2oW.js";import"./addEventListener-DSPI5EF0.js";import"./Pagination-CT65wBFk.js";import"./LeftOutlined-Ci58k6gc.js";import"./index-DoQOayR8.js";const d=window.replyan_ai_admin||{},T=[{title:"Name",key:"name",render:(a,n)=>[n.first_name,n.last_name].filter(Boolean).join(" ")||"—"},{title:"Email",dataIndex:"email",key:"email",render:a=>a?o.jsx("a",{href:`mailto:${a}`,children:a}):"—"},{title:"Phone",dataIndex:"phone",key:"phone",render:a=>a||"—"},{title:"Address",key:"address",render:(a,n)=>{const s=[n.address,n.postal_code].filter(Boolean);return s.length>0?s.join(", "):"—"}},{title:"Date",dataIndex:"created_at",key:"created_at",render:a=>a?new Date(a).toLocaleDateString():"—"}],A=()=>{const[a,n]=r.useState([]),[s,h]=r.useState(!1),[i,p]=r.useState({current:1,pageSize:20,total:0}),[g,S]=r.useState(""),m=r.useRef(null),u=r.useCallback(async(e=1,c=20,f="")=>{h(!0);try{const t=new URLSearchParams;t.append("action","replyan_ai_get_leads"),t.append("nonce",d.messagesNonce),t.append("page",String(e)),t.append("per_page",String(c)),f&&t.append("search",f);const l=await(await fetch(d.ajaxUrl,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},credentials:"same-origin",body:t})).json();l.success&&(n(l.data.leads||[]),p(_=>({..._,current:l.data.page,total:l.data.total})))}catch(t){console.error("Failed to fetch leads",t)}finally{h(!1)}},[]);r.useEffect(()=>{u(1,i.pageSize,g)},[]);const x=e=>{p(c=>({...c,current:e.current,pageSize:e.pageSize})),u(e.current,e.pageSize,g)},w=e=>{S(e),m.current&&clearTimeout(m.current),m.current=setTimeout(()=>{p(c=>({...c,current:1})),u(1,i.pageSize,e)},300)},y=()=>{const e=new URL(d.ajaxUrl);e.searchParams.append("action","replyan_ai_export_leads"),e.searchParams.append("nonce",d.messagesNonce),window.location.href=e.toString()};return o.jsxs(C,{title:"Leads",className:"leads",description:"View and manage captured leads from conversations.",children:[o.jsxs("div",{className:"cai-flex cai-justify-end cai-gap-3 cai-mb-4",children:[o.jsx(b.Search,{placeholder:"Search by name or email",allowClear:!0,onChange:e=>w(e.target.value),className:"cai-max-w-xs"}),o.jsx(j,{icon:o.jsx(z,{}),onClick:y,children:"Export CSV"})]}),o.jsx(L,{columns:T,dataSource:a,rowKey:"id",loading:s,pagination:{current:i.current,pageSize:i.pageSize,total:i.total,showSizeChanger:!0,pageSizeOptions:["10","20","50","100"],showTotal:e=>`Total ${e} leads`},onChange:x})]})};export{A as default};
