document.addEventListener("DOMContentLoaded", function () { let allData = []; let filteredData = []; let currentKategori = "peraturan"; let currentPage = 1; const perPage = 10; function formatTanggal(dateStr) { return new Date(dateStr).toLocaleDateString("id-ID", { day: "numeric", month: "long", year: "numeric" }); } function loadRegulasi() { fetch('/WebNew/Regulasi/GetRegulasi') .then(res => res.json()) .then(data => { allData = data; filterData(); }); } function filterData() { const keyword = document.getElementById("search").value.toLowerCase(); const tahun = document.getElementById("tahun").value; filteredData = allData.filter(item => { const kategori = (item.kategori || "").trim().toLowerCase(); const judul = (item.judul || "").toLowerCase(); return ( kategori === currentKategori && judul.includes(keyword) && (tahun === "" || item.tahun == tahun) ); }); currentPage = 1; renderPagination(); } function renderPagination() { const start = (currentPage - 1) * perPage; const end = start + perPage; const paginatedData = filteredData.slice(start, end); renderData(paginatedData); renderPaginationControls(); renderPaginationInfo(start, end); } function renderData(data) { let html = ""; if (data.length === 0) { html = `
Data tidak ditemukan
`; } data.forEach((item, index) => { let lampiranHtml = ""; if (item.lampiran && item.lampiran.length > 0) { item.lampiran.forEach(l => { lampiranHtml += ` `; }); } html += ` `; }); document.getElementById("list-regulasi").innerHTML = html; } function renderPaginationControls() { const totalPages = Math.ceil(filteredData.length / perPage); let html = ""; if (totalPages === 0) { document.getElementById("pagination").innerHTML = ""; return; } html += ` `; for (let i = 1; i <= totalPages; i++) { html += ` `; } html += ` `; document.getElementById("pagination").innerHTML = html; } function renderPaginationInfo(start, end) { const total = filteredData.length; if (total === 0) { document.getElementById("pagination-info").innerHTML = ""; return; } const from = start + 1; const to = Math.min(end, total); document.getElementById("pagination-info").innerHTML = `Show ${from}–${to} of ${total} data`; } window.changePage = function (page) { currentPage = page; renderPagination(); window.scrollTo({ top: 300, behavior: "smooth" }); }; document.querySelectorAll(".filter-btn").forEach(btn => { btn.addEventListener("click", function () { document.querySelectorAll(".filter-btn").forEach(b => b.classList.remove("active")); this.classList.add("active"); currentKategori = this.dataset.kategori; filterData(); }); }); document.getElementById("search").addEventListener("keyup", filterData); document.getElementById("tahun").addEventListener("change", filterData); loadRegulasi(); });