\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
اختر طريقة الإرسال\
\
\
\
🔗 رابط مباشر
\
يمكنك إضافة رابط المنشور مباشرة
\
\
✓ تم إضافة الرابط - يمكنك الآن الإضافة للسلة
\
\
\
أو
\
\
\
📱 توزيع على المنشورات \
▼ \
\
\
\
أدخل اسم المستخدم واختر المنشورات لتوزيع ال' + currentProduct.label + ' عليها إلغاء
\
\
\
تحميل المنشورات \
\
جاري تحميل المنشورات...
\
\
\
تحميل المزيد \
\
تم اختيار: 0 من أصل 0 منشور\
| 0 ' + currentProduct.label + ' لكل منشور \
\
وصلت للحد الأقصى من المنشورات المسموحة
\
✓ تم اختيار المنشورات - يمكنك الآن الإضافة للسلة
\
\
\
';
var insertPoint = document.querySelector('.product__variants, .product-form__options, .product-form__variants, .product-info__variants');
if (insertPoint) {
insertPoint.parentNode.insertBefore(container, insertPoint.nextSibling);
} else {
form.parentNode.insertBefore(container, form.nextSibling);
}
var expandBtn = document.getElementById('ig-expand-btn');
var selectSection = document.getElementById('ig-select-section');
expandBtn.addEventListener('click', function() {
var isOpen = selectSection.classList.contains('open');
if (isOpen) {
selectSection.classList.remove('open');
expandBtn.classList.remove('active');
} else {
selectSection.classList.add('open');
expandBtn.classList.add('active');
if (allPosts.length > 0 || selectedPosts.length > 0) {
document.getElementById('ig-direct-section').style.opacity = '0.5';
document.getElementById('ig-direct-section').style.pointerEvents = 'none';
}
}
});
var directLinkInput = document.getElementById('ig-direct-link');
directLinkInput.addEventListener('input', handleDirectLinkChange);
directLinkInput.addEventListener('change', handleDirectLinkChange);
function handleDirectLinkChange() {
var link = directLinkInput.value.trim();
var urlMatch = link.match(/(https?:\/\/[^\s]+)/);
if (urlMatch) {
link = urlMatch[1];
directLinkInput.value = link;
}
var directSection = document.getElementById('ig-direct-section');
var directSuccess = document.getElementById('ig-direct-success');
if (link.length > 0 && link.includes('instagram.com')) {
directLink = link;
directSection.classList.add('active');
expandBtn.style.opacity = '0.5';
expandBtn.style.pointerEvents = 'none';
selectSection.classList.remove('open');
expandBtn.classList.remove('active');
directSuccess.style.display = 'block';
selectedPosts = [];
updateInputs();
document.getElementById('ig-selected-posts-input').value = link;
var qty = getSelectedQuantity();
document.getElementById('ig-quantity-input').value = qty.toString();
document.getElementById('ig-per-post-input').value = qty.toString();
window.__igWidgetData = {
selected_posts: link,
total_quantity: qty.toString(),
per_post: qty.toString()
};
} else {
directLink = '';
directSection.classList.remove('active');
expandBtn.style.opacity = '1';
expandBtn.style.pointerEvents = 'auto';
directSuccess.style.display = 'none';
if (selectedPosts.length === 0) {
document.getElementById('ig-selected-posts-input').value = '';
document.getElementById('ig-quantity-input').value = '';
document.getElementById('ig-per-post-input').value = '';
window.__igWidgetData = null;
}
}
}
function updateMaxPosts() {
var qty = getSelectedQuantity();
var maxPosts = qty > 0 ? Math.floor(qty / currentProduct.minPerPost) : 0;
document.getElementById('ig-max-posts').textContent = maxPosts;
return maxPosts;
}
updateMaxPosts();
var variantSelect = document.querySelector('select[name="id"], .product__variants select, select.single-option-selector, .product-form__option select');
if (variantSelect) {
variantSelect.addEventListener('change', function() {
updateMaxPosts();
if (allPosts.length > 0) renderPosts();
updateInputs();
handleDirectLinkChange();
});
}
var addToCartBtn = document.querySelector('button[type="submit"], .product__add-to-cart, button[name="add"], .product-form__submit');
if (addToCartBtn) {
addToCartBtn.addEventListener('click', function(e) {
var hasDirectLink = directLink.length > 0 && directLink.includes('instagram.com');
var hasSelectedPosts = selectedPosts.length > 0;
if (!hasDirectLink && !hasSelectedPosts) {
e.preventDefault();
e.stopPropagation();
alert('يرجى إضافة رابط المنشور أو اختيار منشورات من الأداة أولاً');
var widget = document.getElementById('instagram-post-selector');
if (widget) {
widget.scrollIntoView({ behavior: 'smooth', block: 'center' });
widget.style.boxShadow = '0 0 20px rgba(225,48,108,0.5)';
setTimeout(function() { widget.style.boxShadow = 'none'; }, 2000);
}
return false;
}
}, true);
}
document.getElementById('ig-load-btn').addEventListener('click', loadPosts);
document.getElementById('ig-load-more').addEventListener('click', function() {
visibleCount += LOAD_MORE_COUNT;
renderPosts();
});
document.getElementById('ig-cancel-btn').addEventListener('click', function() {
allPosts = [];
selectedPosts = [];
visibleCount = INITIAL_POSTS;
document.getElementById('ig-input-section').style.display = 'block';
document.getElementById('ig-grid').style.display = 'none';
document.getElementById('ig-grid').innerHTML = '';
document.getElementById('ig-selection-info').style.display = 'none';
document.getElementById('ig-warning').style.display = 'none';
document.getElementById('ig-load-more').style.display = 'none';
document.getElementById('ig-select-success').style.display = 'none';
document.getElementById('ig-username').value = '';
document.getElementById('ig-note-text').textContent = 'أدخل اسم المستخدم واختر المنشورات لتوزيع ال' + currentProduct.label + ' عليها';
document.getElementById('ig-cancel-btn').style.display = 'none';
document.getElementById('ig-direct-section').style.opacity = '1';
document.getElementById('ig-direct-section').style.pointerEvents = 'auto';
document.getElementById('ig-selected-posts-input').value = '';
document.getElementById('ig-quantity-input').value = '';
document.getElementById('ig-per-post-input').value = '';
window.__igWidgetData = null;
});
function loadPosts() {
var username = document.getElementById('ig-username').value.trim().replace('@', '');
if (!username) {
showError('الرجاء إدخال اسم المستخدم');
return;
}
hideError();
document.getElementById('ig-loading').style.display = 'block';
document.getElementById('ig-load-btn').disabled = true;
fetch(API_BASE + '/api/public/instagram/posts?username=' + encodeURIComponent(username))
.then(function(response) { return response.json(); })
.then(function(data) {
document.getElementById('ig-loading').style.display = 'none';
document.getElementById('ig-load-btn').disabled = false;
if (data.isPrivate || data.errorCode === 'PRIVATE_PROFILE') {
showError('الحساب خاص! يجب تحويل الحساب إلى عام لتتمكن من تحميل البوستات');
return;
}
if (data.success && data.posts && data.posts.length > 0) {
allPosts = data.posts;
visibleCount = INITIAL_POSTS;
selectedPosts = [];
document.getElementById('ig-input-section').style.display = 'none';
document.getElementById('ig-grid').style.display = 'grid';
document.getElementById('ig-selection-info').style.display = 'block';
document.getElementById('ig-note-text').textContent = 'اختر المنشورات التي تريد توزيع ال' + currentProduct.label + ' عليها';
document.getElementById('ig-cancel-btn').style.display = 'inline-block';
document.getElementById('ig-direct-section').style.opacity = '0.5';
document.getElementById('ig-direct-section').style.pointerEvents = 'none';
renderPosts();
} else {
showError(data.error || 'لم يتم العثور على منشورات');
}
})
.catch(function() {
document.getElementById('ig-loading').style.display = 'none';
document.getElementById('ig-load-btn').disabled = false;
showError('خطأ في تحميل المنشورات - حاول مرة أخرى');
});
}
function renderPosts() {
var grid = document.getElementById('ig-grid');
var maxPosts = updateMaxPosts();
var atLimit = selectedPosts.length >= maxPosts;
grid.innerHTML = '';
var postsToShow = allPosts.slice(0, visibleCount);
postsToShow.forEach(function(post) {
var isSelected = selectedPosts.indexOf(post.url) > -1;
var isDisabled = !isSelected && atLimit;
var div = document.createElement('div');
div.className = 'ig-post' + (isSelected ? ' selected' : '') + (isDisabled ? ' disabled' : '');
div.dataset.url = post.url;
div.innerHTML = '