function changeCake(id, name, price, delta) {
var input = document.querySelector('[name="cake[' + id + ']"]');
if (input != null) {
var qty = Number(input.value) + delta;
if (qty <= 0) {
var item = input.closest("li");
if (item) item.remove();
updateTotals();
updateCatalogueQty(id, 0);
return;
}
input.value = qty;
var item = input.closest("li");
item.querySelector(".qty").innerText = qty;
item.querySelector(".price").innerText = price * qty + " PLN";
updateTotals();
updateCatalogueQty(id, qty);
return;
}
if (delta <= 0) return;
var item = document.createElement("li");
item.setAttribute("data-cake-id", id);
item.innerHTML =
'
' +
'
' +
name +
"
" +
'
' +
price +
' PLN × 1
' +
"
" +
'' +
price +
" PLN
" +
'" +
'';
document.getElementById("basket-items").appendChild(item);
updateTotals();
updateCatalogueQty(id, 1);
}
function removeCake(btn) {
var item = btn.closest("li");
var id = item.getAttribute("data-cake-id");
item.remove();
updateTotals();
updateCatalogueQty(id, 0);
}
var subtotal = 0;
function updatePrepaid() {
var paid = Number(document.querySelector('[name="paid"]')?.value || 0);
document.getElementById("paid-amount").innerText = paid + " PLN";
document.getElementById("total-price").innerText = subtotal - paid + " PLN";
}
function updateTotals() {
subtotal = 0;
var items = document.querySelectorAll("#basket-items li");
items.forEach(function (item) {
var priceText = item.querySelector(".detail").innerText;
var price = Number(priceText.split(" ")[0]);
var qty = Number(item.querySelector(".qty").innerText);
subtotal += price * qty;
});
document.getElementById("subtotal-price").innerText = subtotal + " PLN";
updatePrepaid();
}
function updateCatalogueQty(id, qty) {
var el = document.getElementById("qty-" + id);
if (el) el.innerText = qty;
}
function filterCatalogue(input) {
var query = input.value.toLowerCase();
var items = document.querySelectorAll("#catalogue-grid > li");
items.forEach(function (item) {
var name = item.getAttribute("data-name").toLowerCase();
item.style.display = name.includes(query) ? "" : "none";
});
}