1import{j as e}from"./ui-CMTTTAxa.js";import{u as Y,a as d}from"./vendor-BDindXV3.js";import{u as O,g as H,H as J,L as V,s as g}from"./index-Bqrg_C6x.js";import{S as K,F as Q,f as h,b as X}from"./calcEngine-D8JVR7GP.js";import{P as Z}from"./PhoneInput-BJHyWppK.js";import{t as B}from"./numericInput-COtjIJ6T.js";import{a as ee,r as te}from"./documentFee-_S0X5OiL.js";import{U as $}from"./user-BBgK2FIE.js";import{M as G}from"./mail-Fao2IFo4.js";import{P as ae}from"./phone-D1amSvms.js";import{M as re}from"./map-pin-BZ43DeO3.js";import{U as se}from"./users-D7qLce9O.js";import{D}from"./dollar-sign-D34Kc1W7.js";import{S as le}from"./search-BCuJaslL.js";import{C}from"./check-BWDmn0Iv.js";import{F as ne}from"./file-text-BsGfDO2N.js";import"./supabase-Czj9ajwo.js";function l({label:n,required:r,hint:u,error:x,children:o}){return e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("label",{className:"text-sm font-medium text-foreground",children:[n,r&&e.jsx("span",{className:"text-[#5BC8E8] ml-0.5",children:"*"})]}),o,u&&e.jsx("p",{className:"text-xs text-muted-foreground",children:u}),x&&e.jsx("p",{className:"text-xs text-red-500",children:x})]})}function m({icon:n,...r}){return e.jsxs("div",{className:"relative",children:[n&&e.jsx(n,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx("input",{...r,className:`w-full h-10 border border-border rounded-lg bg-background text-foreground text-sm placeholder:text-muted-foreground focus:outline-none focus:border-[#5BC8E8] transition-colors ${n?"pl-9 pr-3":"px-3"}`})]})}function ie({gross:n,capitalGains:r,status:u,state:x}){if(!n||!u||!x)return null;const o=X({grossIncome:n,capitalGains:r,filingStatus:u,state:x,giftMode:"pct",giftPct:60});return!o.savings||o.savings<=0?null:e.jsxs("div",{className:"bg-[#e0f7fd] border border-[#5BC8E8]/30 rounded-xl p-4 mt-2",children:[e.jsx("p",{className:"text-xs font-semibold text-[#0c6b87] uppercase tracking-wide mb-3",children:"Preliminary Savings Estimate (60% gift)"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-[#0c6b87] mb-1",children:"Current Liability"}),e.jsx("p",{className:"text-base font-bold text-[#0c6b87]",children:h(o.total_liability)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-[#0c6b87] mb-1",children:"Gift Amount (60%)"}),e.jsx("p",{className:"text-base font-bold text-[#0c6b87]",children:h(o.gift_amt)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-[#0c6b87] mb-1",children:"Est. Net Savings"}),e.jsx("p",{className:"text-xl font-black text-[#059669]",children:h(o.total_savings)})]})]})]})}function Ce(){const n=Y(),{user:r}=O(),[u,x]=d.useState([]),[o,S]=d.useState(""),[P,b]=d.useState(!1),[E,_]=d.useState(!1),[f,k]=d.useState({}),[c,q]=d.useState(null),[R,W]=d.useState(null);d.useEffect(()=>{ee().then(W)},[]);const L=t=>`$${(te({isRenewal:t,settings:R})/100).toLocaleString("en-US")} documents fee`,[j,M]=d.useState(!1),[a,A]=d.useState({first_name:"",last_name:"",email:"",phone:"",address:"",city:"",state:"FL",zip:"",spouse_first_name:"",spouse_last_name:"",spouse_email:"",agi:"",capital_gains:"",filing_status:"single",product_type:"charitable_gift",referring_affiliate_id:"",referring_affiliate_name:"",source:"manual",notes:"",lead_type:"new"});d.useEffect(()=>{async function t(){(r==null?void 0:r.role)==="AFFILIATE"&&A(p=>({...p,source:"affiliate_portal"}));const{data:i}=await g.from("affiliates").select("id, first_name, last_name, company, email").eq("status","active").order("last_name");x(i||[])}t()},[r]);const s=(t,i)=>{A(p=>({...p,[t]:i})),f[t]&&k(p=>({...p,[t]:null}))},v=u.filter(t=>!o||`${t.first_name} ${t.last_name} ${t.company||""}`.toLowerCase().includes(o.toLowerCase())),z=()=>{const t={};return a.first_name.trim()||(t.first_name="Required"),a.last_name.trim()||(t.last_name="Required"),a.email.trim()||(t.email="Required"),a.email&&!a.email.includes("@")&&(t.email="Invalid email"),a.agi&&parseFloat(a.agi)<=0&&(t.agi="Must be greater than 0"),a.state||(t.state="Required"),a.filing_status||(t.filing_status="Required"),k(t),Object.keys(t).length===0},T=async()=>
1{if(z()){_(!0);try{const t=B(a.agi),i=B(a.capital_gains),p=new Date().getFullYear(),{data:y,error:F}=await g.from("clients").insert({first_name:a.first_name.trim(),last_name:a.last_name.trim(),email:a.email.trim(),phone:a.phone||null,address:a.address||null,city:a.city||null,state:a.state,zip:a.zip||null,spouse_first_name:a.spouse_first_name||null,spouse_last_name:a.spouse_last_name||null,spouse_email:a.spouse_email||null,agi:t,filing_status:a.filing_status,capital_gains:i,product_type:a.product_type,pipeline_stage:"NEW_LEAD",stage_updated_at:new Date().toISOString(),referring_affiliate_id:a.referring_affiliate_id||null,referring_affiliate_name:a.referring_affiliate_name||null,source:a.source,notes:a.notes||null,assigned_to:r==null?void 0:r.id,assigned_to_name:(r==null?void 0:r.full_name)||(r==null?void 0:r.email),created_by:r==null?void 0:r.id,created_by_name:(r==null?void 0:r.full_name)||(r==null?void 0:r.email)}).select("id").single();if(F)throw F;const{data:w,error:I}=await g.from("client_years").insert({client_id:y.id,client_name:`${a.first_name} ${a.last_name}`,client_email:a.email,year:p,year_type:a.lead_type,is_renewal:a.lead_type==="renewal",pipeline_stage:"NEW_LEAD",agi:t,filing_status:a.filing_status,state:a.state,capital_gains:i,product_type:a.product_type,created_by:r==null?void 0:r.id}).select().single();if(I)throw I;await g.from("pipeline_notes").insert({client_id:y.id,stage:"NEW_LEAD",note_type:"system",note:`Lead created by ${(r==null?void 0:r.full_name)||(r==null?void 0:r.email)}${t?` with AGI ${h(t)}`:" (AGI not provided)"}.`,is_internal:!0,is_system:!0,added_by:r==null?void 0:r.id,added_by_name:"System",added_by_role:r==null?void 0:r.role}),q({clientId:y.id,clientYearId:(w==null?void 0:w.id)||null,firstName:a.first_name,lastName:a.last_name,hasAgi:t!=null&&t>0}
1),_(!1)}catch(t){console.error("NewLead error:",t),alert("Failed to create lead: "+(t.message||"Unknown error")),_(!1)}}},N=parseFloat(a.agi)||0,U=parseFloat(a.capital_gains)||0;return e.jsxs("div",{className:"max-w-2xl mx-auto space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("button",{onClick:()=>n("/pipeline"),className:"flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors group",children:[e.jsx(H,{className:"w-4 h-4 transition-transform group-hover:-translate-x-0.5"}),"Pipeline"]}),e.jsx("span",{className:"text-muted-foreground",children:"/"}),e.jsx("span",{className:"text-sm font-medium text-foreground",children:"New Lead"})]}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-foreground",children:"New Lead"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"Fill in what you know. AGI is optional â you can add it and run a calculation later."})]}),e.jsxs("div",{className:"bg-card border border-border/50 rounded-xl p-6 space-y-5",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide mb-2",children:"Type of Lead"}),e.jsx("div",{className:"grid grid-cols-2 gap-2",children:[{v:"new",label:"New",hint:`${L(!1)} applies`},{v:"renewal",label:"Renewal",hint:`${L(!0)} (the renewal rate)`}].map(t=>{const i=a.lead_type===t.v;return e.jsxs("button",{type:"button",onClick:()=>s("lead_type",t.v),className:`rounded-lg border px-3 py-2.5 text-left transition-colors ${i?"border-[#5BC8E8] bg-[#5BC8E8]/10":"border-border hover:border-foreground
1/20"}`,children:[e.jsx("span",{className:`block text-sm font-semibold ${i?"text-[#5BC8E8]":"text-foreground"}`,children:t.label}),e.jsx("span",{className:"block text-[11px] text-muted-foreground mt-0.5",children:t.hint})]},t.v)})})]}),e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Primary Borrower"}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsx(l,{label:"First Name",required:!0,error:f.first_name,children:e.jsx(m,{icon:$,placeholder:"Marcus",value:a.first_name,onChange:t=>s("first_name",t.target.value)})}),e.jsx(l,{label:"Last Name",required:!0,error:f.last_name,children:e.jsx(m,{placeholder:"Williams",value:a.last_name,onChange:t=>s("last_name",t.target.value)})})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsx(l,{label:"Email",required:!0,error:f.email,children:e.jsx(m,{icon:G,type:"email",placeholder:"[email protected]",value:a.email,onChange:t=>s("email",t.target.value)})}),e.jsx(l,{label:"Phone",children:e.jsxs("div",{className:"relative",children:[e.jsx(ae,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground pointer-events-none z-10"}
1),e.jsx(Z,{value:a.phone,onChange:t=>s("phone",t),placeholder:"(305)555-0100",className:"w-full h-10 border border-border rounded-lg bg-background text-foreground text-sm placeholder:text-muted-foreground focus:outline-none focus:border-[#5BC8E8] transition-colors pl-9 pr-3"})]})})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsx("div",{className:"col-span-2",children:e.jsx(l,{label:"City",children:e.jsx(m,{icon:re,placeholder:"Miami",value:a.city,onChange:t=>s("city",t.target.value)})})}),e.jsx(l,{label:"State",required:!0,error:f.state,children:e.jsx("select",{value:a.state,onChange:t=>s("state",t.target.value),className:"w-full h-10 border border-border rounded-lg bg-background text-foreground text-sm px-3 focus:outline-none focus:border-[#5BC8E8]",children:K.map(t=>e.jsx("option",{value:t,children:t},t))})})]}),e.jsxs("button",{type:"button",onClick:()=>M(!j),className:"flex items-center gap-2 text-sm text-[#5BC8E8] hover:text-[#4ab8d8] transition-colors",children:[e.jsx(se,{className:"w-4 h-4"}),j?"Remove co-borrower":"+ Add co-borrower / spouse"]}),j&&e.jsxs("div",{className:"border-t border-border/50 pt-4 space-y-4",children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Co-Borrower / Spouse"}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsx(l,{label:"First Name",children:e.jsx(m,{icon:$,placeholder:"Sarah",value:a.spouse_first_name,onChange:t=>s("spouse_first_name",t.target.value)})}),e.jsx(l,{label:"Last Name",children:e.jsx(m,{placeholder:"Williams",value:a.spouse_last_name,onChange:t=>s("spouse_last_name",t.target.value)})})]}),e.jsx(l,{label:"Email",children:e.jsx(m,{icon:G,type:"email",placeholder:"[email protected]",value:a.spouse_email,onChange:t=>s("spouse_email",t.target.value)})})]})]}),e.jsxs("div",{className:"bg-card border border-border/50 rounded-xl p-6 space-y-5",children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Financial Information"}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsx(l,{label:"Adjusted Gross Income (AGI)",hint:"Optional â ordinary income, excluding capital gains",error:f.agi,children:e.jsx(m,{icon:D,type:"number",placeholder:"1000000",value:a.agi,onChange:t=>s("agi",t.target.value)})}),e.jsx(l,{label:"Capital Gains (if applicable)",hint:"Leave blank if none",children:e.jsx(m,{icon:D,type:"number",placeholder:"0",value:a.capital_gains,onChange:t=>s("capital_gains",t.target.value)})})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsx(l,{label:"Filing Status",required:!0,error:f.filing_status,children:e.jsx("select",{value:a.filing_status,onChange:t=>s("filing_status",t.target.value),className:"w-full h-10 border border-border rounded-lg bg-background text-foreground text-sm px-3 focus:outline-none focus:border-[#5BC8E8]",children:Object.entries(Q).map(([t,i])=>e.jsx("option",{value:t,children:i},t))})}),e.jsx(l,{label:"Product Type",children:e.jsxs("select",{value:a.product_type,onChange:t=>s("product_type",t.target.value),className:"w-full h-10 border border-border rounded-lg bg-background text-foreground text-sm px-3 focus:outline-none focus:border-[#5BC8E8]",children:[e.jsx("option",{value:"charitable_gift",children:"Charitable Gift Financing"}),e.jsx("option",{value:"bank_strategy",children:"Bank Strategy"}),e.jsx("option",{value:"capital_gains",children:"Capital Gains"})]})})]}),N>0&&e.jsx(ie,{gross:N,capitalGains:U,status:a.filing_status,state:a.state})]}),e.jsxs("div",{className:"bg-card border border-border/50 rounded-xl p-6 space-y-5",children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Lead Details"}),e.jsx(l,{label:"Referring Affiliate",hint:"Start typing to search",children:e.jsxs("div",{className:"relative",children:[e.jsx(le,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx("input",{type:"text",placeholder:"Search affiliates...",value:a.referring_affiliate_name||o,onChange:t=>{S(t.target.value),s("referring_affiliate_id",""),s("referring_affiliate_name",""),b(!0)},onFocus:()=>b(!0),className:"w-full h-10 border border-border rounded-lg bg-background text-foreground text-sm pl-9 pr-3 focus:outline-none focus:border-[#5BC8E8]"}),a.referring_affiliate_id&&e.jsx(C,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-[#059669]"}),P&&(o||v.length>0)&&e.jsx("div",{className:"absolute z-10 top-full left-0 right-0 mt-1 bg-card border border-border rounded-lg shadow-md overflow-hidden max-h-48 overflow-y-auto scrollbar-cyan",children:v.length===0?e.jsx("div",{className:"px-3 py-2 text-sm text-muted-foreground",children:"No affiliates found"}):v.map(t=>e.jsxs("button",{onClick:()=>{s("referring_affiliate_id",t.id),s("referring_affiliate_name",`${t.first_name} ${t.last_name}${t.company?` â ${t.company}`:""}`),S(""),b(!1)},className:"w-full text-left px-3 py-2 text-sm hover:bg-secondary transition-colors flex items-center justify-between",children:[e.jsxs("span",{className:"text-foreground",children:[t.first_name," ",t.last_name]}),t.company&&e.jsx("span",{className:"text-xs text-muted-foreground",children:t.company})]},t.id))})]})}),e.jsx(l,{label:"Lead Source",children:e.jsxs("select",{value:a.source,onCh
1ange:t=>s("source",t.target.value),className:"w-full h-10 border border-border rounded-lg bg-background text-foreground text-sm px-3 focus:outline-none focus:border-[#5BC8E8]",children:[e.jsx("option",{value:"manual",children:"Manual Entry"}),e.jsx("option",{value:"affiliate_portal",children:"Affiliate Portal"}),e.jsx("option",{value:"email",children:"Email"}),e.jsx("option",{value:"calendar",children:"Calendar Booking"}),e.jsx("option",{value:"form",children:"Web Form"}),e.jsx("option",{value:"salesmate",children:"Salesmate"}),e.jsx("option",{value:"referral",children:"Direct Referral"})]})}),e.jsx(l,{label:"Notes",hint:"Internal only â not visible to client",children:e.jsx("textarea",{value:a.notes,onChange:t=>s("notes",t.target.value),placeholder:"Any initial notes about this lead...",rows:3,className:"w-full border border-border rounded-lg bg-background text-foreground text-sm px-3 py-2.5 placeholder:text-muted-foreground focus:outline-none focus:border-[#5BC8E8] resize-none"})})]}),e.jsxs("div",{className:"bg-secondary/50 border border-border/30 rounded-xl p-4",children:[e.jsx("p",{className:"text-sm font-medium text-foreground mb-2",children:"What happens when you save:"}),e.jsx("ul",{className:"space-y-1.5",children:["Client record created with NEW_LEAD status","Client year created for the current tax year",N>0?"You'll be asked if you want to run a calculation now":"AGI not provided â you can add it and run a calculation later"].map((t,i)=>e.jsxs("li",{className:"flex items-start gap-2 text-sm text-muted-foreground",children:[e.jsx(C,{className:"w-3.5 h-3.5 text-[#5BC8E8] mt-0.5 flex-shrink-0"}),t]},i))})]}),e.jsxs("div",{className:"flex items-center justify-between pb-6",children:[e.jsx("button",{onClick:()=>n(-1),className:"px-4 py-2.5 rounded-lg border border-border text-sm font-medium text-muted-foreground hover:text-foreground hover:bg-secondary transition-colors",children:"Cancel"}),e.jsx(J,{text:"Creates this client as a New Lead with a tax year, ready for a calculation.",children:e.jsx("button",{onClick:T,disabled:E,className:"flex items-center gap-2 px-6 py-2.5 rounded-lg bg-[#5BC8E8] text-black text-sm font-semibold hover:bg-[#4ab8d8] transition-colors disabled:opacity-60",children:E?e.jsxs(e.Fragment,{children:[e.jsx(V,{className:"w-4 h-4 animate-spin"}),"Saving lead..."]}):e.jsxs(e.Fragment,{children:[e.jsx(ne,{className:"w-4 h-4"}),"Save Lead"]})})})]}),c&&e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center p-4",style:{background:"rgba(0,0,0,0.5)"},children:e.jsxs("div",{className:"rounded-2xl p-7 w-full max-w-md
1shadow-xl",style:{background:"rgba(4, 20, 36, 0.95)",border:"1px solid rgba(255,255,255,0.08)"},children:[e.jsx("div",{style:{width:48,height:48,borderRadius:"50%",background:"rgba(16,185,129,0.15)",color:"#34d399",display:"grid",placeItems:"center",margin:"0 auto 14px"},children:e.jsx(C,{className:"w-6 h-6"})}),e.jsx("h2",{className:"text-lg font-bold text-foreground text-center mb-2",children:"Lead created"}),e.jsxs("p",{className:"text-sm text-muted-foreground text-center mb-6",children:[c.firstName," ",c.lastName," saved with NEW_LEAD status.",c.hasAgi?" Would you like to run a calculation now?":" Add AGI to this client later to run a calculation."]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsx("button",{onClick:()=>n(`/client?clientId=${c.clientId}`),className:"flex-1 py-2.5 rounded-xl border border-border text-sm font-medium text-muted-foreground hover:bg-secondary transition-colors",children:c.hasAgi?"Not now":"View lead"}),c.hasAgi&&e.jsx("button",{onClick:()=>n(`/calculator?clientId=${c.clientId}&yearId=${c.clientYearId||""}`),className:"flex-1 flex items-center justify-center gap-2 py-2.5 rounded-xl bg-[#5BC8E8] text-black text-sm font-semibold hover:bg-[#4ab8d8] transition-colors",children:"Yes, run calculation â"})]})]})})]})}export{Ce 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.