/* Add custom Js code below */ /* 1) عنوان المدونة */ document.addEventListener("DOMContentLoaded", function () { let pageTitle = document.querySelector("h1.text-xl.pe-3.mb-1.da-tm"); if (pageTitle && pageTitle.textContent.trim() === "المدونة") { pageTitle.textContent = "برامج"; let dateElements = document.querySelectorAll(".text-gray-500.da-ts.text-xs"); dateElements.forEach(function (date) { date.style.display = "none"; }); } }); /* 2) زر أضف للسلة في صفحة الباقات */ document.addEventListener("click", function (e) { var b = e.target.closest(".tk-landing [data-pid]"); if (!b) return; var id = Number(b.dataset.pid); if (!id) { alert("ID المنتج غير صحيح"); return; } if (!(window.salla && window.salla.cart)) { alert("مكتبة سلة لسه ما اتحمّلتش، جرّب تاني"); return; } b.disabled = true; salla.onReady(function () { salla.cart .addItem({ id: id, quantity: 1 }) .then(function (r) { console.log("added", r); location.href = "/p/tkamul-subscribe"; }) .catch(function (err) { console.error("addItem failed", err); var m = err && err.response && err.response.data && err.response.data.error && err.response.data.error.message; alert("تعذّر الإضافة: " + (m || "راجع الـ Console")); b.disabled = false; }); }); }); /* 3) ملخص السلة في صفحة الاشتراك */ (function () { if (location.pathname.indexOf("tkamul-subscribe") === -1) return; var $ = function (s) { return document.querySelector(s); }; var esc = function (s) { var d = document.createElement("div"); d.textContent = s == null ? "" : s; return d.innerHTML; }; var num = function (v) { if (v && typeof v === "object") v = v.amount != null ? v.amount : v.value; return v == null || v === "" ? null : Number(v); }; var money = function (v) { v = num(v); return v == null || isNaN(v) ? "—" : v.toLocaleString("en-US", { maximumFractionDigits: 2 }) + " ر.س"; }; var setText = function (s, v) { var el = $(s); if (el) el.textContent = v; }; var show = function (s, on) { var el = $(s); if (el) el.hidden = !on; }; var pick = function (r) { return (r && r.data && (r.data.cart || r.data)) || r || {}; }; function whenReady(cb) { if ($("#tkd-items")) return cb(); var obs = new MutationObserver(function () { if ($("#tkd-items")) { obs.disconnect(); cb(); } }); obs.observe(document.body, { childList: true, subtree: true }); } function showEmpty() { show("#tkd-loading", false); show("#tkd-details", false); show("#tkd-empty", true); var link = $("[data-packages]"); var mt = $("#tkd-m-title"); if (mt) mt.innerHTML = 'سلتك فاضية · اختر باقتك'; setText("#tkd-m-sub", ""); document.querySelectorAll(".tkd-total-m").forEach(function (e) { e.textContent = "—"; }); } function render(cart) { var items = (cart && cart.items) || []; if (!items.length) return showEmpty(); $("#tkd-items").innerHTML = items.map(function (i) { var img = i.product_image || (i.image && i.image.url) || (typeof i.image === "string" && i.image) || i.thumbnail || ""; var line = i.amounts && i.amounts.total_price != null ? i.amounts.total_price : (i.total != null ? i.total : i.price); return '
' + (img ? '' : "") + '
' + esc(i.product_name || i.name) + '
الكمية: ' + esc(i.quantity) + '
' + '' + money(line) + '
'; }).join(""); var total = cart.total != null ? cart.total : cart.sub_total; setText("#tkd-sub", money(cart.sub_total != null ? cart.sub_total : total)); setText("#tkd-tax", cart.tax_amount != null ? money(cart.tax_amount) : "شامل الضريبة"); var ship = num(cart.real_shipping_cost != null ? cart.real_shipping_cost : cart.shipping_cost); setText("#tkd-ship", ship ? money(ship) : "مجاني"); setText("#tkd-total", money(total)); document.querySelectorAll(".tkd-total-m").forEach(function (e) { e.textContent = money(total); }); setText("#tkd-m-title", items.length > 1 ? items.length + " منتجات في السلة" : (items[0].product_name || items[0].name)); setText("#tkd-m-sub", "الإجمالي " + money(total)); show("#tkd-loading", false); show("#tkd-empty", false); show("#tkd-details", true); } var done = false; function finish(cart) { if (done) return; done = true; whenReady(function () { render(cart); }); } function load() { setTimeout(function () { finish({}); }, 6000); if (!(window.salla && window.salla.cart)) return finish({}); salla.cart.latest().then(pick).then(function (c) { if (!c || !c.id) return {}; if (c.items && c.items.length) return c; return salla.cart.details(c.id).then(pick); }).then(finish).catch(function (e) { console.warn("tkd failed:", e && (e.name || e.message)); finish({}); }); } if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", load); else load(); })(); /* 4) أيقونة السلة تروح لصفحة السلة المخصصة */ (function () { var MY_CART = "/p/tkamul-subscribe"; document.addEventListener("click", function (e) { var el = e.target.closest('a[href$="/cart"], a[href*="/cart?"], salla-cart-summary, .s-cart-summary-wrapper'); if (!el) return; e.preventDefault(); e.stopPropagation(); location.href = MY_CART; }, true); })();