Best Overall Supplier
${bestOverallSupplier || 'N/A'}
`;
document.getElementById('report-date').textContent = `Report generated on: ${new Date().toLocaleDateString()}`;
}
// --- Tab & Navigation Logic ---
function switchTab(targetTabId) {
if (targetTabId === 'report') {
const data = runComparison();
if (data === null) return;
renderReport(data);
}
currentTab = targetTabId;
tabs.forEach(tab => tab.classList.toggle('active', tab.dataset.tab === currentTab));
tabContents.forEach(content => content.classList.toggle('active', content.id === currentTab));
updateNavButtons();
}
tabs.forEach(tab => tab.addEventListener('click', () => switchTab(tab.dataset.tab)));
nextBtn.addEventListener('click', () => currentTab === 'config' ? switchTab('report') : switchTab('config'));
prevBtn.addEventListener('click', () => currentTab === 'report' ? switchTab('config') : switchTab('report'));
function updateNavButtons() {
if (currentTab === 'config') {
nextBtn.textContent = 'Generate Report';
prevBtn.style.display = 'none';
pdfButtonContainer.style.display = 'none';
} else {
nextBtn.style.display = 'none';
prevBtn.style.display = 'inline-flex';
pdfButtonContainer.style.display = 'block';
}
}
// --- PDF Download ---
downloadPdfBtn.addEventListener('click', async () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'l', unit: 'mm', format: 'a4' });
const content = document.getElementById('pdf-content');
doc.setFont('helvetica', 'bold');
doc.setFontSize(18);
doc.text('Wholesale Price Comparison Report', 14, 22);
doc.setFontSize(11);
doc.setTextColor(100);
doc.text(`Generated on: ${new Date().toLocaleDateString()}`, 14, 28);
await new Promise(resolve => setTimeout(resolve, 0));
const canvas = await html2canvas(content, { scale: 2 });
const imgData = canvas.toDataURL('image/jpeg', 1.0);
const imgProps = doc.getImageProperties(imgData);
const pdfWidth = doc.internal.pageSize.getWidth() - 28;
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
doc.addImage(imgData, 'JPEG', 14, 40, pdfWidth, pdfHeight);
doc.save(`Price-Comparison-Report-${new Date().toISOString().slice(0,10)}.pdf`);
});
// --- Initial Load ---
function populateSampleData() {
['Canvas Tote Bag', 'Stainless Steel Water Bottle', 'Hardcover Notebook'].forEach(p => {
const tr = document.createElement('tr');
tr.innerHTML = `
| | `;
tr.querySelector('input').addEventListener('input', updatePriceMatrix);
tr.querySelector('.remove-row-btn').addEventListener('click', () => { tr.remove(); updatePriceMatrix(); });
productsTableBody.appendChild(tr);
});
['Global Imports', 'American Wholesalers', 'Supply Co.'].forEach(s => {
const tr = document.createElement('tr');
tr.innerHTML = `
| | `;
tr.querySelector('input').addEventListener('input', updatePriceMatrix);
tr.querySelector('.remove-row-btn').addEventListener('click', () => { tr.remove(); updatePriceMatrix(); });
suppliersTableBody.appendChild(tr);
});
updatePriceMatrix();
// Add sample prices
const samplePrices = [
[4.50, 4.75, 4.45],
[8.20, 8.10, 8.50],
[6.00, 5.80, 5.95]
];
samplePrices.forEach((row, pIndex) => {
row.forEach((price, sIndex) => {
const input = priceMatrixBody.querySelector(`[data-product-index="${pIndex}"][data-supplier-index="${sIndex}"]`);
if(input) input.value = price.toFixed(2);
});
});
}
populateSampleData();
switchTab('config');
});