Priced Below Average
${productsBelowAvg}
Priced Above Average
${productsAboveAvg}
Competitive Score
${competitiveScore.toFixed(0)}%
% of products where we are cheapest
| Product |
${appData.myStoreName} |
${appData.competitors.map(c => `${c} | `).join('')}
Market Avg. |
Lowest Price |
vs. Avg |
${tableRows}
Price Trend Analysis
`;
lucide.createIcons();
renderPriceTrendChart(appData.products[0].id);
document.getElementById('product-trend-select').addEventListener('change', (e) => renderPriceTrendChart(parseInt(e.target.value)));
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderPriceTrendChart = (productId) => {
const history = appData.priceHistory.filter(h => h.productId === productId);
if (history.length === 0) { /* Handle no history */ return; }
const labels = history.map(h => new Date(h.date));
const allCompetitors = [appData.myStoreName, ...appData.competitors];
const colors = ['#16a34a', '#3b82f6', '#ef4444', '#f97316'];
const datasets = allCompetitors.map((competitor, i) => ({
label: competitor,
data: history.map(h => h.prices[competitor]),
borderColor: colors[i % colors.length],
backgroundColor: colors[i % colors.length] + '1A',
tension: 0.1,
fill: false
}));
const ctx = document.getElementById('priceTrendChart').getContext('2d');
if (charts.priceTrend) charts.priceTrend.destroy();
charts.priceTrend = new Chart(ctx, {
type: 'line',
data: { labels, datasets },
options: {
responsive: true, maintainAspectRatio: false,
scales: { x: { type: 'time', time: { unit: 'day' } }, y: { ticks: { callback: value => formatCurrency(value) } } }
}
});
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-config');
if (!configContent) return;
let competitorsHtml = appData.competitors.map((c, i) => `
`).join('');
let productsHtml = appData.products.map((p, i) => `
`).join('');
configContent.innerHTML = `
Configure Products & Competitors
`;
document.getElementById('data-config-form').addEventListener('submit', handleConfigUpdate);
};
const handleConfigUpdate = (e) => {
e.preventDefault();
// Update Competitors
const oldCompetitorNames = [...appData.competitors];
const newCompetitorNames = [];
document.querySelectorAll('.competitor-name-input').forEach((input, index) => {
newCompetitorNames.push(input.value);
});
// Update prices to reflect new competitor names
appData.products.forEach(product => {
oldCompetitorNames.forEach((oldName, index) => {
const newName = newCompetitorNames[index];
if (oldName !== newName && product.prices.hasOwnProperty(oldName)) {
product.prices[newName] = product.prices[oldName];
delete product.prices[oldName];
}
});
});
appData.competitors = newCompetitorNames;
// Update Products and Prices
document.querySelectorAll('.product-input').forEach(input => {
const index = parseInt(input.dataset.productIndex);
const field = input.dataset.field;
if (field === 'name') {
appData.products[index].name = input.value;
} else if (field === 'price') {
const competitor = input.dataset.competitor;
appData.products[index].prices[competitor] = parseFloat(input.value) || null;
}
});
alert('Data updated successfully!');
renderDashboard();
renderDataConfig();
};
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.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('Competitor-Price-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: 'Price Dashboard', id: 'dashboard' },
{ name: 'Data Configuration', id: 'data-config' }
];
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();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});