`;
});
});
reportOutput.innerHTML = reportHTML;
};
// --- EVENT LISTENERS ---
tabs.forEach((tab, index) => {
if (tab.btn) tab.btn.addEventListener('click', () => { currentTab = index; updateTabUI(); });
});
nextBtn.addEventListener('click', () => {
if (currentTab < tabs.length - 1) { currentTab++; updateTabUI(); }
});
prevBtn.addEventListener('click', () => {
if (currentTab > 0) { currentTab--; updateTabUI(); }
});
downloadPdfBtn.addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const reportElement = document.getElementById('reportOutput');
const companyName = document.getElementById('companyName')?.value.trim() || "Company";
if (!reportElement) {
console.error("PDF Generation Error: Report output element not found.");
return;
}
html2canvas(reportElement, { scale: 2, useCORS: true }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
const imgWidth = pdfWidth - 20; // with 10mm margin
const imgHeight = imgWidth / ratio;
let heightLeft = imgHeight;
let position = 10;
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - 20);
while (heightLeft > 0) {
position = position - (pdfHeight - 20);
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - 20);
}
const safeFileName = companyName.replace(/[^a-z0-9]/gi, '_').toLowerCase();
pdf.save(`${safeFileName}_environmental_compliance_report.pdf`);
});
});
// --- INITIALIZE APP ---
populateDropdowns();
populateChecklist();
updateTabUI();
});
