5 lines
35 KiB
JavaScript
5 lines
35 KiB
JavaScript
import{Y as $e,n as _e,Z as Ee,$ as ze,r as c,p as fe,t as ee,z as v,j as t,a0 as j,M as Be,T as ve,a1 as oe,l as ke,a2 as Ue,R as Ye,P as Je,a3 as Ve,k as Ze}from"./index-9FS9bw8D.js";function Ke(N,y,o){y.center!==o.center&&N.setLatLng(y.center),y.radius!=null&&y.radius!==o.radius&&N.setRadius(y.radius)}const we=$e(function({center:y,children:o,...O},M){const L=new _e.Circle(y,O);return Ee(L,ze(M,{overlayContainer:L}))},Ke),Qe=({onPlace:N})=>(Ze({click(y){N([y.latlng.lat,y.latlng.lng])}}),null),Xe=N=>{if(N.type==="rotor_impact"||N.type==="spray_fixed")return{kind:"circle",radius:N.throwFeet,degrees:N.degrees||360};if(N.type==="oscillating_fan"){const y=N.lengthFeet||0,o=N.widthFeet||0;return{kind:"rect",length:y,width:o}}return null},at=()=>{const[N,y]=c.useState([]),[o,O]=c.useState(null),[M,L]=c.useState([]),[z,T]=c.useState(!1),[i,k]=c.useState({mount:"above_ground",type:"rotor_impact",gpm:2.5,throwFeet:20,degrees:360,lengthFeet:30,widthFeet:20,durationMinutes:60}),[h,C]=c.useState(null),[B,I]=c.useState([]),[D,f]=c.useState([]),[u,w]=c.useState(!1),[g,p]=c.useState(0),[G,te]=c.useState(null),[ce,ue]=c.useState(null),[U,me]=c.useState([]),[J,he]=c.useState(""),[A,ge]=c.useState(null),q=c.useMemo(()=>D.find(e=>e.id===A),[D,A]),_=c.useMemo(()=>[...D].sort((e,s)=>Number(e.sequence||0)-Number(s.sequence||0)||Number(e.id)-Number(s.id)),[D]),[m,H]=c.useState(null),[pe,Fe]=c.useState(!1),[se,xe]=c.useState(""),[Me,ae]=c.useState(!1),Ne=["#ef4444","#f59e0b","#10b981","#3b82f6","#8b5cf6","#ec4899","#14b8a6","#84cc16","#f97316","#06b6d4","#a855f7","#22c55e"],V=e=>{const s=D.findIndex(a=>a.id===e.id);return Ne[(s>=0?s:0)%Ne.length]},ne=c.useMemo(()=>({}),[]),be=e=>{if(ne[e])return ne[e];const s=`
|
||
<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
|
||
<circle cx="8" cy="8" r="6.5" fill="${e}" stroke="white" stroke-width="2"/>
|
||
</svg>`,a=new _e.Icon({iconUrl:"data:image/svg+xml;base64,"+btoa(s),iconSize:[16,16],iconAnchor:[8,8]});return ne[e]=a,a},je=e=>{ge(e.id),H({durationMinutes:e.duration_minutes||0,mountType:e.sprinkler_mount,sprinklerHeadType:e.sprinkler_head_type,gpm:Number(e.sprinkler_gpm||0),throwFeet:Number(e.sprinkler_throw_feet||0),degrees:Number(e.sprinkler_degrees||360),lengthFeet:Number(e.sprinkler_length_feet||0),widthFeet:Number(e.sprinkler_width_feet||0),headingDegrees:Number(e.sprinkler_heading_degrees||0)})},Ce=(e,s)=>{f(a=>a.map(n=>n.id===e?{...n,sprinkler_degrees:s.degrees!==void 0?s.degrees:n.sprinkler_degrees,sprinkler_heading_degrees:s.headingDegrees!==void 0?s.headingDegrees:n.sprinkler_heading_degrees,sprinkler_throw_feet:s.throwFeet!==void 0?s.throwFeet:n.sprinkler_throw_feet,sprinkler_length_feet:s.lengthFeet!==void 0?s.lengthFeet:n.sprinkler_length_feet,sprinkler_width_feet:s.widthFeet!==void 0?s.widthFeet:n.sprinkler_width_feet,duration_minutes:s.durationMinutes!==void 0?s.durationMinutes:n.duration_minutes,lat:s.lat!==void 0?s.lat:n.lat,lng:s.lng!==void 0?s.lng:n.lng}:n))},b=async(e,s)=>{var a,n;try{Ce(e,s),e===A&&H(l=>l&&{...l,degrees:s.degrees!==void 0?s.degrees:l.degrees,headingDegrees:s.headingDegrees!==void 0?s.headingDegrees:l.headingDegrees,throwFeet:s.throwFeet!==void 0?s.throwFeet:l.throwFeet,lengthFeet:s.lengthFeet!==void 0?s.lengthFeet:l.lengthFeet,widthFeet:s.widthFeet!==void 0?s.widthFeet:l.widthFeet,durationMinutes:s.durationMinutes!==void 0?s.durationMinutes:l.durationMinutes});const d=(n=(a=(await j.updatePoint(e,s)).data)==null?void 0:a.data)==null?void 0:n.point;f(l=>l.map(x=>x.id===e?d:x))}catch{v.error("Failed to update point")}},Z=(e,s,a)=>{const n=Number(e.lat),r=Number(e.lng),d=111320,l=Math.cos(n*Math.PI/180)*111320,x=s*.3048,F=a*.3048,S=n+F/d,P=r+x/l;b(e.id,{lat:S,lng:P})},K=(e,s)=>{let n=(Number(e.sprinkler_heading_degrees||0)+s)%360;n<0&&(n+=360),b(e.id,{headingDegrees:n})};c.useEffect(()=>{(async()=>{var e,s,a,n;try{const[r,d]=await Promise.all([fe.getAll(),ee.getAll()]);y(((s=(e=r.data)==null?void 0:e.data)==null?void 0:s.properties)||[]);const l=(((n=(a=d.data)==null?void 0:a.data)==null?void 0:n.equipment)||[]).filter(x=>(x.categoryName||"").toLowerCase()==="sprinkler");me(l)}catch{v.error("Failed to load properties")}})()},[]);const Se=async e=>{var s,a,n,r,d,l;try{const F=(a=(s=(await fe.getById(e)).data)==null?void 0:s.data)==null?void 0:a.property;O(F);const S=(F.sections||[]).map(W=>({...W,polygonData:typeof W.polygonData=="string"?JSON.parse(W.polygonData):W.polygonData}));L(S);const R=((r=(n=(await j.getPlans({property_id:e})).data)==null?void 0:n.data)==null?void 0:r.plans)||[];if(I(R),R.length){const W=R[0];C(W);const $=await j.getPlanPoints(W.id);f(((l=(d=$.data)==null?void 0:d.data)==null?void 0:l.points)||[])}else C(null),f([])}catch{v.error("Failed to load property")}},Pe=async()=>{var e,s,a,n;if(h)return h;if(!o)return v.error("Select a property first"),null;try{const d=(s=(e=(await j.createPlan({propertyId:o.id,name:"Sprinklers"})).data)==null?void 0:e.data)==null?void 0:s.plan;C(d);const l=await j.getPlans({property_id:o.id});return I(((n=(a=l.data)==null?void 0:a.data)==null?void 0:n.plans)||[]),f([]),d}catch{return v.error("Failed to create plan"),null}},De=async e=>{var a,n;const s=(B||[]).find(r=>r.id===parseInt(e));if(C(s||null),s)try{const r=await j.getPlanPoints(s.id);f(((n=(a=r.data)==null?void 0:a.data)==null?void 0:n.points)||[])}catch{v.error("Failed to load plan points"),f([])}else f([])},Ie=async e=>{const s=await Pe();if(s)try{const a=U.find(l=>l.id===(J?parseInt(J):-1)),n=a?{mount:a.sprinklerMount||i.mount,type:a.sprinklerHeadType||i.type,gpm:a.sprinklerGpm||i.gpm,throwFeet:a.sprinklerThrowFeet||i.throwFeet,degrees:a.sprinklerDegrees!==void 0&&a.sprinklerDegrees!==null?a.sprinklerDegrees:i.degrees,lengthFeet:a.sprinklerLengthFeet||i.lengthFeet,widthFeet:a.sprinklerWidthFeet||i.widthFeet}:i,r={lat:e[0],lng:e[1],durationMinutes:i.durationMinutes,mountType:n.mount,sprinklerHeadType:n.type,gpm:n.gpm,throwFeet:n.throwFeet,degrees:n.degrees,lengthFeet:n.lengthFeet,widthFeet:n.widthFeet,headingDegrees:i.headingDegrees||0,equipmentId:a?a.id:null,equipmentName:a?a.customName||`${a.manufacturer||""} ${a.model||""}`.trim():null},d=await j.addPlanPoint(s.id,r);f(l=>{var x,F;return[...l,(F=(x=d.data)==null?void 0:x.data)==null?void 0:F.point]}),v.success("Sprinkler location added"),T(!1)}catch{v.error("Failed to add sprinkler point")}},ye=c.useMemo(()=>{if(o!=null&&o.latitude&&(o!=null&&o.longitude))return[Number(o.latitude),Number(o.longitude)];if(M!=null&&M.length){const e=M.flatMap(s=>{var a,n;return((n=(a=s.polygonData)==null?void 0:a.coordinates)==null?void 0:n[0])||[]});if(e.length){const s=e.reduce((n,r)=>n+Number(r[0]),0)/e.length,a=e.reduce((n,r)=>n+Number(r[1]),0)/e.length;return[s,a]}}return[39.8,-98.6]},[o,M]),qe=({center:e})=>{const s=Ve();return c.useEffect(()=>{e&&Array.isArray(e)&&s.setView(e)},[e,s]),null};c.useEffect(()=>{xe((h==null?void 0:h.name)||"")},[h]);const Le=()=>{if(D.length===0){v.error("Add at least one point");return}if(!navigator.geolocation){v.error("GPS not available");return}w(!0),p(0);const e=navigator.geolocation.watchPosition(s=>{te({lat:s.coords.latitude,lng:s.coords.longitude})},s=>{console.warn(s),v.error("GPS error")},{enableHighAccuracy:!0,maximumAge:1e3,timeout:1e4});ue(e)},Re=()=>{ce&&(navigator.geolocation.clearWatch(ce),ue(null)),w(!1)},Te=(e,s)=>{const n=x=>x*Math.PI/180,r=n(s.lat-e.lat),d=n(s.lng-e.lng),l=Math.sin(r/2)**2+Math.cos(n(e.lat))*Math.cos(n(s.lat))*Math.sin(d/2)**2;return 2*6371e3*Math.atan2(Math.sqrt(l),Math.sqrt(1-l))*3.28084},Ge=(e,s)=>{const a=S=>S*Math.PI/180,n=S=>(S*180/Math.PI+360)%360,r=a(e.lat),d=a(s.lat),l=a(s.lng-e.lng),x=Math.sin(l)*Math.cos(d),F=Math.cos(r)*Math.sin(d)-Math.sin(r)*Math.cos(d)*Math.cos(l);return Math.round(n(Math.atan2(x,F)))},Ae=(e,s,a,n,r=60)=>{const[d,l]=[Number(e.lat),Number(e.lng)],x=111320,F=Math.cos(d*Math.PI/180)*111320,S=s*.3048,P=[],R=a*Math.PI/180,$=n*Math.PI/180-R,Y=Math.max(8,Math.round(r*Math.abs($)/(2*Math.PI)));P.push([d,l]);for(let E=0;E<=Y;E++){const X=R+$*E/Y,le=S*Math.cos(X),ie=S*Math.sin(X),de=d+ie/x,Oe=l+le/F;P.push([de,Oe])}return P.push([d,l]),P},re=c.useMemo(()=>{const e=i.type;if(e==="rotor_impact"||e==="spray_fixed"){const s=Number(i.throwFeet||0),a=Number(i.degrees||360);return Math.PI*s*s*(a/360)}return e==="oscillating_fan"?Number(i.lengthFeet||0)*Number(i.widthFeet||0):0},[i]),[Q,He]=c.useState(.5),We=c.useMemo(()=>{const e=Number(i.gpm||0);if(!e||!re||!Q)return 0;const s=re*Q*.623;return Math.ceil(s/e)},[i.gpm,re,Q]);return t.jsxs("div",{className:"p-6",children:[t.jsx("h1",{className:"text-2xl font-bold mb-4",children:"Watering - Sprinklers"}),t.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-4 gap-4",children:[t.jsxs("div",{className:"lg:col-span-1 space-y-4",children:[t.jsxs("div",{className:"card",children:[t.jsx("div",{className:"font-medium mb-2",children:"Plan Points"}),t.jsxs("ul",{className:"text-sm space-y-1 max-h-64 overflow-auto",children:[_.map((e,s)=>t.jsxs("li",{className:(e.id===A?"bg-blue-50 ":"")+"rounded px-2 py-1 hover:bg-gray-50 cursor-pointer flex items-center gap-2 justify-between",onClick:()=>je(e),children:[t.jsxs("span",{className:"flex items-center gap-2",children:[t.jsx("span",{style:{backgroundColor:V(e)},className:"inline-block w-3 h-3 rounded-full"}),t.jsxs("span",{className:"font-mono text-xs",children:["#",s+1]}),t.jsxs("span",{className:"text-xs",children:[Number(e.lat).toFixed(5),", ",Number(e.lng).toFixed(5)]})]}),t.jsxs("span",{className:"text-[11px] text-gray-600",children:[e.equipment_name||(e.sprinkler_head_type||"-").replace("_"," "),e.sprinkler_throw_feet?` • ${Number(e.sprinkler_throw_feet)}ft`:"",e.sprinkler_degrees?` • ${e.sprinkler_degrees}°`:""]})]},e.id)),D.length===0&&t.jsx("li",{className:"text-gray-500",children:"No points yet"})]}),q&&m&&t.jsxs("div",{className:"mt-3 border-t pt-3 space-y-2",children:[t.jsx("div",{className:"font-medium",children:"Selected Details"}),t.jsxs("div",{className:"text-xs text-gray-700",children:[q!=null&&q.equipment_name?t.jsxs(t.Fragment,{children:[t.jsx("span",{className:"font-semibold",children:q.equipment_name})," •"]}):null,"Type: ",m.sprinklerHeadType||"-"," • GPM: ",m.gpm||"-"," • Duration: ",m.durationMinutes||0,"m"]}),m.sprinklerHeadType==="rotor_impact"||m.sprinklerHeadType==="spray_fixed"?t.jsxs("div",{className:"text-xs text-gray-700",children:["Throw: ",m.throwFeet||0," ft • Degrees: ",m.degrees||0,"° • Heading: ",m.headingDegrees||0,"°"]}):t.jsxs("div",{className:"text-xs text-gray-700",children:["Length: ",m.lengthFeet||0," ft • Width: ",m.widthFeet||0," ft • Heading: ",m.headingDegrees||0,"°"]})]})]}),t.jsxs("div",{className:"card",children:[t.jsx("label",{className:"block text-sm font-medium mb-1",children:"Property"}),t.jsxs("select",{className:"input",value:(o==null?void 0:o.id)||"",onChange:e=>Se(parseInt(e.target.value)),children:[t.jsx("option",{value:"",children:"Select property"}),N.map(e=>t.jsx("option",{value:e.id,children:e.name},e.id))]})]}),t.jsxs("div",{className:"card space-y-2",children:[t.jsx("div",{className:"font-medium",children:"Plan"}),t.jsxs("select",{className:"input w-full",value:(h==null?void 0:h.id)||"",onChange:e=>De(e.target.value),disabled:!o,children:[t.jsx("option",{value:"",children:"-- Select plan --"}),B.map(e=>t.jsx("option",{value:e.id,children:e.name},e.id))]}),t.jsxs("div",{className:"flex gap-2 items-center flex-wrap",children:[t.jsx("button",{className:"btn-secondary",disabled:!o,onClick:async()=>{var r,d,l,x;const e=`Sprinklers ${new Date().toLocaleDateString()}`,a=(d=(r=(await j.createPlan({propertyId:o.id,name:e})).data)==null?void 0:r.data)==null?void 0:d.plan;C(a);const n=await j.getPlans({property_id:o.id});I(((x=(l=n.data)==null?void 0:l.data)==null?void 0:x.plans)||[]),f([])},children:"New"}),h&&t.jsx("button",{className:"btn-secondary",onClick:async()=>{var d,l,x,F,S,P;const e=`${h.name} (Copy ${new Date().toLocaleDateString()})`,a=(l=(d=(await j.duplicatePlan(h.id,{name:e})).data)==null?void 0:d.data)==null?void 0:l.plan;C(a);const n=await j.getPlans({property_id:o.id});I(((F=(x=n.data)==null?void 0:x.data)==null?void 0:F.plans)||[]);const r=await j.getPlanPoints(a.id);f(((P=(S=r.data)==null?void 0:S.data)==null?void 0:P.points)||[])},children:"Duplicate"}),h&&t.jsx("button",{className:"btn-primary",disabled:!D.length,onClick:()=>ae(!0),children:"Log Watering Run"}),h&&t.jsx("button",{className:"btn-secondary text-red-600 border-red-300",onClick:async()=>{var a,n,r,d;if(!window.confirm("Delete this plan and all points?"))return;await j.deletePlan(h.id);const s=((n=(a=(await j.getPlans({property_id:o.id})).data)==null?void 0:a.data)==null?void 0:n.plans)||[];if(I(s),C(s[0]||null),s[0]){const l=await j.getPlanPoints(s[0].id);f(((d=(r=l.data)==null?void 0:r.data)==null?void 0:d.points)||[])}else f([])},children:"Delete"})]}),h&&t.jsxs("div",{className:"flex gap-2 mt-2",children:[t.jsx("input",{className:"input flex-1",placeholder:"Rename plan",value:se,onChange:e=>xe(e.target.value)}),t.jsx("button",{className:"btn-primary",onClick:async()=>{var n,r,d,l;if(!se.trim())return;const s=(r=(n=(await j.updatePlan(h.id,{name:se.trim()})).data)==null?void 0:n.data)==null?void 0:r.plan;C(s);const a=await j.getPlans({property_id:o.id});I(((l=(d=a.data)==null?void 0:d.data)==null?void 0:l.plans)||[])},children:"Save"})]})]}),t.jsxs("div",{className:"card space-y-2",children:[t.jsx("div",{className:"font-medium",children:"Sprinkler Settings"}),t.jsx("div",{className:"text-sm",children:"Choose Saved Sprinkler"}),t.jsxs("select",{className:"input",value:J,onChange:e=>{const s=e.target.value;he(s);const a=U.find(n=>n.id===parseInt(s));a&&k(n=>({...n,mount:a.sprinklerMount||n.mount,type:a.sprinklerHeadType||n.type,gpm:a.sprinklerGpm||n.gpm,throwFeet:a.sprinklerThrowFeet||n.throwFeet,degrees:a.sprinklerDegrees!==void 0&&a.sprinklerDegrees!==null?a.sprinklerDegrees:n.degrees,lengthFeet:a.sprinklerLengthFeet||n.lengthFeet,widthFeet:a.sprinklerWidthFeet||n.widthFeet}))},children:[t.jsx("option",{value:"",children:"-- none --"}),U.map(e=>t.jsx("option",{value:e.id,children:e.customName||`${e.manufacturer||""} ${e.model||""}`.trim()},e.id))]}),!J&&t.jsx(et,{current:i,onSaved:async e=>{var n,r;const a=(((r=(n=(await ee.getAll()).data)==null?void 0:n.data)==null?void 0:r.equipment)||[]).filter(d=>(d.categoryName||"").toLowerCase()==="sprinkler");me(a),he(String(e.id))}}),t.jsx("div",{className:"text-sm",children:"Mount"}),t.jsxs("select",{className:"input",value:i.mount,onChange:e=>k({...i,mount:e.target.value}),children:[t.jsx("option",{value:"in_ground",children:"In‑Ground"}),t.jsx("option",{value:"above_ground",children:"Above‑Ground"})]}),t.jsx("div",{className:"text-sm",children:"Type"}),t.jsxs("select",{className:"input",value:i.type,onChange:e=>k({...i,type:e.target.value}),children:[t.jsx("option",{value:"rotor_impact",children:"Rotor/Impact"}),t.jsx("option",{value:"oscillating_fan",children:"Oscillating/Fan"}),t.jsx("option",{value:"spray_fixed",children:"Spray (Fixed)"})]}),i.type==="rotor_impact"||i.type==="spray_fixed"?t.jsxs(t.Fragment,{children:[t.jsx("div",{className:"text-sm",children:"GPM"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:i.gpm,onChange:e=>k({...i,gpm:parseFloat(e.target.value)})}),t.jsx("div",{className:"text-sm",children:"Throw distance (ft)"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:i.throwFeet,onChange:e=>k({...i,throwFeet:parseFloat(e.target.value)})}),t.jsx("div",{className:"text-sm",children:"Degrees (0‑360)"}),t.jsx("input",{type:"number",className:"input",value:i.degrees,onChange:e=>k({...i,degrees:parseInt(e.target.value||"0",10)})}),t.jsx("div",{className:"text-sm",children:"Heading (0‑359, 0 = East)"}),t.jsx("input",{type:"number",className:"input",value:i.headingDegrees||0,onChange:e=>k({...i,headingDegrees:(parseInt(e.target.value||"0",10)||0)%360})})]}):t.jsxs(t.Fragment,{children:[t.jsx("div",{className:"text-sm",children:"Length (ft)"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:i.lengthFeet,onChange:e=>k({...i,lengthFeet:parseFloat(e.target.value)})}),t.jsx("div",{className:"text-sm",children:"Width (ft)"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:i.widthFeet,onChange:e=>k({...i,widthFeet:parseFloat(e.target.value)})})]}),t.jsx("div",{className:"text-sm",children:"Target Depth (inches)"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:Q,onChange:e=>He(parseFloat(e.target.value||"0"))}),t.jsxs("div",{className:"text-xs text-gray-600",children:["Suggested runtime: ",We," minutes ",i.gpm?"":"(enter GPM to calculate)"]}),t.jsx("div",{className:"text-sm",children:"Run Duration (minutes)"}),t.jsx("input",{type:"number",className:"input",value:i.durationMinutes,onChange:e=>k({...i,durationMinutes:parseInt(e.target.value||"0",10)})}),t.jsx("button",{className:"btn-primary w-full",disabled:!o,onClick:()=>T(!0),children:"Place Sprinkler on Map"})]}),t.jsxs("div",{className:"card",children:[q&&m&&t.jsxs("div",{className:"space-y-2",children:[t.jsx("div",{className:"font-medium",children:"Edit Selected"}),t.jsxs("div",{children:[t.jsx("label",{className:"text-xs",children:"Saved Sprinkler"}),t.jsxs("div",{className:"flex gap-2 items-center",children:[t.jsxs("select",{className:"input flex-1",value:(q==null?void 0:q.equipment_id)||"",onChange:async e=>{const s=e.target.value;if(!s){await b(A,{equipmentId:null,equipmentName:null});return}const a=U.find(n=>n.id===parseInt(s));a&&await b(A,{equipmentId:a.id,equipmentName:a.customName||`${a.manufacturer||""} ${a.model||""}`.trim()})},children:[t.jsx("option",{value:"",children:"-- none --"}),U.map(e=>t.jsx("option",{value:e.id,children:e.customName||`${e.manufacturer||""} ${e.model||""}`.trim()},e.id))]}),(q==null?void 0:q.equipment_id)&&t.jsx("button",{className:"btn-secondary",onClick:async()=>{await b(A,{equipmentId:null,equipmentName:null})},children:"Unlink"})]})]}),m.sprinklerHeadType==="rotor_impact"||m.sprinklerHeadType==="spray_fixed"?t.jsxs(t.Fragment,{children:[t.jsx("label",{className:"text-xs",children:"Degrees"}),t.jsx("input",{type:"number",className:"input",value:m.degrees,onChange:e=>H({...m,degrees:parseInt(e.target.value||"0",10)})}),t.jsx("label",{className:"text-xs",children:"Heading"}),t.jsx("input",{type:"number",className:"input",value:m.headingDegrees,onChange:e=>H({...m,headingDegrees:parseInt(e.target.value||"0",10)%360})}),t.jsx("label",{className:"text-xs",children:"Throw (ft)"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:m.throwFeet,onChange:e=>H({...m,throwFeet:parseFloat(e.target.value||"0")})})]}):t.jsxs(t.Fragment,{children:[t.jsx("label",{className:"text-xs",children:"Length (ft)"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:m.lengthFeet,onChange:e=>H({...m,lengthFeet:parseFloat(e.target.value||"0")})}),t.jsx("label",{className:"text-xs",children:"Width (ft)"}),t.jsx("input",{type:"number",step:"0.1",className:"input",value:m.widthFeet,onChange:e=>H({...m,widthFeet:parseFloat(e.target.value||"0")})}),t.jsx("label",{className:"text-xs",children:"Heading"}),t.jsx("input",{type:"number",className:"input",value:m.headingDegrees,onChange:e=>H({...m,headingDegrees:parseInt(e.target.value||"0",10)%360})})]}),t.jsx("label",{className:"text-xs",children:"Duration (min)"}),t.jsx("input",{type:"number",className:"input",value:m.durationMinutes,onChange:e=>H({...m,durationMinutes:parseInt(e.target.value||"0",10)})}),t.jsxs("div",{className:"flex gap-2",children:[t.jsx("button",{className:"btn-primary flex-1",onClick:async()=>{await b(A,m)},children:"Save"}),t.jsx("button",{className:"btn-secondary",onClick:async()=>{var e,s;if(await j.deletePoint(A),h){const a=await j.getPlanPoints(h.id);f(((s=(e=a.data)==null?void 0:e.data)==null?void 0:s.points)||[])}else f(a=>a.filter(n=>n.id!==A));ge(null),H(null)},children:"Delete"})]})]}),!q&&t.jsx("div",{className:"text-sm text-gray-500",children:"Select a point to edit"})]})]}),t.jsx("div",{className:"lg:col-span-3",children:t.jsxs("div",{className:"card p-0",style:{height:"70vh",position:"relative"},children:[t.jsxs("div",{className:"flex items-center justify-between p-3 border-b",children:[t.jsx("div",{className:"text-sm text-gray-700",children:u&&G&&_[g]?(()=>{const e={lat:Number(_[g].lat),lng:Number(_[g].lng)},s=Te(G,e),a=Ge(G,e);return s<=15?t.jsxs("span",{className:"text-green-700 font-semibold",children:["Arrived at #",g+1," • ",s.toFixed(0)," ft"]}):t.jsxs(t.Fragment,{children:["→ ",a,"° • ",s.toFixed(0)," ft • Point #",g+1]})})():"Map"}),t.jsxs("div",{className:"flex gap-2 items-center",children:[t.jsxs("label",{className:"text-xs flex items-center gap-1",children:[t.jsx("input",{type:"checkbox",checked:pe,onChange:e=>Fe(e.target.checked)})," Satellite"]}),u?t.jsxs(t.Fragment,{children:[t.jsx("button",{className:"btn-secondary",onClick:()=>p(e=>Math.max(0,e-1)),disabled:g===0,children:"Prev"}),t.jsx("button",{className:"btn-secondary",onClick:()=>p(e=>Math.min(_.length-1,e+1)),disabled:g>=_.length-1,children:"Next"}),t.jsx("button",{className:"btn-primary",onClick:Re,children:"Stop"})]}):t.jsx("button",{className:"btn-secondary",onClick:Le,disabled:_.length===0,children:"Start Guidance"})]})]}),t.jsxs(Be,{center:ye,zoom:18,style:{height:"100%",width:"100%"},children:[t.jsx(qe,{center:ye}),pe?t.jsx(ve,{url:"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"}):t.jsx(ve,{url:"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"}),M.map(e=>{var s,a;return t.jsx(oe,{positions:((a=(s=e.polygonData)==null?void 0:s.coordinates)==null?void 0:a[0])||[],pathOptions:{color:"#16a34a",weight:2,fillOpacity:.1}},e.id)}),z&&t.jsx(Qe,{onPlace:Ie}),u&&G&&_[g]&&t.jsxs(t.Fragment,{children:[t.jsx(ke,{position:[G.lat,G.lng],icon:be("#0ea5e9")}),t.jsx(Ue,{positions:[[G.lat,G.lng],[Number(_[g].lat),Number(_[g].lng)]],pathOptions:{color:"#0ea5e9",dashArray:"6 6"}}),t.jsx(we,{center:[Number(_[g].lat),Number(_[g].lng)],radius:15*.3048,pathOptions:{color:"#22c55e",fillOpacity:.15,weight:2}})]}),_.length>0&&t.jsx("div",{style:{position:"absolute",right:10,bottom:10,zIndex:1e3},children:t.jsxs("div",{className:"bg-white/90 border rounded shadow p-2 max-h-56 overflow-auto text-xs",children:[t.jsx("div",{className:"font-medium mb-1",children:"Legend"}),t.jsx("ul",{className:"space-y-1",children:_.map((e,s)=>t.jsxs("li",{className:"flex items-center gap-2",children:[t.jsx("span",{style:{backgroundColor:V(e)},className:"inline-block w-3 h-3 rounded-full"}),t.jsxs("span",{className:"font-mono",children:["#",s+1]}),t.jsx("span",{className:"text-gray-700",children:e.equipment_name||(e.sprinkler_head_type||"-").replace("_"," ")}),e.sprinkler_gpm?t.jsxs("span",{className:"text-gray-600",children:["• ",Number(e.sprinkler_gpm)," gpm"]}):null,e.duration_minutes?t.jsxs("span",{className:"text-gray-600",children:["• ",e.duration_minutes," min"]}):null]},`lg-${e.id}`))})]})}),_.map(e=>{const s=Xe({type:e.sprinkler_head_type,throwFeet:parseFloat(e.sprinkler_throw_feet||0),degrees:parseInt(e.sprinkler_degrees||360,10),lengthFeet:parseFloat(e.sprinkler_length_feet||0),widthFeet:parseFloat(e.sprinkler_width_feet||0)}),a=V(e);return t.jsxs(Ye.Fragment,{children:[t.jsx(ke,{position:[Number(e.lat),Number(e.lng)],draggable:!0,icon:be(a),eventHandlers:{dragend:n=>{const r=n.target.getLatLng();b(e.id,{lat:r.lat,lng:r.lng})},click:()=>je(e)},children:t.jsx(Je,{children:t.jsxs("div",{className:"space-y-2 text-sm",children:[t.jsx("div",{className:"font-medium",children:e.equipment_name||"Adjust Sprinkler"}),(e.sprinkler_head_type==="rotor_impact"||e.sprinkler_head_type==="spray_fixed")&&t.jsxs(t.Fragment,{children:[t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx("span",{className:"text-xs",children:"Degrees:"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{degrees:Math.max(0,Math.min(360,Number(e.sprinkler_degrees||0)-10))}),children:"-10°"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{degrees:Math.max(0,Math.min(360,Number(e.sprinkler_degrees||0)-1))}),children:"-1°"}),t.jsxs("span",{className:"px-2",children:[Number(e.sprinkler_degrees||0),"°"]}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{degrees:Math.max(0,Math.min(360,Number(e.sprinkler_degrees||0)+1))}),children:"+1°"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{degrees:Math.max(0,Math.min(360,Number(e.sprinkler_degrees||0)+10))}),children:"+10°"})]}),t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx("span",{className:"text-xs",children:"Throw:"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{throwFeet:Math.max(0,Number(e.sprinkler_throw_feet||0)-5)}),children:"-5ft"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{throwFeet:Math.max(0,Number(e.sprinkler_throw_feet||0)-1)}),children:"-1ft"}),t.jsxs("span",{className:"px-2",children:[Number(e.sprinkler_throw_feet||0)," ft"]}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{throwFeet:Math.max(0,Number(e.sprinkler_throw_feet||0)+1)}),children:"+1ft"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{throwFeet:Math.max(0,Number(e.sprinkler_throw_feet||0)+5)}),children:"+5ft"})]})]}),e.sprinkler_head_type==="oscillating_fan"&&t.jsxs(t.Fragment,{children:[t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx("span",{className:"text-xs",children:"Length:"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{lengthFeet:Math.max(0,Number(e.sprinkler_length_feet||0)-5)}),children:"-5ft"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{lengthFeet:Math.max(0,Number(e.sprinkler_length_feet||0)-1)}),children:"-1ft"}),t.jsxs("span",{className:"px-2",children:[Number(e.sprinkler_length_feet||0)," ft"]}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{lengthFeet:Math.max(0,Number(e.sprinkler_length_feet||0)+1)}),children:"+1ft"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{lengthFeet:Math.max(0,Number(e.sprinkler_length_feet||0)+5)}),children:"+5ft"})]}),t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx("span",{className:"text-xs",children:"Width:"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{widthFeet:Math.max(0,Number(e.sprinkler_width_feet||0)-5)}),children:"-5ft"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{widthFeet:Math.max(0,Number(e.sprinkler_width_feet||0)-1)}),children:"-1ft"}),t.jsxs("span",{className:"px-2",children:[Number(e.sprinkler_width_feet||0)," ft"]}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{widthFeet:Math.max(0,Number(e.sprinkler_width_feet||0)+1)}),children:"+1ft"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>b(e.id,{widthFeet:Math.max(0,Number(e.sprinkler_width_feet||0)+5)}),children:"+5ft"})]})]}),t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx("span",{className:"text-xs",children:"Heading:"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>K(e,-10),children:"-10°"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>K(e,-1),children:"-1°"}),t.jsxs("span",{className:"px-2",children:[Number(e.sprinkler_heading_degrees||0),"°"]}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>K(e,1),children:"+1°"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>K(e,10),children:"+10°"})]}),t.jsxs("div",{className:"grid grid-cols-3 gap-2 items-center",children:[t.jsx("div",{}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>Z(e,0,1),children:"▲"}),t.jsx("div",{}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>Z(e,-1,0),children:"◀"}),t.jsx("div",{className:"text-center text-xs",children:"Move 1 ft"}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>Z(e,1,0),children:"▶"}),t.jsx("div",{}),t.jsx("button",{className:"px-2 py-1 border rounded",onClick:()=>Z(e,0,-1),children:"▼"}),t.jsx("div",{})]}),t.jsx("div",{className:"flex gap-2 pt-1",children:t.jsx("button",{className:"btn-secondary",onClick:async()=>{var n,r;if(await j.deletePoint(e.id),h){const d=await j.getPlanPoints(h.id);f(((r=(n=d.data)==null?void 0:n.data)==null?void 0:r.points)||[])}else f(d=>d.filter(l=>l.id!==e.id))},children:"Delete"})})]})})}),(s==null?void 0:s.kind)==="circle"&&(s.degrees&&s.degrees<360?t.jsx(oe,{positions:Ae({lat:Number(e.lat),lng:Number(e.lng)},s.radius,e.sprinkler_heading_degrees||0,(e.sprinkler_heading_degrees||0)+s.degrees),pathOptions:{color:a,fillOpacity:.2}}):t.jsx(we,{center:[e.lat,e.lng],radius:s.radius*.3048,pathOptions:{color:a,fillOpacity:.2}})),(s==null?void 0:s.kind)==="rect"&&(()=>{const n=Number(e.lat),r=Number(e.lng),d=s.length,l=s.width,x=Number(e.sprinkler_heading_degrees||0)*Math.PI/180,F=111320,S=Math.cos(n*Math.PI/180)*111320,P=d/2*.3048,R=l/2*.3048,W=[{x:P,y:R},{x:-P,y:R},{x:-P,y:-R},{x:P,y:-R}].map(({x:Y,y:E})=>{const X=Y*Math.cos(x)-E*Math.sin(x),le=Y*Math.sin(x)+E*Math.cos(x),ie=n+le/F,de=r+X/S;return[ie,de]}),$=V(e);return t.jsx(oe,{positions:W,pathOptions:{color:$,fillOpacity:.2}})})()]},e.id)})]})]})})]}),Me&&h&&t.jsx(tt,{plan:h,property:o,points:_,onClose:()=>ae(!1),onSaved:()=>{ae(!1),v.success("Watering run logged")}})]})},et=({current:N,onSaved:y})=>{const[o,O]=c.useState(""),[M,L]=c.useState(!1),z=async()=>{var T,i,k,h;if(!o.trim()){v.error("Enter a name");return}L(!0);try{const I=(((i=(T=(await ee.getCategories()).data)==null?void 0:T.data)==null?void 0:i.categories)||[]).find(w=>(w.name||"").toLowerCase()==="sprinkler");if(!I){v.error("Sprinkler category not found"),L(!1);return}const D={categoryId:I.id,customName:o.trim(),sprinklerMount:N.mount,sprinklerHeadType:N.type,sprinklerGpm:N.gpm,sprinklerThrowFeet:N.throwFeet,sprinklerDegrees:N.degrees,sprinklerLengthFeet:N.lengthFeet,sprinklerWidthFeet:N.widthFeet,notes:"Saved from Watering settings"},u=(h=(k=(await ee.create(D)).data)==null?void 0:k.data)==null?void 0:h.equipment;v.success("Sprinkler saved"),y&&y(u),O("")}catch{v.error("Failed to save sprinkler")}finally{L(!1)}};return t.jsxs("div",{className:"mt-2 p-2 border rounded bg-gray-50 space-y-2",children:[t.jsx("div",{className:"text-sm",children:"Save current settings as equipment"}),t.jsxs("div",{className:"flex gap-2",children:[t.jsx("input",{className:"input flex-1",placeholder:"Name (e.g., Front Yard Rotor)",value:o,onChange:T=>O(T.target.value)}),t.jsx("button",{className:"btn-secondary",disabled:M,onClick:z,children:M?"Saving...":"Save"})]})]})},tt=({plan:N,property:y,points:o,onClose:O,onSaved:M})=>{const[L,z]=c.useState(new Date().toISOString().slice(0,16)),[T,i]=c.useState(""),[k,h]=c.useState(!1),[C,B]=c.useState(()=>o.map(u=>({planPointId:u.id,enabled:!0,actualDurationMinutes:Number(u.duration_minutes||0),actualGpm:u.sprinkler_gpm==null?"":Number(u.sprinkler_gpm),notes:""}))),I=c.useMemo(()=>C.reduce((u,w)=>{if(!w.enabled)return u;const g=o.find(te=>te.id===w.planPointId),p=Number(w.actualDurationMinutes||0),G=w.actualGpm===""?Number((g==null?void 0:g.sprinkler_gpm)||0):Number(w.actualGpm||0);return u.duration+=p,u.gallons+=p*G,u.coverage+=Number((g==null?void 0:g.coverage_sqft)||0),u},{duration:0,gallons:0,coverage:0}),[C,o]),D=(u,w)=>{B(g=>g.map(p=>p.planPointId===u?{...p,...w}:p))},f=async()=>{var w,g;const u=C.filter(p=>p.enabled).map(p=>({planPointId:p.planPointId,actualDurationMinutes:Number(p.actualDurationMinutes||0),actualGpm:p.actualGpm===""?null:Number(p.actualGpm||0),notes:p.notes}));if(u.length===0){v.error("Select at least one watering point");return}h(!0);try{await j.createLog({planId:N.id,runDate:new Date(L).toISOString(),notes:T,pointRuns:u}),M==null||M()}catch(p){v.error(((g=(w=p.response)==null?void 0:w.data)==null?void 0:g.message)||"Failed to log watering run")}finally{h(!1)}};return t.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4",children:t.jsxs("div",{className:"w-full max-w-4xl rounded-lg bg-white shadow-xl",children:[t.jsxs("div",{className:"flex items-center justify-between border-b px-6 py-4",children:[t.jsxs("div",{children:[t.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Log Watering Run"}),t.jsxs("p",{className:"text-sm text-gray-600",children:[(y==null?void 0:y.name)||"Property"," • ",N.name]})]}),t.jsx("button",{onClick:O,className:"text-gray-500 hover:text-gray-700",children:"✕"})]}),t.jsxs("div",{className:"space-y-4 px-6 py-4",children:[t.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[t.jsxs("div",{children:[t.jsx("label",{className:"mb-1 block text-sm font-medium text-gray-700",children:"Run Date & Time"}),t.jsx("input",{type:"datetime-local",className:"input",value:L,onChange:u=>z(u.target.value)})]}),t.jsxs("div",{className:"rounded-lg bg-gray-50 p-3 text-sm text-gray-700",children:[t.jsxs("div",{children:[t.jsx("span",{className:"font-medium",children:"Total Duration:"})," ",I.duration.toFixed(0)," min"]}),t.jsxs("div",{children:[t.jsx("span",{className:"font-medium",children:"Estimated Gallons:"})," ",I.gallons.toFixed(2)]}),t.jsxs("div",{children:[t.jsx("span",{className:"font-medium",children:"Coverage:"})," ",I.coverage.toFixed(0)," sq ft"]})]}),t.jsxs("div",{children:[t.jsx("label",{className:"mb-1 block text-sm font-medium text-gray-700",children:"Notes"}),t.jsx("textarea",{className:"input min-h-[88px]",value:T,onChange:u=>i(u.target.value),placeholder:"Optional run notes"})]})]}),t.jsx("div",{className:"overflow-x-auto",children:t.jsxs("table",{className:"min-w-full divide-y divide-gray-200 text-sm",children:[t.jsx("thead",{className:"bg-gray-50",children:t.jsxs("tr",{children:[t.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-600",children:"Include"}),t.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-600",children:"Point"}),t.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-600",children:"Equipment"}),t.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-600",children:"Head Type"}),t.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-600",children:"Actual Duration"}),t.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-600",children:"Actual GPM"}),t.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-600",children:"Coverage"})]})}),t.jsx("tbody",{className:"divide-y divide-gray-100 bg-white",children:o.map((u,w)=>{const g=C.find(p=>p.planPointId===u.id);return g?t.jsxs("tr",{children:[t.jsx("td",{className:"px-3 py-2",children:t.jsx("input",{type:"checkbox",checked:g.enabled,onChange:p=>D(u.id,{enabled:p.target.checked})})}),t.jsxs("td",{className:"px-3 py-2",children:["#",w+1]}),t.jsx("td",{className:"px-3 py-2",children:u.equipment_name||"N/A"}),t.jsx("td",{className:"px-3 py-2",children:u.sprinkler_head_type||"N/A"}),t.jsx("td",{className:"px-3 py-2",children:t.jsx("input",{type:"number",min:"0",step:"1",className:"input",value:g.actualDurationMinutes,onChange:p=>D(u.id,{actualDurationMinutes:p.target.value})})}),t.jsx("td",{className:"px-3 py-2",children:t.jsx("input",{type:"number",min:"0",step:"0.1",className:"input",value:g.actualGpm,onChange:p=>D(u.id,{actualGpm:p.target.value})})}),t.jsxs("td",{className:"px-3 py-2",children:[Number(u.coverage_sqft||0).toFixed(0)," sq ft"]})]},u.id):null})})]})})]}),t.jsxs("div",{className:"flex justify-end gap-3 border-t px-6 py-4",children:[t.jsx("button",{className:"btn-secondary",onClick:O,disabled:k,children:"Cancel"}),t.jsx("button",{className:"btn-primary",onClick:f,disabled:k,children:k?"Saving…":"Save Run"})]})]})})};export{at as default};
|