${clause.notes ? `
${pa_escapeHTML(clause.notes)}
` : ''}
`;
}).join('');
categoryDiv.innerHTML = `
${pa_escapeHTML(category)}
${clausesHTML}
`;
targetDiv.appendChild(categoryDiv);
});
}
/**
* Renders a clone for PDF generation
*/
function pa_renderPdfClone() {
pa_pdfRenderClone.innerHTML = `
Prenuptial Agreement Discussion Points
`;
// Render data into the clone's content area
const contentArea = pa_pdfRenderClone.querySelector('.space-y-6');
pa_renderDashboard(contentArea, true);
}
/**
* Generates and downloads a PDF of the discussion list
*/
async function pa_downloadPDF() {
if (pa_data.clauses.length === 0) {
alert("Please add some discussion points before downloading.");
return;
}
if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') {
console.error("PA Tool Error: jsPDF or html2canvas library not loaded.");
alert("Error: PDF libraries failed to load. Please check console.");
return;
}
pa_renderPdfClone(); // Create and populate the clone
const { jsPDF } = window.jspdf;
try {
// Target the entire clone div
const canvas = await html2canvas(pa_pdfRenderClone, {
scale: 1.5, // Increase resolution slightly
useCORS: true,
windowWidth: pa_pdfRenderClone.scrollWidth,
windowHeight: pa_pdfRenderClone.scrollHeight // Capture full height
});
const imgData = canvas.toDataURL('image/png');
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
// Scale image height to fit pdf width, handle multiple pages
const margin = 40;
const contentWidth = pdfWidth - (margin * 2);
const contentHeight = (contentWidth * imgHeight) / imgWidth;
let heightLeft = contentHeight;
let position = 0; // y-position of the image slice on the page
// Add the first page
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
// Add subsequent pages if needed
while (heightLeft > 0) {
position -= (pdfHeight - margin * 2); // Move the image's y-position up
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
}
pdf.save('Prenup_Discussion_List.pdf');
} catch (error) {
console.error("PA Tool Error: PDF generation failed.", error);
alert("An error occurred while generating the PDF. Please try again.");
}
}
// --- EVENT LISTENERS ---
// Tab link clicks
pa_tabLinks.forEach((link, index) => {
link.addEventListener('click', () => pa_switchTab(index));
});
// Next/Prev button clicks
if (pa_prevButton) {
pa_prevButton.addEventListener('click', () => {
if (pa_currentTab > 0) pa_switchTab(pa_currentTab - 1);
});
}
if (pa_nextButton) {
pa_nextButton.addEventListener('click', () => {
// FIX: Check if on the last tab (Config tab, index 1)
if (pa_currentTab === pa_tabLinks.length - 1) {
// Act as Submit: Save data and switch to Dashboard (index 0)
pa_updateDataFromConfig();
pa_switchTab(0);
} else {
// Otherwise, just go to the next tab
if (pa_currentTab < pa_tabLinks.length - 1) pa_switchTab(pa_currentTab + 1);
}
});
}
// PDF download
if (pa_downloadPdfButton) {
pa_downloadPdfButton.addEventListener('click', pa_downloadPDF);
}
// --- Config Tab Listeners ---
if (pa_addClauseButton) {
pa_addClauseButton.addEventListener('click', () => {
pa_clausesContainer.appendChild(pa_createClauseInput());
});
}
if (pa_configTab) {
// Handle remove
pa_configTab.addEventListener('click', (e) => {
const removeButton = e.target.closest('.pa-remove-item');
if (removeButton) {
removeButton.closest('.border[data-id]').remove();
// Prevent having zero rows if applicable (optional)
if(pa_clausesContainer.children.length === 0){
pa_clausesContainer.appendChild(pa_createClauseInput());
}
}
});
// Update data on change (no need for keyup here)
pa_configTab.addEventListener('change', () => {
pa_updateDataFromConfig();
// If user is on dashboard, update it live (optional, maybe better performance to wait for tab switch)
// if (pa_currentTab === 0) { pa_renderDashboard(); }
});
}
// --- INITIALIZATION ---
pa_initSampleData();
pa_renderConfig(); // Populate config tab on load
pa_renderDashboard(); // Show initial state on dashboard
// Set initial tab state
pa_tabPanes.forEach((pane, index) => {
pane.classList.toggle('hidden', index !== 0);
pane.classList.toggle('pa-active', index === 0);
});
pa_tabLinks.forEach((link, index) => {
TAB_CLASSES.active.forEach(cls => link.classList.remove(cls));
TAB_CLASSES.inactive.forEach(cls => link.classList.remove(cls));
if (index === 0) {
TAB_CLASSES.active.forEach(cls => link.classList.add(cls));
} else {
TAB_CLASSES.inactive.forEach(cls => link.classList.add(cls));
}
});
});