`;
}
function showTab(n) {
if (!tabs || !tabButtons) return;
tabs.forEach((tab, index) => {
tab.style.display = (index === n) ? 'block' : 'none';
});
tabButtons.forEach((btn, index) => {
btn.classList.toggle('active', index === n);
});
if (prevBtn) prevBtn.disabled = (n === 0);
if (nextBtn) nextBtn.style.display = (n === (tabs.length - 1)) ? 'none' : 'inline-block';
if (n === tabs.length - 1) {
generateAuditSummary();
}
currentTab = n;
}
function navigateTabs(n) {
if(!tabs || !tabs[currentTab]) return;
currentTab += n;
if (currentTab >= tabs.length) {
currentTab = tabs.length - 1;
} else if (currentTab < 0) {
currentTab = 0;
}
showTab(currentTab);
}
// --- DOMContentLoaded Initializer ---
document.addEventListener('DOMContentLoaded', function() {
// Assign elements
tabs = document.querySelectorAll('.tab-content');
tabButtons = document.querySelectorAll('.tab-button');
prevBtn = document.getElementById('prevBtn');
nextBtn = document.getElementById('nextBtn');
const downloadPdfBtn = document.getElementById('downloadPdfBtn');
async function generatePdf() {
const loader = document.getElementById('pdf-loader');
if (!loader || !downloadPdfBtn) return;
loader.classList.remove('hidden');
downloadPdfBtn.disabled = true;
const content = document.getElementById('pdf-content-area');
try {
const canvas = await html2canvas(content, { scale: 2, useCORS: true, logging: false });
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'letter' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const margin = 40;
const imgProps = pdf.getImageProperties(imgData);
const imgWidth = pdfWidth - (margin * 2);
const imgHeight = (imgProps.height * imgWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', margin, margin, imgWidth, imgHeight);
pdf.save('Debt-Collection-Compliance-Audit.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
} finally {
loader.classList.add('hidden');
downloadPdfBtn.disabled = false;
}
}
// --- EVENT LISTENERS ---
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', generatePdf);
}
// --- INITIALIZATION ---
showTab(currentTab);
});
