const API_URL = "https://raw.githubusercontent.com/sweko/uacs-internet-programming-exams/main/dry-run-mid-term-2024/data/authors.json"; let authors = []; let currentSort = { key: null, direction: 'asc' }; document.addEventListener("DOMContentLoaded", async () => { await fetchAuthors(); initFilters(); renderTable(authors); }); async function fetchAuthors() { try { const res = await fetch(API_URL); authors = await res.json(); console.log(authors); console.log(authors); } catch (err) { console.error("Error fetching authors:", err); } } function initFilters() { const nationalitySet = new Set(authors.map(a => a.nationality)); const select = document.getElementById("nationality-search"); nationalitySet.forEach(n => { const opt = document.createElement("option"); opt.value = n; opt.textContent = n; select.appendChild(opt); }); document.querySelectorAll("#name-search, #nationality-search, #alive-search, #year-search") .forEach(el => el.addEventListener("input", applyFilters)); document.getElementById("clearFilters").addEventListener("click", () => { document.getElementById("name-search").value = ""; document.getElementById("nationality-search").value = ""; document.getElementById("alive-search").checked = false; document.getElementById("year-search").value = ""; renderTable(authors); }); document.querySelectorAll("th[data-sort]").forEach(th => { th.addEventListener("click", () => sortTable(th.dataset.sort)); }); document.getElementById("close-modal").onclick = () => { document.getElementById("booksModal").style.display = "none"; }; } function applyFilters() { let filtered = [...authors]; const name = document.getElementById("name-search").value.toLowerCase(); const nationality = document.getElementById("nationality-search").value; const alive = document.getElementById("alive-search").checked; const year = parseInt(document.getElementById("year-search").value); filtered = filtered.filter(a => { const isAlive = !a.death_date; const { start, end } = getYearsActive(a); return (!name || a.name.toLowerCase().includes(name)) && (!nationality || a.nationality === nationality) && (!alive || isAlive) && (!year || (start <= year && year <= end)); }); renderTable(filtered); } function renderTable(data) { const tbody = document.querySelector("#authorsTable tbody"); tbody.innerHTML = ""; data.forEach(a => { const row = document.createElement("tr"); const isAlive = !a.death_date; const age = calculateAge(a); const { start, end } = getYearsActive(a); const yearsActive = `${start} - ${end}`; const books = a.bibliography || []; const bibliographyCount = books.length; const summary = getBibliographySummary(books); row.innerHTML = `