`;
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderConfig = () => {
const configContent = document.getElementById('content-configuration');
if (!configContent) return;
const itemsHtml = appData.items.map((item, index) => `
`).join('');
configContent.innerHTML = `
`;
attachConfigListeners();
};
const attachConfigListeners = () => {
document.getElementById('add-item-btn').addEventListener('click', () => {
appData.items.push({ name: '', quantity: 1, price: 0 });
renderConfig();
lucide.createIcons();
});
document.getElementById('items-container').addEventListener('click', e => {
if (e.target.closest('.remove-item-btn')) {
appData.items.splice(parseInt(e.target.closest('.remove-item-btn').dataset.index), 1);
renderConfig();
lucide.createIcons();
}
});
document.getElementById('update-btn').addEventListener('click', handleConfigUpdate);
};
const handleConfigUpdate = () => {
appData.company = { name: document.getElementById('company-name').value, address: document.getElementById('company-address').value, logoUrl: document.getElementById('company-logo').value };
appData.customer = { name: document.getElementById('customer-name').value, shippingAddress: document.getElementById('customer-shipping').value, billingAddress: document.getElementById('customer-billing').value };
appData.order.shippingMethod = document.getElementById('order-shipping-method').value;
appData.order.shippingCost = parseFloat(document.getElementById('order-shipping-cost').value) || 0;
appData.order.taxRate = parseFloat(document.getElementById('order-tax-rate').value) || 0;
appData.items = Array.from(document.querySelectorAll('.item-row')).map(row => ({
name: row.querySelector('[data-field="name"]').value,
quantity: parseInt(row.querySelector('[data-field="quantity"]').value) || 0,
price: parseFloat(row.querySelector('[data-field="price"]').value) || 0
}));
orderTotals = null;
renderPreview();
alert('Confirmation details updated!');
switchTab(0);
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content-area');
html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false })
.then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', 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, 'PNG', 0, 0, pdfWidth, imgHeight);
pdf.save(`Order-Confirmation-${appData.order.number}.pdf`);
loadingOverlay.style.display = 'none';
}).catch(err => {
console.error("PDF generation failed:", err);
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: 'Confirmation Preview', id: 'confirmation-preview' }, { name: 'Configuration', id: '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)));
renderPreview();
renderConfig();
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); });
});