Single-Source Suppliers
${singleSourceCount}
Supplier Risk Breakdown
| Supplier | Country | Annual Spend | Risk Score |
${tableRows}
`;
renderRiskChart(suppliers);
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderRiskChart = (suppliers) => {
const ctx = document.getElementById('risk-chart').getContext('2d');
if (charts.risk) charts.risk.destroy();
charts.risk = new Chart(ctx, {
type: 'bar',
data: {
labels: suppliers.map(s => s.name),
datasets: [{
label: 'Risk Score',
data: suppliers.map(s => s.totalScore),
backgroundColor: suppliers.map(s => {
if (s.totalScore >= 75) return '#fee2e2';
if (s.totalScore >= 50) return '#fef3c7';
if (s.totalScore >= 25) return '#dbeafe';
return '#dcfce7';
}),
borderColor: suppliers.map(s => {
if (s.totalScore >= 75) return '#b91c1c';
if (s.totalScore >= 50) return '#f59e0b';
if (s.totalScore >= 25) return '#3b82f6';
return '#16a34a';
}),
borderWidth: 1
}]
},
options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false } }, scales: { y: { beginAtZero: true, max: 100, title: { display: true, text: 'Risk Score (0-100)' } } } }
});
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-configuration');
if (!configContent) return;
const tableRows = appData.suppliers.map(s => `
|
|
|
|
|
|
|
|
`).join('');
configContent.innerHTML = `
Manage Suppliers & Risk Factors
| Supplier Name | Country | Spend ($) | Financial | Quality | Delivery | Single Source? | |
${tableRows}
`;
lucide.createIcons();
attachConfigListeners();
};
// --- EVENT HANDLERS & LOGIC ---
const attachConfigListeners = () => {
document.querySelectorAll('.config-input').forEach(input => input.addEventListener('change', e => {
const id = parseInt(e.target.dataset.id);
const field = e.target.dataset.field;
let value;
if (e.target.type === 'checkbox') value = e.target.checked;
else if (e.target.type === 'number' || e.target.tagName === 'SELECT') value = parseFloat(e.target.value);
else value = e.target.value;
const supplier = appData.suppliers.find(s => s.id === id);
if (supplier) supplier[field] = value;
appData.analysisResults = null;
renderDashboard();
}));
document.querySelectorAll('.remove-supplier-btn').forEach(btn => btn.addEventListener('click', e => {
const id = parseInt(e.currentTarget.dataset.id);
appData.suppliers = appData.suppliers.filter(s => s.id !== id);
appData.analysisResults = null;
renderDataConfig();
renderDashboard();
}));
document.getElementById('add-supplier-btn').addEventListener('click', () => {
const newId = appData.suppliers.length > 0 ? Math.max(...appData.suppliers.map(s => s.id)) + 1 : 1;
appData.suppliers.push({ id: newId, name: 'New Supplier', country: 'USA', financial: 3, quality: 3, delivery: 3, isSingleSource: false, spend: 0 });
renderDataConfig();
});
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleString();
const pdfHeader = document.getElementById('pdf-header');
pdfHeader.classList.remove('hidden');
const fullContent = document.getElementById('pdf-content-area');
html2canvas(fullContent, { scale: 2, useCORS: true, logging: false, windowWidth: 1200 })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.9);
const pdf = new jsPDF({ orientation: 'landscape', 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('Supply-Chain-Risk-Report.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: 'Risk Dashboard', id: 'risk-dashboard' },
{ name: 'Data Configuration', id: 'data-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));
});
renderDashboard();
renderDataConfig();
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); });
});