1import{j as t}from"./vendor-query-DUki8u-D.js";import{r}from"./vendor-react-DHU755GE.js";import{supabase as x}from"./client-Dy2P3he-.js";import{g as _,B as h,s as y,h as k}from"./App-DNURBdOM.js";import{C as u,c as j,a as O,b as T}from"./card-B_TKe1rC.js";import{L as l}from"./label-CQkxHeEJ.js";import{S as $,a as E,b as U,c as L,d as A}from"./select-BJ2LQ1BA.js";import{D as g}from"./date-picker-DXAtzP_K.js";import"./vendor-supabase-D2XPrbcR.js";import"./vendor-pdf-DHUBMtDJ.js";import"./error-reporter-EYiXaqJQ.js";import"./breadcrumbs-CN3GC8vy.js";import"./vendor-charts-DsctC1NR.js";import"./vendor-icons-D7GmeyGG.js";import"./useTheme-DEq4C3Xf.js";import"./vendor-motion-DoyUfQ58.js";import"./index-BdQq_4o_.js";import"./index-BFcYZw7s.js";import"./calendar-BbCNGX3_.js";import"./isSameDay-BWv5-M0E.js";import"./format-D9x0XWGR.js";import"./en-US-BqCXNUOZ.js";import"./subDays-BK6vuqdz.js";import"./startOfMonth-D3_3nocY.js";import"./endOfMonth-B_n1vX73.js";import"./addMonths-Dw7XJecM.js";import"./differenceInCalendarMonths-BSRE4gQg.js";import"./popover-DAwJgSl1.js";import"./parseISO-CIZr9Bmv.js";const f=[{v:"status",l:"CRM Status"},{v:"case_manager",l:"Case Manager"},{v:"data_source",l:"Data Source"},{v:"blocker",l:"Blocker Tag"}];function ht(){var p;const{isAdmin:S}=_(),[o,b]=r.useState(()=>new Date(Date.now()-30*864e5).toISOString().slice(0,10)),[n,v]=r.useState(()=>new Date().toISOString().slice(0,10)),[s,C]=r.useState("status"),[i,w]=r.useState(null),[N,d]=r.useState(!1),m=async()=>{d(!0);const{data:e,error:a}=await x.rpc("crm_reports_rows",{_from:new Date(o).toISOString(),_to:new Date(n+"T23:59:59").toISOString(),_group_by:s});d(!1),a||w(e??[])};r.useEffect(()=>{m()},[s]);const R=async()=>{const{data:{session:e}}=await x.auth.getSession(),D=`https://selwlvtrbqdilvzwrlcv.supabase.co/functions/v1/crm-reports-export?from=${encodeURIComponent(new Date(o).toISOString())}&to=${encodeURIComponent(new Date(n+"T23:59:59").toISOString())}&group_by=${s}`,I=await(await fetch(D,{headers:{Authorization:`Bearer ${e==null?void 0:e.access_token}`}})).blob(),c=document.createElement("a");c.href=URL.createObjectURL(I),c.download=`crm-report-${s}-${Date.now()}.csv`,c.click()};return S?t.jsxs("div",{className:"space-y-4 min-w-0",children:[t.jsx("h1",{className:"text-2xl font-bold",children:"CRM Reports"}),t.jsx(u,{children:t.jsxs(j,{className:"p-4 grid grid-cols-1 md:grid-cols-4 gap-3 items-end",children:[t.jsxs("div",{children:[t.jsx(l,{children:"From"}),t.jsx(g,{value:o,onChange:e=>b(e??"")})]}),t.jsxs("div",{children:[t.jsx(l,{children:"To"}),t.jsx(g,{value:n,onChange:e=>v(e??"")})]}),t.jsxs("div",{children:[t.jsx(l,{children:"Group by"}),t.jsxs($,{value:s,onValueChange:C,children:[t.jsx(E,{children:t.jsx(U,{})}),t.jsx(L,{children:f.map(e=>t.jsx(A,{value:e.v,children:e.l},e.v))})]})]}),t.jsxs("div",{className:"flex gap-2",children:[t.jsx(h,{onClick:m,disabled:N,children:"Run"}),t.jsx(h,{variant:"outline",onClick:R,children:"Export CSV"})]})]})}),t.jsxs(u,{children:[t.jsx(O,{children:t.jsx(T,{className:"text-base",children:"Results"})}),t.jsx(j,{children:i?i.length===0?t.jsx("p",{className:"text-sm text-muted-foreground italic",children:"No data in this range."}):t.jsxs("table",{className:"w-full text-sm",children:[t.jsx("thead",{children:t.jsxs("tr",{className:"text-left text-xs text-muted-foreground border-b",children:[t.jsx("th",{className:"py-2",children:(p=f.find(e=>e.v===s))==null?void 0:p.l}),t.jsx("th",{className:"text-right",children:"Count"}),t.jsx("th",{className:"text-right",children:"Est. Value"})]})}),t.jsx("tbody",{children:i.map((e,a)=>t.jsxs("tr",{className:"border-b last:border-0",children:[t.jsx("td",{className:"py-2",children:e.grp||"â"}),t.jsx("td",{className:"text-right",children:e.count}),t.jsx("td",{className:"text-right",children:k(Number(e.est_value||0))})]},a))})]}):t.jsx(y,{className:"h-40"})})]})]}):t.jsx("div",{className:"p-6 text-sm text-muted-foreground",children:"Admin only."})}export{ht 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.