1import{v as He,r as s,a6 as ue,a8 as V,j as e,B as h,br as me,aW as xe,bp as Ke,u as Ue,aE as We,b8 as Ye}from"./index-BvPgZKEc.js";import{u as he}from"./useMutation-BwiC2GMb.js";import{c as Ge}from"./crmLenders-B20xjULu.js";import{I as o}from"./input-Do2OaUlj.js";import{L as n}from"./label-BTDuUnG0.js";import{C as N,a as _,b as w,d as C}from"./card-BBkXusDc.js";import{S as pe,a as ve,b as ge,c as je,d as ye}from"./select-BRkQ0l-v.js";import{u as Qe}from"./usePipeline-DRtie0m0.js";import{b as Je}from"./pipeline-CztBgcLz.js";import{A as Xe}from"./AddressInput-RVwCZChI.js";import{B as fe,c as Ze}from"./BorrowerMembersInput-DH6AXN3x.js";import{A as et}from"./arrow-left-DqKlNjo4.js";import"./index-BdQq_4o_.js";import"./index-BXHe3jdc.js";function vt(){const d=Ue(),{hasCrmAccess:S,enterpriseId:g,isDemo:L}=We();return!S||!g?e.jsx(Ye,{}):e.jsx(tt,{enterpriseId:g,isDemo:L,onCancel:()=>d("/crm"),onCreated:u=>d(`/crm/deals/${u}`)})}function tt({enterpriseId:d,isDemo:S,onCancel:g,onCreated:L}){const{toast:u}=He(),H=Qe(),[j,be]=s.useState(""),[K,Ne]=s.useState(null),[B,A]=s.useState(!1),[m,U]=s.useState("individual"),[P,_e]=s.useState(""),[F,we]=s.useState(""),[k,W]=s.useState(""),[E,Y]=s.useState(""),[M,Ce]=s.useState(""),[D,G]=s.useState([]),[Q,Se]=s.useState(""),[f,Le]=s.useState({}),[T,Be]=s.useState(""),[$,Ae]=s.useState(""),[R,Pe]=s.useState(""),[J,Fe]=s.useState(""),[p,ke]=s.useState(""),[I,Ee]=s.useState(""),[q,Me]=s.useState(""),[X,De]=s.useState(""),[Z,Te]=s.useState("flip"),[i,b]=s.useState([{lender_name:"",allocation_type:"percent",allocation_value:100}]),{data:z=[]}=ue({queryKey:["crm-borrowers",j],queryFn:()=>V.listBorrowers(d,j||void 0),enabled:!B}),{data:ee=[]}=ue({queryKey:["crm-lenders-names",d],queryFn:()=>Ge.listLenders(d),enabled:!S,staleTime:6e4}),$e=s.useMemo(()=>ee.map(t=>t.name),[ee]),te=he({mutationFn:t=>V.createBorrower(d,t),onError:t=>{u({title:"Error creating borrower",description:t.message,variant:"destructive"})}}),O=he({mutationFn:t=>V.createDeal(d,t),onSuccess:t=>{u({title:"Deal created"}),L(t.id)},onError:t=>{u({title:"Error creating deal",description:t.message,variant:"destructive"})}}),Re=async t=>{var y,oe,le,ie,ce;t.preventDefault();let a=K;if(B){if(m==="llc"){if(!M.trim()){u({title:"LLC name is required",variant:"destructive"});return}}else if(!P||!F){u({title:"First and last name are required",variant:"destructive"});return}try{const v=Ze(D);a=(await te.mutateAsync({borrower_type:m,first_name:m==="llc"?"":P,last_name:m==="llc"?"":F,email:k||void 0,phone:E||void 0,llc_name:m==="llc"?M:void 0,members:v})).id}catch{return}}if(!a){u({title:"Please select or create a borrower",variant:"destructive"});return}const r=i.filter(v=>v.lender_name.trim()).map((v,de)=>({lender_name:v.lender_name.trim(),allocation_type:v.allocation_type,allocation_value:v.allocation_value||0,priority_order:de})),l={property:{address:Q.trim()||void 0,address_line1:((y=f.line1)==null?void 0:y.trim())||void 0,city:((oe=f.city)==null?void 0:oe.trim())||void 0,state:((le=f.state)==null?void 0:le.trim())||void 0,zip:((ie=f.postal_code)==null?void 0:ie.trim())||void 0,purchase_price:T?parseFloat(T):void 0,arv:$?parseFloat($):void 0,renovation_budget:R?parseFloat(R):void 0},loan_summary:{funding_type:X||void 0}},x=(ce=H.stages[0])==null?void 0:ce.key;O.mutate({borrower_id:a,...x?Je(H,x):{},data:l,loan_terms:{loan_start_date:J||null,loan_amount:p?parseFloat(p):null,interest_rate_initial:I?parseFloat(I):null,points_initial:q?parseFloat(q):null,scope:Z},lenders:r.length>0?r:void 0})},Ie=()=>{var t;b([...i,{lender_name:"",allocation_type:((t=i[0])==null?void 0:t.allocation_type)??"percent",allocation_value:0}])},ae=(t,a,r)=>{const l=[...i];l[t]={...l[t],[a]:r},b(l)},qe=t=>{const a=p?parseFloat(p):null,r=i[t],l=r.allocation_type==="percent"?"amount":"percent";let x=r.allocation_value;a&&r.allocation_value&&(x=l==="amount"?Math.round(r.allocation_value*a/100*100)/100:Math.round(r.allocation_value/a*100*100)/100);const y=[...i];y[t]={...r,allocation_type:l,allocation_value:x},b(y)},ze=t=>{b(i.filter((a,r)=>r!==t))}
1,c=p?parseFloat(p):null,se=i.some(t=>t.allocation_type==="amount"),re=i.reduce((t,a)=>a.allocation_type==="amount"?t+(a.allocation_value||0):t+(c!=null?(a.allocation_value||0)*c/100:0),0),ne=i.reduce((t,a)=>a.allocation_type==="percent"?t+(a.allocation_value||0):t+(c?(a.allocation_value||0)/c*100:0),0),Oe=se?`$${Math.round(re).toLocaleString()} allocated`:`${Math.round(ne*100)/100}% allocated`,Ve=se?c!=null&&Math.abs(re-c)<.01:Math.round(ne*100)/100===100;return e.jsxs("form",{onSubmit:Re,className:"max-w-3xl mx-auto py-4 sm:py-8 px-1 sm:px-0 space-y-6",children:[e.jsxs(h,{type:"button",variant:"ghost",onClick:g,className:"pl-0 text-muted-foreground hover:text-foreground hover:bg-transparent hover:underline",children:[e.jsx(et,{className:"mr-2 h-4 w-4"})," Back to Deals"]}),e.jsxs("div",{className:"text-center space-y-1",children:[e.jsx("h2",{className:"text-xl font-semibold",children:"New Deal"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Create a new loan deal with borrower and property details"})]}),e.jsxs(N,{children:[e.jsx(_,{children:e.jsx(w,{className:"text-base",children:"Borrower"})}),e.jsx(C,{className:"space-y-3",children:B?e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{type:"button",onClick:()=>U("individual"),className:`px-3 py-1.5 rounded-md text-sm ${m==="individual"?"bg-foreground text-background":"bg-muted"}`,children:"Individual"}),e.jsx("button",{type:"button",onClick:()=>U("llc"),className:`px-3 py-1.5 rounded-md text-sm ${m==="llc"?"bg-foreground text-background":"bg-muted"}`,children:"LLC"})]}),m==="llc"?e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx(n,{children:"LLC Name *"}),e.jsx(o,{value:M,onChange:t=>Ce(t.target.value),placeholder:"e.g. 123 Main St Holdings LLC"})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(n,{children:"Entity Email"}),e.jsx(o,{type:"email",value:k,onChange:t=>W(t.target.value)})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Entity Phone"}),e.jsx(o,{value:E,onChange:t=>Y(t.target.value)})]})]}),e.jsx(fe,{members:D,onChange:G,borrowerType:"llc"})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(n,{children:"First Name *"}),e.jsx(o,{value:P,onChange:t=>_e(t.target.value)})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Last Name *"}),e.jsx(o,{value:F,onChange:t=>we(t.target.value)})]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(n,{children:"Email"}),e.jsx(o,{type:"email",value:k,onChange:t=>W(t.target.value)})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Phone"}),e.jsx(o,{value:E,onChange:t=>Y(t.target.value)})]})]}),e.jsx(fe,{members:D,onChange:G,borrowerType:"individual"})]}),e.jsx(h,{type:"button",variant:"ghost",size:"sm",onClick:()=>A(!1),children:"Search existing instead"})]}):e.jsxs(e.Fragment,{children:[e.jsx(o,{placeholder:"Search existing borrowers...",value:j,onChange:t=>be(t.target.value)}),z.length>0&&e.jsx("div",{className:"border rounded-md max-h-40 overflow-y-auto",children:z.map(t=>{const a=t.borrower_type==="llc",r=[t.first_name,t.last_name].filter(Boolean).join(" ").trim(),l=a?t.llc_name||r||"LLC":r||"Borrower",x=a?r:t.llc_name;return e.jsxs("button",{type:"button",onClick:()=>Ne(t.id),className:`w-full text-left px-3 py-2 text-sm hover:bg-muted transition-colors ${K===t.id?"bg-muted font-medium":""}`,children:[e.jsx("span",{className:"block truncate",children:l}),x&&e.jsx("span",{className:"block truncate text-xs text-muted-foreground",children:x})]},t.id)})}),z.length===0?e.jsxs("div",{className:"space-y-1",children:[j.trim()&&e.jsxs("p",{className:"text-sm text-muted-foreground",children:["No borrowers match â",j.trim(),"â."]}),e.jsx(h,{type:"button",variant:"outline",size:"sm",onClick:()=>A(!0),children:"+ New Borrower"})]}):e.jsx("button",{type:"button",onClick:()=>A(!0),className:"text-xs text-muted-foreground hover:text-foreground hover:underline",children:"None of these â add a new borrower"})]})})]}),e.jsxs(N,{children:[e.jsx(_,{children:e.jsx(w,{className:"text-base",children:"Property"})}),e.jsxs(C,{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(n,{children:"Address"}),e.jsx(Xe,{value:Q,onChange:Se,onParsed:t=>Le(t)})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx(n,{children:"Purchase Price"}),e.jsx(o,{type:"number",value:T,onChange:t=>Be(t.target.value),placeholder:"0"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"What the borrower says they're paying. Recorded from intake â it does not determine the loan amount."})]}),e.jsxs("div",{children:[e.jsx(n,{children:"ARV (After-Repair Value)"}),e.jsx(o,{type:"number",value:$,onChange:t=>Ae(t.target.value),placeholder:"0"})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Renovation Budget"}),e.jsx(o,{type:"number",value:R,onChange:t=>Pe(t.target.value),placeholder:"0"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Entered once here â it also populates the deal's renovation escrow used for draws."})]})]})]})]}),e.jsxs(N,{children:[e.jsx(_,{children:e.jsx(w,{className:"text-base",children:"Loan Summary"})}),e.jsx(C,{children:e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx(n,{children:"Funding Type"}),e.jsxs(pe,{value:X||void 0,onValueChange:t=>De(t),children:[e.jsx(ve,{children:e.jsx(ge,{placeholder:"Select"})}),e.jsx(je,{children:Object.keys(me).map(t=>e.jsx(ye,{value:t,children:me[t]},t))})]})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Scope of Project"}),e.jsxs(pe,{value:Z,onValueChange:t=>Te(t),children:[e.jsx(ve,{children:e.jsx(ge,{})}),e.jsx(je,{children:Object.keys(xe).map(t=>e.jsx(ye,{value:t,children:xe[t]},t))})]})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Loan Start Date"}),e.jsx(o,{type:"date",value:J,onChange:t=>Fe(t.target.value)}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Needed to accrue interest on payoff."})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Loan Amount"}),e.jsx(o,{type:"number",value:p,onChange:t=>ke(t.target.value),placeholder:"0"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"How much you're lending. Your decision â it can fund part of the purchase, the rehab, or both."})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Interest Rate (%)"}),e.jsx(o,{type:"number",step:"0.01",value:I,onChange:t=>Ee(t.target.value),placeholder:"0"})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Points (%)"}),e.jsx(o,{type:"number",step:"0.01",value:q,onChange:t=>Me(t.target.value),placeholder:"0"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Charged at payoff (end of loan)."})]})]})})]}),e.jsxs(N,{children:[e.jsxs(_,{className:"flex flex-row items-center justify-between",children:[e.jsx(w,{className:"text-base",children:"Lenders"}),e.jsx("span",{className:`text-sm ${Ve?"text-status-success":"text-status-warning"}`,children:Oe})]}),e.jsxs(C,{className:"space-y-3",children:[i.map((t,a)=>{const r=t.allocation_value?t.allocation_type==="percent"?c!=null?`â $${Math.round(t.allocation_value*c/100).toLocaleStr
1ing()}`:null:c?`â ${Math.round(t.allocation_value/c*1e4)/100}%`:null:null;return e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-start gap-2 sm:gap-3 p-3 sm:p-0 border sm:border-0 rounded-md",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx(n,{children:"Name"}),e.jsx(Ke,{value:t.lender_name,onChange:l=>ae(a,"lender_name",l),options:$e}),e.jsx("div",{className:"h-4 mt-1"})]}),e.jsxs("div",{className:"w-full sm:w-40 sm:flex-none",children:[e.jsx(n,{children:"Allocation"}),e.jsxs("div",{className:"flex",children:[e.jsx(h,{type:"button",variant:"outline",size:"sm",className:"rounded-r-none border-r-0 px-2 font-mono w-9 h-10",onClick:()=>qe(a),title:"Toggle % / $",children:t.allocation_type==="amount"?"$":"%"}),e.jsx(o,{type:"number",className:"rounded-l-none",value:t.allocation_value||"",onChange:l=>ae(a,"allocation_value",parseFloat(l.target.value)||0)})]}),e.jsx("div",{className:"h-4 mt-1 text-xs text-muted-foreground truncate",children:r??""})]}),i.length>1&&e.jsx(h,{type:"button",variant:"ghost",size:"sm",className:"self-end sm:self-start sm:mt-7",onClick:()=>ze(a),children:"Remove"})]},a)}),i.length<5&&e.jsx(h,{type:"button",variant:"outline",size:"sm",onClick:Ie,children:"+ Add Lender"})]})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsx(h,{type:"submit",disabled:O.isPending||te.isPending,children:O.isPending?"Creating...":"Create Deal"}),e.jsx(h,{type:"button",variant:"outline",onClick:g,children:"Cancel"})]})]})}export{tt as CrmDealForm,vt as CrmDealFormPage};
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.