/* ============================================ admin.js - لوحة تحكم طلباتك بلس (نسخة آمنة بدون كلمة مرور ثابتة) ============================================ */ // تحديد رابط API بناءً على البيئة const API_BASE = (() => { const host = window.location.hostname; if ( host === "localhost" || host === "127.0.0.1" || host.startsWith("192.168.") ) { return `http://147.93.123.146:3003/api`; } return "/api"; })(); // --- دالة تسجيل الدخول عبر الخادم --- async function checkAuth() { const password = document.getElementById("adminPassword").value; try { const res = await fetch(API_BASE + "/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password }), }); const data = await res.json(); if (data.success) { sessionStorage.setItem("adminPass", password); document.getElementById("loginSection").classList.add("hidden"); document.getElementById("adminContent").classList.remove("hidden"); loadSettings(); // تحميل الإعدادات بعد الدخول } else { alert("❌ " + (data.message || "كلمة المرور غير صحيحة")); } } catch (err) { alert("❌ فشل الاتصال بالخادم. تأكد من تشغيل السيرفر."); } } function logout() { document.getElementById("loginSection").classList.remove("hidden"); document.getElementById("adminContent").classList.add("hidden"); document.getElementById("adminPassword").value = ""; } // --- تحميل الإعدادات من الخادم --- async function loadSettings() { try { const res = await fetch(API_BASE + "/settings"); if (!res.ok) throw new Error("فشل تحميل الإعدادات"); const data = await res.json(); document.getElementById("whatsappNumber").value = data.whatsappNumber || ""; document.getElementById("facebookUrl").value = data.facebookUrl || ""; document.getElementById("instagramUrl").value = data.instagramUrl || ""; document.getElementById("whatsappGroupUrl").value = data.whatsappGroupUrl || ""; document.getElementById("appStoreUrl").value = data.appStoreUrl || ""; document.getElementById("googlePlayUrl").value = data.googlePlayUrl || ""; document.getElementById("apkUrl").value = data.apkUrl || ""; if (data.telegramChatIds && Array.isArray(data.telegramChatIds)) { document.getElementById("telegramChatIds").value = data.telegramChatIds.join("\n"); } renderVideos(data.videos || []); } catch (error) { console.error("خطأ في تحميل الإعدادات:", error); alert("تعذر تحميل الإعدادات. تأكد من تشغيل الخادم."); } } // --- عرض الفيديوهات --- function renderVideos(videos) { const container = document.getElementById("videosList"); container.innerHTML = ""; videos.forEach((url, index) => { const videoId = extractYouTubeVideoId(url); const thumbnail = videoId ? `https://img.youtube.com/vi/${videoId}/mqdefault.jpg` : ""; const card = document.createElement("div"); card.className = "bg-zinc-100 p-4 rounded-2xl relative"; card.innerHTML = ` فيديو

${url}

`; container.appendChild(card); }); } function extractYouTubeVideoId(url) { const patterns = [ /youtube\.com\/watch\?v=([^&]+)/, /youtu\.be\/([^?]+)/, /youtube\.com\/embed\/([^?]+)/, ]; for (let pattern of patterns) { const match = url.match(pattern); if (match && match[1]) return match[1]; } return null; } // --- إضافة فيديو جديد --- async function addVideo() { const url = document.getElementById("newVideoUrl").value.trim(); if (!url) { alert("الرجاء إدخال رابط الفيديو"); return; } try { const res = await fetch(API_BASE + "/settings"); const data = await res.json(); const videos = data.videos || []; videos.push(url); const updateRes = await fetch(API_BASE + "/settings", { method: "POST", headers: { "Content-Type": "application/json", "x-admin-password": sessionStorage.getItem("adminPass") || "", }, body: JSON.stringify({ videos }), }); if (updateRes.ok) { document.getElementById("newVideoUrl").value = ""; loadSettings(); // إعادة تحميل القائمة alert("✅ تمت إضافة الفيديو بنجاح"); } else { alert("❌ فشل إضافة الفيديو"); } } catch (error) { console.error(error); alert("❌ خطأ في الاتصال بالخادم"); } } // --- حذف فيديو --- async function deleteVideo(index) { if (!confirm("هل أنت متأكد من حذف هذا الفيديو؟")) return; try { const res = await fetch(API_BASE + "/settings"); const data = await res.json(); const videos = data.videos || []; videos.splice(index, 1); const updateRes = await fetch(API_BASE + "/settings", { method: "POST", headers: { "Content-Type": "application/json", "x-admin-password": sessionStorage.getItem("adminPass") || "", }, body: JSON.stringify({ videos }), }); if (updateRes.ok) { loadSettings(); alert("✅ تم الحذف"); } else { alert("❌ فشل الحذف"); } } catch (error) { console.error(error); alert("❌ خطأ في الاتصال"); } } // --- دوال تحديث الحقول الفردية --- async function updateWhatsApp() { const value = document.getElementById("whatsappNumber").value.trim(); await updateField("whatsappNumber", value); } async function updateFacebook() { const value = document.getElementById("facebookUrl").value.trim(); await updateField("facebookUrl", value); } async function updateInstagram() { const value = document.getElementById("instagramUrl").value.trim(); await updateField("instagramUrl", value); } async function updateWhatsAppGroup() { const value = document.getElementById("whatsappGroupUrl").value.trim(); await updateField("whatsappGroupUrl", value); } async function updateAppStore() { const value = document.getElementById("appStoreUrl").value.trim(); await updateField("appStoreUrl", value); } async function updateGooglePlay() { const value = document.getElementById("googlePlayUrl").value.trim(); await updateField("googlePlayUrl", value); } async function updateApk() { const value = document.getElementById("apkUrl").value.trim(); await updateField("apkUrl", value); } async function updateTelegramChatIds() { const text = document.getElementById("telegramChatIds").value.trim(); const ids = text .split("\n") .map((line) => line.trim()) .filter((line) => line !== ""); await updateField("telegramChatIds", ids); } // دالة عامة لتحديث حقل واحد async function updateField(key, value) { const password = document.getElementById("adminPassword").value || sessionStorage.getItem("adminPass") || ""; try { const res = await fetch(API_BASE + "/settings", { method: "POST", headers: { "Content-Type": "application/json", "x-admin-password": password, }, body: JSON.stringify({ [key]: value }), }); if (res.ok) { alert("✅ تم الحفظ بنجاح"); } else { const err = await res.json(); alert("❌ فشل الحفظ: " + (err.message || "خطأ غير معروف")); } } catch (error) { console.error(error); alert("❌ خطأ في الاتصال بالخادم"); } } // ربط الدوال بالكائن window window.checkAuth = checkAuth; window.logout = logout; window.addVideo = addVideo; window.deleteVideo = deleteVideo; window.updateWhatsApp = updateWhatsApp; window.updateFacebook = updateFacebook; window.updateInstagram = updateInstagram; window.updateAppStore = updateAppStore; window.updateGooglePlay = updateGooglePlay; window.updateApk = updateApk; window.updateTelegramChatIds = updateTelegramChatIds; window.updateWhatsAppGroup = updateWhatsAppGroup;