`;
if (isPDF) {
// PDF View: Show static status and notes
contentHTML += `
`;
stepDiv.classList.add('flex', 'justify-between', 'items-start'); // Flex layout for PDF rows
} else {
// Dashboard View: Interactive elements
const statusOptions = STATUS_OPTIONS.map(opt =>
``
).join('');
contentHTML += `
`;
}
stepDiv.innerHTML = contentHTML;
targetDiv.appendChild(stepDiv);
});
// Add event listeners only if not rendering for PDF
if (!isPDF) {
pcd_addDashboardListeners();
}
}
/**
* Adds event listeners to dashboard interactive elements
*/
function pcd_addDashboardListeners() {
pcd_dashboardOutput.querySelectorAll('.pcd-dashboard-status').forEach(select => {
select.addEventListener('change', (e) => {
const stepDiv = e.target.closest('[data-id]');
const id = parseInt(stepDiv.getAttribute('data-id'), 10);
const newStatus = e.target.value;
pcd_updateStepData(id, 'status', newStatus);
// Update badge visually immediately
const statusSlug = newStatus.toLowerCase().replace(/ /g, '-').replace(/[^a-z-]/g,'');
// Find or create badge span (needed if we render badge dynamically)
});
});
pcd_dashboardOutput.querySelectorAll('.pcd-dashboard-notes').forEach(textarea => {
// Use 'input' for more immediate saving, or 'change' to save on blur
textarea.addEventListener('input', (e) => {
const stepDiv = e.target.closest('[data-id]');
const id = parseInt(stepDiv.getAttribute('data-id'), 10);
const newNotes = e.target.value;
pcd_updateStepData(id, 'notes', newNotes);
});
});
}
/**
* Updates the status or notes for a specific step in the pcd_data object
*/
function pcd_updateStepData(id, field, value) {
const stepIndex = pcd_data.steps.findIndex(s => s.id === id);
if (stepIndex !== -1) {
pcd_data.steps[stepIndex][field] = value;
// Re-render only if needed (e.g., status change affects badge visually)
if(field === 'status') pcd_renderDashboard();
}
}
/**
* Renders a clone for PDF generation
*/
function pcd_renderPdfClone() {
pcd_pdfRenderClone.innerHTML = `
`;
// Render data into the clone's content area
const contentArea = pcd_pdfRenderClone.querySelector('.space-y-4');
pcd_renderDashboard(contentArea, true);
}
/**
* Generates and downloads a PDF of the discussion list
*/
async function pcd_downloadPDF() {
if (pcd_data.steps.length === 0) {
alert("Please add some filing steps before downloading.");
return;
}
if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') {
console.error("PCD Tool Error: jsPDF or html2canvas library not loaded.");
alert("Error: PDF libraries failed to load. Please check console.");
return;
}
pcd_renderPdfClone(); // Create and populate the clone
const { jsPDF } = window.jspdf;
try {
// Target the entire clone div
const canvas = await html2canvas(pcd_pdfRenderClone, {
scale: 1.5, // Increase resolution slightly
useCORS: true,
windowWidth: pcd_pdfRenderClone.scrollWidth,
windowHeight: pcd_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('Probate_Filing_Guide_Progress.pdf');
} catch (error) {
console.error("PCD Tool Error: PDF generation failed.", error);
alert("An error occurred while generating the PDF. Please try again.");
}
}
// --- EVENT LISTENERS ---
// Tab link clicks
pcd_tabLinks.forEach((link, index) => {
link.addEventListener('click', () => pcd_switchTab(index));
});
// Next/Prev button clicks
if (pcd_prevButton) {
pcd_prevButton.addEventListener('click', () => {
if (pcd_currentTab > 0) pcd_switchTab(pcd_currentTab - 1);
});
}
if (pcd_nextButton) {
pcd_nextButton.addEventListener('click', () => {
// If on the last tab (Config tab, index 1)
if (pcd_currentTab === pcd_tabLinks.length - 1) {
// Act as Submit: Save data and switch to Dashboard (index 0)
pcd_updateDataFromConfig();
pcd_switchTab(0);
} else {
// Otherwise, just go to the next tab
if (pcd_currentTab < pcd_tabLinks.length - 1) pcd_switchTab(pcd_currentTab + 1);
}
});
}
// PDF download
if (pcd_downloadPdfButton) {
pcd_downloadPdfButton.addEventListener('click', pcd_downloadPDF);
}
// --- Config Tab Listeners ---
if (pcd_addStepButton) {
pcd_addStepButton.addEventListener('click', () => {
pcd_stepsContainer.appendChild(pcd_createStepInput());
});
}
if (pcd_configTab) {
// Handle remove
pcd_configTab.addEventListener('click', (e) => {
const removeButton = e.target.closest('.pcd-remove-item');
if (removeButton) {
const entryDiv = removeButton.closest('.border[data-id]');
const idToRemove = parseInt(entryDiv.getAttribute('data-id'));
pcd_data.steps = pcd_data.steps.filter(s => s.id !== idToRemove); // Remove from data
entryDiv.remove(); // Remove from UI
// Prevent having zero rows if applicable
if(pcd_stepsContainer.children.length === 0){
pcd_stepsContainer.appendChild(pcd_createStepInput());
}
}
});
}
// --- INITIALIZATION ---
pcd_initSampleData();
pcd_renderConfig(); // Populate config tab on load
pcd_renderDashboard(); // Show initial state on dashboard
// Set initial tab state
pcd_tabPanes.forEach((pane, index) => {
pane.classList.toggle('hidden', index !== 0);
pane.classList.toggle('pcd-active', index === 0);
});
pcd_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));
}
});
});
${statusBadge}
${step.notes ? `Notes: ${pcd_escapeHTML(step.notes)}
` : ''}