import{r as c,j as e,M as E,T as U,k as G,l as $,P as z,n as P,p as N,z as j,L as T,f as _,o as v,e as S,q,s as Y}from"./index-9FS9bw8D.js";delete P.Icon.Default.prototype._getIconUrl;P.Icon.Default.mergeOptions({iconRetinaUrl:"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png",iconUrl:"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png",shadowUrl:"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png"});function H({position:n,setPosition:p}){return G({click(s){p([s.latlng.lat,s.latlng.lng])}}),n===null?null:e.jsx($,{position:n,children:e.jsxs(z,{children:["Property Location",e.jsx("br",{}),"Drag the map or click to adjust the pin position",e.jsx("br",{}),"Lat: ",n[0].toFixed(6),", Lng: ",n[1].toFixed(6)]})})}const O=({onSubmit:n,onCancel:p,initialData:s=null})=>{const[r,m]=c.useState({name:(s==null?void 0:s.name)||"",address:(s==null?void 0:s.address)||"",latitude:(s==null?void 0:s.latitude)||"",longitude:(s==null?void 0:s.longitude)||""}),[h,x]=c.useState(s!=null&&s.latitude&&(s!=null&&s.longitude)?[s.latitude,s.longitude]:[40.7128,-74.006]),[u,g]=c.useState(s!=null&&s.latitude&&(s!=null&&s.longitude)?[s.latitude,s.longitude]:null),[a,d]=c.useState(!1),[l,i]=c.useState([]),[F,y]=c.useState(!1),f=c.useRef(null),A=async t=>{if(t.length<3){i([]),y(!1);return}try{const R=(await(await fetch(`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(t)}&limit=5&countrycodes=us`)).json()).map(b=>({display_name:b.display_name,lat:parseFloat(b.lat),lon:parseFloat(b.lon)}));i(R),y(!0)}catch(o){console.error("Address search failed:",o)}},C=t=>{const o=t.target.value;m({...r,address:o}),f.current&&clearTimeout(f.current),f.current=setTimeout(()=>{A(o)},300)},k=t=>{m({...r,address:t.display_name,latitude:t.lat,longitude:t.lon}),x([t.lat,t.lon]),g([t.lat,t.lon]),y(!1),d(!0)},M=()=>{if(r.latitude&&r.longitude){const t=parseFloat(r.latitude),o=parseFloat(r.longitude);x([t,o]),g([t,o])}d(!0)},L=t=>{g(t),m({...r,latitude:t[0],longitude:t[1]})},I=async t=>{t.preventDefault();const o={...r,latitude:u?u[0]:r.latitude?parseFloat(r.latitude):null,longitude:u?u[1]:r.longitude?parseFloat(r.longitude):null};try{await n(o)}catch(w){console.error("Form submission failed:",w)}};return e.jsxs("div",{className:"card mb-6",children:[e.jsx("h3",{className:"text-lg font-semibold text-gray-900 mb-4",children:s?"Edit Property":"Add New Property"}),e.jsxs("form",{onSubmit:I,className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Property Name *"}),e.jsx("input",{type:"text",required:!0,className:"input",value:r.name,onChange:t=>m({...r,name:t.target.value}),placeholder:"e.g., Main Lawn, Front Yard"})]}),e.jsxs("div",{className:"relative",children:[e.jsx("label",{className:"label",children:"Address"}),e.jsx("input",{type:"text",className:"input",value:r.address,onChange:C,onFocus:()=>y(l.length>0),placeholder:"123 Main St, City, State"}),F&&l.length>0&&e.jsx("div",{className:"absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-auto",children:l.map((t,o)=>e.jsx("button",{type:"button",className:"w-full text-left px-4 py-2 hover:bg-gray-100 focus:bg-gray-100 focus:outline-none",onClick:()=>k(t),children:t.display_name},o))})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Latitude"}),e.jsx("input",{type:"number",step:"any",className:"input",value:r.latitude,onChange:t=>m({...r,latitude:t.target.value}),placeholder:"40.7128"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Longitude"}),e.jsx("input",{type:"number",step:"any",className:"input",value:r.longitude,onChange:t=>m({...r,longitude:t.target.value}),placeholder:"-74.0060"})]})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsx("button",{type:"button",onClick:M,className:"btn-secondary",children:a?"Hide Map":"Show Map"}),!a&&r.latitude&&r.longitude&&e.jsx("span",{className:"text-sm text-gray-600 self-center",children:'Click "Show Map" to adjust the exact location'})]}),a&&e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"border border-gray-300 rounded-lg overflow-hidden",children:e.jsx("div",{style:{height:"400px",width:"100%"},children:e.jsxs(E,{center:h,zoom:18,style:{height:"100%",width:"100%"},children:[e.jsx(U,{attribution:'© Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community',url:"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"}),e.jsx(H,{position:u,setPosition:L})]})})}),e.jsxs("div",{className:"text-sm text-gray-600 bg-blue-50 p-3 rounded-lg",children:[e.jsx("p",{children:e.jsx("strong",{children:"Instructions:"})}),e.jsxs("ul",{className:"list-disc list-inside mt-1 space-y-1",children:[e.jsx("li",{children:"Click anywhere on the map to place or move the property marker"}),e.jsx("li",{children:"The marker represents the exact location of your property"}),e.jsx("li",{children:"Coordinates will be automatically updated when you move the marker"}),e.jsx("li",{children:"After setting the location, you can add lawn sections in the property details"})]})]})]}),e.jsxs("div",{className:"flex gap-3 pt-4 border-t border-gray-200",children:[e.jsx("button",{type:"submit",className:"btn-primary",children:s?"Update Property":"Create Property"}),e.jsx("button",{type:"button",onClick:p,className:"btn-secondary",children:"Cancel"})]})]})]})},W=()=>{const[n,p]=c.useState([]),[s,r]=c.useState(!0),[m,h]=c.useState(!1);c.useEffect(()=>{x()},[]);const x=async()=>{var a,d;try{r(!0);const l=await N.getAll();console.log("Properties API response:",l),console.log("Response data:",l.data),console.log("Response data type:",typeof l.data);const i=((d=(a=l.data)==null?void 0:a.data)==null?void 0:d.properties)||[];console.log("Properties data:",i),p(i)}catch(l){console.error("Failed to fetch properties:",l),j.error("Failed to load properties"),p([])}finally{r(!1)}},u=async a=>{var d,l;try{await N.create(a),j.success("Property created successfully!"),h(!1),x()}catch(i){throw console.error("Failed to create property:",i),j.error(((l=(d=i.response)==null?void 0:d.data)==null?void 0:l.message)||"Failed to create property"),i}},g=async(a,d)=>{if(window.confirm(`Are you sure you want to delete "${d}"?`))try{await N.delete(a),j.success("Property deleted successfully"),x()}catch(l){console.error("Failed to delete property:",l),j.error("Failed to delete property")}};return s?e.jsxs("div",{className:"p-6",children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-900 mb-6",children:"Properties"}),e.jsx("div",{className:"flex justify-center items-center h-64",children:e.jsx(T,{size:"lg"})})]}):e.jsxs("div",{className:"p-6",children:[e.jsxs("div",{className:"flex justify-between items-center mb-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-900",children:"Properties"}),e.jsx("p",{className:"text-gray-600",children:"Manage your lawn care properties"})]}),e.jsxs("button",{onClick:()=>h(!0),className:"btn-primary flex items-center gap-2",children:[e.jsx(_,{className:"h-5 w-5"}),"Add Property"]})]}),m&&e.jsx(O,{onSubmit:u,onCancel:()=>h(!1)}),!Array.isArray(n)||n.length===0?e.jsxs("div",{className:"card text-center py-12",children:[e.jsx(v,{className:"h-16 w-16 text-gray-300 mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-medium text-gray-900 mb-2",children:"No Properties Yet"}),e.jsx("p",{className:"text-gray-600 mb-6",children:"Get started by adding your first property"}),e.jsx("button",{onClick:()=>h(!0),className:"btn-primary",children:"Add Your First Property"})]}):e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6",children:Array.isArray(n)&&n.map(a=>e.jsxs("div",{className:"card",children:[e.jsxs("div",{className:"flex justify-between items-start mb-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-gray-900",children:a.name}),a.address&&e.jsxs("p",{className:"text-sm text-gray-600 flex items-center mt-1",children:[e.jsx(v,{className:"h-4 w-4 mr-1"}),a.address]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(S,{to:`/properties/${a.id}`,className:"p-1 text-gray-400 hover:text-blue-600 transition-colors",children:e.jsx(q,{className:"h-4 w-4"})}),e.jsx("button",{onClick:()=>g(a.id,a.name),className:"p-1 text-gray-400 hover:text-red-600 transition-colors",children:e.jsx(Y,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{className:"space-y-2 text-sm",children:[a.totalArea&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-gray-600",children:"Total Area:"}),e.jsxs("span",{className:"font-medium",children:[a.totalArea.toLocaleString()," sq ft"]})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-gray-600",children:"Sections:"}),e.jsx("span",{className:"font-medium",children:a.sectionCount||0})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-gray-600",children:"Created:"}),e.jsx("span",{className:"font-medium",children:new Date(a.createdAt).toLocaleDateString()})]})]}),e.jsx("div",{className:"mt-4 pt-4 border-t border-gray-200",children:e.jsx(S,{to:`/properties/${a.id}`,className:"text-blue-600 hover:text-blue-700 text-sm font-medium",children:"View Details →"})})]},a.id))})]})};export{W as default};