Average Pre-Order Price
${formatCurrency(insights.averagePrice)}
Total Customer Savings
${formatCurrency(insights.totalDiscountValue)}
Average Discount
${formatPercent(insights.averageDiscountPercent)}
Pricing Tiers Visualization
`;
renderPricingChart();
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderPricingChart = () => {
const chartCtx = document.getElementById('pricing-tier-chart').getContext('2d');
if (pricingChartInstance) {
pricingChartInstance.destroy();
}
pricingChartInstance = new Chart(chartCtx, {
type: 'bar',
data: {
labels: appData.pricingTiers.map(t => t.name),
datasets: [
{
label: 'Price ($)',
data: appData.pricingTiers.map(t => t.price),
backgroundColor: 'rgba(79, 70, 229, 0.8)',
borderColor: 'rgba(79, 70, 229, 1)',
borderWidth: 1,
yAxisID: 'y',
},
{
label: 'Units Available',
data: appData.pricingTiers.map(t => t.units),
backgroundColor: 'rgba(56, 189, 248, 0.6)',
borderColor: 'rgba(56, 189, 248, 1)',
borderWidth: 1,
yAxisID: 'y1',
type: 'line',
tension: 0.2
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
type: 'linear',
display: true,
position: 'left',
title: { display: true, text: 'Price ($)' },
ticks: { callback: value => formatCurrency(value) }
},
y1: {
type: 'linear',
display: true,
position: 'right',
title: { display: true, text: 'Units Available' },
grid: { drawOnChartArea: false }
}
}
}
});
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-configuration');
if (!configContent) return;
let tiersHtml = appData.pricingTiers.map((tier, index) => `
`).join('');
configContent.innerHTML = `
`;
attachConfigListeners();
};
const attachConfigListeners = () => {
document.getElementById('add-tier-btn').addEventListener('click', () => {
appData.pricingTiers.push({ name: 'New Tier', units: 0, price: 0 });
renderDataConfig();
});
document.getElementById('update-data-btn').addEventListener('click', handleConfigUpdate);
document.getElementById('data-config-form').addEventListener('click', (e) => {
if (e.target.closest('.remove-tier-btn')) {
const index = parseInt(e.target.closest('.remove-tier-btn').dataset.index);
appData.pricingTiers.splice(index, 1);
renderDataConfig();
}
});
};
const handleConfigUpdate = () => {
appData.productName = document.getElementById('product-name').value;
appData.launchPrice = parseFloat(document.getElementById('launch-price').value) || 0;
const newTiers = [];
document.querySelectorAll('.tier-row').forEach(row => {
const index = row.querySelector('input').dataset.index;
newTiers.push({
name: row.querySelector(`[data-index="${index}"][data-field="name"]`).value,
units: parseInt(row.querySelector(`[data-index="${index}"][data-field="units"]`).value) || 0,
price: parseFloat(row.querySelector(`[data-index="${index}"][data-field="price"]`).value) || 0
});
});
appData.pricingTiers = newTiers;
alert('Pricing model updated!');
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('Pre-Order-Pricing-Model.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(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});