document.addEventListener("DOMContentLoaded", function () { const cards = document.querySelectorAll("[data-ref]"); const modal = document.getElementById("modal-imovel"); const modalTitle = document.getElementById("modal-title"); const modalImgs = document.getElementById("modal-imagens"); const modalPreco = document.getElementById("modal-preco"); const modalDesc = document.getElementById("modal-desc"); // Fecha o modal window.fecharModalImovel = function () { modal.classList.add("hidden"); modalTitle.textContent = ""; modalImgs.innerHTML = ""; modalPreco.textContent = ""; modalDesc.textContent = ""; }; // Clique num card cards.forEach(card => { card.addEventListener("click", () => { const ref = card.getAttribute("data-ref"); fetch("https://www.kwone.pt/ficheiros/imoveis.xml") .then(res => res.text()) .then(str => (new window.DOMParser()).parseFromString(str, "text/xml")) .then(data => { const imovel = Array.from(data.getElementsByTagName("property")).find(p => p.querySelector("reference")?.textContent.trim() === ref ); if (!imovel) return alert("Imóvel não encontrado"); modalTitle.textContent = imovel.querySelector("title")?.textContent || "Sem título"; modalPreco.textContent = imovel.querySelector("price")?.textContent + " €" || ""; modalDesc.textContent = imovel.querySelector("description")?.textContent || ""; const fotos = imovel.querySelectorAll("photos photo"); fotos.forEach(foto => { const img = document.createElement("img"); img.src = foto.textContent.trim(); img.alt = "Foto do imóvel"; img.className = "h-40 rounded shadow"; modalImgs.appendChild(img); }); modal.classList.remove("hidden"); }) .catch(err => { console.error("Erro ao carregar imóvel:", err); alert("Erro ao carregar dados do imóvel."); }); }); }); });