`;
questionIndex++;
});
container.innerHTML = questionsHTML;
});
};
// --- Function to Update Tab UI ---
const updateTabUI = () => {
tabs.forEach((tab, index) => tab.classList.toggle('active', index === currentTab));
tabContents.forEach((content, index) => content.classList.toggle('active', index === currentTab));
prevBtn.disabled = currentTab === 0;
nextBtn.disabled = currentTab === tabs.length - 1;
if (currentTab === tabs.length - 1) { // Report Tab
generateComplianceReport();
downloadPdfBtn.disabled = false;
} else {
downloadPdfBtn.disabled = true;
}
};
// --- Report Generation ---
const generateComplianceReport = () => {
const companyName = document.getElementById('companyName').value || 'N/A';
const assessorName = document.getElementById('assessorName').value || 'N/A';
const assessmentDate = document.getElementById('assessmentDate').value;
const formattedDate = assessmentDate ? new Date(assessmentDate).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', timeZone: 'UTC' }) : 'N/A';
let reportHTML = `
`; let totalYes = 0, totalNo = 0, totalApplicable = 0; let questionIndex = 0; Object.keys(checklistData).forEach(category => { reportHTML += `';
checklistData[category].forEach(questionText => {
const answer = document.querySelector(`input[name="q${questionIndex}"]:checked`).value;
const bgColor = answer === 'No' ? '#fee2e2' : (answer === 'Yes' ? '#dcfce7' : '#f1f5f9');
reportHTML += `
';
});
const score = totalApplicable > 0 ? ((totalYes / totalApplicable) * 100).toFixed(0) : 100;
const scoreColor = score >= 80 ? '#198754' : (score >= 50 ? '#FFC107' : '#DC3545');
let summaryHTML = `
Health & Safety Compliance Report
Company: ${companyName}
Assessor: ${assessorName}
Date: ${formattedDate}
`; let totalYes = 0, totalNo = 0, totalApplicable = 0; let questionIndex = 0; Object.keys(checklistData).forEach(category => { reportHTML += `
${category}
`; reportHTML += '| ${questionText} | ${answer} |
Summary
${score}%
Compliance Score
${totalYes}
Compliant Items
${totalNo}
Non-Compliant Items
Note: This checklist is a general guide based on common OSHA standards and is not a substitute for legal advice. A 'No' answer indicates an area that may require immediate attention to ensure compliance.
`; reportPreview.innerHTML = summaryHTML + reportHTML; }; // --- PDF Download Logic --- downloadPdfBtn.addEventListener('click', async () => { if (!window.jspdf || !window.html2canvas) { alert('PDF generation library is not loaded.'); return; } loader.classList.remove('hidden'); downloadPdfBtn.disabled = true; try { const { jsPDF } = window.jspdf; const canvas = await html2canvas(reportPreview, { scale: 2 }); const pdf = new jsPDF('p', 'mm', 'a4'); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgHeight = canvas.height * pdfWidth / canvas.width; let heightLeft = imgHeight; let position = 0; pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, position, pdfWidth, imgHeight); heightLeft -= pdfHeight; while (heightLeft > 0) { position = heightLeft - imgHeight; pdf.addPage(); pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, position, pdfWidth, imgHeight); heightLeft -= pdfHeight; } pdf.save('Health_Safety_Compliance_Report.pdf'); } catch (error) { console.error("PDF Generation Error:", error); alert("An error occurred generating the PDF."); } finally { loader.classList.add('hidden'); downloadPdfBtn.disabled = false; } }); // --- Event Listeners for Navigation --- window.changeTab = (tabIndex) => { currentTab = tabIndex; updateTabUI(); }; prevBtn.addEventListener('click', () => { if (currentTab > 0) { currentTab--; updateTabUI(); } }); nextBtn.addEventListener('click', () => { if (currentTab < tabs.length - 1) { currentTab++; updateTabUI(); } }); // --- Final Setup --- populateChecklists(); updateTabUI(); });