import{r as n,j as s,L as z,M as B,T as _,a1 as $,l as q,n as O,P as R,a4 as p}from"./index-9FS9bw8D.js";const K=()=>{const[C,v]=n.useState(!0),[M,E]=n.useState([]),[g,T]=n.useState(""),[i,k]=n.useState(null),[d,h]=n.useState(null),[b,N]=n.useState(""),[f,w]=n.useState(""),[r,u]=n.useState(null),[S,o]=n.useState([]),P=async()=>{var e,a;try{v(!0);const t=await p.getProperties({search:g});E(((a=(e=t.data)==null?void 0:e.data)==null?void 0:a.properties)||[])}finally{v(!1)}};n.useEffect(()=>{P()},[g]);const y=async e=>{var t,l;const a=await p.getProperty(e);k(((l=(t=a.data)==null?void 0:t.data)==null?void 0:l.property)||null)},G=e=>{h(e),N(e.name),w(e.grassType||(e.grassTypes||[]).join(", "))},L=e=>{var a,t;u(e.id),o(((t=(a=e.polygonData)==null?void 0:a.coordinates)==null?void 0:t[0])||[])},A=async()=>{const e=f.split(",").map(t=>t.trim()).filter(Boolean),a={name:b,area:d.area,polygonData:d.polygonData,grassType:e.join(", "),grassTypes:e};await p.updateSectionAdmin(i.id,d.id,a),await y(i.id),h(null)},D=async()=>{const e=(i.sections||[]).find(t=>t.id===r),a={...e.polygonData||{},coordinates:[S]};await p.updateSectionAdmin(i.id,r,{name:e.name,area:e.area,polygonData:a,grassType:e.grassType,grassTypes:e.grassTypes}),await y(i.id),u(null),o([])},I=()=>{u(null),o([])};return C?s.jsx("div",{className:"p-6",children:s.jsx(z,{})}):s.jsxs("div",{className:"p-6",children:[s.jsxs("div",{className:"flex justify-between items-center mb-4",children:[s.jsx("h1",{className:"text-2xl font-bold",children:"Admin: Properties"}),s.jsx("input",{className:"input w-64",placeholder:"Search by name/address/email",value:g,onChange:e=>T(e.target.value)})]}),s.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[s.jsxs("div",{className:"card",children:[s.jsx("h3",{className:"font-semibold mb-2",children:"All Properties"}),s.jsx("div",{className:"max-h-[60vh] overflow-auto divide-y",children:M.map(e=>s.jsxs("div",{className:`p-3 hover:bg-gray-50 cursor-pointer ${(i==null?void 0:i.id)===e.id?"bg-blue-50":""}`,onClick:()=>y(e.id),children:[s.jsxs("div",{className:"flex justify-between items-center",children:[s.jsxs("div",{children:[s.jsx("div",{className:"font-medium",children:e.name}),s.jsx("div",{className:"text-xs text-gray-600",children:e.userEmail})]}),s.jsxs("div",{className:"text-sm text-gray-700",children:[e.sectionCount," sections • ",(e.calculatedArea||0).toLocaleString()," sq ft"]})]}),s.jsx("div",{className:"text-xs text-gray-500",children:e.address})]},e.id))})]}),s.jsx("div",{className:"card",children:i?s.jsxs("div",{children:[s.jsx("div",{className:"flex justify-between items-center mb-2",children:s.jsxs("div",{children:[s.jsx("h3",{className:"font-semibold",children:i.name}),s.jsx("div",{className:"text-xs text-gray-600",children:i.userEmail})]})}),s.jsx("div",{className:"h-64 rounded overflow-hidden mb-3",children:s.jsxs(B,{center:[i.latitude||39.8,i.longitude||-98.6],zoom:16,style:{height:"100%",width:"100%"},children:[s.jsx(_,{attribution:"© Esri",url:"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"}),(i.sections||[]).map(e=>{var a,t;return s.jsx($,{positions:((t=(a=e.polygonData)==null?void 0:a.coordinates)==null?void 0:t[0])||[],pathOptions:{color:e.id===r?"#f59e0b":"#10b981",weight:2,fillOpacity:.2}},e.id)}),r&&S.map((e,a)=>s.jsx(q,{position:e,draggable:!0,eventHandlers:{dragend:t=>{const{lat:l,lng:m}=t.target.getLatLng();o(x=>x.map((c,j)=>j===a?[l,m]:c))}},icon:new O.Icon({iconUrl:"data:image/svg+xml;base64,"+btoa(` `),iconSize:[12,12],iconAnchor:[6,6]}),children:s.jsx(R,{children:s.jsxs("div",{className:"text-xs",children:["Point ",a+1]})})},`ed-${a}`))]})}),r&&s.jsxs("div",{className:"mb-3 flex gap-2",children:[s.jsx("button",{className:"btn-primary",onClick:D,children:"Save Geometry"}),s.jsx("button",{className:"btn-secondary",onClick:I,children:"Cancel"})]}),s.jsx("h4",{className:"font-medium mb-2",children:"Sections"}),s.jsx("div",{className:"space-y-2 max-h-[40vh] overflow-auto",children:(i.sections||[]).map(e=>{var a,t,l,m,x;return s.jsxs("div",{className:"p-2 border rounded flex justify-between items-center",children:[s.jsxs("div",{children:[s.jsx("div",{className:"font-medium text-sm",children:e.name}),s.jsxs("div",{className:"text-xs text-gray-600",children:[(e.area||0).toLocaleString()," sq ft"]}),s.jsxs("div",{className:"mt-1 flex items-center gap-2",children:[e.captureMethod&&s.jsx("span",{className:`px-1.5 py-0.5 text-[10px] rounded ${e.captureMethod==="gps_points"?"bg-blue-100 text-blue-800":e.captureMethod==="gps_trace"?"bg-yellow-100 text-yellow-800":"bg-gray-100 text-gray-700"}`,children:e.captureMethod}),((a=e.captureMeta)==null?void 0:a.pointsCount)&&s.jsxs("span",{className:"text-[10px] text-gray-600",children:[e.captureMeta.pointsCount," pts"]}),((t=e.captureMeta)==null?void 0:t.accuracyLast)&&s.jsxs("span",{className:"text-[10px] text-gray-600",children:["±",Math.round(e.captureMeta.accuracyLast)," m"]}),((l=e.captureMeta)==null?void 0:l.totalDistanceMeters)&&s.jsxs("span",{className:"text-[10px] text-gray-600",children:[Math.round(e.captureMeta.totalDistanceMeters*3.28084)," ft walked"]})]}),(((m=e.grassTypes)==null?void 0:m.length)>0||e.grassType)&&s.jsx("div",{className:"mt-1 flex flex-wrap gap-1",children:((x=e.grassTypes)!=null&&x.length?e.grassTypes:(e.grassType||"").split(",").map(c=>c.trim()).filter(Boolean)).map((c,j)=>s.jsx("span",{className:"px-1.5 py-0.5 text-[10px] rounded bg-green-100 text-green-800",children:c},j))})]}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx("button",{className:"btn-secondary",onClick:()=>G(e),children:"Edit"}),s.jsx("button",{className:"btn-secondary",onClick:()=>L(e),children:"Edit Geometry"})]})]},e.id)})})]}):s.jsx("div",{className:"text-gray-500",children:"Select a property to view details"})})]}),d&&s.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50",children:s.jsxs("div",{className:"bg-white rounded-lg p-6 w-full max-w-lg",children:[s.jsx("h3",{className:"font-semibold mb-3",children:"Edit Section"}),s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"label",children:"Name"}),s.jsx("input",{className:"input",value:b,onChange:e=>N(e.target.value)})]}),s.jsxs("div",{children:[s.jsx("label",{className:"label",children:"Grass Types (comma separated)"}),s.jsx("input",{className:"input",value:f,onChange:e=>w(e.target.value),placeholder:"e.g., Kentucky Bluegrass, Perennial Ryegrass"})]})]}),s.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[s.jsx("button",{className:"btn-secondary",onClick:()=>h(null),children:"Cancel"}),s.jsx("button",{className:"btn-primary",onClick:A,children:"Save"})]})]})})]})};export{K as default};