// دالة للتعامل مع زر إظهار المزيد
function setupLoadMoreButton() {
console.log('Setting up load more button');
// البحث عن الزر بعد تحميل المحتوى
const loadMoreBtn = document.getElementById('loadMoreBtn');
console.log('Load more button:', loadMoreBtn);
if (!loadMoreBtn) {
console.log('Button not found, will try again later');
// إذا لم يتم العثور على الزر، نحاول مرة أخرى بعد قليل
setTimeout(setupLoadMoreButton, 500);
return;
}
if (loadMoreBtn) {
console.log('Load more button found:', loadMoreBtn);
// إضافة مستمع الحدث للزر
loadMoreBtn.addEventListener('click', function() {
console.log('Load more button clicked');
// الحصول على رقم الصفحة التالية
const nextPage = parseInt(loadMoreBtn.getAttribute('data-page'));
const totalPages = parseInt(loadMoreBtn.getAttribute('data-total-pages'));
console.log('Loading page', nextPage, 'of', totalPages);
// إظهار حالة التحميل
loadMoreBtn.innerHTML = ' جاري التحميل...';
loadMoreBtn.disabled = true;
// الحصول على معايير التصفية الحالية
const wilayaSelect = document.getElementById('wilayaSelect');
const communeSelect = document.getElementById('communeSelect');
let wilayaId = null;
let communeIds = null;
if (wilayaSelect && wilayaSelect.value) {
wilayaId = wilayaSelect.value;
}
if (communeSelect && communeSelect.value) {
communeIds = communeSelect.value;
}
// إرسال طلب AJAX للحصول على الصفحة التالية من المؤسسات
$.ajax({
url: '/getEtablissementsFiltre',
type: 'POST',
data: {
_token: $('meta[name="csrf-token"]').attr('content'),
wilayaId: wilayaId,
communeIds: communeIds,
page: nextPage,
ajax_load_more: true // علامة لتحديد أن هذا طلب لتحميل المزيد
},
success: function(response) {
// التحقق من أن الاستجابة تحتوي على مؤسسات جديدة
if (response.includes('institution-card')) {
// استخراج بطاقات المؤسسات من الاستجابة
const tempDiv = document.createElement('div');
tempDiv.innerHTML = response;
const newCards = tempDiv.querySelectorAll('.institution-card');
const institutionsContainer = document.getElementById('institutionsContainer');
// إضافة البطاقات الجديدة إلى الحاوية
newCards.forEach(card => {
institutionsContainer.appendChild(card);
});
// تحديث زر تحميل المزيد
const newLoadMoreBtn = tempDiv.querySelector('#loadMoreBtn');
if (newLoadMoreBtn) {
// تحديث الزر الحالي بمعلومات الصفحة الجديدة
loadMoreBtn.setAttribute('data-page', newLoadMoreBtn.getAttribute('data-page'));
loadMoreBtn.setAttribute('data-total-pages', newLoadMoreBtn.getAttribute('data-total-pages'));
loadMoreBtn.innerHTML = 'تحميل المزيد ';
loadMoreBtn.disabled = false;
} else {
// إذا لم يعد هناك صفحات أخرى، إخفاء الزر
loadMoreBtn.parentNode.remove();
}
// التمرير إلى آخر بطاقة تم تحميلها
if (newCards.length > 0) {
newCards[newCards.length - 1].scrollIntoView({ behavior: 'smooth', block: 'center' });
}
} else {
// إذا لم يتم العثور على مؤسسات جديدة
loadMoreBtn.parentNode.remove();
}
},
error: function(xhr, status, error) {
console.error('Error loading more institutions:', error);
loadMoreBtn.innerHTML = 'حدث خطأ، حاول مرة أخرى ';
loadMoreBtn.disabled = false;
}
});
});
}
// تشغيل الدالة بعد تحميل الصفحة
document.addEventListener('DOMContentLoaded', function() {
console.log('DOM loaded');
setupLoadMoreButton();
});
// إعادة تشغيل الدالة بعد كل طلب AJAX
$(document).ajaxComplete(function(event, xhr, settings) {
console.log('AJAX request completed');
if (settings.url.includes('getEtablissementsFiltre')) {
console.log('Establishments loaded via AJAX, setting up load more button');
// انتظار قليل للتأكد من تحميل المحتوى
setTimeout(setupLoadMoreButton, 300);
}
});
document.addEventListener('DOMContentLoaded', function() {
// الحصول على زر تحميل المزيد
const loadMoreBtn = document.getElementById('loadMoreBtn');
if (loadMoreBtn) {
console.log('Load more button found:', loadMoreBtn);
// إضافة مستمع الحدث للزر
loadMoreBtn.addEventListener('click', function() {
console.log('Load more button clicked');
// الحصول على رقم الصفحة التالية
const nextPage = parseInt(loadMoreBtn.getAttribute('data-page'));
const totalPages = parseInt(loadMoreBtn.getAttribute('data-total-pages'));
console.log('Loading page', nextPage, 'of', totalPages);
// إظهار حالة التحميل
loadMoreBtn.innerHTML = ' جاري التحميل...';
loadMoreBtn.disabled = true;
// الحصول على معايير التصفية الحالية
const wilayaSelect = document.getElementById('wilayaSelect');
const communeSelect = document.getElementById('communeSelect');
let wilayaId = null;
let communeIds = null;
if (wilayaSelect && wilayaSelect.value) {
wilayaId = wilayaSelect.value;
}
if (communeSelect && communeSelect.value) {
communeIds = communeSelect.value;
}
// إرسال طلب AJAX للحصول على الصفحة التالية من المؤسسات
$.ajax({
url: '/getEtablissementsFiltre',
type: 'POST',
data: {
_token: $('meta[name="csrf-token"]').attr('content'),
wilayaId: wilayaId,
communeIds: communeIds,
page: nextPage,
ajax_load_more: true // علامة لتحديد أن هذا طلب لتحميل المزيد
},
success: function(response) {
// التحقق من أن الاستجابة تحتوي على مؤسسات جديدة
if (response.includes('institution-card')) {
// استخراج بطاقات المؤسسات من الاستجابة
const tempDiv = document.createElement('div');
tempDiv.innerHTML = response;
const newCards = tempDiv.querySelectorAll('.institution-card');
const institutionsContainer = document.getElementById('institutionsContainer');
// إضافة البطاقات الجديدة إلى الحاوية
newCards.forEach(card => {
institutionsContainer.appendChild(card);
});
// تحديث زر تحميل المزيد
const newLoadMoreBtn = tempDiv.querySelector('#loadMoreBtn');
if (newLoadMoreBtn) {
// تحديث الزر الحالي بمعلومات الصفحة الجديدة
loadMoreBtn.setAttribute('data-page', newLoadMoreBtn.getAttribute('data-page'));
loadMoreBtn.setAttribute('data-total-pages', newLoadMoreBtn.getAttribute('data-total-pages'));
loadMoreBtn.innerHTML = 'تحميل المزيد ';
loadMoreBtn.disabled = false;
} else {
// إذا لم يعد هناك صفحات أخرى، إخفاء الزر
loadMoreBtn.parentNode.remove();
}
// التمرير إلى آخر بطاقة تم تحميلها
if (newCards.length > 0) {
newCards[newCards.length - 1].scrollIntoView({ behavior: 'smooth', block: 'center' });
}
} else {
// إذا لم يتم العثور على مؤسسات جديدة
loadMoreBtn.parentNode.remove();
}
},
error: function(xhr, status, error) {
console.error('Error loading more institutions:', error);
loadMoreBtn.innerHTML = 'حدث خطأ، حاول مرة أخرى ';
loadMoreBtn.disabled = false;
}
});
});
}
});