cake-order-tracker/server/static/order.js
2026-06-10 22:14:48 +02:00

74 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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('.summary-item');
if (item) item.remove();
updateTotals();
updateCatalogueQty(id, 0);
return;
}
input.value = qty;
var item = input.closest('.summary-item');
item.querySelector('.cake-qty').innerText = qty;
item.querySelector('.cake-total').innerText = (price * qty) + ' PLN';
updateTotals();
updateCatalogueQty(id, qty);
return;
}
if (delta <= 0) return;
var item = document.createElement('div');
item.className = 'summary-item';
item.setAttribute('data-cake-id', id);
item.innerHTML =
'<div class="summary-item-info">' +
'<div class="summary-item-name cake-name">' + name + '</div>' +
'<div class="summary-item-detail cake-price">' + price + ' PLN × <span class="cake-qty">1</span></div>' +
'</div>' +
'<div class="summary-item-price cake-total">' + price + ' PLN</div>' +
'<button type="button" class="summary-item-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('.summary-item');
var id = item.getAttribute('data-cake-id');
item.remove();
updateTotals();
updateCatalogueQty(id, 0);
}
function updateTotals() {
var subtotal = 0;
var items = document.querySelectorAll('#basket_items .summary-item');
items.forEach(function(item) {
var priceText = item.querySelector('.cake-price').innerText;
var price = Number(priceText.split(' ')[0]);
var qty = Number(item.querySelector('.cake-qty').innerText);
subtotal += price * qty;
});
var paid = Number(document.querySelector('[name="paid"]')?.value || 0);
document.getElementById('subtotal-price').innerText = subtotal + ' PLN';
document.getElementById('paid-amount').innerText = paid + ' PLN';
document.getElementById('total-price').innerText = (subtotal - paid) + ' PLN';
}
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-item');
items.forEach(function(item) {
var name = item.getAttribute('data-name').toLowerCase();
item.style.display = name.includes(query) ? '' : 'none';
});
}