const shoeList =
document.getElementById("shoe-list");
const searchInput =
document.getElementById("searchInput");
const cartItemsContainer =
document.getElementById("cart-items");
const shoes = [
{
name: "Reebok",
price: "$99.99",
image:
"https://media.geeksforgeeks.org/wp-content/uploads/20240301112151/shoes.png",
},
{
name: "Sketchers",
price: "$129.99",
image:
"https://media.geeksforgeeks.org/wp-content/uploads/20240301112151/sneakers.png",
},
{
name: "Adidas",
price: "$149.99",
image:
"https://media.geeksforgeeks.org/wp-content/uploads/20240301112150/running-shoe.png",
},
{
name: "Converse",
price: "$199.99",
image:
"https://media.geeksforgeeks.org/wp-content/uploads/20240301112149/sneakers-(1).png",
},
];
function renderShoes(shoes) {
shoeList.innerHTML = "";
shoes.forEach((shoe) => {
const shoeCard =
document.createElement("li");
shoeCard.classList.add(
"p-2",
);
const shoeLink =
document.createElement("a");
shoeLink.classList.add(
"block",
"bg-gray-200",
"rounded-lg",
"shadow-md",
"hover:bg-gray-300",
"transition-colors",
"duration-300"
);
shoeLink.href = "#";
const shoeImage =
document.createElement("img");
shoeImage.classList.
add("w-60", "h-60", "object-cover", "p-10");
shoeImage.src = `${shoe.image}`;
shoeImage.alt = shoe.name;
const shoeDetails =
document.createElement("div");
shoeDetails.classList.add("p-4");
const shoeTitle =
document.createElement("h3");
shoeTitle.classList.
add("text-lg", "font-bold", "mb-1");
shoeTitle.textContent = shoe.name;
const shoePrice =
document.createElement("p");
shoePrice.classList.
add("text-gray-700");
shoePrice.textContent = shoe.price;
const buyNowButton =
document.createElement("button");
buyNowButton.textContent = "Buy Now";
buyNowButton.classList.add(
"bg-blue-500",
"text-white",
"px-4",
"py-2",
"rounded-md",
"mt-2",
"hover:bg-blue-600",
"transition-colors",
"duration-300"
);
buyNowButton.addEventListener("click", () => {
addToCart(shoe);
updateTotalBillDisplay();
});
shoeDetails.appendChild(shoeTitle);
shoeDetails.appendChild(shoePrice);
shoeDetails.appendChild(buyNowButton);
shoeLink.appendChild(shoeImage);
shoeLink.appendChild(shoeDetails);
shoeCard.appendChild(shoeLink);
shoeList.appendChild(shoeCard);
shoeLink.addEventListener("mouseover", () => {
shoeLink.classList.add("scale-105");
});
shoeLink.addEventListener("mouseout", () => {
shoeLink.classList.remove("scale-105");
});
});
}
function addToCart(item) {
const existingCartItem =
Array.from(cartItemsContainer.children).
find((cartItem) => {
return cartItem.dataset.name === item.name;
}
);
if (existingCartItem) {
const quantityElement =
existingCartItem.querySelector(".quantity");
const quantity =
parseInt(quantityElement.textContent);
quantityElement.textContent = quantity + 1;
} else {
const cartItem =
document.createElement("li");
cartItem.textContent =
`${item.name} - ${item.price} x `;
cartItem.classList.add("cart-item");
cartItem.dataset.name = item.name;
const quantityElement =
document.createElement("span");
quantityElement.textContent = "1";
quantityElement.classList.
add("quantity");
cartItem.appendChild(quantityElement);
cartItemsContainer.appendChild(cartItem);
}
}
function calculateTotalBill() {
let totalBill = 0;
Array.from(cartItemsContainer.children).
forEach((cartItem) => {
const itemName = cartItem.textContent;
// Extract the price from the item name
const itemPrice =
parseFloat(
itemName.split(" - ")[1].replace("$", "")
);
const quantity = parseInt(
cartItem.querySelector(".quantity").textContent
);
totalBill += itemPrice * quantity;
});
return totalBill.toFixed(2);
}
function updateTotalBillDisplay() {
const totalBillElement =
document.getElementById("total-bill");
const totalBill = calculateTotalBill();
totalBillElement.textContent = `$${totalBill}`;
}
renderShoes(shoes);
searchInput.addEventListener("input", filterShoes);
function filterShoes() {
const query = searchInput.value.toLowerCase();
const filteredShoes = shoes.filter((shoe) =>
shoe.name.toLowerCase().includes(query)
);
renderShoes(filteredShoes);
searchInput.value = query;
}
updateTotalBillDisplay();