1import{r,j as e,L as N}from"./react-vendor-C-_weJGF.js";import{b as E,u as D,c as q}from"./query-DA-B5yju.js";import{B as P,s as v,a as w}from"./index-DiEum5Ju.js";import{A as R}from"./AdminLayout-Byts1X3Z.js";import{I as T}from"./input-TSEJWV9X.js";import{L as x,a as $,b as Q}from"./leads-B1C5z5Hw.js";import{aS as O,az as z,aT as B,l as F,aU as I}from"./icons-BZJMmL1V.js";import{f as K}from"./format-Cpjm9erV.js";import"./radix-CMA3xQgM.js";import"./supabase-BppBjPB2.js";import"./motion-CfmjrLVm.js";import"./logo-BWhOaj1e.js";const G={New:"bg-primary/15 text-primary",Contacted:"bg-blue-500/15 text-blue-400",Qualified:"bg-amber-500/15 text-amber-400","Proposal Sent":"bg-purple-500/15 text-purple-400",Won:"bg-emerald-500/15 text-emerald-400",Lost:"bg-muted text-muted-foreground"};function g({status:n}){return e.jsx("span",{className:`inline-flex items-center text-[11px] font-medium px-2 py-0.5 rounded-full ${G[n]??"bg-muted text-muted-foreground"}`,children:n})}function de(){const n=E(),[o,C]=r.useState("dashboard"),[p,S]=r.useState(""),[c,L]=r.useState("all"),[i,k]=r.useState("all"),[m,A]=r.useState("all"),[h,_]=r.useState(!1),{data:b=[],isLoading:j}=D({queryKey:["crm-leads"],queryFn:async()=>{const{data:s,error:a}=await v.from("leads").select("*").order("created_at",{ascending:!1});if(a)throw a;return s||[]}}),d=r.useMemo(()=>{const s=new Map;for(const a of b){const t=(a.email||a.id).trim().toLowerCase(),l=s.get(t);l?l.recordCount+=1:s.set(t,{...a,recordCount:1})}return[...s.values()]},[b]),M=q({mutationFn:async({id:s,status:a})=>{const{error:t}=await v.from("leads").update({status:a}).eq("id",s);if(t)throw t},onSuccess:()=>{n.invalidateQueries({queryKey:["crm-leads"]}),w.success("Status updated")},onError:()=>w.error("Could not update status")}),u=r.useMemo(()=>{const s=p.trim().toLowerCase();return d.filter(t=>(!s||t.name.toLowerCase().includes(s)||t.email.toLowerCase().includes(s)||(t.company??"").toLowerCase().includes(s)||(t.message??"").toLowerCase().includes(s))&&(c==="all"||(t.product??"Unassigned")===c)&&(i==="all"||t.status===i)&&(m==="all"||t.source===m)).sort((t,l)=>h?t.created_at.localeCompare(l.created_at):l.created_at.localeCompare(t.created_at))},[d,p,c,i,m,h]),U=r.useMemo(()=>x.map(s=>({key:s,rows:u.filter(a=>a.status===s)})),[u]),y=r.useMemo(()=>{const s=new Map;return d.forEach(a=>{const t=a.product||"Unassigned";s.set(t,(s.get(t)??0)+1)}),[...s.entries()].sort((a,t)=>t[1]-a[1])},[d]),f="h-9 px-2 rounded-md bg-secondary border border-border text-sm text-foreground";return e.jsxs(R,{children:[e.jsx("h1",{className:"font-display text-2xl font-bold mb-1",children:"CRM"}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-6",children:[d.length," lead",d.length!==1?"s":""," across all products and sources."]}),e.jsx("div",{className:"flex gap-1 mb-5",children:[{key:"dashboard",label:"Dashboard",icon:O},{key:"list",label:"Leads",icon:z},{key:"pipeline",label:"Pipeline",icon:B}].map(s=>e.jsxs("button",{onClick:()=>C(s.key),className:`flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-colors ${o===s.key?"bg-primary text-primary-foreground":"bg-secondary text-muted-foreground hover:text-foreground"}`,children:[e.jsx(s.icon,{className:"w-3.5 h-3.5"}),s.label]},s.key))}),o!=="dashboard"&&e.jsxs("div",{className:"flex flex-col lg:flex-row gap-2 mb-4",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(F,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(T,{placeholder:"Search name, email, company, message...",value:p,onChange:s=>S(s.target.value),className:"pl-10 bg-secondary"})]}),e.jsxs("select",{className:f,value:c,onChange:s=>L(s.target.value),children:[e.jsx("option",{value:"all",children:"All products"}),$.map(s=>e.jsx("option",{value:s,children:s},s)),e.jsx("option",{value:"Unassigned",children:"Unassigned"})]}),e.jsxs("select",{className:f,value:i,onChange:s=>k(s.target.value),children:[e.jsx("option",{value:"all",children:"All statuses"}),x.map(s=>e.jsx("option",{value:s,children:s},s))]}),e.jsxs("select",{className:f,value:m,onChange:s=>A(s.target.value),children:[e.jsx("option",{value:"all",children:"All sources"}),Q.map(s=>e.jsx("option",{value:s,children:s.replace("_"," ")},s))]}),e.jsxs(P,{variant:"outline",size:"sm",onClick:()=>_(s=>!s),className:"h-9",children:[e.jsx(I,{className:"w-3.5 h-3.5 mr-1.5"}),h?"Oldest":"Newest"]})]}),j&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"Loading leads..."}),o==="dashboard"&&e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsxs("div",{className:"rounded-lg border border-border bg-card p-4",children:[e.jsx("h2",{className:"font-medium text-sm mb-3",children:"Leads by status"}),e.jsx("div",{className:"space-y-2",children:x.map(s=>{const a=d.filter(l=>l.status===s).length,t=d.length?a/d.length*100:0;return e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-28 shrink-0",children:e.jsx(g,{status:s})}),e.jsx("div",{className:"flex-1 h-2 rounded-full bg-secondary overflow-hidden",children:e.jsx("div",{className:"h-full bg-primary/70",style:{width:`${t}%`}})}),e.jsx("span",{className:"w-8 text-right text-sm tabular-nums",children:a})]},s)})})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-card p-4",children:[e.jsx("h2",{className:"font-medium text-sm mb-3",children:"Leads by product"}),y.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No leads yet."}),e.jsx("div",{className:"space-y-2",children:y.map(([s,a])=>e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:s}),e.jsx("span",{className:"tabular-nums",children:a})]},s))})]})]}),o==="list"&&e.jsx("div",{className:"rounded-lg border border-border overflow-hidden",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-secondary/60 text-muted-foreground",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left font-medium px-3 py-2",children:"Name"}),e.jsx("th",{className:"text-left font-medium px-3 py-2 hidden md:table-cell",children:"Product"}),e.jsx("th",{className:"text-left font-medium px-3 py-2 hidden lg:table-cell",children:"Source"}),e.jsx("th",{className:"text-left font-medium px-3 py-2",children:"Status"}),e.jsx("th",{className:"text-left font-medium px-3 py-2 hidden sm:table-cell",children:"Created"})]})}),e.jsxs("tbody",{children:[u.map(s=>e.jsxs("tr",{className:"border-t border-border hover:bg-muted/40",children:[e.jsxs("td",{className:"px-3 py-2",children:[e.jsx(N,{to:`/admin/crm/${s.id}`,className:"font-medium hover:text-primary",children:s.name}),s.recordCount>1&&e.jsxs("span",{className:"ml-1.5 inline-flex items-center rounded-full bg-secondary px-1.5 py-0.5 text-[10px] text-foreground
1/70",children:[s.recordCount," records"]}),e.jsx("div",{className:"text-xs text-muted-foreground",children:s.email})]}),e.jsx("td",{className:"px-3 py-2 hidden md:table-cell text-muted-foreground",children:s.product||"-"}),e.jsxs("td",{className:"px-3 py-2 hidden lg:table-cell text-muted-foreground",children:[s.source.replace("_"," "),s.external_source&&e.jsx("span",{className:"ml-1.5 inline-flex items-center rounded-full bg-secondary px-1.5 py-0.5 text-[10px] text-foreground/70",children:s.external_source})]}),e.jsx("td",{className:"px-3 py-2",children:e.jsx(g,{status:s.status})}),e.jsx("td",{className:"px-3 py-2 hidden sm:table-cell text-muted-foreground",children:K(new Date(s.created_at),"MMM d, yyyy")})]},s.id)),!j&&u.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:5,className:"px-3 py-6 text-center text-muted-foreground",children:"No leads match these filters."})})]})]})}),o==="pipeline"&&e.jsx("div",{className:"grid gap-3 md:grid-cols-3 xl:grid-cols-6",children:U.map(s=>e.jsxs("div",{className:"rounded-lg border border-border bg-card/60 p-2 min-h-[120px]",children:[e.jsxs("div",{className:"flex items-center justify-between mb-2 px-1",children:[e.jsx(g,{status:s.key}),e.jsx("span",{className:"text-xs text-muted-foreground tabular-nums",children:s.rows.length})]}),e.jsx("div",{className:"space-y-2",children:s.rows.map(a=>e.jsxs("div",{className:"rounded-md border border-border bg-background p-2",children:[e.jsx(N,{to:`/admin/crm/${a.id}`,className:"text-sm font-medium hover:text-primary block truncate",children:a.name}),e.jsx("div",{className:"text-[11px] text-muted-foreground truncate",children:a.product||"Unassigned"}),e.jsx("select",{className:"mt-2 w-full h-7 rounded bg-secondary border border-border text-[11px] px-1",value:a.status,onChange:t=>M.mutate({id:a.id,status:t.target.value}),children:x.map(t=>e.jsx("option",{value:t,children:t},t))})]},a.id))})]},s.key))})]})}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.