`;
}
}
function showLoadingState(isLoading) {
if (isLoading) {
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = '
';
generateBtn.disabled = true;
generateBtn.textContent = 'Generating...';
showFeedback('AI is writing your copy...', false, 0);
} else {
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Copy';
}
}
downloadPdfBtn.addEventListener('click', () => {
if (!lastGeneratedCopy) {
showFeedback('Please generate copy before downloading.', true, 3000);
return;
}
showFeedback('Generating PDF...', false, 2000);
try {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ unit: 'pt', format: 'letter' });
const pageMargin = 72;
const pageWidth = pdf.internal.pageSize.getWidth();
const textWidth = pageWidth - (pageMargin * 2);
let cursorY = pageMargin;
function addSection(title, content) {
if (!content) return;
if (cursorY > pdf.internal.pageSize.getHeight() - pageMargin - 50) {
pdf.addPage();
cursorY = pageMargin;
}
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(16);
pdf.text(title, pageMargin, cursorY);
cursorY += 25;
pdf.setFont('Helvetica', 'normal');
pdf.setFontSize(11);
const lines = pdf.splitTextToSize(content, textWidth);
pdf.text(lines, pageMargin, cursorY);
cursorY += (lines.length * 11 * 1.2) + 30;
}
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(22);
pdf.text('PAS Framework Copy', pageWidth / 2, cursorY, { align: 'center' });
cursorY += 50;
addSection('Problem', lastGeneratedCopy.problem);
addSection('Agitate', lastGeneratedCopy.agitate);
addSection('Solution', lastGeneratedCopy.solution);
pdf.save('pas-copy.pdf');
} catch(err) {
console.error("PDF Generation Error: ", err);
showFeedback("Error generating PDF.", true, 3000);
}
});
let feedbackTimeout;
function showFeedback(message, isError = false, duration = 3000) {
clearTimeout(feedbackTimeout);
feedbackMessage.innerHTML = message;
feedbackMessage.style.color = isError ? '#ef4444' : '#4a5568';
if (duration !== 0) {
feedbackTimeout = setTimeout(() => { feedbackMessage.innerHTML = ''; }, duration);
}
}
});
