Market Size: ${data.marketSize}
`;
if (data.growthRate) dataHTML += `
Growth Rate: ${data.growthRate}
`;
if (data.keyDataPoints.length > 0) {
dataHTML += `
Key Data Points:${data.keyDataPoints.map(p => `- ${p}
`).join('')}
`;
}
}
previewContainer.innerHTML = `
${data.title}
Executive Summary
${data.execSummary.replace(/\n/g, '
')}
Key Trends
${trendsHTML}
${dataHTML}
Conclusion & Future Outlook
${data.conclusion.replace(/\n/g, '
')}
`;
}
function addNewTrendBlock() {
const container = document.getElementById('key-trends-container');
if (!container) return;
const trendCount = container.children.length + 1;
const newTrend = document.createElement('div');
newTrend.className = 'p-4 border border-gray-200 rounded-lg';
newTrend.innerHTML = `
Trend ${trendCount}
`;
container.appendChild(newTrend);
}
function showMessage() {
messageBox.classList.remove('opacity-0', 'translate-y-10');
messageBox.classList.add('opacity-100', 'translate-y-0');
setTimeout(() => {
messageBox.classList.remove('opacity-100', 'translate-y-0');
messageBox.classList.add('opacity-0', 'translate-y-10');
}, 2000);
}
// --- PDF Generation ---
function generateReportPdf() {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const data = getFormData();
const pageW = pdf.internal.pageSize.getWidth();
const pageH = pdf.internal.pageSize.getHeight();
const margin = 20;
const contentWidth = pageW - (margin * 2);
let y = 0;
const lineH = 7;
const today = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
// --- PDF Helper Functions ---
const addHeaderFooter = () => {
const pageCount = pdf.internal.getNumberOfPages();
for (let i = 1; i <= pageCount; i++) {
pdf.setPage(i);
if (i > 1) { // No header/footer on title page
pdf.setFontSize(8);
pdf.setTextColor('#6b7280'); // gray-500
pdf.text(data.title, margin, 10);
pdf.text(`Page ${i} of ${pageCount}`, pageW - margin, 10, { align: 'right' });
pdf.setDrawColor('#d1d5db'); // gray-300
pdf.setLineWidth(0.2);
pdf.line(margin, 12, pageW - margin, 12);
}
}
};
const checkBreak = (needed = 20) => {
if (y + needed > pageH - margin) {
pdf.addPage();
y = margin + 5;
}
};
const addText = (text, options = {}) => {
const { size=10, style='normal', color='#374151', indent=0, spacing=1 } = options;
checkBreak();
pdf.setFontSize(size);
pdf.setFont(undefined, style);
pdf.setTextColor(color);
const splitText = pdf.splitTextToSize(text, contentWidth - indent);
pdf.text(splitText, margin + indent, y);
y += (splitText.length * lineH * 0.7) + (lineH * spacing);
};
const addSection = (title, content) => {
if (!content || (Array.isArray(content) && content.length === 0)) return;
y += lineH;
checkBreak(30);
addText(title, { size: 14, style: 'bold', color: '#047857' });
y += lineH / 2;
if (Array.isArray(content)) {
content.forEach(item => addText(`• ${item}`, { indent: 5 }));
} else if (typeof content === 'object') { // For trends
content.forEach(trend => {
y += lineH / 2;
addText(trend.title, {size: 11, style: 'bold', color: '#1f2937'});
addText(trend.description);
addText(`Impact: ${trend.impact}`, {style: 'italic'});
});
} else {
addText(content);
}
};
// --- Build PDF Document ---
// Page 1: Title Page
pdf.setFillColor('#064e3b'); // emerald-900
pdf.rect(0, 0, 70, pageH, 'F');
pdf.setFontSize(26);
pdf.setFont(undefined, 'bold');
pdf.setTextColor('#111827'); // gray-900
pdf.text(data.title, 80, 120, { maxWidth: pageW - 90 });
pdf.setFontSize(12);
pdf.setFont(undefined, 'normal');
pdf.setTextColor('#4b5563');
pdf.text(`Industry Report: ${data.industry}`, 80, 160);
pdf.text(`By: ${data.author}`, 80, 170);
pdf.text(`Date: ${today}`, 80, 180);
pdf.addPage();
y = margin + 5;
// Subsequent Pages Content
addSection('Executive Summary', data.execSummary);
addSection('Key Industry Trends', data.trends);
if (data.marketSize || data.growthRate || data.keyDataPoints.length > 0) {
y += lineH;
checkBreak(30);
addText('Data & Projections', { size: 14, style: 'bold', color: '#047857' });
y += lineH / 2;
if(data.marketSize) addText(`Current Market Size: ${data.marketSize}`, {style: 'bold'});
if(data.growthRate) addText(`Projected Growth (CAGR): ${data.growthRate}`, {style: 'bold'});
if (data.keyDataPoints.length > 0) {
y += lineH / 2;
addText('Key Data Points:', {});
data.keyDataPoints.forEach(p => addText(`• ${p}`, {indent: 5}));
}
}
addSection('Conclusion & Future Outlook', data.conclusion);
// Add headers and footers to all pages except the first
addHeaderFooter();
const fileName = `${data.industry.replace(/\s+/g, '_').toLowerCase()}_trends.pdf`;
pdf.save(fileName);
}
// --- Event Listeners ---
tabs.forEach(tab => tab.addEventListener('click', () => goToTab(parseInt(tab.dataset.tab))));
prevButton.addEventListener('click', () => goToTab(currentTab - 1));
nextButton.addEventListener('click', () => goToTab(currentTab + 1));
addTrendButton.addEventListener('click', addNewTrendBlock);
copyButton.addEventListener('click', () => {
const previewContainer = document.getElementById('report-preview-container');
if (previewContainer) {
const textToCopy = previewContainer.innerText;
const textArea = document.createElement("textarea");
textArea.value = textToCopy;
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
showMessage();
} catch (err) { console.error('Copy failed:', err); }
document.body.removeChild(textArea);
}
});
pdfDownloadButton.addEventListener('click', generateReportPdf);
// Initialize the UI
updateTabUI();
});