(function () { // 1. إنشاء الحاوية الرئيسية مع تعيين ID لسهولة التحكم const container = document.createElement("div"); container.id = "wa-chat-container"; // استخدام خطوط النظام لضمان وضوحها على الايفون والاندرويد + أعلى z-index ممكن container.style.cssText = "position: fixed; right: 20px; bottom: 20px; z-index: 2147483647; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; display: flex; flex-direction: column; align-items: flex-end;"; container.dir = "rtl"; // 2. إنشاء نافذة الدردشة (مخفية افتراضياً) const chatBox = document.createElement("div"); chatBox.id = "wa-chat-box"; // تم تحويل العرض ليكون متجاوباً (يأخذ 300 بكسل، وإذا كانت الشاشة صغيرة يأخذ عرض الشاشة ناقص الحواف) chatBox.style.cssText = "display: none; width: 320px; max-width: calc(100vw - 40px); box-sizing: border-box; background: #fff; border-radius: 12px; box-shadow: 0 10px 25px rgba(0,0,0,0.2); margin-bottom: 15px; overflow: hidden; transform: translateY(20px); opacity: 0; transition: all 0.3s ease;"; // تم تكبير مساحة الضغط لزر الإغلاق (padding) ليكون سهل اللمس في الجوال chatBox.innerHTML = `
فريق الدعم الرد خلال دقائق
مرحباً! 👋
كيف يمكننا مساعدتك اليوم؟
بدء المحادثة
`; // 3. إنشاء الزر العائم const btn = document.createElement("div"); btn.className = "wa-floating-btn"; btn.style.cssText = "width: 60px; height: 60px; background: #25D366; border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 4px 15px rgba(0,0,0,0.25); cursor: pointer; transition: transform 0.3s; animation: wa-pulse 2s infinite; touch-action: manipulation;"; btn.innerHTML = ` `; // 4. إضافة CSS متقدم لدعم توافق الجوالات const style = document.createElement("style"); style.innerHTML = ` @keyframes wa-pulse { 0% { box-shadow: 0 0 0 0 rgba(37,211,102,0.6); } 70% { box-shadow: 0 0 0 15px rgba(37,211,102,0); } 100% { box-shadow: 0 0 0 0 rgba(37,211,102,0); } } /* قواعد خاصة بالجوالات الشاشات الصغيرة لتصغير الحواف وضبط المقاسات */ @media (max-width: 480px) { #wa-chat-container { right: 15px !important; bottom: 15px !important; /* حماية الحواف في الايفون الحديث */ bottom: calc(15px + env(safe-area-inset-bottom)) !important; } #wa-chat-box { max-width: calc(100vw - 30px) !important; margin-bottom: 10px !important; } .wa-floating-btn { width: 55px !important; height: 55px !important; } .wa-floating-btn svg { width: 30px !important; height: 30px !important; } } `; document.head.appendChild(style); // 5. دمج العناصر في الصفحة container.appendChild(chatBox); container.appendChild(btn); document.body.appendChild(container); // 6. دالة الإظهار والإخفاء (الأنيميشن) let isOpen = false; function toggleChat(e) { if(e) e.preventDefault(); // لمنع السلوك الافتراضي الذي قد يسبب مشاكل باللمس if (!isOpen) { chatBox.style.display = "block"; // استخدام setTimeout لضمان تفعيل الـ transition setTimeout(() => { chatBox.style.opacity = "1"; chatBox.style.transform = "translateY(0)"; }, 10); } else { chatBox.style.opacity = "0"; chatBox.style.transform = "translateY(20px)"; setTimeout(() => { chatBox.style.display = "none"; }, 300); } isOpen = !isOpen; } // 7. تفعيل الأحداث (مراعاة اللمس على الجوال) btn.addEventListener("click", toggleChat); document.getElementById('wa-close-btn').addEventListener("click", toggleChat); })();