Est. Duration
${caseDuration} Months
Cost Breakdown by Phase
| Phase | Estimated Cost |
| Pre-Trial & Discovery | ${formatCurrency(totalPreTrial)} |
| Trial | ${formatCurrency(totalTrial)} |
| Post-Trial / Appeal | ${formatCurrency(totalPostTrial)} |
| Complexity Multiplier | ${complexityMultiplier.toFixed(1)}x |
| Grand Total | ${formatCurrency(grandTotal)} |
`;
// Render the chart
renderChart(totalPreTrial, totalTrial, totalPostTrial);
};
/**
* Renders or updates the Chart.js doughnut chart.
* @param {number} preTrial - Total cost for pre-trial phase.
* @param {number} trial - Total cost for trial phase.
* @param {number} postTrial - Total cost for post-trial phase.
*/
const renderChart = (preTrial, trial, postTrial) => {
const ctx = document.getElementById('costChart').getContext('2d');
const chartData = {
labels: ['Pre-Trial & Discovery', 'Trial', 'Post-Trial / Appeal'],
datasets: [{
data: [preTrial, trial, postTrial],
backgroundColor: ['#3b82f6', '#ef4444', '#f97316'],
hoverOffset: 4
}]
};
if (costChart) {
costChart.destroy();
}
costChart = new Chart(ctx, {
type: 'doughnut',
data: chartData,
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'Cost Distribution by Phase'
}
}
},
});
};
/**
* Downloads the summary view as a PDF document.
*/
const downloadPDF = async () => {
const { jsPDF } = window.jspdf;
const content = document.getElementById('pdf-output');
downloadPdfBtn.textContent = 'Generating...';
downloadPdfBtn.disabled = true;
try {
// Temporarily hide chart for better PDF rendering of text/tables
const chartContainer = document.getElementById('chart-container');
chartContainer.style.display = 'none';
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'pt',
format: 'a4'
});
await pdf.html(content, {
margin: [40, 40, 40, 40],
autoPaging: 'text',
width: 515,
windowWidth: 650,
html2canvas: { scale: 0.75, useCORS: true }
});
const caseName = (document.getElementById('caseName').value || 'litigation').replace(/[^a-z0-9]/gi, '_').toLowerCase();
pdf.save(`${caseName}_cost_estimate.pdf`);
// Restore chart visibility
chartContainer.style.display = 'flex';
} catch (error) {
console.error("Error generating PDF:", error);
} finally {
downloadPdfBtn.textContent = 'Download Summary as PDF';
downloadPdfBtn.disabled = false;
}
};
// --- Event Listeners ---
tabs.forEach(tab => {
tab.addEventListener('click', () => showTab(parseInt(tab.dataset.tab)));
});
nextBtn.addEventListener('click', () => {
if (currentTab < totalTabs) showTab(currentTab + 1);
});
prevBtn.addEventListener('click', () => {
if (currentTab > 1) showTab(currentTab - 1);
});
if(downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', downloadPDF);
}
// --- Initial Setup ---
showTab(1); // Initialize view to the first tab
});