const catalog = [
{ id: 1, name: "البرمجة من الصفر", price: 25 },
{ id: 2, name: "كوب بِت", price: 8 },
{ id: 3, name: "ملصق بِت", price: 3 },
{ id: 4, name: "قميص بِت", price: 15 },
];
const cart = [];
// ما كتبه القارئ حتى الآن.
let searchText = "";
// ارسم بطاقة لكل منتج
function renderProducts() {
const container = document.querySelector("#products");
container.replaceChildren();
// المنتجات التي يحتوي اسمها النص فقط، دون اعتبار لحالة الأحرف.
const visible = catalog.filter((p) => p.name.toLowerCase().includes(searchText.toLowerCase()));
if (visible.length === 0) {
container.textContent = "لا توجد منتجات مطابقة.";
}
for (const product of visible) {
const card = document.createElement("article");
card.className = "card";
const name = document.createElement("h3");
name.textContent = product.name;
const price = document.createElement("p");
price.textContent = `$${product.price}`;
const button = document.createElement("button");
button.textContent = "أضف إلى السلة";
button.addEventListener("click", () => addToCart(product.id));
card.append(name, price, button);
container.append(card);
}
}
function addToCart(productId) {
const line = cart.find((item) => item.productId === productId);
if (line) {
line.quantity++;
} else {
cart.push({ productId, quantity: 1 });
}
renderCart();
}
function removeFromCart(productId) {
const i = cart.findIndex((item) => item.productId === productId);
if (i !== -1) {
cart.splice(i, 1);
}
renderCart();
}
// أعد رسم السلة من البيانات كلما تغيرت
function renderCart() {
const list = document.querySelector("#cart-lines");
list.replaceChildren();
let total = 0;
for (const item of cart) {
const product = catalog.find((p) => p.id === item.productId);
const lineTotal = product.price * item.quantity;
total += lineTotal;
const li = document.createElement("li");
li.textContent = `${product.name} × ${item.quantity}`;
const amount = document.createElement("span");
amount.textContent = `$${lineTotal}`;
// زر حذف في كل سطر. يغيّر البيانات، ثم يعيد الرسم.
const remove = document.createElement("button");
remove.className = "remove";
remove.textContent = "احذف";
remove.setAttribute("aria-label", `احذف ${product.name}`);
remove.addEventListener("click", () => removeFromCart(product.id));
li.append(amount, remove);
list.append(li);
}
document.querySelector("#cart-total").textContent =
cart.length === 0 ? "سلتك فارغة." : `المجموع: $${total}`;
}
document.querySelector("#search").addEventListener("input", (event) => {
searchText = event.target.value.trim();
renderProducts();
});
renderProducts();
renderCart();