// Version: v5 document.addEventListener('DOMContentLoaded', () => { const printBtn = document.getElementById('printBtn'); const clearBtn = document.getElementById('clearBtn'); // Set default date to current date const today = new Date().toISOString().split('T')[0]; invoiceDateInput.value = today; // Set default invoice number invoiceNumberInput.value = "001"; // Event Listeners for input changes invoiceDateInput.addEventListener('change', updateInvoice); invoiceNumberInput.addEventListener('change', updateInvoice); courseTopicSelect.addEventListener('change', updateInvoice); // Listen to new dropdown // Print button functionality with validation printBtn.addEventListener('click', () => { if (!invoiceDateInput.value || !invoiceNumberInput.value || !courseTopicSelect.value) { alert('Please fill in all required fields: Date, Invoice Number, and Topic.'); return; } // Generate file name in the format: GST_Invoice_INV-MM-DD-YYYY.pdf const dt = new Date(invoiceDateInput.value); const MM = String(dt.getMonth() + 1).padStart(2, '0'); const DD = String(dt.getDate()).padStart(2, '0'); const YYYY = dt.getFullYear(); const invoiceNum = invoiceNumberInput.value; // Assuming this holds the invoice number const generatedFileName = `GST_Invoice_${invoiceNum}-${MM}-${DD}-${YYYY}.pdf`; console.log('Generated file name:', generatedFileName); // Set document title to the filename (without .pdf) for dynamic PDF save name document.title = `GST_Invoice_${invoiceNum}-${MM}-${DD}-${YYYY}`; // First, ensure the invoice content is fully updated updateInvoice(); // Then, trigger the print dialog window.print(); }); // Clear button functionality clearBtn.addEventListener('click', () => { // Clear all input fields and reset the invoice display invoiceDateInput.value = today; invoiceNumberInput.value = "001"; courseTopicSelect.value = ''; // Call updateInvoice to reset displayed values to their defaults updateInvoice(); }); // Initial population of the dropdown when the page loads populateTopicsDropdown(); // Initial invoice update to show default values updateInvoice(); });