${check.question}
`;
configForm.appendChild(div);
});
}
// --- Dashboard & Analysis Logic ---
function analyzeCompliance() {
const formData = new FormData(configForm);
const results = [];
let compliantCount = 0;
complianceChecks.forEach(check => {
const answer = formData.get(check.id);
const isCompliant = answer === check.compliantValue;
if (isCompliant) compliantCount++;
let status, statusClass;
if (isCompliant) {
status = 'Compliant'; statusClass = 'status-compliant';
} else if (['privacy-policy', 'consent-opt-in', 'user-access', 'cookie-consent'].includes(check.id)) {
status = 'High Risk'; statusClass = 'status-high-risk';
} else {
status = 'Needs Review'; statusClass = 'status-review';
}
results.push({ ...check, answer, status, statusClass });
});
const score = (compliantCount / complianceChecks.length) * 100;
return { score, results };
}
function renderDashboard(data) {
if (!data) return;
// Score Bar
const scoreColor = data.score > 75 ? 'bg-green-500' : data.score > 40 ? 'bg-yellow-500' : 'bg-red-500';
complianceScoreBar.innerHTML = `
${data.score.toFixed(0)}%
`;
// Summary
const highRiskCount = data.results.filter(r => r.status === 'High Risk').length;
const needsReviewCount = data.results.filter(r => r.status === 'Needs Review').length;
dashboardSummary.innerHTML = `
Compliant Areas
${data.results.length - highRiskCount - needsReviewCount}
Areas for Review
${needsReviewCount}
High-Risk Areas
${highRiskCount}
`;
// Table
dashboardTableBody.innerHTML = '';
data.results.forEach(r => {
const tr = document.createElement('tr');
tr.className = 'bg-white border-b hover:bg-gray-50';
tr.innerHTML = `
${r.question} |
${r.answer} |
${r.status} |
${r.status === 'Compliant' ? 'Keep up the good work.' : r.recommendation} |
`;
dashboardTableBody.appendChild(tr);
});
document.getElementById('dashboard-date').textContent = `Report generated on: ${new Date().toLocaleDateString()}`;
}
// --- Tab & Navigation Logic ---
function switchTab(targetTabId) {
if (targetTabId === 'dashboard') {
const data = analyzeCompliance();
if (data) renderDashboard(data);
}
currentTab = targetTabId;
tabs.forEach(tab => tab.classList.toggle('active', tab.dataset.tab === currentTab));
tabContents.forEach(content => content.classList.toggle('active', content.id === currentTab));
updateNavButtons();
}
tabs.forEach(tab => tab.addEventListener('click', () => switchTab(tab.dataset.tab)));
nextBtn.addEventListener('click', () => currentTab === 'dashboard' ? switchTab('config') : switchTab('dashboard'));
prevBtn.addEventListener('click', () => currentTab === 'config' ? switchTab('dashboard') : switchTab('config'));
function updateNavButtons() {
if (currentTab === 'dashboard') {
nextBtn.textContent = 'Update Checklist';
prevBtn.style.display = 'none';
pdfButtonContainer.style.display = 'block';
} else {
nextBtn.textContent = 'Generate Report';
prevBtn.style.display = 'inline-flex';
pdfButtonContainer.style.display = 'none';
}
}
// --- PDF Download ---
downloadPdfBtn.addEventListener('click', async () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'l', unit: 'mm', format: 'a4' });
const content = document.getElementById('pdf-content');
await new Promise(r => setTimeout(r, 100));
const canvas = await html2canvas(content, { scale: 2, windowWidth: 1200 });
const imgData = canvas.toDataURL('image/jpeg', 1.0);
doc.setFont('helvetica', 'bold');
doc.setFontSize(18);
doc.text('GDPR & Data Protection Compliance Report', 14, 22);
const imgProps = doc.getImageProperties(imgData);
const pdfWidth = doc.internal.pageSize.getWidth() - 28;
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
doc.addImage(imgData, 'JPEG', 14, 30, pdfWidth, pdfHeight);
doc.save(`Compliance-Report-${new Date().toISOString().slice(0,10)}.pdf`);
});
// --- Initial Load ---
buildChecklist();
switchTab('dashboard');
});