diff --git a/static/js/expense_table.js b/static/js/expense_table.js index 35de2ba..47147d1 100644 --- a/static/js/expense_table.js +++ b/static/js/expense_table.js @@ -4,6 +4,18 @@ document.addEventListener('DOMContentLoaded', () => { const filterButtons = document.querySelectorAll('.range-btn'); const rows = document.querySelectorAll('#listsTableBody tr'); const categoryButtons = document.querySelectorAll('.category-filter'); + const applyCustomBtn = document.getElementById('applyCustomRange'); + const customStartInput = document.getElementById('customStart'); + const customEndInput = document.getElementById('customEnd'); + + if (customStartInput && customEndInput) { + const now = new Date(); + const y = now.getFullYear(); + const m = String(now.getMonth() + 1).padStart(2, '0'); + const d = String(now.getDate()).padStart(2, '0'); + customStartInput.value = `${y}-${m}-01`; + customEndInput.value = `${y}-${m}-${d}`; + } window.selectedCategoryId = ""; let initialLoad = true; @@ -34,10 +46,8 @@ document.addEventListener('DOMContentLoaded', () => { const year = now.getFullYear(); const month = now.toISOString().slice(0, 7); const week = `${year}-${String(getISOWeek(now)).padStart(2, '0')}`; - let startDate = null; let endDate = null; - if (range === 'last30days') { endDate = now; startDate = new Date(); @@ -47,14 +57,12 @@ document.addEventListener('DOMContentLoaded', () => { startDate = new Date(year, now.getMonth(), 1); endDate = now; } - rows.forEach(row => { const rDate = row.dataset.date; const rMonth = row.dataset.month; const rWeek = row.dataset.week; const rYear = row.dataset.year; const rowDateObj = new Date(rDate); - let show = true; if (range === 'day') show = rDate === todayStr; else if (range === 'month') show = rMonth === month; @@ -63,7 +71,6 @@ document.addEventListener('DOMContentLoaded', () => { else if (range === 'all') show = true; else if (range === 'last30days') show = rowDateObj >= startDate && rowDateObj <= endDate; else if (range === 'currentmonth') show = rowDateObj >= startDate && rowDateObj <= endDate; - row.style.display = show ? '' : 'none'; }); } @@ -81,36 +88,36 @@ document.addEventListener('DOMContentLoaded', () => { function applyCategoryFilter() { if (!window.selectedCategoryId) return; - rows.forEach(row => { const categoriesStr = row.dataset.categories || ""; const categories = categoriesStr ? categoriesStr.split(",") : []; - if (window.selectedCategoryId === "none") { - // Bez kategorii - if (categoriesStr.trim() !== "") { - row.style.display = 'none'; - } + if (categoriesStr.trim() !== "") row.style.display = 'none'; } else { - // Normalne filtrowanie po ID kategorii - if (!categories.includes(String(window.selectedCategoryId))) { - row.style.display = 'none'; - } + if (!categories.includes(String(window.selectedCategoryId))) row.style.display = 'none'; } }); } - // Obsługa checkboxów wierszy + function filterByCustomRange(startStr, endStr) { + const start = new Date(startStr); + const end = new Date(endStr); + if (isNaN(start) || isNaN(end)) return; + end.setHours(23, 59, 59, 999); + rows.forEach(row => { + const rowDateObj = new Date(row.dataset.date); + const show = rowDateObj >= start && rowDateObj <= end; + row.style.display = show ? '' : 'none'; + }); + } + checkboxes.forEach(cb => cb.addEventListener('change', updateTotal)); - // Obsługa przycisków zakresu filterButtons.forEach(btn => { btn.addEventListener('click', () => { - initialLoad = false; // po kliknięciu wyłączamy tryb startowy - + initialLoad = false; filterButtons.forEach(b => b.classList.remove('active')); btn.classList.add('active'); - const range = btn.dataset.range; filterByRange(range); applyExpenseFilter(); @@ -119,29 +126,22 @@ document.addEventListener('DOMContentLoaded', () => { }); }); - // Obsługa kliknięcia w kategorię categoryButtons.forEach(btn => { btn.addEventListener('click', () => { categoryButtons.forEach(b => b.classList.remove('btn-success', 'active')); categoryButtons.forEach(b => b.classList.add('btn-outline-light')); btn.classList.remove('btn-outline-light'); btn.classList.add('btn-success', 'active'); - window.selectedCategoryId = btn.dataset.categoryId || ""; - if (initialLoad) { filterByLast30Days(); } else { const activeRange = document.querySelector('.range-btn.active'); - if (activeRange) { - filterByRange(activeRange.dataset.range); - } + if (activeRange) filterByRange(activeRange.dataset.range); } - applyExpenseFilter(); applyCategoryFilter(); updateTotal(); - const chartTab = document.querySelector('#chart-tab'); if (chartTab && chartTab.classList.contains('active') && typeof window.loadExpenses === 'function') { window.loadExpenses(); @@ -149,7 +149,23 @@ document.addEventListener('DOMContentLoaded', () => { }); }); - // Start – domyślnie ostatnie 30 dni + if (applyCustomBtn) { + applyCustomBtn.addEventListener('click', () => { + const startStr = customStartInput?.value; + const endStr = customEndInput?.value; + if (!startStr || !endStr) { + alert('Proszę wybrać obie daty!'); + return; + } + initialLoad = false; + document.querySelectorAll('.range-btn').forEach(b => b.classList.remove('active')); + filterByCustomRange(startStr, endStr); + applyExpenseFilter(); + applyCategoryFilter(); + updateTotal(); + }); + } + filterByLast30Days(); applyExpenseFilter(); applyCategoryFilter();