Total Discount
${formatCurrency(calculationResult.discountAmount)}
Bundle Summary
- Total Original Price: ${formatCurrency(calculationResult.totalOriginalPrice)}
- Total Cost of Goods: ${formatCurrency(calculationResult.totalCost)}
- Bundle Discount (${calculationResult.discountPercent}%): -${formatCurrency(calculationResult.discountAmount)}
- Final Price: ${formatCurrency(calculationResult.finalBundlePrice)}
`;
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderProductConfig = () => {
const configContent = document.getElementById('content-config');
if (!configContent) return;
const productsHtml = appData.products.map((p, index) => `
`).join('');
configContent.innerHTML = `
`;
document.getElementById('product-config-form').addEventListener('submit', handleConfigUpdate);
document.getElementById('add-product-row-btn').addEventListener('click', addProductRow);
};
const addProductRow = () => {
document.getElementById('product-list-container').insertAdjacentHTML('beforeend', `
`);
};
const handleConfigUpdate = (e) => {
e.preventDefault();
const newProducts = [];
let nextId = Math.max(...appData.products.map(p => p.id), 0) + 1;
document.querySelectorAll('.product-config-row').forEach((row, index) => {
const name = row.querySelector('input[data-field="name"]').value;
const cost = parseFloat(row.querySelector('input[data-field="cost"]').value);
const price = parseFloat(row.querySelector('input[data-field="price"]').value);
if (name && !isNaN(cost) && !isNaN(price)) {
newProducts.push({
id: appData.products[index]?.id || nextId++,
name, cost, price
});
}
});
appData.products = newProducts;
alert('Product list updated!');
// Re-render calculator tab to update product dropdown
renderCalculator();
};
const generatePdf = () => {
if (!calculationResult) return;
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content-area');
const pdfHeader = document.getElementById('pdf-header');
document.getElementById('pdf-bundle-name').textContent = appData.bundleName;
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleDateString('en-US');
pdfHeader.classList.remove('hidden');
html2canvas(pdfContent, { scale: 2, useCORS: true })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: [canvas.width, canvas.height] });
pdf.addImage(imgData, 'PNG', 0, 0, canvas.width, canvas.height);
pdf.save(`Bundle-Report-${appData.bundleName.replace(/\s/g, '_')}.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: 'Bundle Calculator', id: 'calculator' },
{ name: 'Product Configuration', id: '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));
});
renderCalculator();
renderProductConfig();
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); });
});