1import{j as e,r as n,B as u,I as h,X as ae,ab as L,k as oe,as as ne,av as ie,s as d,t as c}from"./index-CzAkNt2W.js";import{V as le,S as $,s as A,P as de,p as ce}from"./VoiceNoteInput-T1CPDa7T.js";import{L as p}from"./label-BwWVuV93.js";import{D as pe,a as me,b as ue,c as he,d as xe,f as fe}from"./dialog-DFazxlVc.js";import{T as _e}from"./textarea-CMw1Ny2O.js";import{F as ge,P as je,d as be,g as ve}from"./PdfPreviewDialog-CYGxYJM5.js";import{R as ye}from"./RequireRole-DhUlFC_T.js";import{P as B}from"./plus-B3ftHR68.js";import{U as we}from"./upload-C3gfurJP.js";import{S as Ne}from"./search-D5WQvTvr.js";import{C as ke}from"./camera-CjA2pwTw.js";import{P as Ce}from"./pencil-DRW9I4e2.js";import{T as qe}from"./trash-2-Bb14b2qT.js";import"./loader-circle-B0UeYsVT.js";import"./square-DbfvwTJ1.js";import"./jspdf.es.min-DlZwYFgk.js";import"./printer-C3BfX3tt.js";import"./download-C5tX2Uho.js";const Se=["Pump","Filter","Heater","Salt cell","Chlorinator","Automation","Cleaner","Light","Cover","Other"],Pe=["new","good","fair","poor","needs_replacement"],M={kind:"Pump",brand:"",model_number:"",installed_on:"",warranty_until:"",condition:"good",notes:"",photo_url:"",photo_taken_at:"",photo_notes:""};function De(){const[x,H]=n.useState(null),[m,K]=n.useState([]),[E,V]=n.useState([]),[q,W]=n.useState([]),[S,Q]=n.useState(""),[y,X]=n.useState("all"),[G,w]=n.useState(!1),[_,I]=n.useState(null),[a,i]=n.useState({...M,property_id:""});async function P(){const t=await ie()??"00000000-0000-0000-0000-000000000000",r=await d.auth.getUser(),{data:o}=await d.from("profiles").select("company_id").eq("id",r.data.user.id).single(),l=o?.company_id??null;if(H(l),!l)return;const[{data:s},{data:f},{data:b}]=await Promise.all([d.from("properties").select("id, address, city, state, zip, customer_id").eq("company_id",t).order("address"),d.from("customers").select("id, full_name").eq("company_id",t).order("full_name"),d.from("equipment").select("*").eq("company_id",t).order("created_at",{ascending:!1})]);K(s??[]),V(f??[]),W(b??[])}n.useEffect(()=>{P()},[]);const g=n.useMemo(()=>Object.fromEntries(E.map(t=>[t.id,t])),[E]),j=n.useMemo(()=>Object.fromEntries(m.map(t=>[t.id,t])),[m]),T=n.useMemo(()=>{const t=S.trim().toLowerCase();return q.filter(r=>{if(y!=="all"&&r.property_id!==y)return!1;if(!t)return!0;const o=j[r.property_id],l=o?g[o.customer_id]:void 0;return[r.kind,r.brand,r.model_number,r.notes,o?.address,l?.full_name].filter(Boolean).some(s=>s.toLowerCase().includes(t))})},[q,S,y,j,g]),O=n.useMemo(()=>{const t=new Map;return T.forEach(r=>{const o=t.get(r.property_id)??[];o.push(r),t.set(r.property_id,o)}),Array.from(t.entries())},[T]);function R(t){I(null),i({...M,property_id:t??m[0]?.id??""}),w(!0)}function J(t){I(t),i({property_id:t.property_id,kind:t.kind,brand:t.brand??"",model_number:t.model_number??"",installed_on:t.installed_on??"",warranty_until:t.warranty_until??"",condition:t.condition??"good",notes:t.notes??"",photo_url:t.photo_url??"",photo_taken_at:t.photo_taken_at??"",photo_notes:t.photo_notes??""}),w(!0)}const[D,F]=n.useState(!1);async function Y(t){if(x){F(!0);try{const r=t.name.split(".").pop()?.toLowerCase()||"jpg",o=`${x}/equipment/${crypto.randomUUID()}.${r}`,{error:l}=await d.storage.from(de).upload(o,t,{upsert:!1,contentType:t.type});if(l)throw l;i(s=>({...s,photo_url:ce(o),photo_taken_at:new Date().toISOString()})),c.success("Photo uploaded")}catch(r){c.error(r instanceof Error?r.message:"Upload failed")}finally{F(!1)}}}async function Z(t){if(t.preventDefault(),!x||!a.property_id)return c.error("Pick a property");const r={company_id:x,property_id:a.property_id,kind:a.kind,brand:a.brand||null,model_number:a.model_number||null,installed_on:a.installed_on||null,warranty_until:a.warranty_until||null,condition:a.condition,notes:a.notes||null,photo_url:a.photo_url||null,photo_taken_at:a.photo_taken_at||null,photo_notes:a.photo_notes||null},{error:o}=_?await d.from("equipment").update(r).eq("id",_.id):await d.from("equipment").insert(r);if(o)return c.error(o.message);c.success(_?"Equipment updated":"Equipment added"),w(!1),P()}async function ee(t){if(!confirm("Delete this equipment?"))return;const{error:r}=await d.from("equipment").delete().eq("id",t);if(r)return c.error(r.message);c.success("Deleted"),P()}const[N,U]=n.useState(null);async function z(t){const r=j[t];if(!r||!x)throw new Error("Property not found");const o=g[r.customer_id],l=q.filter(v=>v.property_id===t),{data:s}=await d.from("companies").select("name, logo_url, brand_color, report_footer, report_cover_enabled, report_include_photos, report_include_notes").eq("id",x).single(),f={company_name:s?.name??"SplashIQ",logo_url:s?.logo_url??null,brand_color:s?.brand_color??"#0ea5e9",report_footer:s?.report_footer??null,report_cover_enabled:s?.report_cover_enabled??!0,report_include_photos:s?.report_include_photos??!0,report_include_notes:s?.report_include_notes??!0},b=await Promise.all(l.map(async v=>({...v,photo_url:v.photo_url?await A(v.photo_url,600):null}))),C=await ve({address:r.address,city:r.city,state:r.state,zip:r.zip,customer_name:o?.full_name},b,f),se=r.address.replace(/[^a-z0-9]+/gi,"-").toLowerCase();return{blob:C,filename:`equipment-${se}.pdf`}}async function te(t){try{c.loading("Building reportâ¦",{id:"pdf"});const{blob:r,filename:o}=await z(t);be(r,o),c.success("Report ready",{id:"pdf"})}catch(r){c.error(r instanceof Error?r.message:"Failed to build report",{id:"pdf"})}}const k=N?j[N]:null,re=k?`equipment-${k.address.replace(/[^a-z0-9]+/gi,"-").toLowerCase()}.pdf`:"equipment.pdf";return e.jsxs("div",{className:"p-6 md:p-8 max-w-6xl",children:[e.jsxs("div",{className:"flex flex-wrap items-end justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"font-display text-3xl font-bold",children:"Equipment"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"Track pumps, filters, heaters and more â per property."})]}),e.jsxs(pe,{open:G,onOpenChange:w,children:[e.jsx(me,{asChild:!0,children:e.jsxs(u,{className:"gap-2",onClick:()=>R(),disabled:!m.length,children:[e.jsx(B,{className:"h-4 w-4"})," Add equipment"]})}),e.jsxs(ue,{children:[e.jsx(he,{children:e.jsx(xe,{children:_?"Edit equipment":"New equipment"})}),e.jsxs("form",{onSubmit:Z,className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(p,{children:"Property"}),e.jsxs("select",{required:!0,className:"w-full h-9 rounded-md border border-input bg-transparent px-2 text-sm",value:a.property_id,onChange:t=>i(r=>({...r,property_id:t.target.value})),children:[e.jsx("option",{value:"",children:"Select a propertyâ¦"}),m.map(t=>{const r=g[t.customer_id];return e.jsxs("option",{value:t.id,children:[t.address,r?` â ${r.full_name}`:""]},t.id)})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(p,{children:"Type"}),e.jsx("select",{className:"w-full h-9 rounded-md border border-input bg-transparent px-2 text-sm",value:a.kind,onChange:t=>i(r=>({...r,kind:t.target.value})),children:Se.map(t=>e.jsx("option",{value:t,children:t},t))})]}),e.jsxs("div",{children:[e.jsx(p,{children:"Condition"}),e.jsx("select",{className:"w-full h-9 rounded-md border border-input bg-transparent px-2 text-sm",value:a.condition,onChange:t=>i(r=>({...r,condition:t.target.value})),children:Pe.map(t=>e.jsx("option",{value:t,children:t.replace("_"," ")},t))})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(p,{children:"Brand"}),e.jsx(h,{value:a.brand,onChange:t=>i(r=>({...r,brand:t.target.value}))})]}),e.jsxs("div",{children:[e.jsx(p,{children:"Model #"}),e.jsx(h,{value:a.model_number,onChange:t=>i(r=>({...r,model_number:t.target.value}))})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(p,{children:"Installed on"}),e.jsx(h,{type:"date",value:a.installed_on,onChange:t=>i(r=>({...r,installed_on:t.target.value}))})]}),e.jsxs("div",{children:[e.jsx(p,{children:"Warranty until"}
1),e.jsx(h,{type:"date",value:a.warranty_until,onChange:t=>i(r=>({...r,warranty_until:t.target.value}))})]})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx(p,{children:"Notes"}),e.jsx(le,{compact:!0,onTranscript:t=>{i(r=>({...r,notes:(r.notes?r.notes+" ":"")+t}))}})]}),e.jsx(h,{value:a.notes,onChange:t=>i(r=>({...r,notes:t.target.value})),placeholder:"Serial #, install quirks, etc."})]}),e.jsxs("div",{children:[e.jsx(p,{children:"Photo"}),a.photo_url?e.jsxs("div",{className:"mt-1 space-y-2",children:[e.jsxs("div",{className:"flex gap-3",children:[e.jsxs("div",{className:"relative w-32 h-32 rounded-lg overflow-hidden border border-border shrink-0",children:[e.jsx($,{photoRef:a.photo_url,alt:"Equipment",className:"w-full h-full object-cover"}),e.jsx("button",{type:"button",onClick:()=>i(t=>({...t,photo_url:"",photo_taken_at:"",photo_notes:""})),className:"absolute top-1 right-1 h-6 w-6 rounded-full bg-background/90 grid place-items-center hover:bg-background","aria-label":"Remove photo",children:e.jsx(ae,{className:"h-3.5 w-3.5"})})]}),e.jsxs("div",{className:"flex-1 min-w-0 space-y-1",children:[e.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-muted-foreground",children:[e.jsx(L,{className:"h-3 w-3"}),e.jsxs("span",{children:["Taken ",a.photo_taken_at?new Date(a.photo_taken_at).toLocaleString():"â"]})]}),e.jsx(h,{type:"datetime-local",className:"h-8 text-xs",value:a.photo_taken_at?new Date(a.photo_taken_at).toISOString().slice(0,16):"",onChange:t=>i(r=>({...r,photo_taken_at:t.target.value?new Date(t.target.value).toISOString():""}))})]})]}),e.jsx(_e,{rows:2,placeholder:"Technician notes about this photo (rust, leak, model sticker, etc.)",value:a.photo_notes,onChange:t=>i(r=>({...r,photo_notes:t.target.value}))})]}):e.jsxs("label",{className:"mt-1 flex items-center justify-center gap-2 h-24 rounded-lg border border-dashed border-border cursor-pointer hover:bg-muted/40 transition-colors text-sm text-muted-foreground",children:[D?e.jsx(e.Fragment,{children:"Uploadingâ¦"}):e.jsxs(e.Fragment,{children:[e.jsx(we,{className:"h-4 w-4"})," Upload photo (camera or file)"]}),e.jsx("input",{type:"file",accept:"image/*",capture:"environment",className:"hidden",disabled:D,onChange:t=>{const r=t.target.files?.[0];r&&Y(r),t.target.value=""}})]})]}),e.jsx(fe,{children:e.jsx(u,{type:"submit",disabled:D,children:_?"Save changes":"Add equipment"})})]})]})]})]}),e.jsxs("div",{className:"mt-6 flex flex-wrap gap-2",children:[e.jsxs("div",{className:"relative flex-1 min-w-[220px]",children:[e.jsx(Ne,{className:"h-4 w-4 absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground"}),e.jsx(h,{value:S,onChange:t=>Q(t.target.value),placeholder:"Search type, brand, model, customerâ¦",className:"pl-9"})]}),e.jsxs("select",{className:"h-9 rounded-md border border-input bg-transparent px-2 text-sm",value:y,onChange:t=>X(t.target.value),children:[e.jsx("option",{value:"all",children:"All properties"}),m.map(t=>e.jsx("option",{value:t.id,children:t.address},t.id))]})]}),m.length?O.length===0?e.jsxs("div",{className:"mt-8 rounded-xl border border-dashed border-border p-10 text-center text-sm text-muted-foreground",children:["No equipment yet. Click ",e.jsx("span",{className:"font-medium text-foreground",children:"Add equipment"})," to get started."]}):e.jsx("div",{className:"mt-6 space-y-5",children:O.map(([t,r])=>{const o=j[t],l=o?g[o.customer_id]:void 0;return e.jsxs("div",{className:"rounded-xl border border-border bg-card overflow-hidden",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3 px-5 py-3 border-b border-border bg-muted/30",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 font-semibold truncate",children:[e.jsx(oe,{className:"h-4 w-4 text-primary shrink-0"}),o?.address??"Unknown property"]}),e.jsxs("div",{className:"text-xs text-muted-foreground truncate",children:[l?.full_name,o?.city?` · ${o.city}${o.state?`, ${o.state}`:""}`:""]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(u,{size:"sm",variant:"outline",className:"gap-1.5",onClick:()=>U(t),children:[e.jsx(ne,{className:"h-3.5 w-3.5"})," Preview"]}),e.jsxs(u,{size:"sm",variant:"outline",className:"gap-1.5",onClick:()=>te(t),children:[e.jsx(ge,{className:"h-3.5 w-3.5"})," Export PDF"]}),e.jsxs(u,{size:"sm",variant:"outline",className:"gap-1.5",onClick:()=>R(t),children:[e.jsx(B,{className:"h-3.5 w-3.5"})," Add"]})]})]}),e.jsx("div",{className:"divide-y divide-border",children:r.map(s=>{const f=s.warranty_until&&new Date(s.warranty_until)<new Date;return e.jsxs("div",{className:"flex items-center gap-3 px-5 py-3",children:[s.photo_url?e.jsx("a",{href:s.photo_url,target:"_blank",rel:"noreferrer",onClick:async b=>{b.preventDefault();const C=await A(s.photo_url);C&&window.open(C,"_blank","noopener")},className:"shrink-0",children:e.jsx($,{photoRef:s.photo_url,alt:s.kind,className:"h-12 w-12 rounded-lg object-cover border border-border"})}):e.jsx("div",{className:"h-12 w-12 rounded-lg bg-secondary grid place-items-center shrink-0",children:e.jsx(ke,{className:"h-4 w-4 text-muted-foreground"})}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"font-medium truncate",children:[s.kind,s.brand&&e.jsxs("span",{className:"text-muted-foreground font-normal",children:[" · ",s.brand]}),s.model_number&&e.jsxs("span",{className:"text-muted-foreground font-normal",children:[" · ",s.model_number]})]}),e.jsxs("div",{className:"text-xs text-muted-foreground flex flex-wrap gap-x-3",children:[s.condition&&e.jsxs("span",{children:["Condition: ",s.condition.replace("_"," ")]}),s.installed_on&&e.jsxs("span",{children:["Installed ",s.installed_on]}),s.warranty_until&&e.jsxs("span",{className:f?"text-destructive":"",children:["Warranty ",f?"expired":"until"," ",s.warranty_until]}),s.photo_taken_at&&e.jsxs("span",{className:"inline-flex items-center gap-1",children:[e.jsx(L,{className:"h-3 w-3"})," Photo ",new Date(s.photo_taken_at).toLocaleDateString()]})]}),s.photo_notes&&e.jsxs("div",{className:"mt-1 text-xs text-foreground
1/80 line-clamp-2 italic",children:['"',s.photo_notes,'"']})]}),e.jsxs("div",{className:"flex gap-1",children:[e.jsx(u,{size:"icon",variant:"ghost",onClick:()=>J(s),"aria-label":"Edit",children:e.jsx(Ce,{className:"h-4 w-4"})}),e.jsx(u,{size:"icon",variant:"ghost",onClick:()=>ee(s.id),"aria-label":"Delete",children:e.jsx(qe,{className:"h-4 w-4"})})]})]},s.id)})})]},t)})}):e.jsx("div",{className:"mt-8 rounded-xl border border-dashed border-border p-10 text-center text-sm text-muted-foreground",children:"Add a customer and property first to start tracking equipment."}),e.jsx(je,{open:!!N,onOpenChange:t=>{t||U(null)},title:k?`Equipment report â ${k.address}`:"Equipment report",filename:re,build:async()=>{const{blob:t}=await z(N);return t}})]})}const Xe=()=>e.jsx(ye,{allowed:["owner","office","technician"],children:e.jsx(De,{})});export{Xe 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.