/* كود فلترة المنتجات حسب المدينة - مع redirect دائم عند تغيير المدينة */
(function() {
'use strict';
let selectedCity = null;
let isApplying = false;
const cityPages = {
"الرياض": "https://moojwater.com/ar/%D9%85%D9%86%D8%AA%D8%AC%D8%A7%D8%AA-%D8%A7%D9%84%D8%B1%D9%8A%D8%A7%D8%B6/c1159548594",
"جدة": "https://moojwater.com/ar/%D9%85%D9%86%D8%AA%D8%AC%D8%A7%D8%AA-%D8%AC%D8%AF%D9%87/c1017510319",
"مكة": "https://moojwater.com/ar/%D9%85%D9%86%D8%AA%D8%AC%D8%A7%D8%AA-%D9%85%D9%83%D8%A9/c243407016"
};
const allowedSections = ["الباقات الشهرية", "الباقات الشهريه", "باقات شهرية", "باقات شهريه", "العروض", "عروض", "عروض مياه الشرب", "عروض المياه"];
const excludedCities = {
"الرياض": ["منتجات جدة", "منتجات جده", "منتجات مكة"],
"جدة": ["منتجات الرياض", "منتجات مكة"],
"مكة": ["منتجات الرياض", "منتجات جدة", "منتجات جده"]
};
function isHomePage() {
return document.body.classList.contains('index');
}
function getSavedCity() {
try {
return sessionStorage.getItem("selectedCity") || null;
} catch(e) {
return window.currentSelectedCity || null;
}
}
function addCityButton() {
if (document.getElementById('city-btn-mobile')) return;
const mainMenu = document.querySelector('ul.main-menu');
if (!mainMenu) {
setTimeout(addCityButton, 150);
return;
}
const savedCity = getSavedCity();
const cityText = savedCity || 'اختر المدينة';
const mobileLi = document.createElement('li');
mobileLi.className = 'lg:hidden text-sm font-bold';
mobileLi.id = 'city-btn-mobile';
mobileLi.innerHTML = `
📍 ${cityText}
`;
const desktopLi = document.createElement('li');
desktopLi.className = '!hidden lg:!block root-level lg:!inline-block';
desktopLi.id = 'city-btn-desktop';
desktopLi.innerHTML = `
📍 ${cityText}
`;
mainMenu.appendChild(mobileLi);
mainMenu.appendChild(desktopLi);
mobileLi.querySelector('a').onclick = () => showCityPopup(true);
desktopLi.querySelector('a').onclick = () => showCityPopup(true);
}
function showConfirmation(city) {
const confirmation = document.createElement("div");
confirmation.id = "city-confirmation";
confirmation.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);display:flex;align-items:center;justify-content:center;z-index:999999999;';
confirmation.innerHTML = `
✅
تم اختيار المدينة
📍 ${city}
جاري التحويل لصفحة المنتجات...
`;
document.body.appendChild(confirmation);
}
function showCityPopup(isReopening = false) {
const existing = document.getElementById("city-popup");
if (existing) existing.remove();
const popup = document.createElement("div");
popup.id = "city-popup";
popup.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);display:flex;align-items:center;justify-content:center;z-index:999999999;';
popup.innerHTML = `
${isReopening ? 'تغيير المدينة' : 'اختر مدينتك'}
⚠️ اختر مدينتك بدقة لعرض المنتجات المتاحة للشحن
⚠️ الرجاء اختيار المدينة أولاً
`;
document.body.appendChild(popup);
const select = document.getElementById("city-select");
if (selectedCity && isReopening) select.value = selectedCity;
const saveBtn = document.getElementById("save-city");
saveBtn.onmouseover = () => saveBtn.style.background = "#2c5f8a";
saveBtn.onmouseout = () => saveBtn.style.background = "#3e77aa";
saveBtn.onclick = () => {
const city = select.value;
if (!city) {
document.getElementById("error-msg").style.display = "block";
return;
}
selectedCity = city;
window.currentSelectedCity = city;
try {
sessionStorage.setItem("selectedCity", city);
sessionStorage.setItem("citySelectedInSession", "true");
} catch(e) {}
popup.remove();
// عرض رسالة التأكيد
showConfirmation(city);
// الانتقال بعد ثانية ونصف - تم إزالة شرط isHomePage()
setTimeout(() => {
if (cityPages[city]) {
window.location.href = cityPages[city];
} else {
const conf = document.getElementById("city-confirmation");
if (conf) conf.remove();
applyFilters();
}
}, 1500);
};
}
function applyFilters() {
if (isApplying) return;
isApplying = true;
requestAnimationFrame(() => {
const sections = document.querySelectorAll('section.s-block, salla-products-slider');
sections.forEach(section => {
const text = section.textContent || '';
const isAllowed = allowedSections.some(a => text.includes(a));
if (isAllowed) {
toggleButtons(section, true);
return;
}
if (!selectedCity) {
toggleButtons(section, false);
return;
}
const shouldHide = (excludedCities[selectedCity] || []).some(c => text.includes(c));
toggleButtons(section, !shouldHide);
});
handleCategoryPages();
isApplying = false;
});
}
function toggleButtons(element, show) {
if (!element) return;
const selectors = 'salla-add-product-button, .s-product-card-content-footer, button[product-id], .s-add-product-button-main';
const buttons = element.querySelectorAll(selectors);
buttons.forEach(btn => {
if (btn.closest('[class*="wishlist"]') || btn.querySelector('.sicon-heart, .sicon-share')) return;
if (show) {
btn.style.display = '';
btn.style.pointerEvents = '';
btn.style.visibility = '';
btn.style.opacity = '';
btn.removeAttribute('disabled');
} else {
btn.style.display = 'none';
btn.style.pointerEvents = 'none';
btn.style.visibility = 'hidden';
btn.style.opacity = '0';
btn.setAttribute('disabled', 'true');
}
});
}
function handleCategoryPages() {
const url = window.location.href;
if (!url.includes('/c') && !url.includes('/product/')) return;
const blocked = {
"الرياض": ["1017510319", "243407016"],
"جدة": ["1159548594", "243407016"],
"مكة": ["1159548594", "1017510319"]
};
const shouldBlock = (blocked[selectedCity] || []).some(id => url.includes(id));
if (shouldBlock) {
document.querySelectorAll('salla-add-product-button, button[product-id]').forEach(btn => {
if (!btn.closest('[class*="wishlist"]')) {
btn.style.display = 'none';
btn.setAttribute('disabled', 'true');
}
});
}
}
function ensureButtonExists() {
const btn = document.getElementById('city-btn-mobile');
const menu = document.querySelector('ul.main-menu');
if (menu && !btn) {
addCityButton();
}
if (btn) {
const savedCity = getSavedCity();
const currentText = btn.querySelector('.city-name')?.textContent;
const expectedText = savedCity || 'اختر المدينة';
if (currentText !== expectedText) {
const mobileSpan = document.querySelector('#city-btn-mobile .city-name');
const desktopSpan = document.querySelector('#city-btn-desktop .city-name');
if (mobileSpan) mobileSpan.textContent = expectedText;
if (desktopSpan) desktopSpan.textContent = expectedText;
}
}
}
function init() {
selectedCity = getSavedCity();
window.currentSelectedCity = selectedCity;
addCityButton();
if (isHomePage()) {
try {
if (sessionStorage.getItem("citySelectedInSession") !== "true") {
selectedCity = null;
sessionStorage.removeItem("selectedCity");
showCityPopup();
}
} catch(e) {
showCityPopup();
}
}
applyFilters();
setInterval(ensureButtonExists, 2000);
}
if (document.readyState !== 'loading') {
init();
} else {
document.addEventListener('DOMContentLoaded', init);
}
let debounce;
const observer = new MutationObserver(() => {
clearTimeout(debounce);
debounce = setTimeout(() => {
ensureButtonExists();
if (!isApplying) applyFilters();
}, 200);
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})()