document.addEventListener('DOMContentLoaded', function () { const pageEl = document.getElementById('history-page'); if (!pageEl) return; const apiUrl = pageEl.dataset.historyApi; const detailBase = pageEl.dataset.historyDetailBase || '/upst/history/details/__id__'; const loadingEl = document.getElementById('history-loading'); const emptyEl = document.getElementById('history-empty'); const listEl = document.getElementById('history-list'); const paginationEl = document.getElementById('history-pagination'); const pageInfoEl = document.getElementById('history-page-info'); const totalInfoEl = document.getElementById('history-total-info'); const pageButtonsEl = document.getElementById('history-page-buttons'); const prevBtn = document.getElementById('history-prev-page'); const nextBtn = document.getElementById('history-next-page'); const fromDateInput = document.getElementById('history-from-date-filter'); const toDateInput = document.getElementById('history-to-date-filter'); const applyFilterBtn = document.getElementById('history-apply-filter'); const resetFilterBtn = document.getElementById('history-reset-filter'); const state = { page: 1, pageSize: 5, totalPages: 1, totalItems: 0, fromDate: '', toDate: '' }; function formatTanggal(dateString) { const date = new Date(dateString); return new Intl.DateTimeFormat('id-ID', { day: '2-digit', month: 'short', year: 'numeric' }).format(date); } function formatWaktu(dateString) { const date = new Date(dateString); return new Intl.DateTimeFormat('id-ID', { hour: '2-digit', minute: '2-digit' }).format(date); } function getStatusMarkup(status) { if ((status || '').toLowerCase() === 'completed') { return 'Selesai'; } return 'Proses'; } function buildDetailUrl(id) { return detailBase.replace('__id__', String(id)); } function renderItems(items) { listEl.innerHTML = ''; items.forEach(function (item) { const card = document.createElement('a'); card.href = buildDetailUrl(item.id); card.className = 'block group'; card.innerHTML = `
Nomor Dokumen
${item.noSpj}
Silakan coba lagi beberapa saat.
`; if (window.lucide) { window.lucide.createIcons(); } } } function applyDateRangeFilter() { const nextFromDate = fromDateInput?.value || ''; const nextToDate = toDateInput?.value || ''; if (nextFromDate && nextToDate && nextFromDate > nextToDate) { const temp = nextFromDate; state.fromDate = nextToDate; state.toDate = temp; if (fromDateInput) fromDateInput.value = state.fromDate; if (toDateInput) toDateInput.value = state.toDate; } else { state.fromDate = nextFromDate; state.toDate = nextToDate; } loadHistory(1); } applyFilterBtn?.addEventListener('click', function () { applyDateRangeFilter(); }); resetFilterBtn?.addEventListener('click', function () { state.fromDate = ''; state.toDate = ''; if (fromDateInput) fromDateInput.value = ''; if (toDateInput) toDateInput.value = ''; loadHistory(1); }); [fromDateInput, toDateInput].forEach(function (input) { input?.addEventListener('keydown', function (event) { if (event.key === 'Enter') { event.preventDefault(); applyDateRangeFilter(); } }); }); prevBtn?.addEventListener('click', function () { if (state.page > 1) { loadHistory(state.page - 1); } }); nextBtn?.addEventListener('click', function () { if (state.page < state.totalPages) { loadHistory(state.page + 1); } }); loadHistory(1); });