No asset classes were selected or an allocation could not be determined.
';
}
reportKeyActionsEl.innerHTML = '';
data.actions.forEach(action => {
const li = document.createElement('li');
li.textContent = action;
reportKeyActionsEl.appendChild(li);
});
if(data.actions.length === 0) reportKeyActionsEl.innerHTML = '
No specific actions generated.';
reportConsiderationsEl.innerHTML = '';
data.considerations.forEach(consideration => {
const li = document.createElement('li');
li.textContent = consideration;
reportConsiderationsEl.appendChild(li);
});
if(data.considerations.length === 0) reportConsiderationsEl.innerHTML = '
No specific considerations noted.';
}
if (generateStrategyBtn) {
generateStrategyBtn.addEventListener('click', () => {
const strategyData = generateStrategy();
displayStrategy(strategyData);
if (tabs[2]) tabs[2].click();
});
}
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', () => {
if (!currentStrategyData) {
alert("Please generate a strategy first.");
return;
}
// ** CORRECTED JSPDF LIBRARY CHECK **
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
alert('Error: The jsPDF library (window.jspdf.jsPDF) could not be found. Please check your internet connection or the CDN link for the library.');
return;
}
const jsPDFConstructor = window.jspdf.jsPDF; // This is the jsPDF constructor
const tempDoc = new jsPDFConstructor(); // Create a temporary instance
if (typeof tempDoc.autoTable !== 'function') {
alert('Error: The jsPDF-AutoTable plugin (doc.autoTable) could not be found. Please ensure it is loaded after the main jsPDF library and check your internet connection or the CDN link for the plugin.');
return;
}
// ** END OF CORRECTED CHECK **
const doc = new jsPDFConstructor(); // Create the actual document for use
const pageHeight = doc.internal.pageSize.height;
const pageWidth = doc.internal.pageSize.width;
const margin = 15;
let yPos = 20;
const primaryColorPDF = getComputedStyle(document.documentElement).getPropertyValue('--primary-color').trim();
const textColorPDF = getComputedStyle(document.documentElement).getPropertyValue('--text-color').trim();
doc.setFontSize(18);
doc.setTextColor(primaryColorPDF);
doc.text("Investment Strategy Report", pageWidth / 2, yPos, { align: 'center' });
yPos += 12;
doc.setFontSize(14);
doc.setTextColor(primaryColorPDF);
doc.text("Strategy Overview", margin, yPos);
yPos += 7;
doc.setFontSize(10);
doc.setTextColor(textColorPDF);
doc.text(`Strategy Name: ${currentStrategyData.name || 'N/A'}`, margin + 5, yPos); yPos += 6;
const principleLines = doc.splitTextToSize(`Core Principle: ${currentStrategyData.principle || 'N/A'}`, pageWidth - (2 * margin) - 5);
doc.text(principleLines, margin + 5, yPos); yPos += principleLines.length * 5 + 4;
doc.setFontSize(14); doc.setTextColor(primaryColorPDF);
doc.text("User Profile Summary", margin, yPos); yPos += 7;
doc.setFontSize(10); doc.setTextColor(textColorPDF);
doc.text(`Risk Tolerance: ${riskToleranceEl.options[riskToleranceEl.selectedIndex].text}`, margin + 5, yPos); yPos += 6;
doc.text(`Investment Horizon: ${investmentHorizonEl.options[investmentHorizonEl.selectedIndex].text}`, margin + 5, yPos); yPos += 6;
if (currentStrategyData.amount) {
doc.text(`Hypothetical Amount: $${currentStrategyData.amount.toLocaleString('en-US')} USD`, margin + 5, yPos); yPos += 6;
}
yPos += 4;
doc.setFontSize(14); doc.setTextColor(primaryColorPDF);
doc.text("Suggested Asset Allocation", margin, yPos); yPos += 7;
doc.setFontSize(10); doc.setTextColor(textColorPDF);
const allocationData = Object.entries(currentStrategyData.allocation)
.filter(([, percentage]) => percentage > 0)
.map(([asset, percentage]) => {
const assetName = asset.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase());
return [assetName, `${percentage}%`];
});
if (allocationData.length > 0) {
doc.autoTable({
startY: yPos,
head: [['Asset Class', 'Percentage']],
body: allocationData,
theme: 'grid',
headStyles: { fillColor: primaryColorPDF, textColor: '#ffffff' },
margin: { left: margin, right: margin }
});
yPos = doc.lastAutoTable.finalY + 10;
} else {
doc.text("No specific asset allocation could be determined based on selections.", margin + 5, yPos); yPos += 10;
}
function addSectionWithList(title, items) {
if (yPos + 20 > pageHeight - margin) { doc.addPage(); yPos = 20; }
doc.setFontSize(14); doc.setTextColor(primaryColorPDF);
doc.text(title, margin, yPos); yPos += 7;
doc.setFontSize(10); doc.setTextColor(textColorPDF);
if (items && items.length > 0) {
items.forEach(item => {
if (yPos + 10 > pageHeight - margin) { doc.addPage(); yPos = 20; }
const itemLines = doc.splitTextToSize(`• ${item}`, pageWidth - (2 * margin) - 10);
doc.text(itemLines, margin + 5, yPos);
yPos += itemLines.length * 5 + 2;
});
} else {
doc.text("N/A", margin + 5, yPos); yPos += 6;
}
yPos += 4;
}
addSectionWithList("Key Actions & Focus", currentStrategyData.actions);
addSectionWithList("Important Considerations & Potential Risks", currentStrategyData.considerations);
if (yPos + 20 > pageHeight - margin) { doc.addPage(); yPos = 20; }
doc.setFontSize(8);
doc.setTextColor(120);
const disclaimerText = "This generated strategy is for illustrative and informational purposes only and does not constitute financial advice. Consult with a qualified financial advisor before making investment decisions. All investments carry risk.";
const disclaimerLines = doc.splitTextToSize(disclaimerText, pageWidth - (2*margin));
doc.text(disclaimerLines, margin, yPos);
yPos += disclaimerLines.length * 4 + 5;
doc.setFontSize(8);
doc.setTextColor(150);
doc.text(`Report generated on: ${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}`, margin, pageHeight - 10);
doc.save("Investment_Strategy_Report.pdf");
});
}
// Initialize
if (tabs.length > 0 && tabContents.length > 0) {
let isActiveSet = false;
tabs.forEach(tab => { if (tab.classList.contains('active')) isActiveSet = true; });
if (!isActiveSet) {
tabs[0].classList.add('active');
tabContents[0].classList.add('active');
}
currentStrategyTabIndex = Array.from(tabs).findIndex(tab => tab.classList.contains('active'));
updateNavButtons();
} else {
console.error("Strategy generator tabs or content areas not found.");
}
});