document.addEventListener("DOMContentLoaded", () => { getItemsCms(); }); async function getItemsCms() { try { const response = await fetch( "https://wf-search.mpp.agency/fetch-agr-items-rak", { method: "GET", headers: { "Content-Type": "application/json", Lang: typeof wfSearchLang !== "undefined" ? wfSearchLang : "en", }, }, ); const data = await response.json(); const items = data.items || []; const stats = { apartments: 0, villas: 0, penthouses: 0, }; items.forEach((item) => { const rawType = item.fieldData["13-tip-nedvizhimosti"] || ""; const types = rawType.split(",").map((t) => t.trim().toLowerCase()); if (types.includes("apartments")) stats.apartments++; if (types.includes("villas")) stats.villas++; if (types.includes("penthouses")) stats.penthouses++; }); updateUI(stats); return stats; } catch (error) { console.error("Error fetching data:", error); } } function updateUI(stats) { const countElements = document.querySelectorAll("[data-count-type]"); countElements.forEach((el) => { const type = el.getAttribute("data-count-type"); if (stats[type] !== undefined) { el.textContent = stats[type]; } }); }