`;
// Comparison Summary
pdfHtml += `
Comparison Summary
`;
// Assuming comparison with the first pro quote for simplicity in PDF summary
const firstPro = pros[0];
const diffOutOfPocket = diy.outOfPocket - firstPro.adjustedCost;
const diffEconomic = diy.economicCost - firstPro.adjustedCost;
pdfHtml += `
Total DIY Out-of-Pocket: $${diy.outOfPocket.toFixed(2)}
Total DIY Economic Cost: $${diy.economicCost.toFixed(2)}
Adjusted Professional Cost (${firstPro.name || 'Quote 1'}): $${firstPro.adjustedCost.toFixed(2)}
Net Difference (DIY Out-of-Pocket - Pro):
$${diffOutOfPocket.toFixed(2)}
(${diffOutOfPocket <= 0 ? 'DIY Cheaper' : 'Pro Cheaper'})
Net Difference (DIY Economic - Pro):
$${diffEconomic.toFixed(2)}
(${diffEconomic <= 0 ? 'DIY Cheaper' : 'Pro Cheaper'})
`;
}
pdfHtml += `
Qualitative Factors & Notes
${getStr(qualitativeNotesEl, "No additional notes entered.").replace(/\n/g, '
')}
Remember to consider factors like your skill level, time availability, desired quality, potential risks, and whether a warranty is important.
`;
pdfHtml += `
`; // Close dvp-pdf-output
if(pdfOutputEl) pdfOutputEl.innerHTML = pdfHtml;
const opt = {
margin: [0.5, 0.5, 0.5, 0.5],
filename: `DIY_vs_Pro_Comparison_${getStr(projectNameEl, "Project").replace(/[^a-zA-Z0-9]/g, '_')}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, useCORS: true, logging: false, width: pdfOutputEl ? pdfOutputEl.scrollWidth : 1000, height: pdfOutputEl ? pdfOutputEl.scrollHeight + 50 : 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."); });
} else {
alert("PDF library not loaded or output element missing.");
}
});
}
// --- Tab Navigation ---
window.dvp_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 => {
if (!link) return false;
const onclickAttr = link.getAttribute('onclick');
return onclickAttr && onclickAttr.includes(`'${tabName}'`);
});
if (clickedIndex !== -1 && tabLinks[clickedIndex]) {
tabLinks[clickedIndex].classList.add('active');
} else {
if (tabLinks.length > 0 && tabLinks[0]) tabLinks[0].classList.add('active');
clickedIndex = 0;
}
}
currentTabIndex = (clickedIndex !== -1 && clickedIndex < tabs.length) ? clickedIndex : 0;
dvp_updateNavButtons();
if (tabName === 'dvp-tab2') { // When navigating to comparison tab
dvp_updateDiyCostSummary(); // Ensure DIY totals are updated before this tab is fully used
}
}
window.dvp_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 dvp_updateNavButtons() {
if(prevButton) prevButton.disabled = currentTabIndex === 0;
if(nextButton && tabs) nextButton.disabled = currentTabIndex >= tabs.length - 1;
}
// --- LocalStorage (Minimal for this tool) ---
function loadSettings() {
const storedValueTime = localStorage.getItem(`${toolId}_valueTime`);
if (storedValueTime && valueTimeEl) valueTimeEl.value = storedValueTime;
}
function saveSettings() {
if(valueTimeEl) localStorage.setItem(`${toolId}_valueTime`, valueTimeEl.value);
}
if(valueTimeEl) valueTimeEl.addEventListener('change', saveSettings);
// --- Initialization ---
function initializeTool() {
if (!tabs.length || !tabLinks.length || !tabs[0] || !tabLinks[0]) {
console.error("CRITICAL: Tab or Tab Link elements are missing during init. UI will not function correctly.");
return;
}
loadSettings();
dvp_renderMaterialsTable(); // Initial render for empty tables
dvp_renderToolsTable();
dvp_updateDiyCostSummary(); // Calculate initial DIY costs (which will be 0)
dvp_openTab(null, 'dvp-tab1');
}
let dvp_attempts = 0;
function checkLibrariesAndInit_dvp() {
if (!document.getElementById(toolId)) { // Ensure container is loaded
if (dvp_attempts < 20) { dvp_attempts++; setTimeout(checkLibrariesAndInit_dvp, 100); }
else { console.error("Tool container not found for dvp tool."); }
return;
}
dvp_attempts = 0; // Reset for library check
function checkPdfLib() {
if (typeof html2pdf !== 'undefined') {
initializeTool();
} else if (dvp_attempts < 20) {
dvp_attempts++;
setTimeout(checkPdfLib, 100);
} else {
console.warn("Warning: PDF generation library (html2pdf.js) could not be loaded. PDF export may not work for dvp tool.");
initializeTool();
}
}
checkPdfLib();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', checkLibrariesAndInit_dvp);
} else {
checkLibrariesAndInit_dvp();
}
})();