87 lines
2.6 KiB
JavaScript
87 lines
2.6 KiB
JavaScript
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 =
|
||
'<div style="flex:1;min-width:0;padding-right:0.5rem">' +
|
||
'<div class="name">' +
|
||
name +
|
||
"</div>" +
|
||
'<div class="detail">' +
|
||
price +
|
||
' PLN × <span class="qty">1</span></div>' +
|
||
"</div>" +
|
||
'<div class="price">' +
|
||
price +
|
||
" PLN</div>" +
|
||
'<button type="button" class="remove" onclick="removeCake(this)">' +
|
||
'<span class="material-symbols-outlined">close</span>' +
|
||
"</button>" +
|
||
'<input type="hidden" name="cake[' +
|
||
id +
|
||
']" value="1">';
|
||
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";
|
||
});
|
||
}
|