Please fill out the form or use a profile to autofill.
`;
return;
}
const subtotal = appData.cart.reduce((sum, item) => sum + item.price * item.qty, 0);
const tax = subtotal * 0.08;
orderSummary = {
shipping: { fullName: shippingName, line1: document.getElementById('ship-line1').value, city: document.getElementById('ship-city').value, state: document.getElementById('ship-state').value, zip: document.getElementById('ship-zip').value },
payment: { cardholder: document.getElementById('pay-cardholder').value, number: document.getElementById('pay-number').value },
cart: [...appData.cart],
total: subtotal + tax,
};
resultEl.innerHTML = `
Order Placed Successfully!
`;
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderConfiguration = () => {
const configContent = document.getElementById('content-config');
if (!configContent) return;
const profilesHtml = appData.profiles.map(p => `
${p.name}
${p.address.fullName}, ${p.address.line1}
${p.payment.type} ending in ${p.payment.number.slice(-4)}
`).join('');
configContent.innerHTML = `
Manage Autofill Profiles
${profilesHtml}
`;
lucide.createIcons();
document.getElementById('add-profile-btn').addEventListener('click', () => renderProfileForm());
document.querySelectorAll('.edit-profile-btn').forEach(btn => btn.addEventListener('click', (e) => renderProfileForm(parseInt(e.currentTarget.dataset.id))));
document.querySelectorAll('.remove-profile-btn').forEach(btn => btn.addEventListener('click', (e) => {
appData.profiles = appData.profiles.filter(p => p.id !== parseInt(e.currentTarget.dataset.id));
renderConfiguration();
renderCheckoutForm(); // Update profile buttons
}));
};
const renderProfileForm = (profileId = null) => {
const container = document.getElementById('profile-form-container');
const profile = profileId ? appData.profiles.find(p => p.id === profileId) : {};
const isNew = !profileId;
const { name='', address={}, payment={} } = profile;
const { fullName='', line1='', line2='', city='', state='', zip='' } = address;
const { cardholder='', number='', expiry='', type='Visa' } = payment;
container.innerHTML = `
${isNew ? 'Add New Profile' : 'Edit Profile'}
`;
document.getElementById('profile-form').addEventListener('submit', handleProfileSave);
document.getElementById('cancel-form-btn').addEventListener('click', () => container.innerHTML = '');
};
const handleProfileSave = (e) => {
e.preventDefault();
const profileId = parseInt(document.getElementById('profile-id').value);
const newProfileData = {
name: document.getElementById('profile-name').value,
address: {
fullName: document.getElementById('profile-fullName').value,
line1: document.getElementById('profile-line1').value,
city: document.getElementById('profile-city').value,
state: document.getElementById('profile-state').value,
zip: document.getElementById('profile-zip').value,
},
payment: {
cardholder: document.getElementById('profile-cardholder').value,
number: document.getElementById('profile-number').value,
expiry: document.getElementById('profile-expiry').value,
type: document.getElementById('profile-type').value,
}
};
if (profileId) { // Update existing
const index = appData.profiles.findIndex(p => p.id === profileId);
appData.profiles[index] = { ...appData.profiles[index], ...newProfileData };
} else { // Add new
const newId = Math.max(0, ...appData.profiles.map(p => p.id)) + 1;
appData.profiles.push({ id: newId, ...newProfileData });
}
renderConfiguration();
renderCheckoutForm(); // Refresh autofill buttons
};
const generatePdf = () => {
if (!orderSummary) return;
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('Order Summary', pdf.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 20;
pdf.setFontSize(10).setFont('helvetica', 'normal').text(`Order #${Math.floor(Math.random() * 90000) + 10000}`, 40, y);
pdf.text(`Date: ${new Date().toLocaleDateString()}`, pdf.internal.pageSize.getWidth() - 40, y, { align: 'right' });
y += 30;
pdf.setFontSize(12).setFont('helvetica', 'bold').text('Shipping To:', 40, y);
y += 15;
pdf.setFontSize(10).setFont('helvetica', 'normal')
.text(`${orderSummary.shipping.fullName}\n${orderSummary.shipping.line1}\n${orderSummary.shipping.city}, ${orderSummary.shipping.state} ${orderSummary.shipping.zip}`, 40, y);
y += 60;
pdf.autoTable({
startY: y,
head: [['Item', 'Quantity', 'Price', 'Total']],
body: orderSummary.cart.map(item => [item.name, item.qty, formatCurrency(item.price), formatCurrency(item.price * item.qty)]),
theme: 'striped',
foot: [
[{ content: 'Total', colSpan: 3, styles: { halign: 'right', fontStyle: 'bold' } },
{ content: formatCurrency(orderSummary.total), styles: { fontStyle: 'bold' } }]
]
});
y = pdf.autoTable.previous.finalY + 30;
pdf.setFontSize(12).setFont('helvetica', 'bold').text('Payment Method', 40, y);
y += 15;
pdf.setFontSize(10).setFont('helvetica', 'normal').text(`Paid with ${orderSummary.payment.number}`, 40, y);
pdf.save(`Order-Summary.pdf`);
loadingOverlay.style.display = 'none';
};
// --- 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: 'Checkout Form', id: 'checkout' },
{ name: 'Profile Management', 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));
});
renderCheckoutForm();
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); });
});