Inventory${preOrderCount} / ${p.inventoryLimit}
`;
}).join('');
configContent.innerHTML = `
Manage Products
${productsHtml}
`;
lucide.createIcons();
document.getElementById('add-product-btn').addEventListener('click', () => renderProductForm());
document.querySelectorAll('.edit-product-btn').forEach(btn => btn.addEventListener('click', (e) => renderProductForm(parseInt(e.target.closest('.product-row').dataset.id))));
document.querySelectorAll('.remove-product-btn').forEach(btn => btn.addEventListener('click', (e) => {
const productId = parseInt(e.target.closest('.product-row').dataset.id);
appData.products = appData.products.filter(p => p.id !== productId);
renderConfiguration();
}));
};
const renderProductForm = (productId = null) => {
const container = document.getElementById('product-form-container');
const product = productId ? appData.products.find(p => p.id === productId) : {};
const isNew = !productId;
container.innerHTML = `
${isNew ? 'Add New Product' : 'Edit Product'}
`;
document.getElementById('product-form').addEventListener('submit', handleProductSave);
document.getElementById('cancel-form-btn').addEventListener('click', () => container.innerHTML = '');
};
const handleProductSave = (e) => {
e.preventDefault();
const productId = parseInt(document.getElementById('product-id').value);
const newProductData = {
name: document.getElementById('product-name').value,
price: parseFloat(document.getElementById('product-price').value),
inventoryLimit: parseInt(document.getElementById('product-inventory').value),
releaseDate: document.getElementById('product-release').value
};
if (!newProductData.name || isNaN(newProductData.price)) {
alert('Please enter a valid name and price.'); return;
}
if (productId) {
const index = appData.products.findIndex(p => p.id === productId);
appData.products[index] = { ...appData.products[index], ...newProductData };
} else {
const newId = Math.max(0, ...appData.products.map(p => p.id)) + 1;
appData.products.push({ id: newId, ...newProductData });
}
renderDashboard();
renderConfiguration();
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
let y = 40;
pdf.setFontSize(18).setFont('helvetica', 'bold').text('Pre-Order Summary Report', pdf.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 30;
pdf.autoTable({
startY: y,
head: [['Order ID', 'Customer', 'Product', 'Order Date', 'Status']],
body: appData.orders.map(order => {
const product = appData.products.find(p => p.id === order.productId);
return [`#${order.id}`, order.customerName, product ? product.name : 'N/A', formatDate(order.orderDate), order.status];
}),
theme: 'grid',
headStyles: { fillColor: [124, 58, 237] } // Violet header
});
pdf.save(`Pre-Order-Report.pdf`);
loadingOverlay.style.display = 'none';
};
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: 'Pre-Order Dashboard', id: 'dashboard' },
{ 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));
});
renderDashboard();
renderConfiguration();
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); });
});