`;
// Section 2: Usage Scenario Inputs (from current state of Tab 2)
pdfHtml += `
Medical Usage Scenario Inputs Used for Comparison
Generic Annual Medical Costs (subject to ded/coins): $${getNum(genericMedicalCostsEl).toFixed(2)}
Number of Doctor Visits (specific copay): ${getNum(doctorVisitsEl)}
Number of Prescriptions (specific copay): ${getNum(prescriptionsEl)}
`;
// Section 3: Comparison Table (clone from display or rebuild if necessary)
pdfHtml += `
Comparison Results
`;
if (resultsDisplayAreaEl && resultsDisplayAreaEl.querySelector('table')) {
pdfHtml += resultsDisplayAreaEl.innerHTML; // Clone the already generated table
} else {
pdfHtml += "
No comparison results were calculated for the current inputs.
";
}
pdfHtml += `
`;
pdfHtml += `
`; // Close icct-pdf-output
if(pdfOutputEl) pdfOutputEl.innerHTML = pdfHtml;
const opt = {
margin: [0.5, 0.4, 0.5, 0.4],
filename: `Insurance_Plan_Comparison_${new Date().toISOString().slice(0,10)}.pdf`,
image: { type: 'jpeg', quality: 0.95 },
html2canvas: { scale: 2, useCORS: true, logging: false, width: pdfOutputEl ? pdfOutputEl.scrollWidth : 1000, height: pdfOutputEl ? pdfOutputEl.scrollHeight + 20 : 1400 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
if (typeof html2pdf !== 'undefined' && pdfOutputEl) {
html2pdf().from(pdfOutputEl).set(opt).save()
.catch(err => { console.error("PDF generation failed:", err); alert("PDF generation error. See console."); });
} else {
alert("PDF library not loaded or output element missing.");
}
}
// --- Tab Navigation ---
window.icct_openTab = function(evt, tabName) {
tabs.forEach(tab => { if(tab) tab.style.display = 'none'; });
tabLinks.forEach(link => { if(link) link.classList.remove('active'); });
const activeTabContent = toolContainer.querySelector('#' + tabName);
if(activeTabContent) activeTabContent.style.display = 'block';
else { console.error(`Tab content for ${tabName} not found.`); return; }
let clickedIndex = -1;
if (evt && evt.currentTarget) {
evt.currentTarget.classList.add('active');
clickedIndex = tabLinks.indexOf(evt.currentTarget);
} else {
clickedIndex = tabLinks.findIndex(link => {
const onclickAttr = link.getAttribute('onclick');
return onclickAttr && onclickAttr.includes(tabName);
});
if (clickedIndex !== -1 && tabLinks[clickedIndex]) tabLinks[clickedIndex].classList.add('active');
}
currentTabIndex = clickedIndex !== -1 ? clickedIndex : 0;
icct_updateNavButtons();
}
window.icct_navigateTab = function(direction) {
let newIndex = currentTabIndex;
if (direction === 'next' && currentTabIndex < tabs.length - 1) newIndex++;
else if (direction === 'prev' && currentTabIndex > 0) newIndex--;
if (tabLinks[newIndex] && newIndex !== currentTabIndex) tabLinks[newIndex].click();
}
function icct_updateNavButtons() {
if(prevButton) prevButton.disabled = currentTabIndex === 0;
if(nextButton) nextButton.disabled = currentTabIndex >= tabs.length - 1;
}
// --- Initialization ---
function initializeTool() {
icct_renderPlanInputFields();
// Set default for usage scenario (can be cleared by user)
icct_setUsageScenario('medium');
icct_openTab(null, 'icct-tab1');
}
let attempts = 0;
function checkLibrariesAndInit_icct() {
if (typeof html2pdf !== 'undefined') { // Chart.js is not used in this version
initializeTool();
} else if (attempts < 20) {
attempts++;
setTimeout(checkLibrariesAndInit_icct, 100);
} else {
alert("Error: PDF generation library (html2pdf.js) could not be loaded. PDF export will not work.");
initializeTool(); // Initialize anyway
}
}
checkLibrariesAndInit_icct();
})();