(function () { const tbody = document.querySelector('#produkty-body'); const celInput = document.querySelector('#cel'); const box = document.querySelector('#celSyncBox'); const msg = document.querySelector('#celSyncMsg'); const btn = document.querySelector('#btnApplyCelFromSum'); if (!tbody || !celInput || !box || !msg || !btn) return; const EPS = 0.01; // tolerancja porównania function parsePrice(raw) { if (!raw) return NaN; const s = String(raw).trim().replace(/\s+/g, '').replace(',', '.'); const n = Number(s); return Number.isFinite(n) && n >= 0 ? n : NaN; } function getRows() { return Array.from(tbody.querySelectorAll('tr')); } function computeSum() { const rows = getRows(); let hasNamed = false; let sumAll = 0; // suma ze wszystkich wierszy z nazwą i poprawną ceną let sumToBuy = 0; // suma tylko z wierszy NIE oznaczonych jako "Kupione" for (const tr of rows) { const nameInput = tr.querySelector('input[name="item_nazwa[]"]'); const priceInput = tr.querySelector('input[name="item_cena[]"]'); const kupioneSwitch = tr.querySelector('.kupione-switch'); const name = nameInput ? nameInput.value.trim() : ''; if (!name) continue; // ignoruj puste wiersze bez nazwy hasNamed = true; const priceVal = priceInput ? parsePrice(priceInput.value) : NaN; if (Number.isNaN(priceVal)) continue; // zawsze dolicz do sumy wszystkich sumAll += priceVal; // do sumy do-kupienia tylko jeśli nie jest oznaczone jako kupione if (!(kupioneSwitch && kupioneSwitch.checked)) { sumToBuy += priceVal; } } return { hasNamed, sumAll, sumToBuy }; } function readCel() { const v = parsePrice(celInput.value); return Number.isNaN(v) ? null : v; } function formatPln(n) { // Nie narzucamy locale – prosto 2 miejsca return n.toFixed(2); } function updateUI() { const { hasNamed, sumAll, sumToBuy } = computeSum(); // Brak produktów (brak nazw) lub obie sumy = 0 → nic nie pokazuj if (!hasNamed || (sumAll <= 0 && sumToBuy <= 0)) { box.classList.add('d-none'); btn.classList.add('d-none'); box.classList.remove('alert-success', 'alert-info'); msg.textContent = ''; return; } const cel = readCel(); const target = sumToBuy; // porównujemy do kwoty POZOSTAŁE DO KUPIENIA // Jeśli cel nie ustawiony lub NaN → zaproponuj ustawienie celu = sumToBuy if (cel === null) { box.classList.remove('d-none'); box.classList.remove('alert-success'); box.classList.add('alert-info'); // pokazujemy obie sumy w komunikacie msg.innerHTML = `