1import{g as ee,j as e,cZ as se,b as ae,p as te,r as l,L as ne,bQ as re,k as A,B as h,I as o,X as ie,W as le,s as c,t as m}from"./index-CzAkNt2W.js";import{L as n}from"./label-BwWVuV93.js";import{D as oe,a as ce,b as de,c as me,d as ue,f as pe}from"./dialog-DFazxlVc.js";import{T as xe}from"./textarea-CMw1Ny2O.js";import{P as he}from"./PortalInviteDialog-BUY6KeBg.js";import{R as ge}from"./RequireRole-DhUlFC_T.js";import{P as fe}from"./pencil-DRW9I4e2.js";import{C as je}from"./circle-check-gM2QolJ6.js";import{U as ye}from"./user-plus-oIrmPAoZ.js";import{S as ve}from"./save-CP_iJoQe.js";import{P as _e}from"./plus-B3ftHR68.js";import{C as Ne}from"./copy-Ba4NJFqU.js";import"./useServerFn-7MfwXlwR.js";import"./portal-invites.functions-cOQxcl_V.js";import"./send-Bd9t90n1.js";const be=[["circle",{cx:"18",cy:"5",r:"3",key:"gq8acd"}],["circle",{cx:"6",cy:"12",r:"3",key:"w7nqdw"}],["circle",{cx:"18",cy:"19",r:"3",key:"1xt0gg"}],["line",{x1:"8.59",x2:"15.42",y1:"13.51",y2:"17.49",key:"47mynk"}],["line",{x1:"15.41",x2:"8.59",y1:"6.51",y2:"10.49",key:"1n3mei"}]],we=ee("share-2",be);function Ce(){const{id:g}=se.useParams(),R=ae(),_=te(R?.user),q=!!_?.some(s=>["owner","office","company_marketing","company_success","superadmin"].includes(s)),N=!!_?.some(s=>["owner","office","superadmin"].includes(s)),b=!!_?.some(s=>["owner","office","company_success","superadmin"].includes(s)),[t,P]=l.useState(null),[z,E]=l.useState([]),[I,U]=l.useState({}),[M,W]=l.useState([]),[$,D]=l.useState(!1),[i,p]=l.useState({address:"",city:"",state:"",zip:"",pool_type:"in-ground",pool_size_gallons:"",service_frequency:"weekly"}),[F,H]=l.useState(null),[w,Z]=l.useState(null),[L,T]=l.useState(!1),[B,C]=l.useState(!1),[k,O]=l.useState(!1),[r,u]=l.useState({full_name:"",email:"",phone:"",address_line1:"",city:"",state:"",postal_code:"",notes:""});async function f(){const s=(await c.auth.getUser()).data.user;if(!s){P(null),E([]);return}const[{data:a},{data:d},{data:y}]=await Promise.all([c.from("customers").select("*").eq("id",g).maybeSingle(),c.from("properties").select("*").eq("customer_id",g).order("created_at"),c.from("profiles").select("company_id").eq("id",s.id).single()]);if(P(a),E(d??[]),H(y?.company_id??null),y?.company_id){const[{data:S},{data:v},{data:x}]=await Promise.all([c.from("profiles").select("id, full_name").eq("company_id",y.company_id),c.from("user_roles").select("user_id, role"),c.from("companies").select("name, brand_color, logo_url").eq("id",y.company_id).single()]),Y=new Set((v??[]).filter(j=>["technician","owner","office"].includes(j.role)).map(j=>j.user_id));W((S??[]).filter(j=>Y.has(j.id))),x&&Z(x)}if(d&&d.length){const{data:S}=await c.from("equipment").select("*").in("property_id",d.map(x=>x.id)),v={};(S??[]).forEach(x=>{(v[x.property_id]||=[]).push(x)}),U(v)}}l.useEffect(()=>{f()},[g]);function Q(){t&&(u({full_name:t.full_name??"",email:t.email??"",phone:t.phone??"",address_line1:t.address_line1??"",city:t.city??"",state:t.state??"",postal_code:t.postal_code??"",notes:t.notes??""}),C(!0))}async function X(s){s.preventDefault(),O(!0);const{error:a}=await c.from("customers").update({full_name:r.full_name,email:r.email||null,phone:r.phone||null,address_line1:r.address_line1||null,city:r.city||null,state:r.state||null,postal_code:r.postal_code||null,notes:r.notes||null}).eq("id",g);if(O(!1),a)return m.error(a.message);m.success("Customer updated"),C(!1),f()}async function G(s){if(s.preventDefault(),!F)return;const{error:a}=await c.from("properties").insert({company_id:F,customer_id:g,address:i.address,city:i.city||null,state:i.state||null,zip:i.zip||null,pool_type:i.pool_type||null,pool_size_gallons:i.pool_size_gallons?Number(i.pool_size_gallons):null,service_frequency:i.service_frequency});if(a)return m.error(a.message);m.success("Property added"),D(!1),p({address:"",city:"",state:"",zip:"",pool_type:"in-ground",pool_size_gallons:"",service_frequency:"weekly"}),f()}async function J(s,a){const{error:d}=await c.from("properties").update({assigned_technician_id:a}).eq("id",s);if(d)return m.error(d.message);m.success(a?"Technician assigned":"Cleared assignment"),f()}async function K(s,a){const{error:d}=await c.from("properties").update({service_frequency:a}).eq("id",s);if(d)return m.error(d.message);m.success("Frequency updated"),f()}async function V(s){if(!s)return m.error("No share token");const a=`${window.location.origin}/portal/${s}`;await navigator.clipboard.writeText(a),m.success("Homeowner link copied")}return t?e.jsxs("div",{className:"p-6 md:p-8 max-w-5xl",children:[e.jsxs(ne,{to:"/customers",className:"text-sm text-muted-foreground inline-flex items-center gap-1.5 hover:text-foreground",children:[e.jsx(re,{className:"h-4 w-4"})," Back to customers"]}),e.jsxs("div",{className:"mt-3 flex flex-wrap items-start justify-between gap-4",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("h1",{className:"font-display text-3xl font-bold",children:t.full_name}),e.jsx("p",{className:"text-sm text-muted-foreground",children:[t.email,t.ph
1one].filter(Boolean).join(" · ")}),(t.address_line1||t.city)&&e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[e.jsx(A,{className:"inline h-3.5 w-3.5 mr-1"}),[t.address_line1,t.city,t.state,t.postal_code].filter(Boolean).join(", ")]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[q&&!B&&e.jsxs(h,{size:"sm",variant:"outline",className:"gap-2",onClick:Q,children:[e.jsx(fe,{className:"h-4 w-4"})," Edit"]}),b&&(t.auth_user_id?e.jsxs("span",{className:"inline-flex items-center gap-1.5 text-sm text-emerald-600 font-medium",children:[e.jsx(je,{className:"h-4 w-4"})," Portal account active"]}):e.jsxs(h,{size:"sm",className:"gap-2",onClick:()=>T(!0),disabled:!t.email,title:t.email?"Send portal invitation":"Add an email first",children:[e.jsx(ye,{className:"h-4 w-4"})," Invite to portal"]}))]})]}),q&&B&&e.jsxs("form",{onSubmit:X,className:"mt-4 rounded-xl border border-border bg-card p-5 space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"Full name"}),e.jsx(o,{required:!0,value:r.full_name,onChange:s=>u(a=>({...a,full_name:s.target.value}))})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"Email"}),e.jsx(o,{type:"email",value:r.email,onChange:s=>u(a=>({...a,email:s.target.value}))})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"Phone"}),e.jsx(o,{value:r.phone,onChange:s=>u(a=>({...a,phone:s.target.value}))})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"Address"}),e.jsx(o,{value:r.address_line1,onChange:s=>u(a=>({...a,address_line1:s.target.value}))})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"City"}),e.jsx(o,{value:r.city,onChange:s=>u(a=>({...a,city:s.target.value}))})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"State"}),e.jsx(o,{value:r.state,onChange:s=>u(a=>({...a,state:s.target.value}))})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"ZIP"}),e.jsx(o,{value:r.postal_code,onChange:s=>u(a=>({...a,postal_code:s.target.value}))})]})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"Notes"}),e.jsx(xe,{rows:3,value:r.notes,onChange:s=>u(a=>({...a,notes:s.target.value}))})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsxs(h,{type:"button",variant:"outline",size:"sm",className:"gap-1.5",onClick:()=>C(!1),disabled:k,children:[e.jsx(ie,{className:"h-4 w-4"})," Cancel"]}),e.jsxs(h,{type:"submit",size:"sm",className:"gap-1.5",disabled:k,children:[e.jsx(ve,{className:"h-4 w-4"})," ",k?"Savingâ¦":"Save changes"]})]})]}),b&&L&&e.jsx(he,{open:L,onOpenChange:T,customerId:t.id,customerName:t.full_name,defaultEmail:t.email,companyName:w?.name,brandColor:w?.brand_color,logoUrl:w?.logo_url}),e.jsxs("div",{className:"mt-8 flex items-center justify-between",children:[e.jsx("h2",{className:"font-display text-xl font-bold",children:"Properties"}),N&&e.jsxs(oe,{open:$,onOpenChange:D,children:[e.jsx(ce,{asChild:!0,children:e.jsxs(h,{size:"sm",className:"gap-2",children:[e.jsx(_e,{className:"h-4 w-4"})," Add property"]})}),e.jsxs(de,{children:[e.jsx(me,{children:e.jsx(ue,{children:"New property"})}),e.jsxs("form",{onSubmit:G,className:"space-y-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(n,{children:"Address"}),e.jsx(o,{required:!0,value:i.address,onChange:s=>p(a=>({...a,address:s.target.value}))})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[e.jsxs("div",{children:[e.jsx(n,{children:"City"}),e.jsx(o,{value:i.city,onChange:s=>p(a=>({...a,city:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(n,{children:"State"}),e.jsx(o,{value:i.state,onChange:s=>p(a=>({...a,state:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(n,{children:"ZIP"}),e.jsx(o,{value:i.zip,onChange:s=>p(a=>({...a,zip:s.target.value}))})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(n,{children:"Pool type"}),e.jsxs("select",{className:"w-full h-9 rounded-md border border-input bg-transparent px-2 text-sm",value:i.pool_type,onCh
1ange:s=>p(a=>({...a,pool_type:s.target.value})),children:[e.jsx("option",{value:"in-ground",children:"In-ground"}),e.jsx("option",{value:"above-ground",children:"Above-ground"}),e.jsx("option",{value:"spa",children:"Spa"})]})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Size (gal)"}),e.jsx(o,{type:"number",value:i.pool_size_gallons,onChange:s=>p(a=>({...a,pool_size_gallons:s.target.value}))})]})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Service frequency"}),e.jsxs("select",{className:"w-full h-9 rounded-md border border-input bg-transparent px-2 text-sm",value:i.service_frequency,onChange:s=>p(a=>({...a,service_frequency:s.target.value})),children:[e.jsx("option",{value:"weekly",children:"Weekly"}),e.jsx("option",{value:"biweekly",children:"Biweekly"}),e.jsx("option",{value:"monthly",children:"Monthly"}),e.jsx("option",{value:"on_demand",children:"On demand"})]})]}),e.jsx(pe,{children:e.jsx(h,{type:"submit",children:"Create property"})})]})]})]})]}),e.jsxs("div",{className:"mt-4 grid gap-3",children:[z.length===0&&e.jsx("div",{className:"rounded-xl border border-dashed border-border p-8 text-center text-sm text-muted-foreground",children:"No properties yet"}),z.map(s=>e.jsxs("div",{className:"rounded-xl border border-border bg-card p-5",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3 flex-wrap",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 font-semibold",children:[e.jsx(A,{className:"h-4 w-4 text-primary"}),s.address]}),e.jsxs("div",{className:"text-sm text-muted-foreground mt-1",children:[[s.city,s.state].filter(Boolean).join(", ")," · ",s.pool_type," · ",s.pool_size_gallons?`${s.pool_size_gallons.toLocaleString()} gal`:"â"]}),N&&e.jsxs("div",{className:"mt-2 flex items-center gap-1.5",children:[e.jsx(n,{className:"text-xs text-muted-foreground",children:"Frequency:"}),e.jsxs("select",{value:s.service_frequency??"weekly",onChange:a=>K(s.id,a.target.value),className:"h-7 rounded-md border border-input bg-transparent px-2 text-xs",children:[e.jsx("option",{value:"weekly",children:"Weekly"}),e.jsx("option",{value:"biweekly",children:"Biweekly"}),e.jsx("option",{value:"monthly",children:"Monthly"}),e.jsx("option",{value:"one_time",children:"One time"})]})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[N&&e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(n,{className:"text-xs text-muted-foreground",children:"Tech:"}),e.jsxs("select",{value:s.assigned_technician_id??"",onChange:a=>J(s.id,a.target.value||null),className:"h-8 rounded-md border border-input bg-transparent px-2 text-xs",children:[e.jsx("option",{value:"",children:"Unassigned"}),M.map(a=>e.jsx("option",{value:a.id,children:a.full_name??"Unnamed"},a.id))]})]}),b&&e.jsxs(h,{size:"sm",variant:"outline",className:"gap-1.5",onClick:()=>V(s.share_token),title:"Copy homeowner portal link",children:[e.jsx(we,{className:"h-3.5 w-3.5"})," Portal link ",e.jsx(Ne,{className:"h-3 w-3"})]})]})]}),I[s.id]?.length?e.jsx("div",{className:"mt-3 pt-3 border-t border-border space-y-1.5",children:I[s.id].map(a=>e.jsxs("div",{className:"text-sm flex items-center gap-2",children:[e.jsx(le,{className:"h-3.5 w-3.5 text-muted-foreground"}),a.kind," ",a.brand&&`â ${a.brand}`," ",a.condition&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",a.condition,")"]})]},a.id))}):null]},s.id))]})]}):e.jsx("div",{className:"p-8 text-muted-foreground",children:"Loadingâ¦"})}const Ue=()=>e.jsx(ge,{allowed:["owner","office","technician","bookkeeper","company_marketing","company_success"],children:e.jsx(Ce,{})});export{Ue as component};
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.