`;
// 7. Assemble final HTML
previewContent.innerHTML = venueHtml + caseHtml + titleHtml + bodyHtml + juratHtml;
};
// --- Event Listeners ---
form.addEventListener("input", generateAffidavit);
form.addEventListener("change", generateAffidavit); // For checkbox
toggleCase.addEventListener("change", () => {
caseInfoGroup.style.display = toggleCase.checked ? "block" : "none";
generateAffidavit();
});
// --- Button: Copy to Clipboard ---
copyBtn.addEventListener("click", () => {
const textToCopy = previewContent.innerText; // .innerText preserves line breaks
const tempTextArea = document.createElement("textarea");
tempTextArea.value = textToCopy;
document.body.appendChild(tempTextArea);
tempTextArea.select();
try {
document.execCommand("copy"); // Use specified fallback
showToast("Copied to clipboard!");
} catch (err) {
console.error("ATG Copy Error:", err);
showToast("Failed to copy.", true);
}
document.body.removeChild(tempTextArea);
});
// --- Button: Download PDF ---
pdfBtn.addEventListener("click", () => {
const { jsPDF } = window.jspdf;
const pdfArea = toolContainer.querySelector("#atg-export-area");
const affiant = affiantName.value || "Affidavit";
const fileName = `${affiant.replace(/ /g, '_')}_Affidavit.pdf`;
html2canvas(pdfArea, {
scale: 2, // Improve resolution
useCORS: true,
backgroundColor: '#ffffff'
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
// Use A4 dimensions: 595.28 pt width, 841.89 pt height
const doc = new jsPDF({
orientation: 'p',
unit: 'pt',
format: 'a4'
});
const pdfWidth = doc.internal.pageSize.getWidth();
const pdfHeight = doc.internal.pageSize.getHeight();
const imgProps = doc.getImageProperties(imgData);
const imgWidth = imgProps.width;
const imgHeight = imgProps.height;
// Calculate ratio to fit A4 page width, with margins
const margin = 40; // 40pt margin
const usableWidth = pdfWidth - (2 * margin);
const ratio = usableWidth / imgWidth;
const scaledHeight = imgHeight * ratio;
// Check if it fits on one page
if (scaledHeight < pdfHeight - (2 * margin)) {
doc.addImage(imgData, 'PNG', margin, margin, usableWidth, scaledHeight);
} else {
// Handle multi-page (simple split)
let position = margin;
const pageHeight = pdfHeight - (2 * margin);
let heightLeft = scaledHeight;
doc.addImage(imgData, 'PNG', margin, position, usableWidth, scaledHeight);
heightLeft -= pageHeight;
while (heightLeft > 0) {
position = margin - heightLeft;
doc.addPage();
doc.addImage(imgData, 'PNG', margin, position, usableWidth, scaledHeight);
heightLeft -= pageHeight;
}
}
doc.save(fileName);
}).catch(err => {
console.error("ATG PDF Error:", err);
alert("An error occurred while generating the PDF.");
});
});
// --- Utility: Show Toast Message ---
let toastTimer;
const showToast = (message, isError = false) => {
if (!copyToast) return;
clearTimeout(toastTimer);
copyToast.textContent = message;
copyToast.style.backgroundColor = isError ? "#dc3545" : "var(--atg-primary-color)";
copyToast.style.display = "block";
toastTimer = setTimeout(() => {
copyToast.style.display = "none";
}, 2000);
};
// --- Initial Load ---
generateAffidavit(); // Populate with default values
});
