Based on your selections for the following channels: ${selectedChannelNames.join(', ')}
- ${contributingFactors.join('
- ')}
`;
resultsSection.style.display = 'block';
// Store for PDF download
resultsSection.dataset.impactLevel = impactLevel;
resultsSection.dataset.contributingFactors = JSON.stringify(contributingFactors);
resultsSection.dataset.selectedChannelNames = JSON.stringify(selectedChannelNames);
resultsSection.dataset.userSelectionsForPdf = JSON.stringify(userSelectionsForPdf);
}
function clearAll() {
document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => checkbox.checked = false);
document.querySelectorAll('.ad-impact-channel-options').forEach(optionsDiv => optionsDiv.style.display = 'none');
document.querySelectorAll('select').forEach(select => select.selectedIndex = 0); // Reset to first option
document.querySelectorAll('input[type="radio"]').forEach(radio => {
if (radio.value === 'renewable' || radio.name === 'digitalEnergy' && radio.value === 'renewable') { // Default digital energy to renewable
radio.checked = true;
} else if (radio.name !== 'digitalEnergy') { // Clear other radios (though not explicitly needed for this setup)
radio.checked = false;
}
});
// Re-check default radio for digital (if it exists)
const defaultDigitalEnergyRadio = document.querySelector('input[name="digitalEnergy"][value="renewable"]');
if (defaultDigitalEnergyRadio) {
defaultDigitalEnergyRadio.checked = true;
}
resultsSection.style.display = 'none';
clearError();
// Clear stored data for PDF
delete resultsSection.dataset.impactLevel;
delete resultsSection.dataset.contributingFactors;
delete resultsSection.dataset.selectedChannelNames;
delete resultsSection.dataset.userSelectionsForPdf;
}
async function downloadPdf() {
const doc = new jsPDF();
const primaryColorRgb = getRgbFromCssVar('--primary-color');
const primaryDarkColorRgb = getRgbFromCssVar('--primary-dark-color');
const textColorRgb = getRgbFromCssVar('--text-color');
const cardBgRgb = getRgbFromCssVar('--card-background');
const borderColorRgb = getRgbFromCssVar('--border-color');
const warningColorRgb = getRgbFromCssVar('--secondary-color'); // Using yellow for warnings/disclaimers
let y = 20;
const margin = 15;
const lineHeight = 7;
doc.setFontSize(22);
doc.setTextColor(primaryDarkColorRgb[0], primaryDarkColorRgb[1], primaryDarkColorRgb[2]);
doc.text("Advertising Environmental Impact Report", margin, y);
y += lineHeight * 2;
doc.setFontSize(10);
doc.setTextColor(warningColorRgb[0], warningColorRgb[1], warningColorRgb[2]);
const disclaimerText = "Disclaimer: This report is generated by a conceptual tool for educational and awareness purposes only. It does not provide precise carbon emission (CO2e) calculations. Always consult with environmental experts for detailed assessments.";
const disclaimerLines = doc.splitTextToSize(disclaimerText, doc.internal.pageSize.width - 2 * margin);
doc.text(disclaimerLines, margin, y);
y += lineHeight * disclaimerLines.length + lineHeight;
doc.setFontSize(14);
doc.setTextColor(primaryDarkColorRgb[0], primaryDarkColorRgb[1], primaryDarkColorRgb[2]);
if (y + lineHeight * 2 > doc.internal.pageSize.height - margin) {
doc.addPage();
y = margin;
}
doc.text("Your Selections:", margin, y);
y += lineHeight * 1.5;
const selectedChannelNames = JSON.parse(resultsSection.dataset.selectedChannelNames || '[]');
const userSelectionsForPdf = JSON.parse(resultsSection.dataset.userSelectionsForPdf || '{}');
if (selectedChannelNames.length === 0) {
doc.text("No channels selected or calculation performed.", margin, y);
y += lineHeight * 2;
} else {
for (const channel of selectedChannelNames) {
const channelKey = channel.toLowerCase().replace(/[^a-z0-9]/g, ''); // e.g., "digitaladvertising"
let dataKey;
if (channel.includes("Digital")) dataKey = 'digital';
else if (channel.includes("Print")) dataKey = 'print';
else if (channel.includes("Out-of-Home")) dataKey = 'ooh';
else if (channel.includes("TV/Radio")) dataKey = 'tvRadio';
if (userSelectionsForPdf[dataKey]) {
if (y + lineHeight * 3 > doc.internal.pageSize.height - margin) { doc.addPage(); y = margin; }
doc.setFontSize(12);
doc.setTextColor(primaryColorRgb[0], primaryColorRgb[1], primaryColorRgb[2]);
doc.text(channel, margin, y);
y += lineHeight;
const channelData = Object.entries(userSelectionsForPdf[dataKey]).map(([param, value]) => [param, value]);
doc.autoTable({
startY: y,
head: [['Parameter', 'Your Selection']],
body: channelData,
theme: 'grid',
headStyles: {
fillColor: primaryColorRgb,
textColor: 255,
fontStyle: 'bold',
fontSize: 9
},
styles: {
fontSize: 8,
textColor: textColorRgb,
lineColor: borderColorRgb,
lineWidth: 0.1,
cellPadding: 2,
valign: 'middle'
},
columnStyles: {
0: { cellWidth: 70 },
1: { cellWidth: 'auto' }
},
alternateRowStyles: {
fillColor: cardBgRgb
},
didDrawPage: function (data) {
y = data.cursor.y;
}
});
y = doc.autoTable.previous.finalY + lineHeight;
}
}
}
y += lineHeight; // Extra space after selections
doc.setFontSize(14);
doc.setTextColor(primaryDarkColorRgb[0], primaryDarkColorRgb[1], primaryDarkColorRgb[2]);
if (y + lineHeight * 2 > doc.internal.pageSize.height - margin) {
doc.addPage();
y = margin;
}
doc.text("Estimation Results:", margin, y);
y += lineHeight * 1.5;
const impactLevel = resultsSection.dataset.impactLevel || 'N/A';
const contributingFactors = JSON.parse(resultsSection.dataset.contributingFactors || '[]');
const resultsData = [
['Overall Estimated Impact', impactLevel]
];
doc.autoTable({
startY: y,
head: [['Metric', 'Value']],
body: resultsData,
theme: 'grid',
headStyles: {
fillColor: primaryColorRgb,
textColor: 255,
fontStyle: 'bold',
fontSize: 10
},
styles: {
fontSize: 9,
textColor: textColorRgb,
lineColor: borderColorRgb,
lineWidth: 0.1
},
alternateRowStyles: {
fillColor: cardBgRgb
},
didDrawPage: function (data) {
y = data.cursor.y;
}
});
y = doc.autoTable.previous.finalY + lineHeight * 1.5;
doc.setFontSize(10);
doc.setTextColor(primaryDarkColorRgb[0], primaryDarkColorRgb[1], primaryDarkColorRgb[2]);
if (y + lineHeight * 2 > doc.internal.pageSize.height - margin) {
doc.addPage();
y = margin;
}
doc.text("Key Contributing Factors:", margin, y);
y += lineHeight;
doc.setFontSize(9);
doc.setTextColor(textColorRgb[0], textColorRgb[1], textColorRgb[2]);
if (contributingFactors.length > 0) {
const factorsText = contributingFactors.map(f => `- ${f}`).join('\n');
let lines = doc.splitTextToSize(factorsText, doc.internal.pageSize.width - 2 * margin);
if (y + lineHeight * lines.length > doc.internal.pageSize.height - margin) {
doc.addPage();
y = margin;
}
doc.text(lines, margin, y);
y += lineHeight * lines.length + lineHeight;
} else {
if (y + lineHeight > doc.internal.pageSize.height - margin) { doc.addPage(); y = margin; }
doc.text("No specific factors identified (no channels selected or calculation performed).", margin, y);
y += lineHeight;
}
doc.save('Advertising_Environmental_Impact_Report.pdf');
}
// --- Event Listeners ---
channelDigital.addEventListener('change', toggleOptionsVisibility);
channelPrint.addEventListener('change', toggleOptionsVisibility);
channelOOH.addEventListener('change', toggleOptionsVisibility);
channelTVRadio.addEventListener('change', toggleOptionsVisibility);
calculateBtn.addEventListener('click', calculateImpact);
clearBtn.addEventListener('click', clearAll);
downloadPdfBtn.addEventListener('click', downloadPdf);
// Initial state
toggleOptionsVisibility();
});