`;
const resultsOutput = document.getElementById('results-output');
if (resultsOutput) {
resultsOutput.innerHTML = tableHtml;
}
// Switch to results tab
openTab('results-tab');
};
// Tab Navigation
window.openTab = function (tabId) {
const tabs = document.querySelectorAll('.tab-content');
const tabLinks = document.querySelectorAll('.tab-link');
tabs.forEach(tab => tab.classList.remove('active'));
tabLinks.forEach(link => link.classList.remove('active'));
const activeTab = document.getElementById(tabId);
if (activeTab) {
activeTab.classList.add('active');
const activeLink = document.querySelector(`.tab-link[onclick="openTab('${tabId}')"]`);
if (activeLink) activeLink.classList.add('active');
}
};
// Download PDF
window.downloadPDF = function () {
const resultsOutput = document.getElementById('results-output');
const pdfContent = resultsOutput.querySelector('.pdf-content');
if (!resultsOutput || !pdfContent || !pdfContent.querySelector('.results-table')) {
alert('No results available to download. Please assess investment first.');
return;
}
// Extract table data
const rows = [];
const headers = ['Investment Type', 'ESG Score', 'Risk Level', 'Total Return (USD)', 'Annualized Return (%)'];
rows.push(headers.map(header => ({ text: header, style: 'tableHeader' })));
const tableRow = pdfContent.querySelector('.results-table tbody tr');
if (tableRow) {
const cells = tableRow.querySelectorAll('td');
const rowData = Array.from(cells).map(cell => ({ text: cell.textContent }));
rows.push(rowData);
}
// Extract recommendations
const recommendations = pdfContent.querySelector('.recommendations p')?.textContent || '';
// Define PDF document structure
const docDefinition = {
content: [
{ text: 'ESG Scoring & Risk Assessment Report', style: 'header' },
{ text: '\n' },
{
table: {
headerRows: 1,
widths: ['*', 'auto', 'auto', 'auto', 'auto'],
body: rows
},
layout: {
fillColor: function (rowIndex) {
return (rowIndex % 2 === 0 && rowIndex > 0) ? '#f2f2f2' : null;
},
hLineColor: '#ddd',
vLineColor: '#ddd'
}
},
{ text: '\n' },
{ text: 'Recommendations', style: 'subheader' },
{ text: recommendations, style: 'body' }
],
styles: {
header: {
fontSize: 18,
bold: true,
alignment: 'center',
color: '#333'
},
subheader: {
fontSize: 14,
bold: true,
color: '#333'
},
tableHeader: {
bold: true,
fontSize: 12,
color: 'white',
fillColor: '#4CAF50'
},
body: {
fontSize: 10,
color: '#333'
}
},
defaultStyle: {
fontSize: 10,
color: '#333'
},
pageMargins: [40, 60, 40, 60]
};
try {
// Generate and download PDF
pdfMake.createPdf(docDefinition).download('ESG_Scoring_Risk_Assessment.pdf');
} catch (err) {
console.error('PDF generation failed:', err);
alert('Failed to generate PDF. Please try again.');
}
};
});