(function(){document.querySelectorAll(".grid-items").forEach(function(g){g.addEventListener("dragstart",function(e){e.dataTransfer.setData("text/plain",e.target.dataset.id);});g.addEventListener("dragover",function(e){e.preventDefault();});g.addEventListener("drop",function(e){e.preventDefault();var id=e.dataTransfer.getData("text/plain");var target=e.target.closest(".listing-card");if(target&&target.dataset.id!==id){g.insertBefore(document.querySelector("[data-id=\""+id+"\"]"),target);saveOrder(g);}});});
function saveOrder(grid){var positions={};grid.querySelectorAll(".listing-card").forEach(function(c,i){positions[c.dataset.id]=i;});var csrf=document.querySelector("meta[name=csrf-token]").content;var body="system_action=modify&entity=listing_order&id=0&_csrf="+csrf;for(var k in positions)body+="&data[positions]["+k+"]="+positions[k];fetch("index.php",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:body});}})();
function editListing(id){document.getElementById("listing-grid").style.display="none";document.getElementById("listing-editor").style.display="block";fetch("api/search.php?scope=listings&q="+id).then(r=>r.json()).then(d=>{if(d.success)document.getElementById("listing-editor").dispatchEvent(new CustomEvent("load-listing",{detail:d.data[0]}));});}
