`).join('');
} else {
comparisonHtml = `
No Suitable Coverage Found
The package value exceeds the maximum coverage for all available providers.
`;
}
resultHtml = `
Analysis Results
`;
}
dashboardContent.innerHTML = `
Enter Shipment Details
${resultHtml}
`;
document.getElementById('analyze-btn').addEventListener('click', () => {
appData.packageDetails.value = parseFloat(document.getElementById('package-value').value) || 0;
appData.packageDetails.destinationRisk = document.getElementById('destination-risk').value;
runAnalysis();
renderDashboard();
lucide.createIcons();
});
if (analysisResult) {
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
lucide.createIcons();
}
};
const renderConfig = () => {
const configContent = document.getElementById('content-configuration');
if (!configContent) return;
const providersHtml = appData.providers.map((provider, index) => `
`).join('');
configContent.innerHTML = `
Insurance Provider Configuration
${providersHtml}
`;
attachConfigListeners();
};
const attachConfigListeners = () => {
document.getElementById('add-provider-btn').addEventListener('click', () => {
const newId = appData.providers.length > 0 ? Math.max(...appData.providers.map(p => p.id)) + 1 : 1;
appData.providers.push({ id: newId, name: 'New Provider', baseRate: 2.0, maxCoverage: 1000, deductible: 0, riskMultiplier: { low: 1.0, medium: 1.0, high: 1.0 } });
renderConfig();
lucide.createIcons();
});
document.getElementById('providers-container').addEventListener('click', e => {
if (e.target.closest('.remove-provider-btn')) {
const index = parseInt(e.target.closest('.remove-provider-btn').dataset.index);
appData.providers.splice(index, 1);
renderConfig();
lucide.createIcons();
}
});
document.getElementById('update-btn').addEventListener('click', handleConfigUpdate);
};
const handleConfigUpdate = () => {
const newProviders = Array.from(document.querySelectorAll('.provider-row')).map((row, index) => {
return {
id: appData.providers[index].id,
name: row.querySelector('[data-field="name"]').value,
baseRate: parseFloat(row.querySelector('[data-field="baseRate"]').value) || 0,
maxCoverage: parseFloat(row.querySelector('[data-field="maxCoverage"]').value) || 0,
deductible: parseFloat(row.querySelector('[data-field="deductible"]').value) || 0,
riskMultiplier: {
low: parseFloat(row.querySelector('[data-risk="low"]').value) || 1.0,
medium: parseFloat(row.querySelector('[data-risk="medium"]').value) || 1.0,
high: parseFloat(row.querySelector('[data-risk="high"]').value) || 1.0,
}
};
});
appData.providers = newProviders;
analysisResult = null;
renderDashboard();
alert('Configuration saved!');
switchTab(0);
};
const generatePdf = () => {
if (!analysisResult) return;
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content-area');
const pdfHeader = document.getElementById('pdf-header');
pdfHeader.classList.remove('hidden');
html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.95);
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight);
pdf.save('Shipping-Insurance-Analysis.pdf');
loadingOverlay.style.display = 'none';
}).catch(err => {
console.error("PDF generation failed:", err);
pdfHeader.classList.add('hidden');
loadingOverlay.style.display = 'none';
alert('An error occurred generating the PDF.');
});
};
// --- TAB NAVIGATION & INITIALIZATION ---
const switchTab = (tabIndex) => {
activeTabIndex = tabIndex;
document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex));
document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex));
updateNavButtons();
};
const updateNavButtons = () => {
prevTabBtn.disabled = activeTabIndex === 0;
nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1;
};
const initializeUI = () => {
const tabs = [ { name: 'Insurance Analyzer', id: 'dashboard' }, { name: 'Configuration', id: 'configuration' } ];
tabIdentifiers = tabs.map(t => t.id);
tabsContainer.innerHTML = tabs.map(tab => `
`).join('');
mainContent.innerHTML = tabs.map(tab => `
`).join('');
tabs.forEach((tab, index) => document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index)));
runAnalysis();
renderDashboard();
renderConfig();
switchTab(0);
lucide.createIcons();
};
initializeUI();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});