`;
});
surveyPreview.innerHTML = previewHtml;
};
const getAnswerHtml = (type) => {
if (type === 'scale-5') return '[ ] 1 [ ] 2 [ ] 3 [ ] 4 [ ] 5';
if (type === 'nps') return '[ ] 0 [ ] 1 [ ] 2 [ ] 3 [ ] 4 [ ] 5 [ ] 6 [ ] 7 [ ] 8 [ ] 9 [ ] 10';
if (type === 'text') return '_________________________________';
if (type === 'yes-no') return '[ ] Yes [ ] No';
return '';
};
// --- PDF Download Logic ---
downloadPdfBtn.addEventListener('click', () => {
if (!window.jspdf || !window.jspdf.jsPDF.autoTable) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'letter' });
const page = { width: doc.internal.pageSize.getWidth(), margin: 72 };
let y = page.margin;
// Header
doc.setFont('times', 'bold');
doc.setFontSize(20);
doc.text(surveyData.title, page.width / 2, y, { align: 'center' });
y += 25;
doc.setFont('times', 'normal');
doc.setFontSize(12);
doc.text(surveyData.company, page.width / 2, y, { align: 'center' });
y += 35;
// Intro
const introLines = doc.splitTextToSize(surveyData.intro, page.width - page.margin * 2);
doc.text(introLines, page.margin, y, { lineHeightFactor: 1.5 });
y += introLines.length * 12 * 1.5 + 20;
// Questions
surveyData.questions.forEach((q, i) => {
const questionText = `${i + 1}. ${q.text}`;
const questionLines = doc.splitTextToSize(questionText, page.width - page.margin * 2);
const answerHeight = q.type === 'text' ? 60 : 20;
if (y + (questionLines.length * 12 * 1.5) + answerHeight > doc.internal.pageSize.getHeight() - page.margin) {
doc.addPage();
y = page.margin;
}
doc.setFont('times', 'bold');
doc.text(questionLines, page.margin, y, { lineHeightFactor: 1.5 });
y += questionLines.length * 12 * 1.5;
doc.setFont('times', 'normal');
y += 15;
// Draw response fields
if (q.type === 'scale-5') {
for (let j = 1; j <= 5; j++) doc.rect(page.margin + (j-1)*50, y, 12, 12);
} else if (q.type === 'nps') {
for (let j = 0; j <= 10; j++) doc.rect(page.margin + j*45, y, 12, 12);
} else if (q.type === 'yes-no') {
doc.rect(page.margin, y, 12, 12); doc.text('Yes', page.margin + 20, y+10);
doc.rect(page.margin + 80, y, 12, 12); doc.text('No', page.margin + 100, y+10);
} else if (q.type === 'text') {
doc.setDrawColor(200);
doc.line(page.margin, y, page.width - page.margin, y);
doc.line(page.margin, y + 20, page.width - page.margin, y + 20);
doc.line(page.margin, y + 40, page.width - page.margin, y + 40);
}
y += answerHeight + 20;
});
doc.save(`${surveyData.title.replace(/\s/g, '_') || 'Survey'}.pdf`);
});
// --- Initial Setup ---
switchTab('setup');
});
