1import{r as c,b as h,t as k,j as e,L as Y}from"./index-DdU35jPJ.js";import{c as q}from"./index-CtlYlah8.js";import{B as _}from"./button-BfxMpK8P.js";import{I as $}from"./input-cpzcpc5y.js";import{A as V}from"./arrow-left-CoQakR5q.js";import{B as X}from"./building-2-DLUiP0-v.js";import{P as G}from"./phone-BiN9hL5_.js";import{M as H}from"./mail-DvcgW5d2.js";import{F as J}from"./file-text-BIInLmy-.js";import{T as Q}from"./truck-BQgQtOKT.js";import{D as Z}from"./download-BAHbJTmZ.js";import{C as ee}from"./calendar-WJIH9OsN.js";import{W as te}from"./wallet-6HLGA_Th.js";import{T as se}from"./trending-up-D6tLkDJM.js";import{U as ae}from"./users-ecdZz2CG.js";import{A as re}from"./award-BXY6iOl3.js";import{R,C as ne,X as ie,Y as le,T,q as de,r as oe}from"./generateCategoricalChart-CuyUVvyI.js";import{L as ce,a as u}from"./LineChart-CndyA6_s.js";import{P as me,a as xe}from"./PieChart-ByiN4i4v.js";import"./index-gN4drEn4.js";import"./index-Dp3B9jqt.js";import"./clsx-B-dksMZM.js";import"./utils-DCmAa5as.js";import"./bundle-mjs-BTmSotT-.js";import"./createLucideIcon-DMxicyrC.js";import"./tiny-invariant-BaFNuDhB.js";function Ie(){var C,S,D;const[d,A]=c.useState([]),[L,F]=c.useState([]),[P,U]=c.useState([]),[B,E]=c.useState([]),[O,g]=c.useState(!0),[l,f]=c.useState({start:"",end:""}),[i,W]=c.useState(null),j=new URLSearchParams(window.location.search).get("id");c.useEffect(()=>{z()},[]);const z=async()=>{g(!0);try{const[t,a,s,n]=await Promise.all([h.entities.Customer.list(),h.entities.Order.list("-created_date"),h.entities.RedemptionRequest.list("-created_date"),h.entities.Purchase.list("-created_date")]);if(A(t),F(a),U(s),E(n),j){const r=t.find(m=>m.id===j);r&&W(r)}}catch(t){console.error(t),k.error("Failed to load data")}g(!1)},p=(t,a="created_date")=>!l.start&&!l.end?t:t.filter(s=>{const n=new Date(s[a]),r=l.start?new Date(l.start):new Date(0),m=l.end?new Date(l.end):new Date;return n>=r&&n<=m}),N=p(L),w=p(P);p(B);const v=d.reduce((t,a)=>t+(a.credit_balance||0),0),I=d.length>0?v/d.length:0,b=d.filter(t=>(t.credit_balance||0)>0).length,x=d.map(t=>{const a=N.filter(n=>n.customer_id===t.id&&n.status==="completed"),s=w.filter(n=>n.customer_id===t.id&&n.status==="approved");return{name:t.company_name,orders:a.length,redemptions:s.length,spent:a.reduce((n,r)=>n+(r.final_amount||r.total_amount||0),0),redeemed:s.reduce((n,r)=>n+(r.amount||0),0),balance:t.credit_balance||0}}).sort((t,a)=>a.orders-t.orders).slice(0,10),K=(()=>{const t={},a=new Date;for(let s=5;s>=0;s--){const r=new Date(a.getFullYear(),a.getMonth()-s,1).toLocaleDateString("en-US",{month:"short",year:"numeric"});t[r]={month:r,earned:0,redeemed:0,net:0}}return N.filter(s=>s.status==="completed").forEach(s=>{const r=new Date(s.created_date).toLocaleDateString("en-US",{month:"short",year:"numeric"});t[r]&&(t[r].earned+=s.credits_earned||0)}),w.filter(s=>s.status==="approved").forEach(s=>{const r=new Date(s.created_date).toLocaleDateString("en-US",{month:"short",year:"numeric"});t[r]&&(t[r].redeemed+=s.amount||0)}),Object.keys(t).forEach(s=>{t[s].net=t[s].earned-t[s].redeemed}),Object.values(t)})(),y=(()=>{const t=[{name:"$0",min:0,max:0,count:0,color:"#9699a6"},{name:"$1-50",min:1,max:50,count:0,color:"#0073ea"},{name:"$51-100",min:51,max:100,count:0,color:"#00c875"},{name:"$101-200",min:101,max:200,count:0,color:"#fdab3d"},{name:"$201+",min:201,max:1/0,count:0,color:"#e2445c"}];return d.forEach(a=>{const s=a.credit_balance||0,n=t.find(r=>s>=r.min&&s<=r.max);n&&n.count++}),t.filter(a=>a.count>0)})(),M=()=>{const t=["Customer","Orders","Total Spent","Redemptions","Total Redeemed","Current Balance"],a=x.map(o=>[o.name,o.orders,`$${o.spent.toFixed(2)}`,o.redemptions,`$${o.redeemed.toFixed(2)}`,`$${o.balance.toFixed(2)}`]),s=[t,...a].map(o=>o.join(",")).join(` 2`),n=new Blob([s],{type:"text/csv"}),r=URL.createObjectURL(n),m=document.createElement("a");m.href=r,m.download=`customer-analytics-${new Date().toISOString().split("T")[0]}.csv`,m.click(),k.success("Analytics exported")};return O?e.jsx("div",{className:"min-h-screen bg-[#f6f7fb] flex items-center justify-center",children:e.jsx("div",{className:"w-8 h-8 border-2 border-[#0073ea] border-t-transparent roun
2ded-full animate-spin"})}):e.jsx("div",{className:"min-h-screen bg-[#f6f7fb] p-4 lg:p-10",children:e.jsxs("div",{className:"max-w-7xl mx-auto",children:[i&&e.jsxs(Y,{to:q("CustomerAnalytics"),className:"inline-flex items-center gap-2 text-[#676879] hover:text-[#323338] mb-6",children:[e.jsx(V,{className:"w-4 h-4"}),"Back to Customers"]}),i&&e.jsxs("div",{className:"bg-white rounded-2xl p-6 border border-[#e6e9ef] shadow-sm mb-6",children:[e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-3 text-[#323338]",children:[e.jsx(X,{className:"w-6 h-6 text-[#0073ea]"}),i.company_name]}),e.jsxs("div",{className:"grid md:grid-cols-3 gap-4",children:[e.jsxs("div",{className:"bg-[#f6f7fb] rounded-lg p-4",children:[e.jsx("p",{className:"text-[#676879] text-xs uppercase tracking-wider mb-1",children:"Contact Name"}),e.jsx("p",{className:"text-[#323338] font-semibold text-lg",children:i.contact_name||"N/A"})]}),e.jsxs("div",{className:"bg-[#f6f7fb] rounded-lg p-4",children:[e.jsxs("p",{className:"text-[#676879] text-xs uppercase tracking-wider mb-1 flex items-center gap-1",children:[e.jsx(G,{className:"w-3 h-3"}),"Phone Number"]}),e.jsx("p",{className:"text-[#323338] font-semibold text-lg",children:i.phone_number?e.jsx("a",{href:`tel:${i.phone_number}`,className:"text-[#0073ea] hover:underline",children:i.phone_number}):"N/A"})]}),e.jsxs("div",{className:"bg-[#f6f7fb] rounded-lg p-4",children:[e.jsxs("p",{className:"text-[#676879] text-xs uppercase tracking-wider mb-1 flex items-center gap-1",children:[e.jsx(H,{className:"w-3 h-3"}),"Email"]}),e.jsx("p",{className:"text-[#323338] font-semibold text-sm",children:i.user_email?e.jsx("a",{href:`mailto:${i.user_email}`,className:"text-[#0073ea] hover:underline",children:i.user_email}):"N/A"})]}),e.jsxs("div",{className:"bg-[#f6f7fb] rounded-lg p-4",children:[e.jsxs("p",{className:"text-[#676879] text-xs uppercase tracking-wider mb-1 flex items-center gap-1",children:[e.jsx(J,{className:"w-3 h-3"}),"DOT Number"]}),e.jsx("p",{className:"text-[#323338] font-semibold text-lg font-mono",children:i.dot_number||"N/A"})]}),e.jsxs("div",{className:"bg-[#f6f7fb] rounded-lg p-4",children:[e.jsx("p",{className:"text-[#676879] text-xs uppercase tracking-wider mb-1",children:"Business Type"}),e.jsx("p",{className:"text-[#323338] font-semibold text-lg capitalize",children:((C=i.business_type)==null?void 0:C.replace("_"," "))||"N/A"})]}),e.jsxs("div",{className:"bg-[#f6f7fb] rounded-lg p-4",children:[e.jsxs("p",{className:"text-[#676879] text-xs uppercase tracking-wider mb-1 flex items-center gap-1",children:[e.jsx(Q,{className:"w-3 h-3"}),"Fleet Size"]}),e.jsx("p",{className:"text-[#323338] font-semibold text-lg",children:i.fleet_size?`${i.fleet_size} trucks`:"N/A"})]})]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-8",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold mb-2 text-[#323338]",children:i?`${i.company_name} - Analytics`:"Customer Analytics"}),e.jsx("p",{className:"text-[#676879]",children:i?"Detailed insights for this customer":"Comprehensive insights into customer credit activity"})]}),e.jsxs(_,{onClick:M,className:"bg-[#0073ea] text-white hover:bg-[#0060b9] border-none font-semibold shadow-sm",children:[e.jsx(Z,{className:"w-4 h-4 mr-2"}),"Export CSV"]})]}),e.jsxs("div",{className:"bg-white rounded-2xl p-5 mb-6 border border-[#e6e9ef] shadow-sm",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[e.jsx(ee,{className:"w-5 h-5 text-[#0073ea]"}),e.jsx("h3",{className:"text-[#323338] font-semibold",children:"Date Range Filter"})]}),e.jsxs("div",{className:"flex flex-wrap gap-4 items-end",children:[e.jsxs("div",{className:"flex-1 min-w-[200px]",children:[e.jsx("label",{className:"text-[#676879] text-sm mb-2 block",children:"Start Date"}),e.jsx($,{type:"date",value:l.start,onChange:t=>f({...l,start:t.target.value}),className:"bg-white border-[#e6e9ef] text-[#323338]"})]}),e.jsxs("div",{className:"flex-1 min-w-[200px]",children:[e.jsx("label",{className:"text-[#676879] text-sm mb-2 block",children:"End Date"}),e.jsx($,{type:"date",value:l.end,onChange:t=>f({...l,end:t.target.value}),className:"bg-white border-[#e6e9ef] text-[#323338]"})]}),e.jsx(_,{onClick:()=>f({start:"",end:""}),variant:"outline",className:"border-[#e6e9ef] text-[#323338] hover:bg-[#f6f7fb]",children:"Clear"})]}),(l.start||l.end)&&e.jsxs("p",{className:"text-[#9699a6] text-xs mt-3",children:["Filtering data from ",l.start||"beginning"," to ",l.end||"now"]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-8",children:[e.jsxs("div",{className:"bg-white rounded-2xl p-5 border border-[#e6e9ef] shadow-sm",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[e.jsx("div",{className:"w-10 h-10 bg-[#0073ea]/10 rounded-xl flex items-center justify-center",children:e.jsx(te,{className:"w-5 h-5 text-[#0073ea]"})}),e.jsx("p",{className:"text-[#676879] text-sm",children:"Total Credits"})]}),e.jsxs("p",{className:"text-[#323338] text-3xl font-bold",children:["$",v.toLocaleStr
2ing("en-US",{minimumFractionDigits:2})]}),e.jsxs("p",{className:"text-[#9699a6] text-xs mt-1",children:[b," customers with balance"]})]}),e.jsxs("div",{className:"bg-white rounded-2xl p-5 border border-[#e6e9ef] shadow-sm",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[e.jsx("div",{className:"w-10 h-10 bg-[#00c875]/10 rounded-xl flex items-center justify-center",children:e.jsx(se,{className:"w-5 h-5 text-[#00c875]"})}),e.jsx("p",{className:"text-[#676879] text-sm",children:"Avg Credit Balance"})]}),e.jsxs("p",{className:"text-[#323338] text-3xl font-bold",children:["$",I.toLocaleString("en-US",{minimumFractionDigits:2})]}),e.jsx("p",{className:"text-[#9699a6] text-xs mt-1",children:"Per customer"})]}),e.jsxs("div",{className:"bg-white rounded-2xl p-5 border border-[#e6e9ef] shadow-sm",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[e.jsx("div",{className:"w-10 h-10 bg-[#a25ddc]/10 rounded-xl flex items-center justify-center",children:e.jsx(ae,{className:"w-5 h-5 text-[#a25ddc]"})}),e.jsx("p",{className:"text-[#676879] text-sm",children:"Total Customers"})]}),e.jsx("p",{className:"text-[#323338] text-3xl font-bold",children:d.length}),e.jsxs("p",{className:"text-[#9699a6] text-xs mt-1",children:[b," active (",(b/d.length*100).toFixed(0),"%)"]})]}),e.jsxs("div",{className:"bg-white rounded-2xl p-5 border border-[#e6e9ef] shadow-sm",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[e.jsx("div",{className:"w-10 h-10 bg-[#fdab3d]/10 rounded-xl flex items-center justify-center",children:e.jsx(re,{className:"w-5 h-5 text-[#fdab3d]"})}),e.jsx("p",{className:"text-[#676879] text-sm",children:"Top Customer"})]}),e.jsx("p",{className:"text-[#323338] text-xl font-bold truncate",children:((S=x[0])==null?void 0:S.name)||"N/A"}),e.jsxs("p",{className:"text-[#9699a6] text-xs mt-1",children:[((D=x[0])==null?void 0:D.orders)||0," orders"]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6",children:[e.jsxs("div",{className:"bg-white rounded-2xl p-5 border border-[#e6e9ef] shadow-sm",children:[e.jsx("h3",{className:"text-[#323338] font-semibold mb-4",children:"Credit Trends (Last 6 Months)"}),e.jsx(R,{width:"100%",height:300,children:e.jsxs(ce,{data:K,children:[e.jsx(ne,{strokeDasharray:"3 3",stroke:"#e6e9ef"}),e.jsx(ie,{dataKey:"month",stroke:"#676879",style:{fontSize:"12px"}}),e.jsx(le,{stroke:"#676879",style:{fontSize:"12px"}}),e.jsx(T,{contentStyle:{backgroundColor:"#ffffff",border:"1px solid #e6e9ef",borderRadius:"8px"},labelStyle:{color:"#323338"},cursor:!1}),e.jsx(de,{}),e.jsx(u,{type:"monotone",dataKey:"earned",stroke:"#00c875",strokeWidth:3,name:"Credits Earned",dot:{fill:"#00c875",r:6,strokeWidth:2,stroke:"#ffffff"},activeDot:{r:8}}),e.jsx(u,{type:"monotone",dataKey:"redeemed",stroke:"#e2445c",strokeWidth:3,name:"Credits Redeemed",dot:{fill:"#e2445c",r:6,strokeWidth:2,stroke:"#ffffff"},activeDot:{r:8}}),e.jsx(u,{type:"monotone",dataKey:"net",stroke:"#0060b9",strokeWidth:3,name:"Net Change",dot:{fill:"#0073ea",r:6,strokeWidth:2,stroke:"#ffffff"},activeDot:{r:8}})]})})]}),e.jsxs("div",{className:"bg-white rounded-2xl p-5 border border-[#e6e9ef] shadow-sm",children:[e.jsx("h3",{className:"text-[#323338] font-semibold mb-4",children:"Credit Balance Distribution"}),e.jsx(R,{width:"100%",height:300,children:e.jsxs(me,{children:[e.jsx(xe,{data:y,cx:"50%",cy:"50%",labelLine:!1,label:t=>`${t.name}: ${t.count}`,outerRadius:100,fill:"#8884d8",dataKey:"count",children:y.map((t,a)=>e.jsx(oe,{fill:t.color},`cell-${a}`))}),e.jsx(T,{contentStyle:{backgroundColor:"#ffffff",border:"1px solid #e6e9ef",borderRadius:"8px"},labelStyle:{color:"#323338"}})]})})]})]}),e.jsxs("div",{className:"bg-white rounded-2xl p-5 border border-[#e6e9ef] shadow-sm",children:[e.jsx("h3",{className:"text-[#323338] font-semibold mb-4",children:"Top 10 Most Active Customers"}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-[#e6e9ef]",children:[e.jsx("th",{className:"text-left text-[#676879] text-sm font-medium pb-3",children:"#"}),e.jsx("th",{className:"text-left text-[#676879] text-sm font-medium pb-3",children:"Customer"}),e.jsx("th",{className:"text-right text-[#676879] text-sm font-medium pb-3",children:"Orders"}),e.jsx("th",{className:"text-right text-[#676879] text-sm font-medium pb-3",children:"Total Spent"}),e.jsx("th",{className:"text-right text-[#676879] text-sm font-medium pb-3",children:"Redemptions"}),e.jsx("th",{className:"text-right text-[#676879] text-sm font-medium pb-3",children:"Redeemed"}),e.jsx("th",{className:"text-right text-[#676879] text-sm font-medium pb-3",children:"Balance"})]})}),e.jsx("tbody",{children:x.map((t,a)=>e.jsxs("tr",{className:"border-b border-[#e6e9ef] hover:bg-[#f6f7fb] transition-colors",children:[e.jsx("td",{className:"py-3 text-[#9699a6]",children:a+1}),e.jsx("td",{className:"py-3 text-[#323338] font-medium",children:t.name}),e.jsx("td",{className:"py-3 text-right text-[#323338]",children:t.orders}),e.jsxs("td",{className:"py-3 text-right text-[#323338]",children:["$",t.spent.toLocaleStr
2ing("en-US",{minimumFractionDigits:2})]}),e.jsx("td",{className:"py-3 text-right text-[#323338]",children:t.redemptions}),e.jsxs("td",{className:"py-3 text-right text-[#00c875]",children:["$",t.redeemed.toLocaleString("en-US",{minimumFractionDigits:2})]}),e.jsxs("td",{className:"py-3 text-right text-[#0073ea] font-bold",children:["$",t.balance.toLocaleString("en-US",{minimumFractionDigits:2})]})]},a))})]})})]})]})})}export{Ie 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.