Price Increase Opportunities
${insights.opportunityCount}
Direct Price Matches
${insights.matches}
Pricing Heatmap
${tableHeaderHtml}
${tableBodyHtml}
`;
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-configuration');
if (!configContent) return;
let competitorsHeaderHtml = appData.competitors.map((name, index) => `
`).join('');
let productsHtml = appData.products.map((p, pIndex) => {
let priceInputs = appData.competitors.map((c, cIndex) => `
|
`).join('');
return `
|
|
${priceInputs}
|
`;
}).join('');
configContent.innerHTML = `
`;
attachConfigListeners();
};
const attachConfigListeners = () => {
document.getElementById('add-competitor-btn').addEventListener('click', () => {
if (appData.competitors.length < 5) { // Limit competitors for layout
appData.competitors.push(`Competitor ${appData.competitors.length + 1}`);
renderDataConfig();
} else {
alert('A maximum of 5 competitors is recommended.');
}
});
document.getElementById('add-product-btn').addEventListener('click', () => {
appData.products.push({ name: 'New Product', ourPrice: 0, competitorPrices: Array(appData.competitors.length).fill(0) });
renderDataConfig();
});
document.getElementById('update-data-btn').addEventListener('click', handleConfigUpdate);
// Use event delegation for dynamically added remove buttons
const configForm = document.getElementById('data-config-form');
configForm.addEventListener('click', (e) => {
if (e.target.closest('.remove-competitor-btn')) {
const index = parseInt(e.target.closest('.remove-competitor-btn').dataset.index);
appData.competitors.splice(index, 1);
appData.products.forEach(p => p.competitorPrices.splice(index, 1));
renderDataConfig();
}
if (e.target.closest('.remove-product-btn')) {
const index = parseInt(e.target.closest('.remove-product-btn').dataset.pIndex);
appData.products.splice(index, 1);
renderDataConfig();
}
});
};
const handleConfigUpdate = () => {
// Update competitors
const newCompetitors = [];
document.querySelectorAll('.competitor-name-input').forEach(input => {
newCompetitors.push(input.value);
});
appData.competitors = newCompetitors;
// Update products
const newProducts = [];
document.querySelectorAll('.product-row').forEach((row, pIndex) => {
const name = row.querySelector('.product-name-input').value;
const ourPrice = parseFloat(row.querySelector('.product-our-price-input').value) || 0;
const competitorPrices = [];
row.querySelectorAll('.product-price-input').forEach(priceInput => {
competitorPrices.push(parseFloat(priceInput.value) || 0);
});
newProducts.push({ name, ourPrice, competitorPrices });
});
appData.products = newProducts;
alert('Data updated! The heatmap will now refresh.');
renderDashboard();
switchTab(0);
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content-area');
const pdfHeader = document.getElementById('pdf-header');
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
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: '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('Pricing-Heatmap-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: 'Pricing Dashboard', id: 'pricing-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);
};
initializeUI();
lucide.createIcons();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(active- 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});