document.addEventListener("DOMContentLoaded", function () { var isEnglish = window.location.pathname.indexOf("/en") !== -1; var isMobile = window.innerWidth <= 768; var t = { title: isEnglish ? "Jothrah Smart Assistant" : "مساعد جذرة الذكي", status: isEnglish ? "Online now" : "متصل الآن", welcome: isEnglish ? "Hello 👋
I am Jothrah Smart Assistant. How can I help you today?" : "أهلاً بك 👋
أنا مساعد جذرة الذكي، كيف أقدر أخدمك اليوم؟", quick1: isEnglish ? "Active ingredient inquiry" : "الاستعلام عن مادة فعالة", quick2: isEnglish ? "Crop pest diagnosis" : "تشخيص آفة زراعية", quick3: isEnglish ? "Suggest suitable pesticide" : "اقتراح مبيد مناسب", quick4: isEnglish ? "Open smart assistant" : "الدخول إلى البوت الذكي", contactText: isEnglish ? "You can also choose your preferred contact method:" : "يمكنك أيضًا اختيار وسيلة التواصل المناسبة:", whatsapp: isEnglish ? "WhatsApp" : "واتساب", telegram: isEnglish ? "Telegram" : "تلجرام", bot: isEnglish ? "Smart Assistant" : "المساعد الذكي", note: isEnglish ? 'For the full conversation with the assistant, click "Smart Assistant".' : 'للمحادثة الكاملة مع المساعد، اضغط على "المساعد الذكي".', placeholder: isEnglish ? "Write your question here..." : "اكتب سؤالك هنا...", send: isEnglish ? "Send" : "إرسال", botLinkReply: isEnglish ? 'For the full conversation with the smart assistant, click this link.' : 'للمحادثة الكاملة مع المساعد الذكي، اضغط على هذا الرابط.', waReply: isEnglish ? 'To contact us via WhatsApp, click here.' : 'للتواصل عبر واتساب اضغط هنا.', genericReply: isEnglish ? 'I can quickly guide you to the smart assistant, WhatsApp, or Telegram. For the full conversation, click "Smart Assistant".' : 'يمكنني توجيهك سريعًا إلى المساعد الذكي أو واتساب أو تلجرام. للمحادثة الكاملة اضغط على "المساعد الذكي".', openBotReply: isEnglish ? 'Click here to start chatting with the smart assistant: Open bot' : 'اضغط هنا لبدء المحادثة مع المساعد الذكي: فتح البوت', quickReply: isEnglish ? 'To continue the request fully, click "Smart Assistant" from the options above.' : 'لفتح المساعد الذكي ومتابعة الطلب بشكل كامل، اضغط على "المساعد الذكي" من الخيارات أعلاه.', ariaOpen: isEnglish ? "Open chat" : "فتح المحادثة", ariaClose: isEnglish ? "Close" : "إغلاق", dir: isEnglish ? "ltr" : "rtl", align: isEnglish ? "left" : "right" }; document.body.insertAdjacentHTML("beforeend", `
🌿
${t.title}
${t.status}
${t.welcome}
${t.contactText}
${t.note}
`); var toggle = document.getElementById("jthChatToggle"); var closeBtn = document.getElementById("jthChatClose"); var panel = document.getElementById("jthChatPanel"); var input = document.getElementById("jthChatInput"); var sendBtn = document.getElementById("jthSendBtn"); var body = document.querySelector(".jth-chat-body"); var quickBtns = document.querySelectorAll(".jth-quick-btn"); function openPanel() { panel.classList.add("show"); toggle.classList.add("attention-stop"); } function closePanel() { panel.classList.remove("show"); } function addUserMessage(text) { if (!text.trim()) return; var div = document.createElement("div"); div.className = "jth-msg user"; div.textContent = text; body.appendChild(div); body.scrollTop = body.scrollHeight; } function addBotMessage(text) { var div = document.createElement("div"); div.className = "jth-msg bot"; div.innerHTML = text; body.appendChild(div); body.scrollTop = body.scrollHeight; } function handleSend() { var text = input.value.trim(); if (!text) return; addUserMessage(text); input.value = ""; var lower = text.toLowerCase(); if ( text.includes("بوت") || text.includes("مساعد") || text.includes("ذكي") || lower.includes("bot") || lower.includes("assistant") || lower.includes("smart") ) { addBotMessage(t.botLinkReply); return; } if ( text.includes("واتساب") || lower.includes("whatsapp") ) { addBotMessage(t.waReply); return; } addBotMessage(t.genericReply); } if (toggle && panel) { toggle.addEventListener("click", function () { panel.classList.toggle("show"); toggle.classList.add("attention-stop"); }); } if (closeBtn) { closeBtn.addEventListener("click", closePanel); } if (sendBtn) { sendBtn.addEventListener("click", handleSend); } if (input) { input.addEventListener("keydown", function (e) { if (e.key === "Enter") handleSend(); }); } quickBtns.forEach(function (btn) { btn.addEventListener("click", function () { var text = btn.getAttribute("data-text") || ""; addUserMessage(text); if (text === t.quick4) { addBotMessage(t.openBotReply); } else { addBotMessage(t.quickReply); } }); }); if (!isMobile) { setTimeout(function () { if (!sessionStorage.getItem("jth_chat_auto_opened")) { openPanel(); sessionStorage.setItem("jth_chat_auto_opened", "yes"); } }, 2500); } });