`;
};
/**
* Downloads the generated statement as a PDF document.
*/
const downloadPDF = async () => {
// Ensure the required objects are available on the window object.
if (typeof window.jspdf === 'undefined' || typeof window.html2canvas === 'undefined') {
console.error("jsPDF or html2canvas is not loaded.");
return; // Exit if libraries aren't loaded.
}
const { jsPDF } = window.jspdf;
const content = document.getElementById('pdf-output-container');
downloadPdfBtn.textContent = 'Generating...';
downloadPdfBtn.disabled = true;
try {
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'pt',
format: 'letter' // US Letter size
});
await pdf.html(content, {
margin: [60, 60, 60, 60], // Margins for a formal document
autoPaging: 'text',
width: 495, // Letter width (612pt) - margins (60pt * 2)
windowWidth: 700,
html2canvas: {
scale: 0.8,
useCORS: true
}
});
const caseName = (getText('caseName', 'witness_statement')).replace(/[^a-z0-9]/gi, '_').toLowerCase();
pdf.save(`${caseName}.pdf`);
} catch (error) {
console.error("Error generating PDF:", error);
} finally {
downloadPdfBtn.textContent = 'Download as PDF';
downloadPdfBtn.disabled = false;
}
};
// --- Event Listeners ---
tabs.forEach(tab => {
tab.addEventListener('click', () => showTab(parseInt(tab.dataset.tab, 10)));
});
nextBtn.addEventListener('click', () => {
if (currentTab < totalTabs) showTab(currentTab + 1);
});
prevBtn.addEventListener('click', () => {
if (currentTab > 1) showTab(currentTab - 1);
});
if(downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', downloadPDF);
}
// Auto-fill declarant name from witness name if it's empty
const witnessNameInput = document.getElementById('witnessName');
const declarantNameInput = document.getElementById('declarantName');
if (witnessNameInput && declarantNameInput) {
witnessNameInput.addEventListener('blur', () => {
if (!declarantNameInput.value.trim()) {
declarantNameInput.value = witnessNameInput.value;
}
});
}
// --- Initial Setup ---
showTab(1); // Initialize view to the first tab
});
