document.addEventListener('DOMContentLoaded', function() { // Initialize variables let products = []; let rtrAmount = 0; let taxRate = 8.25; // DOM elements const productList = document.getElementById('productList'); const addProductBtn = document.getElementById('addProductBtn'); const rtrCheckbox = document.getElementById('rtrCheckbox'); const rtrContainer = document.getElementById('rtrContainer'); const rtrAmountInput = document.getElementById('rtrAmount'); const taxRateInput = document.getElementById('taxRate'); const generateBtn = document.getElementById('generateBtn'); const downloadBtn = document.getElementById('downloadBtn'); const resetBtn = document.getElementById('resetBtn'); const receiptPreview = document.getElementById('receiptPreview'); const storeNameInput = document.getElementById('storeName'); const storeAddressInput = document.getElementById('storeAddress'); const storeCityInput = document.getElementById('storeCity'); const logoUpload = document.getElementById('logoUpload'); const logoPreview = document.getElementById('logoPreview'); // Event listeners addProductBtn.addEventListener('click', addProduct); rtrCheckbox.addEventListener('change', toggleRtr); rtrAmountInput.addEventListener('input', updateRtrAmount); taxRateInput.addEventListener('input', updateTaxRate); generateBtn.addEventListener('click', generateReceipt); downloadBtn.addEventListener('click', downloadPDF); resetBtn.addEventListener('click', resetForm); logoUpload.addEventListener('change', handleLogoUpload); // Handle logo upload function handleLogoUpload(event) { const file = event.target.files[0]; if (file) { const reader = new FileReader(); reader.onload = function(e) { logoPreview.src = e.target.result; logoPreview.classList.remove('hidden'); updateReceipt(); }; reader.readAsDataURL(file); } } // Add product function function addProduct() { const productId = Date.now(); const productItem = document.createElement('div'); productItem.className = 'product-item'; productItem.dataset.id = productId; productItem.innerHTML = ` `; productList.appendChild(productItem); feather.replace(); // Add event listener to remove button const removeBtn = productItem.querySelector('.remove-product'); removeBtn.addEventListener('click', () => { productList.removeChild(productItem); updateReceipt(); }); // Add event listeners to inputs const nameInput = productItem.querySelector('.product-name'); const priceInput = productItem.querySelector('.product-price'); nameInput.addEventListener('input', updateReceipt); priceInput.addEventListener('input', updateReceipt); } // Toggle RTR payment function toggleRtr() { rtrContainer.classList.toggle('hidden', !rtrCheckbox.checked); if (!rtrCheckbox.checked) { rtrAmount = 0; rtrAmountInput.value = ''; } updateReceipt(); } // Update RTR amount function updateRtrAmount() { rtrAmount = parseFloat(rtrAmountInput.value) || 0; updateReceipt(); } // Update tax rate function updateTaxRate() { taxRate = parseFloat(taxRateInput.value) || 0; updateReceipt(); } // Generate receipt preview function updateReceipt() { // Get all products products = []; const productItems = document.querySelectorAll('.product-item'); productItems.forEach(item => { const name = item.querySelector('.product-name').value; const price = parseFloat(item.querySelector('.product-price').value) || 0; if (name && price > 0) { products.push({ name, price }); } }); // Calculate totals const subtotal = products.reduce((sum, product) => sum + product.price, 0); const taxableAmount = rtrCheckbox.checked ? subtotal : subtotal + rtrAmount; const tax = taxableAmount * (taxRate / 100); const total = subtotal + tax + (rtrCheckbox.checked ? rtrAmount : 0); // Format current date and time const now = new Date(); const date = now.toLocaleDateString('en-US', { month: '2-digit', day: '2-digit', year: 'numeric' }); const time = now.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit' }); // Generate receipt HTML let receiptHTML = ''; // Add logo if uploaded if (logoPreview.src && !logoPreview.classList.contains('hidden')) { receiptHTML += `