PageSourceSearch

https://buildslot.ai/assets/CRM-PCoQ1cu8.js

js buildslot.ai collected 2026-10-05 12:12:59 UTC 113,436 bytes, 64 lines download raw bytes

1import{q as Ue,r as _,v as D,m as e,B as w,T as ie,w as R,x as y,u as I,a2 as J,a7 as pe,a9 as W,D as ge,G as U,C as z,o as G,s as O,M as ss,k as Re,aS as ts,aT as as,E as ls,aU as ns,aW as be,ac as is,I as rs,U as we,aK as ze,a4 as cs,F as Ne,a6 as os}from"./index-CpSGH3si.js";import{T as Ke,a as Ve,b as T,c as B}from"./tabs-C2Ucxcpc.js";import{C as S,b as $,c as M,a as k}from"./card--3jAFIY6.js";import{S as K}from"./skeleton-D7CKVNpx.js";import{D as ce,a as oe,b as de,c as xe,d as ds}from"./dialog-DjQOlXK_.js";import{I as f}from"./input-CJQwYE0h.js";import{L as g}from"./label-mxM2ZpWz.js";import{S as te,a as ae,b as le,c as ne,d as C}from"./select-DF7dIvp6.js";import{S as Qe}from"./sparkles-Dcvw6KO-.js";import{A as Ge}from"./index-CtsrXVb9.js";import{m as E}from"./proxy-DFy_m_5J.js";import{C as Se}from"./circle-check-big-BfTSBK82.js";import{C as Ce}from"./circle-alert-CRwkKVNs.js";import{L as xs,a as He}from"./link-2-kaCKXa55.js";import{T as F}from"./textarea-DnsCXU4p.js";import{toast as Q}from"./index-VOExC7zx.js";import{a as he}from"./format-DRR4LvZs.js";import{C as Y}from"./circle-check-zYGxtVka.js";import{i as me}from"./isPast-DOqFiOqV.js";import{i as Z}from"./isToday-BVLSP7-1.js";import{i as ke}from"./isTomorrow-2hkyI4Bz.js";import{L as Ye,G as ms}from"./layout-grid-D-KzKlrw.js";import{M as ee}from"./mail-32WMcMo6.js";import{P as re}from"./phone-B5j7AMqT.js";import{B as hs}from"./building-BxQ7zmgQ.js";import{E as Xe,B as us}from"./ellipsis-vertical-Btc1YSCE.js";import{T as ue}from"./target-C2qJztff.js";import{T as Ae}from"./triangle-alert-Dji1vfjp.js";import{Q as ps}from"./QuoteRequestCard-ChxVCewN.js";import{C as gs}from"./circle-x-BtX7uimO.js";import{E as De}from"./eye-B1WA26hs.js";import{D as js,C as bs,P as Ns}from"./dnd.esm-DwkYpmJ5.js";import{i as $e,E as fs}from"./emailValidator-CgDbf28y.js";import{v as se}from"./numericValidators-CORrfhZy.js";import{S as _s}from"./StatePicker-daNglNSY.js";import{S as X}from"./switch-DZOHvmxy.js";import{Z as P}from"./zap-DMV_c2aX.js";import{S as Je}from"./square-pen-Cn9yPLMp.js";import{S as ys}from"./square-check-big-Cs6Iu64b.js";import{S as vs}from"./send-CpRLLNKN.js";import{A as ws}from"./award-DKmag8Pk.js";import{A as Cs}from"./arrow-up-right-CnqVUcsD.js";import{R as Me,B as Ss,a as Te,X as Be,Y as qe,T as Pe,b as ks,C as As}from"./BarChart-BB8s17z4.js";import{L as Ls,a as Rs}from"./LineChart-BUUpR55M.js";import{s as fe}from"./subDays-D-snu2mw.js";import{i as _e}from"./isAfter-X1VCSQ-p.js";import{C as Ds}from"./copy-CewTimSu.js";import{S as $s}from"./save-D9ShgAbO.js";import{W as Ze}from"./wrench-BbTR8Tzx.js";import{P as Ms}from"./pencil-DQhK_CFW.js";import{d as Ts}from"./differenceInDays-CBb1kXVt.js";import{I as Bs}from"./inbox-CC8eM3to.js";import{S as qs}from"./settings-CgpP5zcL.js";import"./index-BdQq_4o_.js";import"./index-Bs3UMnWC.js";import"./index-Cy91vPlr.js";import"./chevron-up-xOaegNmi.js";import"./differenceInCalendarDays-CFJcUmo7.js";import"./isSameDay-CnIkoyse.js";import"./addDays-DbcpP0x6.js";import"./user-DdcnLghC.js";import"./loader-circle-CZ4nBqpt.js";import"./tiny-invariant-BaFNuDhB.js";/**
2 * @license lucide-react v0.475.0 - ISC
3 *
4 * This source code is licensed under the ISC license.
5 * See the LICENSE file in the root directory of this source tree.
6 */const Ps=[["path",{d:"M10.268 21a2 2 0 0 0 3.464 0",key:"vwvbt9"}],["path",{d:"M17 17H4a1 1 0 0 1-.74-1.673C4.59 13.956 6 12.499 6 8a6 6 0 0 1 .258-1.742",key:"178tsu"}],["path",{d:"m2 2 20 20",key:"1ooewy"}],["path",{d:"M8.668 3.01A6 6 0 0 1 18 8c0 2.687.77 4.653 1.707 6.05",key:"1hqiys"}]],Es=Ue("BellOff",Ps);/**
7 * @license lucide-react v0.475.0 - ISC
8 *
9 * This source code is licensed under the ISC license.
10 * See the LICENSE file in the root directory of this source tree.
11 */const Fs=[["path",{d:"M16 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V8Z",key:"qazsjp"}],["path",{d:"M15 3v4a2 2 0 0 0 2 2h4",key:"40519r"}]],es=Ue("StickyNote",Fs);function Is({lead:t}){var s,i,d;const[u,p]=_.useState(null),x=D({mutationFn:async()=>{const m=`Analyze this sales lead and provide strategic insights:
12
13Lead Details:
14- Name: ${t.name}
15- Company: ${t.company||"N/A"}
16- Project Type: ${t.project_type||"N/A"}
17- Estimated Value: $${t.estimated_value||"Unknown"}
18- Status: ${t.status}
19- Source: ${t.source}
20- Location: ${t.city}, ${t.state}
21- Expected Close Date: ${t.expected_close_date||"Not set"}
22- Probability: ${t.probability}%
23- Notes: ${t.notes||"None"}
24
25Provide:
261. A lead quality score (0-100)
272. Top 3 strengths about this lead
283. Top 3 concerns or risks
294. Recommended next actions (3 specific steps)
305. Suggested approach strategy`;return await y.integrations.Core.InvokeLLM({prompt:m,response_json_schema:{type:"object",properties:{quality_score:{type:"number"},strengths:{type:"array",items:{type:"string"}},concerns:{type:"array",items:{type:"string"}},next_actions:{type:"array",items:{type:"string"}},strategy:{type:"string"}}}})},onSuccess:m=>{p(m)}}),n=m=>m>=80?"text-green-600 bg-green-100":m>=60?"text-amber-600 bg-amber-100":"text-red-600 bg-red-100";return e.jsxs(S,{children:[e.jsx($,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(M,{className:"flex items-center gap-2",children:[e.jsx(Qe,{className:"w-5 h-5 text-purple-600"}),"AI Lead Insights"]}),e.jsx(w,{onClick:()=>x.mutate(),disabled:x.isPending,size:"sm",className:"bg-purple-600 hover:bg-purple-700",children:x.isPending?"Analyzing...":"Generate Insights"})]})}),e.jsx(k,{children:e.jsxs(Ge,{mode:"wait",children:[!u&&!x.isPending&&e.jsx(E.div,{initial:{opacity:0}
30,animate:{opacity:1},exit:{opacity:0},className:"text-center py-8 text-slate-500",children:'Click "Generate Insights" to get AI-powered analysis of this lead'}),x.isPending&&e.jsxs(E.div,{initial:{opacity:0},animate:{opacity:1},exit:{opacity:0},className:"text-center py-8",children:[e.jsx("div",{className:"animate-spin w-8 h-8 border-4 border-purple-600 border-t-transparent rounded-full mx-auto mb-3"}),e.jsx("p",{className:"text-slate-600",children:"Analyzing lead with AI..."})]}),u&&e.jsxs(E.div,{initial:{opacity:0,y:20},animate:{opacity:1,y:0},exit:{opacity:0,y:-20},className:"space-y-6",children:[e.jsxs("div",{className:"text-center p-4 bg-slate-50 rounded-lg",children:[e.jsx("p",{className:"text-sm text-slate-600 mb-2",children:"Lead Quality Score"}),e.jsxs("div",{className:`inline-flex items-center gap-2 px-4 py-2 rounded-full font-bold text-2xl ${n(u.quality_score)}`,children:[e.jsx(ie,{className:"w-6 h-6"}),u.quality_score,"/100"]})]}),e.jsxs("div",{children:[e.jsxs("h4",{className:"font-semibold text-slate-900 mb-3 flex items-center gap-2",children:[e.jsx(Se,{className:"w-4 h-4 text-green-600"}),"Strengths"]}),e.jsx("div",{className:"space-y-2",children:(s=u.strengths)==null?void 0:s.map((m,l)=>e.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[e.jsx("span",{className:"text-green-600 mt-0.5",children:"✓"}),e.jsx("span",{className:"text-slate-700",children:m})]},l))})]}),e.jsxs("div",{children:[e.jsxs("h4",{className:"font-semibold text-slate-900 mb-3 flex items-center gap-2",children:[e.jsx(Ce,{className:"w-4 h-4 text-amber-600"}),"Concerns & Risks"]}),e.jsx("div",{className:"space-y-2",children:(i=u.concerns)==null?void 0:i.map((m,l)=>e.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[e.jsx("span",{className:"text-amber-600 mt-0.5",children:"⚠"}),e.jsx("span",{className:"text-slate-700",children:m})]},l))})]}),e.jsxs("div",{children:[e.jsxs("h4",{className:"font-semibold text-slate-900 mb-3 flex items-center gap-2",children:[e.jsx(xs,{className:"w-4 h-4 text-blue-600"}),"Recommended Next Actions"]}),e.jsx("div",{className:"space-y-2",children:(d=u.next_actions)==null?void 0:d.map((m,l)=>e.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[e.jsx(R,{variant:"outline",className:"mt-0.5",children:l+1}),e.jsx("span",{className:"text-slate-700",children:m})]},l))})]}),e.jsxs("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-4",children:[e.jsx("h4",{className:"font-semibold text-blue-900 mb-2",children:"Suggested Strategy"}),e.jsx("p",{className:"text-sm text-blue-800",children:u.strategy})]})]})]})})]})}function Os({lead:t}){const u=I(),[p,x]=_.useState(""),n=D({mutationFn:m=>y.entities.Lead.update(t.id,{internal_notes:m}),onSuccess:()=>{u.invalidateQueries({queryKey:["leads"]}),x(""),Q.success("Note saved")}}),s=t.internal_notes||[],i=()=>{if(!p.trim())return;const m={id:crypto.randomUUID(),text:p.trim(),created_at:new Date().toISOString(),author:"You"};n.mutate([...s,m])},d=m=>{n.mutate(s.filter(l=>l.id!==m))};return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("h3",{className:"font-semibold text-slate-900 flex items-center gap-2",children:[e.jsx(es,{className:"w-4 h-4 text-amber-500"}),"Internal Notes",e.jsxs("span",{className:"ml-auto text-xs font-normal text-slate-400",children:[s.length," note",s.length!==1?"s":""]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(F,{placeholder:"Add an internal note (only visible to you)...",value:p,onChange:m=>x(m.target.value),rows:3,className:"resize-none text-sm"}),e.jsxs(w,{size:"sm",className:"bg-amber-500 hover:bg-amber-600 text-white gap-1.5",onClick:i,disabled:!p.trim()||n.isPending,children:[e.jsx(J,{className:"w-3.5 h-3.5"}),"Add Note"]})]}),e.jsxs("div",{className:"space-y-2 max-h-64 overflow-y-auto",children:[s.length===0&&e.jsx("p",{className:"text-xs text-slate-400 italic py-2",children:"No internal notes yet."}),[...s].reverse().map(m=>e.jsxs("div",{className:"bg-amber-50 border border-amber-100 rounded-lg p-3 group",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsx("p",{className:"text-sm text-slate-800 whitespace-pre-wrap flex-1",children:m.text}),e.jsx("button",{onClick:()=>d(m.id),className:"opacity-0 group-hover:opacity-100 transition-opacity text-slate-400 hover:text-red-500 shrink-0",children:e.jsx(pe,{className:"w-3.5 h-3.5"})})]}),e.jsx("p",{className:"text-xs text-slate-400 mt-1",children:he(ne
30w Date(m.created_at),"MMM d, yyyy 'at' h:mm a")})]},m.id))]})]})}function Ws(t){if(!t)return null;const u=new Date(t);return me(u)&&!Z(u)?{label:"Overdue",color:"text-red-600 bg-red-50 border-red-200"}:Z(u)?{label:"Today",color:"text-orange-600 bg-orange-50 border-orange-200"}:ke(u)?{label:"Tomorrow",color:"text-amber-600 bg-amber-50 border-amber-200"}:{label:he(u,"MMM d"),color:"text-blue-600 bg-blue-50 border-blue-200"}}function Us({lead:t}){const u=I(),[p,x]=_.useState(t.follow_up_reminder?t.follow_up_reminder.slice(0,16):""),[n,s]=_.useState(t.follow_up_note||""),[i,d]=_.useState(!1),m=D({mutationFn:j=>y.entities.Lead.update(t.id,j),onSuccess:()=>{u.invalidateQueries({queryKey:["leads"]}),d(!0),setTimeout(()=>d(!1),2e3),Q.success("Follow-up reminder saved")}}),l=()=>{m.mutate({follow_up_reminder:p?new Date(p).toISOString():null,follow_up_note:n})},a=()=>{x(""),s(""),m.mutate({follow_up_reminder:null,follow_up_note:""})},h=Ws(t.follow_up_reminder);return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("h3",{className:"font-semibold text-slate-900 flex items-center gap-2",children:[e.jsx(W,{className:"w-4 h-4 text-blue-500"}),"Follow-Up Reminder",h&&e.jsx("span",{className:`ml-auto text-xs font-semibold px-2 py-0.5 rounded-full border ${h.color}`,children:h.label})]}),t.follow_up_reminder&&e.jsxs("div",{className:`rounded-lg border p-3 text-sm ${(h==null?void 0:h.color)||"text-slate-600 bg-slate-50 border-slate-200"}`,children:[e.jsxs("p",{className:"font-medium",children:["Call scheduled: ",he(new Date(t.follow_up_reminder),"EEEE, MMM d 'at' h:mm a")]}),t.follow_up_note&&e.jsx("p",{className:"mt-1 text-xs opacity-80",children:t.follow_up_note})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{className:"text-xs",children:"Date & Time"}),e.jsx(f,{type:"datetime-local",value:p,onChange:j=>x(j.target.value),className:"text-sm"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{className:"text-xs",children:"What to discuss / action needed"}),e.jsx(F,{placeholder:"e.g. Call back about roofing quote, check if they got the estimate...",value:n,onChange:j=>s(j.target.value),rows:2,className:"resize-none text-sm"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(w,{size:"sm",className:"flex-1 bg-blue-600 hover:bg-blue-700 gap-1.5",onClick:l,disabled:m.isPending,children:[i?e.jsx(Y,{className:"w-3.5 h-3.5"}):e.jsx(W,{className:"w-3.5 h-3.5"}),i?"Saved!":"Set Reminder"]}),t.follow_up_reminder&&e.jsxs(w,{size:"sm",variant:"outline",onClick:a,className:"gap-1.5 text-slate-500",children:[e.jsx(Es,{className:"w-3.5 h-3.5"}),"Clear"]})]})]})}const zs={new:"bg-slate-100 text-slate-700",contacted:"bg-blue-100 text-blue-700",auto_responded:"bg-purple-100 text-purple-700",appointment_scheduled:"bg-green-100 text-green-700",appointment_completed:"bg-teal-100 text-teal-700",qualified:"bg-amber-100 text-amber-700",proposal_sent:"bg-orange-100 text-orange-700",negotiating:"bg-yellow-100 text-yellow-700",won:"bg-emerald-100 text-emerald-700",lost:"bg-red-100 text-red-700"},Ks=[["new","New"],["contacted","Contacted"],["auto_responded","Auto-Responded"],["appointment_scheduled","Appt. Scheduled"],["appointment_completed","Appt. Completed"],["qualified","Qualified"],["proposal_sent","Proposal Sent"],["negotiating","Negotiating"],["won","Won"],["lost","Lost"]],Vs=[{key:"new",label:"New",color:"bg-slate-100 border-slate-300 text-slate-700"},{key:"contacted",label:"Contacted",color:"bg-blue-100 border-blue-300 text-blue-700"},{key:"qualified",label:"Qualified",color:"bg-purple-100 border-purple-300 text-purple-700"},{key:"proposal_sent",label:"Proposal Sent",color:"bg-amber-100 border-amber-300 text-amber-700"},{key:"negotiating",label:"Negotiating",color:"bg-orange-100 border-orange-300 text-orange-700"},{key:"won",label:"Won ✓",color:"bg-green-100 border-green-300 text-green-700"},{key:"lost",label:"Lost",color:"bg-red-100 border-red-300 text-red-700"}];function Le({lead:t,open:u,onOpenChange:p}){var j,o;const x=I(),[n,s]=_.useState(""),[i,d]=_.useState("overview"),m=D({mutationFn:r=>y.entities.Lead.update(t.id,r),onSuccess:()=>{x.invalidateQueries({queryKey:["leads"]}),s(""),Q.success("Lead updated!")}});if(!t)return null;const l=r=>{if(r===t.status)return;const b={status:r};r==="won"&&n&&(b.actual_revenue=parseFloat(n)),m.mutate(b)},a=((j=t.internal_notes)==null?void 0:j.length)||0,h=!!t.follow_up_reminder;return e.jsx(ce,{open:u,onOpenChange:p,children:e.jsxs(oe,{className:"max-w-4xl max-h-[90vh] overflow-y-auto",children:[e.jsx(de,{children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx(xe,{className:"text-2xl",children:t.name}),t.company&&e.jsx("p",{className:"text-slate-600 mt-1",children:t.company})]}),e.jsx(R,{className:zs[t.status]||"bg-slate-100 text-slate-700",children:(o=t.status)==null?void 0:o.replace(/_/g," ")})]})}),e.jsx("div",{className:"flex flex-wrap gap-1.5 pt-1 pb-2 border-b border-slate-100",children:Vs.map(r=>e.jsx("button",{onClick:()=>l(r.key),disabled:m.isPending,className:`px-3 py-1 rounded-full text-xs font-semibold border transition-all ${t.status===r.key?`${r.color} ring-2 ring-offset-1 ring-current`:"bg-white border-slate-200 text-slate-500 hover:border-slate-400"}`,children:r.label},r.key))}),e.jsxs(Ke,{value:i,onValueChange:d,children:[e.jsxs(Ve,{className:"bg-slate-100 w-full",children:[e.jsxs(T,{value:"overview",className:"flex-1 gap-1.5",children:[e.jsx(Ye,{className:"w-3.5 h-3.5"})," Overview"]}),e.jsxs(T,{value:"notes",className:"flex-1 gap-1.5 relative",children:[e.jsx(es,{className:"w-3.5 h-3.5"})," Notes",a>0&&e.jsx("span",{className:"ml-1 bg-amber-500 text-white text-xs rounded-full w-4 h-4 flex items-center justify-center leading-none",children:a})]}),e.jsxs(T,{value:"reminder",className:"flex-1 gap-1.5",children:[e.jsx(W,{className:`w-3.5 h-3.5 ${h?"text-blue-500":""}`}),"Follow-Up",h&&e.jsx("span",{className:"ml-1 w-2 h-2 rounded-full bg-blue-500"})]}),e.jsx(T,{value:"ai",className:"flex-1 gap-1.5",children:"AI Insights"})]}),e.jsx(B,{value:"overview",className:"mt-4",children:e.jsxs("div",{className:"grid md:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-slate-900 mb-3",children:"Contact Information"}),e.jsxs("div",{className:"space-y-2",children:[t.email&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ee,{className:"w-4 h-4 text-slate-400"}),e.jsx("span",{className:"text-slate-700",children:t.email})]}),t.phone&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(re,{className:"w-4 h-4 text-slate-400"}),e.jsx("span",{className:"text-slate-700",children:t.phone})]}),t.city&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ge,{className:"w-4 h-4 text-slate-400"}),e.jsxs("span",{className:"text-slate-700",children:[t.city,", ",t.state," ",t.zip]})]}),t.address&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(hs,{className:"w-4 h-4 text-slate-400"}),e.jsx("span",{className:"text-slate-700",children:t.address})]})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-slate-900 mb-3",children:"Project Details"}),e.jsxs("div",{className:"space-y-2",children:[t.project_type&&e.jsxs("div",{children:[e.jsx("span",{className:"text-sm text-slate-500",children:"Type: "}),e.jsx("span",{className:"text-slate-900",children:t.project_type})]}),t.estimated_value&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(U,{className:"w-4 h-4 text-slate-400"}),e.jsx("span",{className:"text-sm text-slate-500",children:"Estimated:"}),e.jsxs("span",{className:"font-semibold text-slate-700",children:["$",t.estimated_value.toLocaleStr
30ing()]})]}),t.actual_revenue>0&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ie,{className:"w-4 h-4 text-green-500"}),e.jsx("span",{className:"text-sm text-slate-500",children:"Actual Revenue:"}),e.jsxs("span",{className:"font-bold text-green-600",children:["$",t.actual_revenue.toLocaleString()]})]}),t.expected_close_date&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(z,{className:"w-4 h-4 text-slate-400"}),e.jsx("span",{className:"text-sm text-slate-500",children:"Expected Close:"}),e.jsx("span",{className:"text-slate-900",children:new Date(t.expected_close_date).toLocaleDateString()})]}),t.probability!==void 0&&e.jsxs("div",{children:[e.jsx("span",{className:"text-sm text-slate-500",children:"Win Probability: "}),e.jsxs("span",{className:"text-slate-900",children:[t.probability,"%"]})]})]})]}),t.notes&&e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-slate-900 mb-2",children:"Description"}),e.jsx("p",{className:"text-sm text-slate-700 whitespace-pre-wrap bg-slate-50 rounded-lg p-3",children:t.notes})]})]}),e.jsx("div",{className:"space-y-4",children:e.jsxs("div",{className:"border border-slate-200 rounded-xl p-4 space-y-3 bg-slate-50",children:[e.jsxs("h3",{className:"font-semibold text-slate-900 flex items-center gap-2",children:[e.jsx(Y,{className:"w-4 h-4 text-green-600"}),"Update Status"]}),e.jsxs(te,{onValueChange:r=>{r==="won"&&!n||m.mutate({status:r,...r==="won"&&n?{actual_revenue:parseFloat(n)}:{}})},children:[e.jsx(ae,{children:e.jsx(le,{placeholder:"Change status to..."})}),e.jsx(ne,{children:Ks.map(([r,b])=>e.jsx(C,{value:r,children:b},r))})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(g,{className:"text-xs text-slate-500",children:"Actual Revenue (for Won leads)"}),e.jsx(f,{type:"number",placeholder:"e.g. 4200",value:n,onChange:r=>s(r.target.value),className:"bg-white"})]})]})})]})}),e.jsx(B,{value:"notes",className:"mt-4",children:e.jsx(Os,{lead:t})}),e.jsx(B,{value:"reminder",className:"mt-4",children:e.jsx(Us,{lead:t})}),e.jsx(B,{value:"ai",className:"mt-4",children:e.jsx(Is,{lead:t})})]})]})})}const Qs={new:"bg-slate-100 text-slate-700",contacted:"bg-blue-100 text-blue-700",qualified:"bg-purple-100 text-purple-700",proposal_sent:"bg-amber-100 text-amber-700",negotiating:"bg-orange-100 text-orange-700",won:"bg-green-100 text-green-700",lost:"bg-red-100 text-red-700"},Gs={buildslot:"BuildSlot",referral:"Referral",website:"Website",phone_call:"Phone Call",walk_in:"Walk-in",social_media:"Social Media",advertisement:"Advertisement",other:"Other"};function Hs({leads:t,isLoading:u,defaultOpenLeadId:p}){const[x,n]=_.useState(null);return _.useEffect(()=>{if(p&&t){const s=t.find(i=>i.id===p);s&&n(s)}},[p,t]),u?e.jsx("div",{className:"space-y-4",children:[...Array(5)].map((s,i)=>e.jsx(K,{className:"h-32"},i))}):!t||t.length===0?e.jsx(S,{children:e.jsx(k,{className:"p-12 text-center",children:e.jsx("p",{className:"text-slate-500",children:"No leads yet. Add your first lead to get started."})})}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"space-y-4",children:t.map((s,i)=>e.jsx(E.div,{initial:{opacity:0,y:20},animate:{opacity:1,y:0},transition:{delay:i*.05},children:e.jsx(S,{className:"hover:shadow-md transition-shadow cursor-pointer",onClick:()=>n(s),children:e.jsx(k,{className:"p-6",children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1 space-y-3",children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-lg text-slate-900",children:s.name}),s.company&&e.jsx("p",{className:"text-slate-600",children:s.company})]}),e.jsx(R,{className:Qs[s.status],children:s.status.replace(/_/g," ")})]}),e.jsxs("div",{className:"flex flex-wrap gap-4",children:[s.email&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-slate-600",children:[e.jsx(ee,{className:"w-4 h-4"}),e.jsx("span",{children:s.email})]}),s.phone&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-slate-600",children:[e.jsx(re,{className:"w-4 h-4"}),e.jsx("span",{children:s.phone})]}),s.city&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-slate-600",children:[e.jsx(ge,{className:"w-4 h-4"}),e.jsxs("span",{children:[s.city,", ",s.state]})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-3",children:[s.project_type&&e.jsx(R,{variant:"outline",children:s.project_type}),s.source&&e.jsxs(R,{variant:"outline",className:"text-xs",children:["Source: ",Gs[s.source]]}),s.estimated_value&&e.jsxs("div",{className:"flex items-center gap-1 text-sm font-medium text-green-600",children:[e.jsx(U,{className:"w-4 h-4"}
30),e.jsxs("span",{children:["$",s.estimated_value.toLocaleString()]})]}),s.expected_close_date&&e.jsxs("div",{className:"flex items-center gap-1 text-sm text-slate-600",children:[e.jsx(z,{className:"w-4 h-4"}),e.jsxs("span",{children:["Close: ",new Date(s.expected_close_date).toLocaleDateString()]})]})]}),s.notes&&e.jsx("p",{className:"text-sm text-slate-600 line-clamp-2",children:s.notes})]}),e.jsx(w,{variant:"ghost",size:"icon",children:e.jsx(Xe,{className:"w-4 h-4"})})]})})})},s.id))}),e.jsx(Le,{lead:x,open:!!x,onOpenChange:s=>!s&&n(null)})]})}function Ys({bid:t}){var s,i,d;const[u,p]=_.useState(null),x=D({mutationFn:async()=>{const m=`Analyze this construction bid and provide optimization insights:
31
32Bid Details:
33- Project: ${t.project_title}
34- Customer: ${t.customer_name}
35- Bid Amount: $${t.bid_amount}
36- Labor Cost: $${t.labor_cost||"Not specified"}
37- Materials Cost: $${t.materials_cost||"Not specified"}
38- Duration: ${t.estimated_duration_days||"Not specified"} days
39- Status: ${t.status}
40- Description: ${t.project_description||"None"}
41
42Provide:
431. Win probability percentage (0-100)
442. Pricing assessment (is it competitive, too high, too low?)
453. Key strengths of this bid
464. Potential weaknesses or concerns
475. 3 specific recommendations to improve win rate`;return await y.integrations.Core.InvokeLLM({prompt:m,response_json_schema:{type:"object",properties:{win_probability:{type:"number"},pricing_assessment:{type:"string"},strengths:{type:"array",items:{type:"string"}},weaknesses:{type:"array",items:{type:"string"}},recommendations:{type:"array",items:{type:"string"}}}}})},onSuccess:m=>{p(m)}}),n=m=>m>=70?"text-green-600 bg-green-100":m>=40?"text-amber-600 bg-amber-100":"text-red-600 bg-red-100";return e.jsxs(S,{children:[e.jsx($,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(M,{className:"flex items-center gap-2",children:[e.jsx(Qe,{className:"w-5 h-5 text-blue-600"}),"AI Bid Analysis"]}),e.jsx(w,{onClick:()=>x.mutate(),disabled:x.isPending,size:"sm",className:"bg-blue-600 hover:bg-blue-700",children:x.isPending?"Analyzing...":"Analyze Bid"})]})}),e.jsx(k,{children:e.jsxs(Ge,{mode:"wait",children:[!u&&!x.isPending&&e.jsx(E.div,{initial:{opacity:0}
47,animate:{opacity:1},exit:{opacity:0},className:"text-center py-8 text-slate-500",children:"Get AI-powered insights to optimize your bid"}),x.isPending&&e.jsxs(E.div,{initial:{opacity:0},animate:{opacity:1},exit:{opacity:0},className:"text-center py-8",children:[e.jsx("div",{className:"animate-spin w-8 h-8 border-4 border-blue-600 border-t-transparent rounded-full mx-auto mb-3"}),e.jsx("p",{className:"text-slate-600",children:"Analyzing bid with AI..."})]}),u&&e.jsxs(E.div,{initial:{opacity:0,y:20},animate:{opacity:1,y:0},exit:{opacity:0,y:-20},className:"space-y-6",children:[e.jsxs("div",{className:"text-center p-4 bg-slate-50 rounded-lg",children:[e.jsx("p",{className:"text-sm text-slate-600 mb-2",children:"Win Probability"}),e.jsxs("div",{className:`inline-flex items-center gap-2 px-4 py-2 rounded-full font-bold text-2xl ${n(u.win_probability)}`,children:[e.jsx(ue,{className:"w-6 h-6"}),u.win_probability,"%"]})]}),e.jsxs("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-4",children:[e.jsxs("h4",{className:"font-semibold text-blue-900 mb-2 flex items-center gap-2",children:[e.jsx(U,{className:"w-4 h-4"}),"Pricing Assessment"]}),e.jsx("p",{className:"text-sm text-blue-800",children:u.pricing_assessment})]}),e.jsxs("div",{children:[e.jsxs("h4",{className:"font-semibold text-slate-900 mb-3 flex items-center gap-2",children:[e.jsx(ie,{className:"w-4 h-4 text-green-600"}),"Bid Strengths"]}),e.jsx("div",{className:"space-y-2",children:(s=u.strengths)==null?void 0:s.map((m,l)=>e.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[e.jsx("span",{className:"text-green-600 mt-0.5",children:"✓"}),e.jsx("span",{className:"text-slate-700",children:m})]},l))})]}),e.jsxs("div",{children:[e.jsxs("h4",{className:"font-semibold text-slate-900 mb-3 flex items-center gap-2",children:[e.jsx(Ae,{className:"w-4 h-4 text-amber-600"}),"Potential Weaknesses"]}),e.jsx("div",{className:"space-y-2",children:(i=u.weaknesses)==null?void 0:i.map((m,l)=>e.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[e.jsx("span",{className:"text-amber-600 mt-0.5",children:"⚠"}),e.jsx("span",{className:"text-slate-700",children:m})]},l))})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold text-slate-900 mb-3",children:"Recommendations to Improve"}),e.jsx("div",{className:"space-y-2",children:(d=u.recommendations)==null?void 0:d.map((m,l)=>e.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[e.jsx(R,{variant:"outline",className:"mt-0.5",children:l+1}),e.jsx("span",{className:"text-slate-700",children:m})]},l))})]})]})]})})]})}const Xs={draft:{label:"Draft",color:"bg-slate-100 text-slate-700"},sent:{label:"Sent",color:"bg-blue-100 text-blue-700"},viewed:{label:"Viewed",color:"bg-purple-100 text-purple-700"},accepted:{label:"Accepted",color:"bg-green-100 text-green-700"},rejected:{label:"Rejected",color:"bg-red-100 text-red-700"},expired:{label:"Expired",color:"bg-amber-100 text-amber-700"}};function Js({bid:t,open:u,onOpenChange:p}){var n;if(!t)return null;const x=Xs[t.status];return e.jsx(ce,{open:u,onOpenChange:p,children:e.jsxs(oe,{className:"max-w-4xl max-h-[90vh] overflow-y-auto",children:[e.jsx(de,{children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{children:[e.jsx(xe,{className:"text-2xl",children:t.project_title}),e.jsx("p",{className:"text-slate-600 mt-1",children:t.customer_name})]}),e.jsx(R,{className:x.color,children:x.label})]})}),e.jsxs("div",{className:"grid md:grid-cols-2 gap-6 pt-4",children:[e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-slate-900 mb-3",children:"Customer Information"}),e.jsxs("div",{className:"space-y-2",children:[t.customer_email&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ee,{className:"w-4 h-4 text-slate-400"}),e.jsx("span",{className:"text-slate-700",children:t.customer_email})]}),t.customer_phone&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(re,{className:"w-4 h-4 text-slate-400"}),e.jsx("span",{className:"text-slate-700",children:t.customer_phone})]}),t.project_address&&e.jsxs("div",{className:"text-sm",children:[e.jsx("span",{className:"text-slate-500",children:"Address:"}),e.jsx("p",{className:"text-slate-900",children:t.project_address})]})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-slate-900 mb-3",children:"Bid Breakdown"}),e.jsxs("div",{className:"space-y-3 bg-slate-50 rounded-lg p-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-sm text-slate-600",children:"Total Bid Amount"}),e.jsxs("span",{className:"font-bold text-lg text-green-600",children:["$",(n=t.bid_amount)==null?void 0:n.toLocaleStr
47ing()]})]}),t.labor_cost&&e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{className:"text-slate-600",children:"Labor Cost"}),e.jsxs("span",{className:"text-slate-900",children:["$",t.labor_cost.toLocaleString()]})]}),t.materials_cost&&e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{className:"text-slate-600",children:"Materials Cost"}),e.jsxs("span",{className:"text-slate-900",children:["$",t.materials_cost.toLocaleString()]})]}),t.other_costs&&e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{className:"text-slate-600",children:"Other Costs"}),e.jsxs("span",{className:"text-slate-900",children:["$",t.other_costs.toLocaleString()]})]})]})]}),t.project_description&&e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-slate-900 mb-2",children:"Project Description"}),e.jsx("p",{className:"text-sm text-slate-700 whitespace-pre-wrap",children:t.project_description})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[t.estimated_duration_days&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(G,{className:"w-4 h-4 text-slate-400"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-slate-500",children:"Duration"}),e.jsxs("p",{className:"text-sm font-medium",children:[t.estimated_duration_days," days"]})]})]}),t.proposed_start_date&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(z,{className:"w-4 h-4 text-slate-400"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-slate-500",children:"Start Date"}),e.jsx("p",{className:"text-sm font-medium",children:new Date(t.proposed_start_date).toLocaleDateString()})]})]})]})]}),e.jsx("div",{children:e.jsx(Ys,{bid:t})})]})]})})}const Ee={draft:{label:"Draft",color:"bg-slate-100 text-slate-700",icon:G},sent:{label:"Sent",color:"bg-blue-100 text-blue-700",icon:De},viewed:{label:"Viewed",color:"bg-purple-100 text-purple-700",icon:De},accepted:{label:"Accepted",color:"bg-green-100 text-green-700",icon:Se},rejected:{label:"Rejected",color:"bg-red-100 text-red-700",icon:gs},expired:{label:"Expired",color:"bg-amber-100 text-amber-700",icon:G}};function Zs({bids:t,isLoading:u,contractorId:p}){const[x,n]=_.useState(null);if(u)return e.jsx("div",{className:"space-y-4",children:[...Array(5)].map((d,m)=>e.jsx(K,{className:"h-40"},m))});const s=(t==null?void 0:t.filter(d=>d.status==="quote_requested"))||[],i=(t==null?void 0:t.filter(d=>d.status!=="quote_requested"))||[];return!t||t.length===0?e.jsx(S,{children:e.jsx(k,{className:"p-12 text-center",children:e.jsx("p",{className:"text-slate-500",children:"No bids yet. Create your first bid to get started."})})}):e.jsxs(e.Fragment,{children:[s.length>0&&e.jsxs("div",{className:"mb-6 space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(W,{className:"w-4 h-4 text-amber-500"}),e.jsxs("h3",{className:"font-semibold text-slate-800",children:["Incoming Quote Requests",e.jsxs("span",{className:"ml-2 px-2 py-0.5 text-xs bg-amber-100 text-amber-700 rounded-full",children:[s.length," new"]})]})]}),s.map(d=>e.jsx(ps,{bid:d,contractorId:p},d.id)),i.length>0&&e.jsx("hr",{className:"border-slate-200 mt-4"})]}),e.jsx("div",{className:"space-y-4",children:i.map((d,m)=>{var h;const l=Ee[d.status]||Ee.draft,a=l.icon;return e.jsx(E.div,{initial:{opacity:0,y:20},animate:{opacity:1,y:0},transition:{delay:m*.05},children:e.jsx(S,{className:"hover:shadow-md transition-shadow cursor-pointer",onClick:()=>n(d),children:e.jsx(k,{className:"p-6",children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1 space-y-3",children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-lg text-slate-900",children:d.project_title}),e.jsx("p",{className:"text-slate-600",children:d.customer_name})]}),e.jsxs(R,{className:l.color,children:[e.jsx(a,{className:"w-3 h-3 mr-1"}),l.label]})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(U,{className:"w-5 h-5 text-green-600"}),e.jsxs("span",{className:"text-2xl font-bold text-green-600",children:["$",(h=d.bid_amount)==null?void 0:h.toLocaleStr
47ing()]})]}),d.project_description&&e.jsx("p",{className:"text-sm text-slate-600 line-clamp-2",children:d.project_description}),e.jsxs("div",{className:"flex flex-wrap gap-4 text-sm text-slate-600",children:[d.sent_date&&e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(z,{className:"w-4 h-4"}),e.jsxs("span",{children:["Sent: ",new Date(d.sent_date).toLocaleDateString()]})]}),d.expiry_date&&e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(G,{className:"w-4 h-4"}),e.jsxs("span",{children:["Expires: ",new Date(d.expiry_date).toLocaleDateString()]})]}),d.estimated_duration_days&&e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(z,{className:"w-4 h-4"}),e.jsxs("span",{children:[d.estimated_duration_days," days"]})]})]}),d.line_items&&d.line_items.length>0&&e.jsx("div",{className:"pt-2 border-t border-slate-100",children:e.jsxs("p",{className:"text-xs text-slate-500 mb-1",children:[d.line_items.length," line items"]})})]}),e.jsx(w,{variant:"ghost",size:"icon",children:e.jsx(Xe,{className:"w-4 h-4"})})]})})})},d.id)})}),e.jsx(Js,{bid:x,open:!!x,onOpenChange:d=>!d&&n(null)})]})}const Fe=[{key:"new",label:"New",color:"bg-slate-500",light:"bg-slate-50  border-slate-200",badge:"bg-slate-100 text-slate-700"},{key:"contacted",label:"Contacted",color:"bg-blue-500",light:"bg-blue-50   border-blue-200",badge:"bg-blue-100  text-blue-700"},{key:"proposal_sent",label:"Quote Sent",color:"bg-amber-500",light:"bg-amber-50  border-amber-200",badge:"bg-amber-100 text-amber-700"},{key:"negotiating",label:"Negotiating",color:"bg-orange-500",light:"bg-orange-50 border-orange-200",badge:"bg-orange-100 text-orange-700"},{key:"won",label:"Won",color:"bg-green-500",light:"bg-green-50  border-green-200",badge:"bg-green-100 text-green-700"},{key:"lost",label:"Lost",color:"bg-red-500",light:"bg-red-50    border-red-200",badge:"bg-red-100   text-red-700"}];function et({leads:t,isLoading:u,contractorId:p}){const[x,n]=_.useState(t||[]),[s,i]=_.useState(null),d=I();_.useEffect(()=>{t&&n(t)},[t]);const m=async l=>{const{destination:a,source:h,draggableId:j}=l;if(!a||a.droppableId===h.droppableId&&a.index===h.index)return;const o=a.droppableId;n(r=>r.map(b=>b.id===j?{...b,status:o}:b));try{await y.entities.Lead.update(j,{status:o}),d.invalidateQueries({queryKey:["leads",p]})}catch(r){console.error("Failed to update lead status:",r),n(t||[])}};return u?e.jsx("div",{className:"flex gap-4 overflow-x-auto pb-4",children:Fe.map(l=>e.jsx(K,{className:"h-96 w-56 shrink-0"},l.key))}):e.jsxs(e.Fragment,{children:[e.jsx(js,{onDragEnd:m,children:e.jsx("div",{className:"flex gap-3 overflow-x-auto pb-6 -mx-1 px-1",children:Fe.map(l=>{const a=x.filter(j=>j.status===l.key),h=a.reduce((j,o)=>j+(o.estimated_value||0),0);return e.jsxs("div",{className:"flex flex-col shrink-0 w-56",children:[e.jsxs("div",{className:`rounded-t-xl border px-3 py-3 ${l.light}`,children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:`w-2 h-2 rounded-full ${l.color}`}),e.jsx("span",{className:"font-semibold text-sm text-slate-800",children:l.label})]}),e.jsx(R,{className:`${l.badge} text-xs font-semibold`,children:a.length})]}),h>0&&e.jsxs("div",{className:"flex items-center gap-1 mt-1.5 text-xs text-slate-500 font-medium",children:[e.jsx(U,{className:"w-3 h-3"}),h.toLocaleString()]})]}),e.jsx(bs,{droppableId:l.key,children:(j,o)=>e.jsxs("div",{ref:j.innerRef,...j.droppableProps,className:`flex-1 min-h-[420px] border-x border-b rounded-b-xl p-2 space-y-2 transition-colors ${o.isDraggingOver?`${l.light} border-opacity-80`:"bg-slate-50 border-slate-200"}`,children:[a.map((r,b)=>e.jsx(Ns,{draggableId:r.id,index:b,children:(N,c)=>e.jsxs("div",{ref:N.innerRef,...N.draggableProps,className:`bg-white rounded-lg border border-slate-200 p-3 space-y-2 select-none transition-shadow ${c.isDragging?"shadow-lg ring-2 ring-blue-300 rotate-1":"hover:shadow-md"}`,children:[e.jsxs("div",{className:"flex items-start gap-1.5",children:[e.jsx("div",{...N.dragHandleProps,className:"mt-0.5 text-slate-300 hover:text-slate-500 cursor-grab active:cursor-grabbing shrink-0",children:e.jsx(ms,{className:"w-3.5 h-3.5"})}),e.jsxs("div",{className:"min-w-0 flex-1 cursor-pointer",onClick:()=>i(r),children:[e.jsx("p",{className:"font-semibold text-sm text-slate-900 leading-tight truncate",children:r.name}),r.company&&e.jsx("p",{className:"text-xs text-slate-500 truncate",children:r.company})]})]}),r.project_type&&e.jsx(R,{variant:"outline",className:"text-xs truncate max-w-full",children:r.project_type.replace(/_/g," ")}),r.estimated_value>0&&e.jsxs("div",{className:"flex items-center gap-1 text-xs font-semibold text-green-600",children:[e.jsx(U,{className:"w-3 h-3 shrink-0"}
47),r.estimated_value.toLocaleString()]}),r.expected_close_date&&e.jsxs("div",{className:"flex items-center gap-1 text-xs text-slate-400",children:[e.jsx(z,{className:"w-3 h-3 shrink-0"}),new Date(r.expected_close_date).toLocaleDateString("en-US",{month:"short",day:"numeric"})]})]})},r.id)),j.placeholder,a.length===0&&e.jsx("div",{className:"h-16 flex items-center justify-center text-xs text-slate-300 border-2 border-dashed border-slate-200 rounded-lg",children:"Drop here"})]})})]},l.key)})})}),e.jsx(Le,{lead:s,open:!!s,onOpenChange:l=>!l&&i(null)})]})}function st({open:t,onOpenChange:u,contractorId:p,contractor:x}){const n=I(),{data:s}=O({queryKey:["planLimits",x==null?void 0:x.subscription_plan],queryFn:()=>y.entities.PlanLimits.filter({plan_name:(x==null?void 0:x.subscription_plan)||"free"}),enabled:!!x}),i=s==null?void 0:s[0],{data:d}=O({queryKey:["leads-count",p],queryFn:()=>y.entities.Lead.filter({contractor_id:p}),enabled:!!p}),m=i&&d?d.length>=i.max_leads:!1,[l,a]=_.useState({name:"",email:"",phone:"",company:"",source:"buildslot",status:"new",project_type:"",estimated_value:"",probability:50,expected_close_date:"",address:"",city:"",state:"",zip:"",notes:""}),[h,j]=_.useState(!1),o=h&&!$e(l.email)?fs:null,r=D({mutationFn:c=>y.entities.Lead.create({...c,contractor_id:p}),onSuccess:()=>{n.invalidateQueries({queryKey:["leads"]}),u(!1),a({name:"",email:"",phone:"",company:"",source:"buildslot",status:"new",project_type:"",estimated_value:"",probability:50,expected_close_date:"",address:"",city:"",state:"",zip:"",notes:""})}}),b=se.lead_estimated_value(l.estimated_value),N=c=>{if(c.preventDefault(),j(!0),!$e(l.email)||b)return;const v={...l};v.estimated_value&&(v.estimated_value=parseFloat(v.estimated_value)),r.mutate(v)};return e.jsx(ce,{open:t,onOpenChange:u,children:e.jsxs(oe,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(de,{children:e.jsx(xe,{children:"Add New Lead"})}),m&&e.jsxs("div",{className:"flex items-start gap-2 p-3 bg-amber-50 border border-amber-200 rounded-lg text-sm text-amber-800 mt-2",children:[e.jsx(Ae,{className:"w-4 h-4 mt-0.5 shrink-0 text-amber-600"}),e.jsxs("span",{children:["You've reached the ",e.jsxs("strong",{children:[i==null?void 0:i.max_leads," lead limit"]})," for your"," ",e.jsx("span",{className:"capitalize",children:x==null?void 0:x.subscription_plan})," plan."," ",e.jsx("a",{href:"/ContractorPricing",className:"underline font-semibold",children:"Upgrade"})," to add more leads."]})]}),e.jsxs("form",{onSubmit:N,className:"space-y-4 pt-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Name *"}),e.jsx(f,{value:l.name,onChange:c=>a({...l,name:c.target.value}),required:!0})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Company"}),e.jsx(f,{value:l.company,onChange:c=>a({...l,company:c.target.value})})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Email"}),e.jsx(f,{type:"email",value:l.email,onChange:c=>a({...l,email:c.target.value}),onBlur:()=>j(!0),className:o?"border-red-500 focus-visible:ring-red-500":""}),o&&e.jsx("p",{className:"text-xs text-red-600",children:o})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Phone"}),e.jsx(f,{value:l.phone,onChange:c=>a({...l,phone:c.target.value})})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Source"}),e.jsxs(te,{value:l.source,onValueChange:c=>a({...l,source:c}),children:[e.jsx(ae,{children:e.jsx(le,{})}),e.jsxs(ne,{children:[e.jsx(C,{value:"buildslot",children:"BuildSlot"}),e.jsx(C,{value:"referral",children:"Referral"}),e.jsx(C,{value:"website",children:"Website"}),e.jsx(C,{value:"phone_call",children:"Phone Call"}),e.jsx(C,{value:"walk_in",children:"Walk-in"}),e.jsx(C,{value:"social_media",children:"Social Media"}),e.jsx(C,{value:"advertisement",children:"Advertisement"}),e.jsx(C,{value:"other",children:"Other"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Status"}),e.jsxs(te,{value:l.status,onValueChange:c=>a({...l,status:c}),children:[e.jsx(ae,{children:e.jsx(le,{})}),e.jsxs(ne,{children:[e.jsx(C,{value:"new",children:"New"}),e.jsx(C,{value:"contacted",children:"Contacted"}
47),e.jsx(C,{value:"qualified",children:"Qualified"}),e.jsx(C,{value:"proposal_sent",children:"Proposal Sent"}),e.jsx(C,{value:"negotiating",children:"Negotiating"})]})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Project Type"}),e.jsx(f,{value:l.project_type,onChange:c=>a({...l,project_type:c.target.value}),placeholder:"e.g. Kitchen Remodel"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Estimated Value ($)"}),e.jsx(f,{type:"number",value:l.estimated_value,onChange:c=>a({...l,estimated_value:c.target.value}),className:b?"border-red-500":""}),b&&e.jsx("p",{className:"text-xs text-red-600",children:b})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Expected Close Date"}),e.jsx(f,{type:"date",value:l.expected_close_date,onChange:c=>a({...l,expected_close_date:c.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Address"}),e.jsx(f,{value:l.address,onChange:c=>a({...l,address:c.target.value})})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"City"}),e.jsx(f,{value:l.city,onChange:c=>a({...l,city:c.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"State"}),e.jsx(_s,{value:l.state,onChange:c=>a({...l,state:c})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"ZIP"}),e.jsx(f,{value:l.zip,onChange:c=>a({...l,zip:c.target.value})})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Notes"}),e.jsx(F,{value:l.notes,onChange:c=>a({...l,notes:c.target.value}),rows:3})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4",children:[e.jsx(w,{type:"button",variant:"outline",onClick:()=>u(!1),children:"Cancel"}),e.jsx(w,{type:"submit",className:"bg-green-600 hover:bg-green-700",disabled:m,children:"Add Lead"})]})]})]})})}function tt({open:t,onOpenChange:u,contractorId:p}){const x=I(),[n,s]=_.useState({customer_name:"",customer_email:"",customer_phone:"",project_title:"",project_description:"",project_address:"",bid_amount:"",labor_cost:"",materials_cost:"",other_costs:"",estimated_duration_days:"",proposed_start_date:"",expiry_date:"",notes:""}),i=D({mutationFn:a=>y.entities.Bid.create({...a,contractor_id:p,status:"draft"}),onSuccess:()=>{x.invalidateQueries({queryKey:["bids"]}),u(!1),s({customer_name:"",customer_email:"",customer_phone:"",project_title:"",project_description:"",project_address:"",bid_amount:"",labor_cost:"",materials_cost:"",other_costs:"",estimated_duration_days:"",proposed_start_date:"",expiry_date:"",notes:""})}}),d={bid_amount:se.bid_amount(n.bid_amount),labor_cost:se.bid_labor_cost(n.labor_cost),materials_cost:se.bid_materials_cost(n.materials_cost),other_costs:se.bid_other_costs(n.other_costs),estimated_duration_days:se.bid_duration_days(n.estimated_duration_days)},m=Object.values(d).some(Boolean),l=a=>{if(a.preventDefault(),m)return;const h={...n};h.bid_amount&&(h.bid_amount=parseFloat(h.bid_amount)),h.labor_cost&&(h.labor_cost=parseFloat(h.labor_cost)),h.materials_cost&&(h.materials_cost=parseFloat(h.materials_cost)),h.other_costs&&(h.other_costs=parseFloat(h.other_costs)),h.estimated_duration_days&&(h.estimated_duration_days=parseInt(h.estimated_duration_days)),i.mutate(h)};return e.jsx(ce,{open:t,onOpenChange:u,children:e.jsxs(oe,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(de,{children:e.jsx(xe,{children:"Create New Bid"})}),e.jsxs("form",{onSubmit:l,className:"space-y-4 pt-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Project Title *"}),e.jsx(f,{value:n.project_title,onChange:a=>s({...n,project_title:a.target.value}),required:!0})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Customer Name *"}),e.jsx(f,{value:n.customer_name,onChange:a=>s({...n,customer_name:a.target.value}),required:!0})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Customer Email"}),e.jsx(f,{type:"email",value:n.customer_email,onChange:a=>s({...n,customer_email:a.target.value})})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Customer Phone"}),e.jsx(f,{value:n.customer_phone,onChange:a=>s({...n,customer_phone:a.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Project Address"}),e.jsx(f,{value:n.project_address,onChange:a=>s({...n,project_address:a.target.value})})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Project Description"}),e.jsx(F,{value:n.project_description,onChange:a=>s({...n,project_description:a.target.value}),rows:3})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Bid Amount ($) *"}),e.jsx(f,{type:"number",value:n.bid_amount,onChange:a=>s({...n,bid_amount:a.target.value}),className:d.bid_amount?"border-red-500":"",required:!0}),d.bid_amount&&e.jsx("p",{className:"text-xs text-red-600",children:d.bid_amount})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Labor Cost ($)"}),e.jsx(f,{type:"number",value:n.labor_cost,onChange:a=>s({...n,labor_cost:a.target.value}),className:d.labor_cost?"border-red-500":""}),d.labor_cost&&e.jsx("p",{className:"text-xs text-red-600",children:d.labor_cost})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Materials Cost ($)"}),e.jsx(f,{type:"number",value:n.materials_cost,onChange:a=>s({...n,materials_cost:a.target.value}),className:d.materials_cost?"border-red-500":""}),d.materials_cost&&e.jsx("p",{className:"text-xs text-red-600",children:d.materials_cost})]})]}),e.jsx("div",{className:"grid grid-cols-2 gap-4",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Other Costs ($)"}),e.jsx(f,{type:"number",value:n.other_costs,onChange:a=>s({...n,other_costs:a.target.value}),className:d.other_costs?"border-red-500":""}),d.other_costs&&e.jsx("p",{className:"text-xs text-red-600",children:d.other_costs})]})}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Duration (days)"}),e.jsx(f,{type:"number",value:n.estimated_duration_days,onChange:a=>s({...n,estimated_duration_days:a.target.value}),className:d.estimated_duration_days?"border-red-500":""}),d.estimated_duration_days&&e.jsx("p",{className:"text-xs text-red-600",children:d.estimated_duration_days})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Start Date"}),e.jsx(f,{type:"date",value:n.proposed_start_date,onChange:a=>s({...n,proposed_start_date:a.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Expiry Date"}),e.jsx(f,{type:"date",value:n.expiry_date,onChange:a=>s({...n,expiry_date:a.target.value})})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Internal Notes"}),e.jsx(F,{value:n.notes,onChange:a=>s({...n,notes:a.target.value}),rows:2})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4",children:[e.jsx(w,{type:"button",variant:"outline",onClick:()=>u(!1),children:"Cancel"}),e.jsx(w,{type:"submit",disabled:m,className:"bg-green-600 hover:bg-green-700",children:"Create Bid"})]})]})]})})}function at({open:t,onOpenChange:u,contractorId:p,rule:x}){const n=I(),[s,i]=_.useState({name:"",trigger_type:"lead_status_change",trigger_condition:{},action_type:"send_email",action_config:{}});_.useEffect(()=>{i(x?{name:x.name,trigger_type:x.trigger_type,trigger_condition:x.trigger_condition||{},action_type:x.action_type,action_config:x.action_config||{}}:{name:"",trigger_type:"lead_status_change",trigger_condition:{},action_type:"send_email",action_config:{}})},[x,t]);const d=D({mutationFn:l=>x?y.entities.AutomationRule.update(x.id,l):y.entities.AutomationRule.create({...l,contractor_id:p}),onSuccess:()=>{n.invalidateQueries({queryKey:["automation-rules"]}),u(!1)}}),m=l=>{l.preventDefault(),d.mutate(s)};return e.jsx(ce,{open:t,onOpenChange:u,children:e.jsxs(oe,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(de,{children:e.jsxs(xe,{children:[x?"Edit":"Create"," Automation Rule"]})}),e.jsxs("form",{onSubmit:m,className:"space-y-6 pt-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Rule Name *"}),e.jsx(f,{value:s.name,onChange:l=>i({...s,name:l.target.value}),placeholder:"e.g., Follow up on qualified leads",required:!0})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Trigger *"}),e.jsxs(te,{value:s.trigger_type,onValueChange:l=>i({...s,trigger_type:l,trigger_condition:{}}),children:[e.jsx(ae,{children:e.jsx(le,{})}),e.jsxs(ne,{children:[e.jsx(C,{value:"lead_status_change",children:"Lead Status Changes"}),e.jsx(C,{value:"bid_status_change",children:"Bid Status Changes"}),e.jsx(C,{value:"bid_expiring_soon",children:"Bid Expiring Soon"}),e.jsx(C,{value:"lead_no_followup",children:"Lead Needs Follow-up"}),e.jsx(C,{value:"lead_created",children:"New Lead Created"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Action *"}),e.jsxs(te,{value:s.action_type,onValueChange:l=>i({...s,action_type:l,action_config:{}}),children:[e.jsx(ae,{children:e.jsx(le,{})}),e.jsxs(ne,{children:[e.jsx(C,{value:"send_email",children:"Send Email"}),e.jsx(C,{value:"send_notification",children:"Send Notification"}),e.jsx(C,{value:"create_task",children:"Create Task"})]})]})]})]}),(s.trigger_type==="lead_status_change"||s.trigger_type==="bid_status_change")&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"When status becomes"}),e.jsxs(te,{value:s.trigger_condition.status||"",onValueChange:l=>i({...s,trigger_condition:{...s.trigger_condition,status:l}}),children:[e.jsx(ae,{children:e.jsx(le,{placeholder:"Select status"})}),e.jsx(ne,{children:s.trigger_type==="lead_status_change"?e.jsxs(e.Fragment,{children:[e.jsx(C,{value:"qual
47ified",children:"Qualified"}),e.jsx(C,{value:"proposal_sent",children:"Proposal Sent"}),e.jsx(C,{value:"negotiating",children:"Negotiating"}),e.jsx(C,{value:"won",children:"Won"}),e.jsx(C,{value:"lost",children:"Lost"})]}):e.jsxs(e.Fragment,{children:[e.jsx(C,{value:"sent",children:"Sent"}),e.jsx(C,{value:"viewed",children:"Viewed"}),e.jsx(C,{value:"accepted",children:"Accepted"}),e.jsx(C,{value:"rejected",children:"Rejected"})]})})]})]}),(s.trigger_type==="bid_expiring_soon"||s.trigger_type==="lead_no_followup")&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Days threshold"}),e.jsx(f,{type:"number",value:s.trigger_condition.days_threshold||"",onChange:l=>i({...s,trigger_condition:{...s.trigger_condition,days_threshold:parseInt(l.target.value)}}),placeholder:"e.g., 3"}),e.jsx("p",{className:"text-xs text-slate-500",children:s.trigger_type==="bid_expiring_soon"?"Trigger when bid expires in X days":"Trigger when no follow-up after X days"})]}),s.action_type==="send_email"&&e.jsxs("div",{className:"space-y-4 p-4 bg-slate-50 rounded-lg",children:[e.jsx("h4",{className:"font-semibold text-slate-900",children:"Email Configuration"}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Email Subject"}),e.jsx(f,{value:s.action_config.email_subject||"",onChange:l=>i({...s,action_config:{...s.action_config,email_subject:l.target.value}}),placeholder:"e.g., Follow up on your proposal"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Email Body"}),e.jsx(F,{value:s.action_config.email_body||"",onChange:l=>i({...s,action_config:{...s.action_config,email_body:l.target.value}}),rows:4,placeholder:"Email content..."}),e.jsxs("p",{className:"text-xs text-slate-500",children:["Use variables: ","{lead_name}",", ","{company}",", ","{project_type}"]})]})]}),s.action_type==="send_notification"&&e.jsxs("div",{className:"space-y-2 p-4 bg-slate-50 rounded-lg",children:[e.jsx("h4",{className:"font-semibold text-slate-900",children:"Notification Configuration"}),e.jsx(g,{children:"Notification Message"}),e.jsx(F,{value:s.action_config.notification_message||"",onChange:l=>i({...s,action_config:{...s.action_config,notification_message:l.target.value}}),rows:2,placeholder:"Notification message..."})]}),s.action_type==="create_task"&&e.jsxs("div",{className:"space-y-4 p-4 bg-slate-50 rounded-lg",children:[e.jsx("h4",{className:"font-semibold text-slate-900",children:"Task Configuration"}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Task Title"}),e.jsx(f,{value:s.action_config.task_title||"",onChange:l=>i({...s,action_config:{...s.action_config,task_title:l.target.value}}),placeholder:"e.g., Follow up with lead"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Task Description"}),e.jsx(F,{value:s.action_config.task_description||"",onChange:l=>i({...s,action_config:{...s.action_config,task_description:l.target.value}}),rows:2})]})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4",children:[e.jsx(w,{type:"button",variant:"outline",onClick:()=>u(!1),children:"Cancel"}),e.jsxs(w,{type:"submit",className:"bg-amber-600 hover:bg-amber-700",children:[x?"Update":"Create"," Rule"]})]})]})]})})}const lt={lead_status_change:"Lead Status Changes",bid_status_change:"Bid Status Changes",bid_expiring_soon:"Bid Expiring Soon",lead_no_followup:"Lead Needs Follow-up",lead_created:"New Lead Created"},nt={send_email:ee,create_task:ys,send_notification:W,update_status:Je};function it({contractorId:t}){const[u,p]=_.useState(!1),[x,n]=_.useState(null),s=I(),{data:i,isLoading:d}=O({queryKey:["automation-rules",t],queryFn:()=>y.entities.AutomationRule.filter({contractor_id:t},"-created_date"),enabled:!!t}),m=D({mutationFn:({id:a,is_active:h})=>y.entities.AutomationRule.update(a,{is_active:h}),onSuccess:()=>{s.invalidateQueries({queryKey:["automation-rules"]})}}),l=D({mutationFn:a=>y.entities.AutomationRule.delete(a),onSuccess:()=>{s.invalidateQueries({queryKey:["automation-rules"]})}});return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("h2",{className:"text-2xl font-bold text-slate-900 flex items-center gap-2",children:[e.jsx(P,{className:"w-6 h-6 text-amber-600"}),"Automation Rules"]}),e.jsx("p",{className:"text-slate-600 mt-1",children:"Set up automated follow-ups and reminders"})]}),e.jsxs(w,{onClick:()=>{n(null),p(!0)},className:"bg-amber-600 hover:bg-amber-700",children:[e.jsx(J,{className:"w-4 h-4 mr-2"}),"New Rule"]})]}),d?e.jsx("div",{className:"grid md:grid-cols-2 gap-4",children:[...Array(4)].map((a,h)=>e.jsx(S,{className:"animate-pulse",children:e.jsxs(k,{className:"p-6 space-y-3",children:[e.jsx("div",{className:"h-5 bg-slate-200 rounded w-2/3"}),e.jsx("div",{className:"h-4 bg-slate-200 rounded w-full"})]})},h))}):i&&i.length>0?e.jsx("div",{className:"grid md:grid-cols-2 gap-4",children:i.map((a,h)=>{var o;const j=nt[a.action_type]||W;return e.jsx(E.div,{initial:{opacity:0,y:20}
47,animate:{opacity:1,y:0},transition:{delay:h*.05},children:e.jsxs(S,{className:`${!a.is_active&&"opacity-60"}`,children:[e.jsx($,{className:"pb-3",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsx("div",{className:"flex-1",children:e.jsxs(M,{className:"text-lg flex items-center gap-2",children:[e.jsx(j,{className:"w-4 h-4 text-amber-600"}),a.name]})}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(X,{checked:a.is_active,onCheckedChange:r=>m.mutate({id:a.id,is_active:r})}),e.jsx(w,{variant:"ghost",size:"icon",className:"h-8 w-8",onClick:()=>{n(a),p(!0)},children:e.jsx(Je,{className:"w-4 h-4"})}),e.jsx(w,{variant:"ghost",size:"icon",className:"h-8 w-8 text-red-600",onClick:()=>confirm("Delete this automation rule?")&&l.mutate(a.id),children:e.jsx(pe,{className:"w-4 h-4"})})]})]})}),e.jsxs(k,{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(R,{variant:"outline",className:"mb-2",children:lt[a.trigger_type]}),a.trigger_condition&&e.jsxs("p",{className:"text-sm text-slate-600",children:[a.trigger_condition.status&&`When status is "${a.trigger_condition.status}"`,a.trigger_condition.days_threshold&&`When ${a.trigger_condition.days_threshold} days pass`]})]}),e.jsxs("div",{className:"pt-2 border-t border-slate-100",children:[e.jsxs("p",{className:"text-sm text-slate-600",children:["Action: ",e.jsx("span",{className:"font-medium text-slate-900",children:a.action_type.replace(/_/g," ")})]}),((o=a.action_config)==null?void 0:o.email_subject)&&e.jsxs("p",{className:"text-xs text-slate-500 mt-1",children:["Email: ",a.action_config.email_subject]})]}),a.last_triggered&&e.jsxs("p",{className:"text-xs text-slate-400",children:["Last triggered: ",new Date(a.last_triggered).toLocaleDateString()]})]})]})},a.id)})}):e.jsx(S,{children:e.jsxs(k,{className:"p-12 text-center",children:[e.jsx(P,{className:"w-12 h-12 text-slate-300 mx-auto mb-3"}),e.jsx("h3",{className:"font-semibold text-slate-900 mb-2",children:"No automation rules yet"}),e.jsx("p",{className:"text-slate-600 mb-4",children:"Create automated follow-ups, reminders, and notifications to never miss an opportunity"}),e.jsxs(w,{onClick:()=>p(!0),className:"bg-amber-600 hover:bg-amber-700",children:[e.jsx(J,{className:"w-4 h-4 mr-2"}),"Create Your First Rule"]})]})}),e.jsx(at,{open:u,onOpenChange:p,contractorId:t,rule:x})]})}function rt({contractorId:t}){const{data:u}=O({queryKey:["automation-rules",t],queryFn:()=>y.entities.AutomationRule.filter({contractor_id:t,is_active:!0}),enabled:!!t,refetchInterval:6e4}),{data:p}=O({queryKey:["leads",t],queryFn:()=>y.entities.Lead.filter({contractor_id:t}),enabled:!!t}),{data:x}=O({queryKey:["bids",t],queryFn:()=>y.entities.Bid.filter({contractor_id:t}),enabled:!!t});_.useEffect(()=>{if(!u||!t)return;(async()=>{var i,d;for(const m of u)try{if(m.trigger_type==="bid_expiring_soon"&&x){const l=((i=m.trigger_condition)==null?void 0:i.days_threshold)||3,a=x.filter(h=>{if(!h.expiry_date||h.status==="accepted"||h.status==="rejected")return!1;const j=Math.ceil((new Date(h.expiry_date)-new Date)/(1e3*60*60*24));return j<=l&&j>0});for(const h of a)await n(m,{bid:h,type:"bid"})}if(m.trigger_type==="lead_no_followup"&&p){const l=((d=m.trigger_condition)==null?void 0:d.days_threshold)||7,a=p.filter(h=>h.status==="won"||h.status==="lost"?!1:(h.last_contact_date?Math.ceil((new Date-new Date(h.last_contact_date))/(1e3*60*60*24)):999)>=l);for(const h of a)await n(m,{lead:h,type:"lead"})}}catch(l){console.error("Automation error:",l)}})()},[u,p,x,t]);const n=async(s,i)=>{var a,h,j;const{lead:d,bid:m,type:l}=i;if(await y.entities.AutomationRule.update(s.id,{last_triggered:new Date().toISOString()}),s.action_type==="send_email"){const o=((a=s.action_config)==null?void 0:a.email_subject)||"Follow-up Reminder";let r=((h=s.action_config)==null?void 0:h.email_body)||"";d&&(r=r.replace(/\{lead_name\}/g,d.name).replace(/\{company\}/g,d.company||"").replace(/\{project_type\}/g,d.project_type||"")),m&&(r=r.replace(/\{customer_name\}/g,m.customer_name).replace(/\{project_title\}/g,m.project_title).replace(/\{bid_amount\}
47/g,((j=m.bid_amount)==null?void 0:j.toLocaleString())||""));const b=await y.auth.me();await y.integrations.Core.SendEmail({to:b.email,subject:o,body:`<p>${r.replace(/\n/g,"<br>")}</p>
48               <p style="margin-top: 20px; padding-top: 20px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px;">
49                 This is an automated reminder from your BuildSlot CRM
50               </p>`})}};return null}const Ie={new:{label:"New",color:"bg-red-100 text-red-700",icon:Ce},contacted:{label:"Contacted",color:"bg-blue-100 text-blue-700",icon:ss},auto_responded:{label:"Auto-Responded",color:"bg-purple-100 text-purple-700",icon:P},appointment_scheduled:{label:"Appt. Scheduled",color:"bg-green-100 text-green-700",icon:z},appointment_completed:{label:"Appt. Completed",color:"bg-teal-100 text-teal-700",icon:Y},qualified:{label:"Qualified",color:"bg-amber-100 text-amber-700",icon:Y},proposal_sent:{label:"Proposal Sent",color:"bg-orange-100 text-orange-700",icon:vs},won:{label:"Won",color:"bg-emerald-100 text-emerald-700",icon:Y},lost:{label:"Lost",color:"bg-slate-100 text-slate-600",icon:Ce}},ct={buildslot:"BuildSlot",referral:"Referral",website:"Website",phone_call:"Phone",walk_in:"Walk-in",social_media:"Social",facebook_ads:"Facebook Ads",google_lsa:"Google LSA",angi:"Angi",thumbtack:"Thumbtack",sms:"SMS",advertisement:"Ad",other:"Other"},Oe={facebook_ads:"bg-blue-100 text-blue-700",google_lsa:"bg-orange-100 text-orange-700",angi:"bg-red-100 text-red-700",thumbtack:"bg-blue-100 text-blue-800",sms:"bg-green-100 text-green-700",buildslot:"bg-emerald-100 text-emerald-700",website:"bg-purple-100 text-purple-700",default:"bg-slate-100 text-slate-600"};function ot({leads:t,isLoading:u,contractorId:p,contractor:x}){const n=I(),[s,i]=_.useState("all"),d=D({mutationFn:({id:o,data:r})=>y.entities.Lead.update(o,r),onSuccess:()=>n.invalidateQueries({queryKey:["leads"]})}),m=async(o,r)=>{r==="appointment"?(await y.entities.Appointment.create({contractor_id:o.contractor_id,lead_id:o.id,customer_name:o.name,customer_phone:o.phone,customer_email:o.email,appointment_type:"estimate",start_datetime:new Date().toISOString(),status:"scheduled",location:o.address||""}),await d.mutateAsync({id:o.id,data:{status:"appointment_scheduled"}}),Q.success("Appointment created!")):r==="customer"?(await y.entities.Customer.create({contractor_id:o.contractor_id,name:o.name,phone:o.phone,email:o.email,lead_source:o.source}),await d.mutateAsync({id:o.id,data:{status:"qualified"}}),Q.success("Customer record created!")):r==="job"&&(await y.entities.Job.create({contractor_id:o.contractor_id,customer_name:o.name,customer_phone:o.phone,service_type:o.project_type||"General",scheduled_date:new Date().toISOString().split("T")[0],status:"lead",estimated_revenue:o.estimated_value}),await d.mutateAsync({id:o.id,data:{status:"proposal_sent"}}),Q.success("Job created!"))},l=async o=>{const r=`${window.location.origin}/Schedule?contractor=${p}&lead=${o.id}`;if(await d.mutateAsync({id:o.id,data:{status:"auto_responded",auto_responded_at:new Date().toISOString(),scheduling_link_sent:!0,last_contact_date:new Date().toISOString().split("T")[0]}}),o.email){const b=(x==null?void 0:x.business_name)||"the contractor";await y.integrations.Core.SendEmail({to:o.email,subject:`Schedule your consultation with ${b}`,body:`<p>Hi ${o.name},</p>
51               <p>Thank you for reaching out! We'd love to discuss your project.</p>
52               <p>Click below to choose a time that works for you:</p>
53               <p style="margin: 20px 0;">
54                 <a href="${r}" style="background: #16a34a; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600;">
55                   Schedule Your Consultation →
56                 </a>
57               </p>
58               <p style="color: #64748b; font-size: 14px;">— ${b}</p>`})}Q.success("Scheduling link sent!")},a=[{key:"all",label:"All"},{key:"new",label:"New"},{key:"auto_responded",label:"Auto-Responded"},{key:"appointment_scheduled",label:"Scheduled"},{key:"won",label:"Won"}],h=(t||[]).filter(o=>s==="all"||o.status===s),j=(t||[]).filter(o=>o.status==="new").length;return u?e.jsx("div",{className:"space-y-4",children:[...Array(4)].map((o,r)=>
58e.jsx(K,{className:"h-28"},r))}):e.jsxs("div",{className:"space-y-5",children:[e.jsx("div",{className:"flex flex-wrap gap-2",children:a.map(o=>e.jsxs("button",{onClick:()=>i(o.key),className:`px-4 py-1.5 rounded-full text-sm font-medium transition-colors border ${s===o.key?"bg-slate-900 text-white border-slate-900":"bg-white text-slate-600 border-slate-200 hover:border-slate-400"}`,children:[o.label,o.key==="new"&&j>0&&e.jsx("span",{className:"ml-1.5 bg-red-500 text-white rounded-full text-xs px-1.5",children:j})]},o.key))}),h.length===0?e.jsx(S,{children:e.jsxs(k,{className:"p-12 text-center",children:[e.jsx(P,{className:"w-10 h-10 text-slate-300 mx-auto mb-3"}),e.jsx("p",{className:"text-slate-500",children:"No leads in this category yet."})]})}):e.jsx("div",{className:"space-y-3",children:h.map((o,r)=>{var q,V;const b=Ie[o.status]||Ie.new,N=b.icon,c=Oe[o.source]||Oe.default,v=o.status==="new",L=o.created_date?Math.floor((Date.now()-new Date(o.created_date))/6e4):null;return e.jsx(E.div,{initial:{opacity:0,y:15},animate:{opacity:1,y:0},transition:{delay:r*.04},children:e.jsx(S,{className:`${v?"border-red-200 bg-red-50/30":"bg-white"} transition-all hover:shadow-md`,children:e.jsx(k,{className:"p-5",children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:`w-11 h-11 rounded-full flex items-center justify-center font-semibold text-lg shrink-0 ${v?"bg-red-100 text-red-700":"bg-slate-100 text-slate-600"}`,children:(V=(q=o.name)==null?void 0:q.charAt(0))==null?void 0:V.toUpperCase()}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2 mb-1",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("h3",{className:"font-semibold text-slate-900",children:o.name}),e.jsxs(R,{className:b.color+" text-xs",children:[e.jsx(N,{className:"w-3 h-3 mr-1"}),b.label]}),e.jsx(R,{className:c+" text-xs",children:ct[o.source]||o.source})]}),e.jsx("span",{className:"text-xs text-slate-400 whitespace-nowrap shrink-0",children:o.created_date&&Re(new Date(o.created_date),{addSuffix:!0})})]}),e.jsxs("div",{className:"flex flex-wrap gap-4 text-sm text-slate-600 mb-3",children:[o.phone&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(re,{className:"w-3.5 h-3.5"}),o.phone]}),o.email&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(ee,{className:"w-3.5 h-3.5"}),o.email]}),o.project_type&&e.jsxs("span",{className:"flex items-center gap-1 text-slate-500",children:["• ",o.project_type]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[v&&L!==null&&L<60&&e.jsxs("span",{className:"flex items-center gap-1 text-xs text-red-600 font-medium bg-red-50 px-2 py-1 rounded-full",children:[e.jsx(G,{className:"w-3 h-3"})," New ",L,"m ago — respond now!"]}),o.auto_responded_at&&e.jsxs("span",{className:"flex items-center gap-1 text-xs text-purple-600 bg-purple-50 px-2 py-1 rounded-full",children:[e.jsx(P,{className:"w-3 h-3"})," Auto-responded ",Re(new Date(o.auto_responded_at),{addSuffix:!0})]}),o.appointment_date&&e.jsxs("span",{className:"flex items-center gap-1 text-xs text-green-600 bg-green-50 px-2 py-1 rounded-full",children:[e.jsx(z,{className:"w-3 h-3"})," Appt: ",new Date(o.appointment_date).toLocaleDateString()," at ",new Date(o.appointment_date).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})]}),o.follow_up_count>0&&e.jsxs("span",{className:"text-xs text-slate-500",children:[o.follow_up_count," follow-up",o.follow_up_count>1?"s":""," sent"]})]})]}),e.jsxs("div",{className:"flex flex-col gap-2 shrink-0",children:[(v||o.status==="contacted")&&e.jsxs(w,{size:"sm",className:"bg-green-600 hover:bg-green-700 text-xs gap-1",onClick:()=>l(o),children:[e.jsx(He,{className:"w-3.5 h-3.5"}),"Send Booking Link"]}),e.jsxs(w,{size:"sm",variant:"outline",className:"text-xs gap-1",onClick:()=>d.mutate({id:o.id,data:{status:"contacted",last_contact_date:new Date().toISOString().split("T")[0]}}),children:[e.jsx(re,{className:"w-3.5 h-3.5"}),"Mark Contacted"]}),e.jsxs(ts,{children:[e.jsx(as,{asChild:!0,children:e.jsxs(w,{size:"sm",variant:"outline",className:"text-xs gap-1",children:["Convert ",e.jsx(ls,{className:"w-3 h-3"})]})}),e.jsxs(ns,{align:"end",children:[e.jsxs(be,{onClick:()=>m(o,"appointment"),className:"gap-2",children:[e.jsx(z,{className:"w-4 h-4 text-blue-600"})," Create Appointment"]}),e.jsxs(be,{onClick:()=>m(o,"customer"),className:"gap-2",children:[e.jsx(is,{className:"w-4 h-4 text-purple-600"})," Create Customer"]}),e.jsxs(be,{onClick:()=>m(o,"job"),className:"gap-2",children:[e.jsx(rs,{className:"w-4 h-4 text-amber-600"})," Create Job"]})]})]})]})]})})})},o.id)})})]})}const dt={buildslot:"BuildSlot",referral:"Referral",website:"Website",phone_call:"Phone Call",walk_in:"Walk-in",social_media:"Social Media",facebook_ads:"Facebook Ads",google_lsa:"Google LSA",angi:"Angi",thumbtack:"Thumbtack",sms:"SMS",advertisement:"Advertisement",other:"Other"},ye=["#16a34a","#0ea5e9","#8b5cf6","#f59e0b","#ef4444","#06b6d4","#ec4899","#84cc16"],ve=79;function xt({leads:t}){const[u,p]=_.useState("all"),x=_.useMemo(()=>
58{if(!t)return[];if(u==="all")return t;const i=fe(new Date,parseInt(u));return t.filter(d=>_e(new Date(d.created_date),i))},[t,u]),n=_.useMemo(()=>{if(!x.length)return null;const i=x.length,d=x.filter(N=>["appointment_scheduled","appointment_completed","won"].includes(N.status)).length,m=x.filter(N=>N.status==="won").length,l=x.reduce((N,c)=>N+(c.actual_revenue||0),0),a=x.filter(N=>N.auto_responded_at).length,h={};x.forEach(N=>{const c=N.source||"other";h[c]||(h[c]={leads:0,appointments:0,won:0,revenue:0}),h[c].leads++,["appointment_scheduled","appointment_completed","won"].includes(N.status)&&h[c].appointments++,N.status==="won"&&(h[c].won++,h[c].revenue+=N.actual_revenue||0)});const j=Object.entries(h).map(([N,c])=>({key:N,name:dt[N]||N,...c,bookingRate:c.leads>0?Math.round(c.appointments/c.leads*100):0,winRate:c.leads>0?Math.round(c.won/c.leads*100):0,revenuePerLead:c.leads>0?Math.round(c.revenue/c.leads):0})).sort((N,c)=>c.revenue-N.revenue||c.leads-N.leads),o=Array.from({length:8},(N,c)=>{const v=fe(new Date,c*7),L=fe(new Date,c*7+7),q=x.filter(V=>{const A=new Date(V.created_date);return _e(A,L)&&!_e(A,v)});return{week:he(v,"MMM d"),leads:q.length,revenue:q.reduce((V,A)=>V+(A.actual_revenue||0),0)}}).reverse(),r=j.filter(N=>N.revenue>0).slice(0,7).map((N,c)=>({name:N.name,revenue:N.revenue,leads:N.leads,color:ye[c]})),b=u==="all"?Math.max(1,Math.ceil((Date.now()-Math.min(...x.map(N=>new Date(N.created_date))))/(1e3*60*60*24*30))):Math.ceil(parseInt(u)/30);return{total:i,appointed:d,won:m,totalRevenue:l,autoResponded:a,bookingRate:i>0?Math.round(d/i*100):0,winRate:i>0?Math.round(m/i*100):0,sourceRows:j,weeklyData:o,revenueBySource:r,subscriptionCost:b*ve,roi:l>0?Math.round(l/(b*ve)*100):0}},[x,u]);if(!x.length)return e.jsx(S,{children:e.jsxs(k,{className:"p-12 text-center",children:[e.jsx(ie,{className:"w-10 h-10 text-slate-300 mx-auto mb-3"}),e.jsx("p",{className:"text-slate-500",children:"No lead data yet. Add leads to see your ROI."})]})});const s=[{label:"Total Leads",value:n.total,icon:we,color:"text-blue-600",bg:"bg-blue-50"},{label:"Appts. Scheduled",value:n.appointed,icon:z,color:"text-purple-600",bg:"bg-purple-50"},{label:"Jobs Won",value:n.won,icon:ws,color:"text-green-600",bg:"bg-green-50"},{label:"Revenue Generated",value:`$${n.totalRevenue.toLocaleString()}`,icon:U,color:"text-emerald-600",bg:"bg-emerald-50"},{label:"Booking Rate",value:`${n.bookingRate}%`,icon:ue,color:"text-amber-600",bg:"bg-amber-50"},{label:"Win Rate",value:`${n.winRate}%`,icon:ie,color:"text-orange-600",bg:"bg-orange-50"}];return e.jsxs("div",{className:"space-y-6",children:[e.jsx("div",{className:"flex gap-2",children:[["all","All Time"],["90","Last 90 Days"],["30","Last 30 Days"]].map(([i,d])=>e.jsx("button",{onClick:()=>p(i),className:`px-4 py-1.5 rounded-full text-sm font-medium border transition-colors ${u===i?"bg-slate-900 text-white border-slate-900":"bg-white text-slate-600 border-slate-200 hover:border-slate-400"}`,children:d},i))}),e.jsx("div",{className:"grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4",children:s.map(i=>e.jsx(S,{children:e.jsxs(k,{className:"p-4",children:[e.jsx("div",{className:`w-9 h-9 rounded-lg ${i.bg} flex items-center justify-center mb-3`,children:e.jsx(i.icon,{className:`w-5 h-5 ${i.color}`})}),e.jsx("p",{className:"text-2xl font-bold text-slate-900",children:i.value}),e.jsx("p",{className:"text-xs text-slate-500 mt-0.5",children:i.label})]})},i.label))}),e.jsx(S,{className:"bg-gradient-to-r from-green-600 to-emerald-700 text-white border-0",children:e.jsxs(k,{className:"p-6",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[e.jsx(P,{className:"w-6 h-6 text-green-200"}),e.jsx("h3",{className:"text-lg font-bold",children:"BuildSlot Subscription ROI"})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-6",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-green-200 text-sm",children:"Leads Tracked"}),e.jsx("p",{className:"text-3xl font-bold",children:n.total})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-green-200 text-sm",children:"Appts via BuildSlot"}),e.jsx("p",{className:"text-3xl font-bold",children:n.appointed})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-green-200 text-sm",children:"Revenue Generated"}),e.jsxs("p",{className:"text-3xl font-bold",children:["$",n.totalRevenue.toLocaleStr
58ing()]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-green-200 text-sm",children:"vs. Subscription Cost"}),e.jsxs("div",{className:"flex items-end gap-2",children:[e.jsxs("p",{className:"text-3xl font-bold",children:["$",n.subscriptionCost]}),n.roi>0&&e.jsxs("span",{className:"flex items-center text-green-200 text-sm mb-1",children:[e.jsx(Cs,{className:"w-4 h-4"}),n.roi,"x ROI"]})]}),e.jsxs("p",{className:"text-green-200 text-xs mt-0.5",children:["$",ve,"/mo subscription"]})]})]})]})}),e.jsxs(S,{children:[e.jsx($,{children:e.jsxs(M,{className:"text-base flex items-center gap-2",children:[e.jsx(ue,{className:"w-5 h-5 text-slate-500"}),"Lead Source Performance"]})}),e.jsx(k,{className:"p-0",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-slate-100 bg-slate-50",children:[e.jsx("th",{className:"text-left py-3 px-4 font-semibold text-slate-600",children:"Source"}),e.jsx("th",{className:"text-right py-3 px-4 font-semibold text-slate-600",children:"Leads"}),e.jsx("th",{className:"text-right py-3 px-4 font-semibold text-slate-600",children:"Appts"}),e.jsx("th",{className:"text-right py-3 px-4 font-semibold text-slate-600",children:"Won"}),e.jsx("th",{className:"text-right py-3 px-4 font-semibold text-slate-600",children:"Revenue"}),e.jsx("th",{className:"text-right py-3 px-4 font-semibold text-slate-600",children:"Booking %"}),e.jsx("th",{className:"text-right py-3 px-4 font-semibold text-slate-600",children:"Win %"}),e.jsx("th",{className:"text-right py-3 px-4 font-semibold text-slate-600",children:"Rev/Lead"})]})}),e.jsx("tbody",{children:n.sourceRows.map((i,d)=>e.jsxs("tr",{className:"border-b border-slate-50 hover:bg-slate-50 transition-colors",children:[e.jsx("td",{className:"py-3 px-4",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-2.5 h-2.5 rounded-full",style:{background:ye[d%ye.length]}}),e.jsx("span",{className:"font-medium text-slate-900",children:i.name})]})}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-700",children:i.leads}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-700",children:i.appointments}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-700",children:i.won}),e.jsx("td",{className:"py-3 px-4 text-right font-semibold text-green-600",children:i.revenue>0?`$${i.revenue.toLocaleString()}`:"—"}),e.jsx("td",{className:"py-3 px-4 text-right",children:e.jsxs("span",{className:`px-2 py-0.5 rounded-full text-xs font-medium ${i.bookingRate>=50?"bg-green-100 text-green-700":i.bookingRate>=25?"bg-amber-100 text-amber-700":"bg-slate-100 text-slate-600"}`,children:[i.bookingRate,"%"]})}),e.jsx("td",{className:"py-3 px-4 text-right",children:e.jsxs("span",{className:`px-2 py-0.5 rounded-full text-xs font-medium ${i.winRate>=30?"bg-green-100 text-green-700":i.winRate>=15?"bg-amber-100 text-amber-700":"bg-slate-100 text-slate-600"}`,children:[i.winRate,"%"]})}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-600",children:i.revenuePerLead>0?`$${i.revenuePerLead.toLocaleString()}`:"—"})]},i.key))}),e.jsx("tfoot",{children:e.jsxs("tr",{className:"bg-slate-50 font-semibold border-t-2 border-slate-200",children:[e.jsx("td",{className:"py-3 px-4 text-slate-900",children:"Total"}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-900",children:n.total}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-900",children:n.appointed}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-900",children:n.won}),e.jsxs("td",{className:"py-3 px-4 text-right text-green-600",children:["$",n.totalRevenue.toLocaleString()]}),e.jsxs("td",{className:"py-3 px-4 text-right text-slate-900",children:[n.bookingRate,"%"]}),e.jsxs("td",{className:"py-3 px-4 text-right text-slate-900",children:[n.winRate,"%"]}),e.jsx("td",{className:"py-3 px-4 text-right text-slate-600",children:n.total>0?`$${Math.round(n.totalRevenue/n.total).toLocaleString()}`:"—"})]})})]})})})]}),e.jsxs("div",{className:"grid md:grid-cols-2 gap-6",children:[n.revenueBySource.length>0&&e.jsxs(S,{children:[e.jsx($,{children:e.jsx(M,{className:"text-base",children:"Revenue by Source"})}),e.jsx(k,{children:e.jsx(Me,{width:"100%",height:200,children:e.jsxs(Ss,{data:n.revenueBySource,margin:{left:10},children:[e.jsx(Te,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(Be,{dataKey:"name",tick:{fontSize:10}}),e.jsx(qe,{tick:{fontSize:11}
58,tickFormatter:i=>`$${(i/1e3).toFixed(0)}k`}),e.jsx(Pe,{formatter:i=>[`$${i.toLocaleString()}`,"Revenue"]}),e.jsx(ks,{dataKey:"revenue",radius:[4,4,0,0],children:n.revenueBySource.map((i,d)=>e.jsx(As,{fill:i.color},d))})]})})})]}),e.jsxs(S,{children:[e.jsx($,{children:e.jsx(M,{className:"text-base",children:"Weekly Lead Volume"})}),e.jsx(k,{children:e.jsx(Me,{width:"100%",height:200,children:e.jsxs(Ls,{data:n.weeklyData,children:[e.jsx(Te,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(Be,{dataKey:"week",tick:{fontSize:11}}),e.jsx(qe,{allowDecimals:!1,tick:{fontSize:11}}),e.jsx(Pe,{}),e.jsx(Rs,{type:"monotone",dataKey:"leads",stroke:"#16a34a",strokeWidth:2,dot:{r:4},name:"Leads"})]})})})]})]})]})}const We=[{delay_minutes:10,message:"Hi {name}, just following up on your inquiry. We'd love to help with your project. Have you had a chance to pick a time? {scheduling_link}",enabled:!0},{delay_minutes:120,message:"Hi {name}, still here if you need us! Booking a quick consultation is easy — just click here: {scheduling_link}",enabled:!0},{delay_minutes:1440,message:"Hi {name}, last follow-up from us. We have availability this week if you'd still like to connect. {scheduling_link}",enabled:!0}];function mt({contractorId:t,contractor:u}){const p=I(),[x,n]=_.useState(!1),[s,i]=_.useState({auto_response_enabled:!0,response_message:`Hello {name}, thanks for reaching out to {business_name}.
59
60We're often on job sites and may miss calls, but you can schedule a time to discuss your project using the link below.
61
62{scheduling_link}
63
64We look forward to helping with your project.`,include_scheduling_link:!0,followup_sequence:We,working_hours_start:"08:00",working_hours_end:"17:00",appointment_duration_minutes:30,buffer_minutes:15,working_days:[1,2,3,4,5]}),{data:d}=O({queryKey:["auto-response-settings",t],queryFn:()=>y.entities.AutoResponseSettings.filter({contractor_id:t}),enabled:!!t,select:c=>c==null?void 0:c[0]});_.useEffect(()=>{var c;d&&i({...s,...d,followup_sequence:(c=d.followup_sequence)!=null&&c.length?d.followup_sequence:We})},[d]);const m=D({mutationFn:c=>d?y.entities.AutoResponseSettings.update(d.id,c):y.entities.AutoResponseSettings.create({...c,contractor_id:t}),onSuccess:()=>{p.invalidateQueries({queryKey:["auto-response-settings"]}),Q.success("Settings saved!")}}),l=u?`${window.location.origin}/Schedule?contractor=${t}`:"",a=()=>{navigator.clipboard.writeText(l),n(!0),setTimeout(()=>n(!1),2e3)},h=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],j=c=>{const v=s.working_days.includes(c)?s.working_days.filter(L=>L!==c):[...s.working_days,c].sort();i({...s,working_days:v})},o=(c,v,L)=>{const q=[...s.followup_sequence];q[c]={...q[c],[v]:L},i({...s,followup_sequence:q})},r=()=>{i({...s,followup_sequence:[...s.followup_sequence,{delay_minutes:60,message:"",enabled:!0}]})},b=c=>{i({...s,followup_sequence:s.followup_sequence.filter((v,L)=>L!==c)})},N=c=>c<60?`${c} min`:c<1440?`${c/60}h`:`${c/1440}d`;return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(S,{className:"border-green-200 bg-green-50/30",children:[e.jsx($,{children:e.jsxs(M,{className:"text-base flex items-center gap-2",children:[e.jsx(He,{className:"w-5 h-5 text-green-600"}),"Your Scheduling Link"]})}),e.jsxs(k,{className:"space-y-3",children:[e.jsx("p",{className:"text-sm text-slate-600",children:"Share this link in your auto-response messages so homeowners can book directly."}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(f,{value:l,readOnly:!0,className:"bg-white text-sm font-mono"}),e.jsxs(w,{variant:"outline",className:"gap-2 shrink-0",onClick:a,children:[x?e.jsx(Y,{className:"w-4 h-4 text-green-600"}):e.jsx(Ds,{className:"w-4 h-4"}),x?"Copied!":"Copy"]})]})]})]}),e.jsxs(S,{children:[e.jsx($,{children:e.jsxs(M,{className:"text-base flex items-center gap-2",children:[e.jsx(P,{className:"w-5 h-5 text-amber-600"}),"Instant Auto-Response"]})}),e.jsxs(k,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium text-slate-900",children:"Enable Auto-Response"}),e.jsx("p",{className:"text-sm text-slate-500",children:"Automatically send a reply when a new lead arrives"})]}),e.jsx(X,{checked:s.auto_response_enabled,onCheckedChange:c=>i({...s,auto_response_enabled:c})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Auto-Response Message"}),e.jsx(F,{rows:4,value:s.response_message,onChange:c=>i({...s,response_message:c.target.value})}),e.jsxs("p",{className:"text-xs text-slate-400",children:["Variables: ",e.jsx("code",{className:"bg-slate-100 px-1 rounded",children:"{name}"})," ",e.jsx("code",{className:"bg-slate-100 px-1 rounded",children:"{business_name}"})," ",e.jsx("code",{className:"bg-slate-100 px-1 rounded",children:"{scheduling_link}"})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium text-slate-900",children:"Include Scheduling Link"}),e.jsx("p",{className:"text-sm text-slate-500",children:"Append your booking link automatically"})]}),e.jsx(X,{checked:s.include_scheduling_link,onCheckedChange:c=>i({...s,include_scheduling_link:c})})]})]})]}),e.jsxs(S,{children:[e.jsx($,{children:e.jsxs(M,{className:"text-base flex items-center gap-2",children:[e.jsx(G,{className:"w-5 h-5 text-blue-600"}),"Availability & Scheduling"]})}),e.jsxs(k,{className:"space-y-5",children:[e.jsxs("div",{children:[e.jsx(g,{className:"mb-2 block",children:"Working Days"}),e.jsx("div",{className:"flex gap-2",children:h.map((c,v)=>e.jsx("button",{onClick:()=>j(v),className:`w-10 h-10 rounded-lg text-sm font-medium transition-colors border-2 ${s.working_days.includes(v)?"bg-green-600 text-white border-green-600":"bg-white text-slate-600 border-slate-200 hover:border-slate-400"}`,children:c},c))})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(g,{children:"Hours Start"}
64),e.jsx(f,{type:"time",value:s.working_hours_start,onChange:c=>i({...s,working_hours_start:c.target.value})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(g,{children:"Hours End"}),e.jsx(f,{type:"time",value:s.working_hours_end,onChange:c=>i({...s,working_hours_end:c.target.value})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(g,{children:"Appointment Duration (min)"}),e.jsx(f,{type:"number",value:s.appointment_duration_minutes,onChange:c=>i({...s,appointment_duration_minutes:parseInt(c.target.value)})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(g,{children:"Buffer Between Appts (min)"}),e.jsx(f,{type:"number",value:s.buffer_minutes,onChange:c=>i({...s,buffer_minutes:parseInt(c.target.value)})})]})]})]})]}),e.jsxs(S,{children:[e.jsx($,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(M,{className:"text-base flex items-center gap-2",children:[e.jsx(ee,{className:"w-5 h-5 text-purple-600"}),"Follow-Up Sequence"]}),e.jsxs(w,{size:"sm",variant:"outline",onClick:r,className:"gap-1",children:[e.jsx(J,{className:"w-4 h-4"})," Add Step"]})]})}),e.jsxs(k,{className:"space-y-4",children:[e.jsx("p",{className:"text-sm text-slate-500",children:"Automatically follow up if the homeowner hasn't booked."}),s.followup_sequence.map((c,v)=>e.jsxs("div",{className:"border border-slate-200 rounded-xl p-4 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs(R,{variant:"outline",className:"font-mono",children:["Step ",v+1]}),e.jsx(X,{checked:c.enabled,onCheckedChange:L=>o(v,"enabled",L)}),e.jsx("span",{className:"text-sm text-slate-600",children:c.enabled?"Active":"Disabled"})]}),e.jsx(w,{variant:"ghost",size:"icon",className:"text-red-500 h-8 w-8",onClick:()=>b(v),children:e.jsx(pe,{className:"w-4 h-4"})})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(g,{className:"text-xs",children:"Delay (minutes)"}),e.jsx(f,{type:"number",value:c.delay_minutes,onChange:L=>o(v,"delay_minutes",parseInt(L.target.value)),className:"h-8 text-sm"}),e.jsxs("p",{className:"text-xs text-slate-400",children:[N(c.delay_minutes)," after last contact"]})]}),e.jsxs("div",{className:"col-span-2 space-y-1",children:[e.jsx(g,{className:"text-xs",children:"Message"}),e.jsx(F,{rows:2,value:c.message,onChange:L=>o(v,"message",L.target.value),className:"text-sm",placeholder:"Follow-up message..."})]})]})]},v))]})]}),e.jsx("div",{className:"flex justify-end",children:e.jsxs(w,{className:"bg-green-600 hover:bg-green-700 gap-2",onClick:()=>m.mutate(s),disabled:m.isPending,children:[e.jsx($s,{className:"w-4 h-4"}),m.isPending?"Saving...":"Save Settings"]})})]})}const ht=["Roofing","Plumbing","Electrical","HVAC","Painting","Flooring","Carpentry","Masonry","Landscaping","Concrete","Drywall","Fencing","Windows & Doors","Siding","Insulation","Gutters","Deck/Patio","Bathroom Remodel","Kitchen Remodel","General Contracting"],ut=["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"],pt={rule_name:"",trades:[],budget_min:"",budget_max:"",location_states:[],location_cities:"",interest_message:"",bid_percentage:"80",is_active:!0,auto_submit:!1};function gt({rule:t,onClose:u,onSave:p,isSaving:x}){const[n,s]=_.useState(t?{...t,location_cities:(t.location_cities||[]).join(", "),budget_min:t.budget_min??"",budget_max:t.budget_max??"",bid_percentage:t.bid_percentage??"80"}:{...pt}),i=a=>{s(h=>({...h,trades:h.trades.includes(a)?h.trades.filter(j=>j!==a):[...h.trades,a]}))},d=a=>{s(h=>({...h,location_states:h.location_states.includes(a)?h.location_states.filter(j=>j!==a):[...h.location_states,a]}))},m=()=>{const a=n.location_cities?n.location_cities.split(",").map(h=>h.trim()).filter(Boolean):[];p({...n,location_cities:a,budget_min:n.budget_min?parseFloat(n.budget_min):void 0,budget_max:n.budget_max?parseFloat(n.budget_max):void 0,bid_percentage:n.bid_percentage?parseFloat(n.bid_percentage):void 0})},l=n.rule_name.trim()&&n.trades.length>0;return e.jsx(ce,{open:!0,onOpenChange:u,children:e.jsxs(oe,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(de,{children:e.jsxs(xe,{className:"flex items-center gap-2",children:[e.jsx(P,{className:"w-5 h-5 text-amber-500"}),t?"Edit Auto-Bid Rule":"Create Auto-Bid Rule"]})}),e.jsxs("div",{className:"space-y-5 py-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(g,{children:["Rule Name ",e.jsx("span",{className:"text-red-500",children:"*"})]}),e.jsx(f,{placeholder:'e.g. "Roofing jobs in Michigan $5k–$30k"',value:n.rule_name,onChange:a=>s({...n,rule_name:a.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(g,{className:"flex items-center gap-1",children:[e.jsx(Ze,{className:"w-3.5 h-3.5"}),"Applicable Trades ",e.jsx("span",{className:"text-red-500",children:"*"})]}),e.jsx("p",{className:"text-xs text-slate-500",children:"Select all trade types this rule should match against incoming leads."}),e.jsx("div",{className:"flex flex-wrap gap-2",children:ht.map(a=>e.jsx("button",{type:"button",onClick:()=>i(a),className:`px-3 py-1.5 rounded-full text-xs font-medium border transition-all ${n.trades.includes(a)?"bg-green-600 text-white border-green-600":"bg-white text-slate-600 border-slate-200 hover:border-green-300"}`,children:a},a))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(g,{className:"flex items-center gap-1",children:[e.jsx(U,{className:"w-3.5 h-3.5"}),"Budget Range (optional)"]}),e.jsx("p",{className:"text-xs text-slate-500",children:"Only bid on leads within this estimated project budget."}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsx("div",{children:e.jsx(f,{type:"number",placeholder:"Min ($)",value:n.budget_min,onChange:a=>s({...n,budget_min:a.target.value})})}),e.jsx("div",{children:e.jsx(f,{type:"number",placeholder:"Max ($)",value:n.budget_max,onChange:a=>s({...n,budget_max:a.target.value})})})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(g,{className:"flex items-center gap-1",children:[e.jsx(ge,{className:"w-3.5 h-3.5"}),"Target States (optional – empty = any state)"]}),e.jsx("div",{className:"flex flex-wrap gap-1.5 max-h-28 overflow-y-auto p-2 border border-slate-200 rounded-lg",children:ut.map(a=>e.jsx("button",{type:"button",onClick:()=>d(a),className:`px-2 py-0.5 rounded text-xs font-mono font-medium border transition-all ${n.location_states.includes(a)?"bg-blue-600 text-white border-blue-600":"bg-white text-slate-600 border-slate-200 hover:border-blue-300"}`,children:a},a))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Target Cities (comma-separated, optional)"}),e.jsx(f,{placeholder:"e.g. Detroit, Ann Arbor, Grand Rapids",value:n.location_cities,onChange:a=>s({...n,location_cities:a.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Bid Amount (% of lead's estimated budget)"}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(f,{type:"number",min:"1",max:"200",className:"w-24",value:n.bid_percentage,onChange:a=>s({...n,bid_percentage:a.target.value})}),e.jsx("span",{className:"text-sm text-slate-500",children:"% of project budget (e.g. 80 = bid $8,000 on a $10,000 project)"})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Initial Interest Message (optional)"}),e.jsx(F,{placeholder:"Hi, I'm very interested in your project and would love to schedule a time to discu
64ss...",value:n.interest_message,onChange:a=>s({...n,interest_message:a.target.value}),rows:3}),e.jsx("p",{className:"text-xs text-slate-500",children:"This message is added to the auto-generated bid. Leave blank for a default message."})]}),e.jsxs("div",{className:"space-y-3 p-4 bg-slate-50 rounded-lg border border-slate-200",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium text-slate-900 text-sm",children:"Auto-Submit Bids"}),e.jsx("p",{className:"text-xs text-slate-500",children:"Send bids immediately when triggered, without manual review."})]}),e.jsx(X,{checked:n.auto_submit,onCheckedChange:a=>s({...n,auto_submit:a})})]}),!n.auto_submit&&e.jsxs("div",{className:"flex items-start gap-2 text-xs text-amber-700 bg-amber-50 p-2.5 rounded-lg",children:[e.jsx(ze,{className:"w-3.5 h-3.5 mt-0.5 shrink-0"}),"Bids will be created as ",e.jsx("strong",{children:"Drafts"})," for your review before sending."]}),n.auto_submit&&e.jsxs("div",{className:"flex items-start gap-2 text-xs text-green-700 bg-green-50 p-2.5 rounded-lg",children:[e.jsx(Se,{className:"w-3.5 h-3.5 mt-0.5 shrink-0"}),"Bids will be ",e.jsx("strong",{children:"automatically sent"})," to matching leads on your behalf."]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(g,{children:"Rule Active"}),e.jsx(X,{checked:n.is_active,onCheckedChange:a=>s({...n,is_active:a})})]})]}),e.jsxs(ds,{children:[e.jsx(w,{variant:"outline",onClick:u,children:"Cancel"}),e.jsx(w,{className:"bg-green-600 hover:bg-green-700",onClick:m,disabled:!l||x,children:x?"Saving...":t?"Save Changes":"Create Rule"})]})]})})}function jt({contractorId:t}){const[u,p]=_.useState(!1),[x,n]=_.useState(null),s=I(),{data:i,isLoading:d}=O({queryKey:["autoBidRules",t],queryFn:()=>y.entities.AutoBidRule.filter({contractor_id:t},"-created_date"),enabled:!!t}),m=D({mutationFn:r=>y.entities.AutoBidRule.create({...r,contractor_id:t}),onSuccess:()=>{s.invalidateQueries({queryKey:["autoBidRules",t]}),p(!1),n(null)}}),l=D({mutationFn:({id:r,data:b})=>y.entities.AutoBidRule.update(r,b),onSuccess:()=>{s.invalidateQueries({queryKey:["autoBidRules",t]}),p(!1),n(null)}}),a=D({mutationFn:r=>y.entities.AutoBidRule.delete(r),onSuccess:()=>s.invalidateQueries({queryKey:["autoBidRules",t]})}),h=D({mutationFn:({id:r,is_active:b})=>y.entities.AutoBidRule.update(r,{is_active:b}),onSuccess:()=>s.invalidateQueries({queryKey:["autoBidRules",t]})}),j=r=>{x?l.mutate({id:x.id,data:r}):m.mutate(r)},o=(i||[]).filter(r=>r.is_active).length;return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{children:[e.jsxs("h2",{className:"text-lg font-semibold text-slate-900 flex items-center gap-2",children:[e.jsx(P,{className:"w-5 h-5 text-amber-500"}),"Auto-Bid Rules"]}),e.jsx("p",{className:"text-sm text-slate-500 mt-1",children:"Set criteria so BuildSlot automatically expresses interest in matching leads on your behalf."})]}),e.jsxs(w,{className:"bg-green-600 hover:bg-green-700 shrink-0",onClick:()=>{n(null),p(!0)},children:[e.jsx(J,{className:"w-4 h-4 mr-2"}),"New Rule"]})]}),e.jsx(S,{className:"bg-amber-50 border-amber-200",children:e.jsxs(k,{className:"p-4 flex gap-3",children:[e.jsx(ze,{className:"w-5 h-5 text-amber-600 shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-amber-800",children:[e.jsx("p",{className:"font-medium mb-1",children:"How Auto-Bid Works"}),e.jsxs("ol",{className:"list-decimal list-inside space-y-1 text-amber-700",children:[e.jsx("li",{children:"A new lead arrives matching your trade, budget, and location criteria."}),e.jsx("li",{children:"BuildSlot creates a bid on your behalf using your interest message."}),e.jsxs("li",{children:["If ",e.jsx("strong",{children:"Auto-Submit"})," is off, the bid is saved as a ",e.jsx("em",{children:"Draft"})," for your review."]}),e.jsxs("li",{children:["If ",e.jsx("strong",{children:"Auto-Submit"})," is on, the bid is sent immediately to the lead."]})]})]})]})}),i&&i.length>0&&e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"bg-white rounded-lg border border-slate-200 p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-slate-900",children:i.length}),e.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"Total Rules"})]}),e.jsxs("div",{className:"bg-white rounded-lg border border-slate-200 p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-green-600",children:o}),e.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"Active"})]}),e.jsxs("div",{className:"bg-white rounded-lg border border-slate-200 p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-amber-600",children:(i||[]).reduce((r,b)=>r+(b.total_triggered||0),0)}),e.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"Total Triggered"})]})]}),d?e.jsx("div",{className:"space-y-3",children:[...Array(2)].map((r,b)=>
64e.jsx("div",{className:"h-28 bg-white rounded-xl border border-slate-200 animate-pulse"},b))}):!i||i.length===0?e.jsx(S,{className:"border-dashed",children:e.jsxs(k,{className:"py-16 text-center",children:[e.jsx(P,{className:"w-12 h-12 text-slate-300 mx-auto mb-3"}),e.jsx("p",{className:"font-medium text-slate-600",children:"No auto-bid rules yet"}),e.jsx("p",{className:"text-sm text-slate-400 mt-1 mb-4",children:"Create your first rule to let BuildSlot bid on matching projects automatically."}),e.jsxs(w,{className:"bg-green-600 hover:bg-green-700",onClick:()=>{n(null),p(!0)},children:[e.jsx(J,{className:"w-4 h-4 mr-2"}),"Create First Rule"]})]})}):e.jsx("div",{className:"space-y-4",children:i.map((r,b)=>e.jsx(E.div,{initial:{opacity:0,y:10},animate:{opacity:1,y:0},transition:{delay:b*.05},children:e.jsx(S,{className:`transition-all ${r.is_active?"":"opacity-60"}`,children:e.jsx(k,{className:"p-5",children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:`w-10 h-10 rounded-lg flex items-center justify-center shrink-0 ${r.is_active?"bg-amber-100":"bg-slate-100"}`,children:e.jsx(P,{className:`w-5 h-5 ${r.is_active?"text-amber-500":"text-slate-400"}`})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap mb-2",children:[e.jsx("h3",{className:"font-semibold text-slate-900",children:r.rule_name}),e.jsx(R,{className:r.is_active?"bg-green-100 text-green-700":"bg-slate-100 text-slate-500",children:r.is_active?"Active":"Paused"}),r.auto_submit&&e.jsx(R,{className:"bg-blue-100 text-blue-700",children:"Auto-Submit"}),!r.auto_submit&&e.jsx(R,{variant:"outline",className:"text-xs",children:"Draft Mode"})]}),e.jsxs("div",{className:"flex flex-wrap gap-x-4 gap-y-1 text-sm text-slate-500",children:[r.trades&&r.trades.length>0&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(Ze,{className:"w-3.5 h-3.5"}),r.trades.slice(0,3).join(", "),r.trades.length>3&&` +${r.trades.length-3} more`]}),(r.budget_min||r.budget_max)&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(U,{className:"w-3.5 h-3.5"}),r.budget_min?`$${r.budget_min.toLocaleString()}`:"$0"," – ",r.budget_max?`$${r.budget_max.toLocaleString()}`:"unlimited"]}),r.location_states&&r.location_states.length>0&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(ge,{className:"w-3.5 h-3.5"}),r.location_states.slice(0,4).join(", "),r.location_states.length>4&&` +${r.location_states.length-4}`]}),r.bid_percentage&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(ue,{className:"w-3.5 h-3.5"}),r.bid_percentage,"% of budget"]}),r.total_triggered>0&&e.jsxs("span",{className:"flex items-center gap-1 text-amber-600",children:[e.jsx(G,{className:"w-3.5 h-3.5"}),"Triggered ",r.total_triggered,"×"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[e.jsx(X,{checked:r.is_active,onCheckedChange:N=>h.mutate({id:r.id,is_active:N})}),e.jsx(w,{variant:"ghost",size:"icon",onClick:()=>{n(r),p(!0)},children:e.jsx(Ms,{className:"w-4 h-4 text-slate-500"})}),e.jsx(w,{variant:"ghost",size:"icon",onClick:()=>a.mutate(r.id),children:e.jsx(pe,{className:"w-4 h-4 text-red-400"})})]})]})})})},r.id))}),u&&e.jsx(gt,{rule:x,onClose:()=>{p(!1),n(null)},onSave:j,isSaving:m.isPending||l.isPending})]})}const bt={new:"bg-slate-100 text-slate-700",contacted:"bg-blue-100 text-blue-700",qualified:"bg-purple-100 text-purple-700",proposal_sent:"bg-amber-100 text-amber-700",negotiating:"bg-orange-100 text-orange-700",won:"bg-green-100 text-green-700",lost:"bg-red-100 text-red-700"};function Nt(t){const u=new Date(t);return me(u)&&!Z(u)?{label:"Overdue",bg:"bg-red-50 border-red-200",badge:"bg-red-100 text-red-700",icon:Ae,iconColor:"text-red-500"}:Z(u)?{label:"Today",bg:"bg-orange-50 border-orange-200",badge:"bg-orange-100 text-orange-700",icon:W,iconColor:"text-orange-500"}:ke(u)?{label:"Tomorrow",bg:"bg-amber-50 border-amber-200",badge:"bg-amber-100 text-amber-700",icon:G,iconColor:"text-amber-500"}:{label:`In ${Ts(u,new Date)}d`,bg:"bg-blue-50 border-blue-100",badge:"bg-blue-100 text-blue-700",icon:Y,iconColor:"text-blue-400"}}function ft({leads:t}){const[u,p]=_.useState(null),x=(t||[]).filter(l=>l.follow_up_reminder).sort((l,a)=>new Date(l.follow_up_reminder)-new Date(a.follow_up_reminder)),n=x.filter(l=>me(new Date(l.follow_up_reminder))&&!Z(new Date(l.follow_up_reminder))),s=x.filter(l=>Z(new Date(l.follow_up_reminder))),i=x.filter(l=>!me(new Date(l.follow_up_reminder))||ke(new Date(l.follow_up_reminder)));if(x.length===0)return e.jsx(S,{children:e.jsxs(k,{className:"p-12 text-center",children:[e.jsx(W,{className:"w-10 h-10 text-slate-300 mx-auto mb-3"}),e.jsx("p",{className:"text-slate-500 font-medium",children:"No follow-up reminders set"}),e.jsx("p",{className:"text-sm text-slate-400 mt-1",children:"Open any lead and set a follow-up call reminder to see it here."})]})});const d=({lead:l})=>{var j;const a=Nt(l.follow_up_reminder),h=a.icon;return e.jsx(S,{className:`border cursor-pointer hover:shadow-md transition-shadow ${a.bg}`,onClick:()=>p(l),children:e.jsx(k,{className:"p-4",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"w-8 h-8 rounded-lg flex items-center justify-center shrink-0 bg-white border",children:e.jsx(h,{className:`w-4 h-4 ${a.iconColor}`})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("p",{className:"font-semibold text-slate-900 text-sm truncate",children:l.name}),e.jsx(R,{className:`text-xs px-1.5 ${a.badge}`,children:a.label}),e.jsx(R,{className:`text-xs px-1.5 ${bt[l.status]||"bg-slate-100 text-slate-700"}`,children:(j=l.status)==null?void 0:j.replace(/_/g," ")})]}),e.jsx("p",{className:"text-xs text-slate-500 mt-0.5",children:he(ne
64w Date(l.follow_up_reminder),"EEE, MMM d 'at' h:mm a")}),l.follow_up_note&&e.jsxs("p",{className:"text-xs text-slate-600 mt-1 line-clamp-1 italic",children:['"',l.follow_up_note,'"']}),e.jsxs("div",{className:"flex gap-3 mt-2",children:[l.phone&&e.jsxs("span",{className:"flex items-center gap-1 text-xs text-slate-500",children:[e.jsx(re,{className:"w-3 h-3"}),l.phone]}),l.email&&e.jsxs("span",{className:"flex items-center gap-1 text-xs text-slate-500",children:[e.jsx(ee,{className:"w-3 h-3"}),l.email]})]})]}),e.jsx(cs,{className:"w-4 h-4 text-slate-400 shrink-0 mt-1"})]})})})},m=({title:l,items:a,color:h})=>a.length>0?e.jsxs("div",{className:"space-y-3",children:[e.jsxs("h3",{className:`text-sm font-semibold ${h} flex items-center gap-2`,children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-current"}),l," (",a.length,")"]}),a.map(j=>e.jsx(d,{lead:j},j.id))]}):null;return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-6",children:[e.jsx("div",{className:"flex items-center justify-between",children:e.jsxs("p",{className:"text-sm text-slate-500",children:[x.length," total reminder",x.length!==1?"s":""]})}),e.jsx(m,{title:"Overdue",items:n,color:"text-red-600"}),e.jsx(m,{title:"Today",items:s,color:"text-orange-600"}),e.jsx(m,{title:"Upcoming",items:i,color:"text-blue-600"})]}),e.jsx(Le,{lead:u,open:!!u,onOpenChange:l=>!l&&p(null)})]})}function Ra(){const t=new URLSearchParams(window.location.search),u=t.get("lead"),p=t.get("tab"),[x,n]=_.useState(null),[s,i]=_.useState(null),[d,m]=_.useState(u?"leads":p||"leads"),[l,a]=_.useState(!1),[h,j]=_.useState(!1);_.useEffect(()=>{(async()=>{try{const H=await y.auth.me();n(H);const je=await y.entities.Contractor.filter({created_by:H.email},"-created_date",1);je&&je.length>0&&i(je[0])}catch{y.auth.redirectToLogin()}})()},[]);const{data:o,isLoading:r}=O({queryKey:["leads",s==null?void 0:s.id],queryFn:()=>y.entities.Lead.filter({contractor_id:s==null?void 0:s.id},"-created_date"),enabled:!!s}),{data:b,isLoading:N}=O({queryKey:["bids",s==null?void 0:s.id],queryFn:()=>y.entities.Bid.filter({contractor_id:s==null?void 0:s.id},"-created_date"),enabled:!!s}),c=(o==null?void 0:o.filter(A=>!["won","lost"].includes(A.status)))||[],v=(o==null?void 0:o.filter(A=>A.follow_up_reminder&&(me(new Date(A.follow_up_reminder))||Z(new Date(A.follow_up_reminder)))).length)||0,L=c.reduce((A,H)=>A+(H.estimated_value||0),0),q=(b==null?void 0:b.filter(A=>A.status==="sent"||A.status==="viewed"))||[],V=(b==null?void 0:b.filter(A=>A.status==="accepted"))||[];return q.reduce((A,H)=>A+(H.bid_amount||0),0),x?s?e.jsxs("div",{className:"min-h-screen bg-slate-50",children:[e.jsx(rt,{contractorId:s==null?void 0:s.id}),e.jsxs("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8",children:[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 text-slate-900",children:"CRM"}),e.jsx("p",{className:"text-slate-600 mt-1",children:"Manage your leads, bids, and sales pipeline"})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs(w,{onClick:()=>a(!0),className:"bg-green-600 hover:bg-green-700",disabled:!s,children:[e.jsx(J,{className:"w-4 h-4 mr-2"}),"Add Lead"]}),e.jsxs(w,{onClick:()=>j(!0),variant:"outline",disabled:!s,children:[e.jsx(Ne,{className:"w-4 h-4 mr-2"}),"Create Bid"]})]})]}),e.jsxs("div",{className:"grid sm:grid-cols-2 lg:grid-cols-5 gap-6 mb-8",children:[e.jsxs(S,{children:[e.jsxs($,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(M,{className:"text-sm font-medium text-slate-600",children:"Active Leads"}),e.jsx(we,{className:"w-4 h-4 text-slate-400"})]}),e.jsx(k,{children:r?e.jsx(K,{className:"h-8 w-16"}):e.jsx("div",{className:"text-2xl font-bold text-slate-900",children:c.length})})]}),e.jsxs(S,{children:[e.jsxs($,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(M,{className:"text-sm font-medium text-slate-600",children:"Pipeline Value"}),e.jsx(ie,{className:"w-4 h-4 text-slate-400"})]}),e.jsx(k,{children:r?e.jsx(K,{className:"h-8 w-24"}):e.jsxs("div",{className:"text-2xl font-bold text-slate-900",children:["$",L.toLocaleStr
64ing()]})})]}),e.jsxs(S,{children:[e.jsxs($,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(M,{className:"text-sm font-medium text-slate-600",children:"Active Bids"}),e.jsx(Ne,{className:"w-4 h-4 text-slate-400"})]}),e.jsx(k,{children:N?e.jsx(K,{className:"h-8 w-16"}):e.jsx("div",{className:"text-2xl font-bold text-slate-900",children:q.length})})]}),e.jsxs(S,{children:[e.jsxs($,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(M,{className:"text-sm font-medium text-slate-600",children:"Won This Month"}),e.jsx(U,{className:"w-4 h-4 text-slate-400"})]}),e.jsx(k,{children:N?e.jsx(K,{className:"h-8 w-16"}):e.jsx("div",{className:"text-2xl font-bold text-green-600",children:V.length})})]}),e.jsxs(S,{className:v>0?"border-red-200 bg-red-50":"",children:[e.jsxs($,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(M,{className:"text-sm font-medium text-slate-600",children:"Follow-Ups Due"}),e.jsx(W,{className:`w-4 h-4 ${v>0?"text-red-500":"text-slate-400"}`})]}),e.jsx(k,{children:r?e.jsx(K,{className:"h-8 w-16"}):e.jsx("div",{className:`text-2xl font-bold ${v>0?"text-red-600":"text-slate-900"}`,children:v})})]})]}),e.jsxs(Ke,{value:d,onValueChange:m,children:[e.jsxs(Ve,{className:"bg-white border border-slate-200",children:[e.jsxs(T,{value:"pipeline",children:[e.jsx(Ye,{className:"w-4 h-4 mr-2"}),"Pipeline"]}),e.jsxs(T,{value:"leads",children:[e.jsx(we,{className:"w-4 h-4 mr-2"}),"Leads"]}),e.jsxs(T,{value:"bids",children:[e.jsx(Ne,{className:"w-4 h-4 mr-2"}),"Bids & Quotes"]}),e.jsxs(T,{value:"inbox",children:[e.jsx(Bs,{className:"w-4 h-4 mr-2"}),"Lead Inbox"]}),e.jsxs(T,{value:"analytics",children:[e.jsx(os,{className:"w-4 h-4 mr-2"}),"Analytics"]}),e.jsxs(T,{value:"automations",children:[e.jsx(P,{className:"w-4 h-4 mr-2"}),"Automations"]}),e.jsxs(T,{value:"response_settings",children:[e.jsx(qs,{className:"w-4 h-4 mr-2"}),"Auto-Response"]}),e.jsxs(T,{value:"auto_bid",children:[e.jsx(us,{className:"w-4 h-4 mr-2"}),"Auto-Bid"]}),e.jsxs(T,{value:"reminders",className:"relative",children:[e.jsx(W,{className:"w-4 h-4 mr-2"}),"Reminders",v>0&&e.jsx("span",{className:"ml-1 bg-red-500 text-white text-xs rounded-full w-4 h-4 flex items-center justify-center leading-none",children:v})]})]}),e.jsx(B,{value:"pipeline",className:"mt-6",children:e.jsx(et,{leads:o,isLoading:r,contractorId:s==null?void 0:s.id})}),e.jsx(B,{value:"leads",className:"mt-6",children:e.jsx(Hs,{leads:o,isLoading:r,defaultOpenLeadId:u})}),e.jsx(B,{value:"bids",className:"mt-6",children:e.jsx(Zs,{bids:b,isLoading:N})}),e.jsx(B,{value:"inbox",className:"mt-6",children:e.jsx(ot,{leads:o,isLoading:r,contractorId:s==null?void 0:s.id,contractor:s})}),e.jsx(B,{value:"analytics",className:"mt-6",children:e.jsx(xt,{leads:o})}),e.jsx(B,{value:"automations",className:"mt-6",children:e.jsx(it,{contractorId:s==null?void 0:s.id})}),e.jsx(B,{value:"response_settings",className:"mt-6",children:e.jsx(mt,{contractorId:s==null?void 0:s.id,contractor:s})}),e.jsx(B,{value:"auto_bid",className:"mt-6",children:e.jsx(jt,{contractorId:s==null?void 0:s.id})}),e.jsx(B,{value:"reminders",className:"mt-6",children:e.jsx(ft,{leads:o})})]})]}),e.jsx(st,{open:l,onOpenChange:a,contractorId:s==null?void 0:s.id,contractor:s}),e.jsx(tt,{open:h,onOpenChange:j,contractorId:s==null?void 0:s.id})]}):e.jsx("div",{className:"min-h-screen bg-slate-50",children:e.jsx("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8",children:e.jsxs("div",{className:"text-center py-12",children:[e.jsx("h2",{className:"text-xl font-semibold text-slate-900 mb-2",children:"No contractor profile found"}),e.jsx("p",{className:"text-slate-600 mb-4",children:"You need to create a contractor profile first."}),e.jsx(w,{onClick:()=>window.location.href="/ContractorSignup",children:"Create Contractor Profile"})]})})}):e.jsx("div",{className:"min-h-screen bg-slate-50 flex items-center justify-center",children:e.jsx("div",{className:"text-center",children:e.jsx("p",{className:"text-slate-600",children:"Loading..."})})})}export{Ra 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.