No license data to display. Add licenses in the Data Configuration tab.
';
return;
}
const table = document.createElement('table');
table.id = "dashboard-table-for-pdf";
table.className = 'min-w-full divide-y divide-gray-200';
table.innerHTML = `
| Status |
License Name |
Issuing Authority |
Renewal Date |
Cost |
`;
const tbody = table.querySelector('tbody');
const statusColors = {
red: 'bg-red-100 text-red-800',
yellow: 'bg-yellow-100 text-yellow-800',
green: 'bg-green-100 text-green-800'
};
licenses.forEach(license => {
const status = getStatus(license.date);
const row = document.createElement('tr');
row.innerHTML = `
${status.text}
|
${license.name} |
${license.authority} |
${new Date(license.date).toLocaleDateString()} |
$${parseFloat(license.cost).toFixed(2)} |
`;
tbody.appendChild(row);
});
licenseDashboardView.appendChild(table);
}
// --- TAB NAVIGATION ---
window.changeTab = (tabIndex) => {
if (tabIndex === currentTab) return;
tabButtons[currentTab].classList.remove('active');
tabContents[currentTab].classList.remove('active');
currentTab = tabIndex;
tabButtons[currentTab].classList.add('active');
tabContents[currentTab].classList.add('active');
updateNavButtons();
};
window.navigateTabs = (direction) => {
const newTab = direction === 'next' ? currentTab + 1 : currentTab - 1;
if (newTab >= 0 && newTab < tabButtons.length) {
changeTab(newTab);
}
};
function updateNavButtons() {
prevBtn.style.visibility = currentTab === 0 ? 'hidden' : 'visible';
nextBtn.style.visibility = currentTab === tabButtons.length - 1 ? 'hidden' : 'visible';
pdfDownloadSection.style.display = currentTab === 0 ? 'block' : 'none';
}
// --- EVENT HANDLING ---
if (licenseForm) {
licenseForm.addEventListener('submit', function(e) {
e.preventDefault();
const id = licenseIdInput.value ? parseInt(licenseIdInput.value) : null;
const licenseData = {
name: document.getElementById('license-name').value,
authority: document.getElementById('issuing-authority').value,
date: document.getElementById('renewal-date').value,
cost: document.getElementById('renewal-cost').value,
};
if (id) {
const index = licenses.findIndex(l => l.id === id);
if (index !== -1) licenses[index] = { ...licenses[index], ...licenseData };
} else {
licenses.push({ id: Date.now(), ...licenseData });
}
sortLicenses();
renderAll();
resetForm();
});
}
if (cancelEditBtn) {
cancelEditBtn.addEventListener('click', resetForm);
}
window.app = {
editLicense: (id) => {
const license = licenses.find(l => l.id === id);
if (!license) return;
licenseIdInput.value = license.id;
document.getElementById('license-name').value = license.name;
document.getElementById('issuing-authority').value = license.authority;
document.getElementById('renewal-date').value = license.date;
document.getElementById('renewal-cost').value = license.cost;
formTitle.textContent = 'Edit License';
cancelEditBtn.style.display = 'inline-block';
licenseForm.querySelector('button[type="submit"]').textContent = 'Update License';
changeTab(1);
},
deleteLicense: (id) => {
// A custom modal would be better than confirm()
if (confirm('Are you sure you want to delete this license?')) {
licenses = licenses.filter(l => l.id !== id);
renderAll();
}
}
};
function resetForm() {
licenseForm.reset();
licenseIdInput.value = '';
formTitle.textContent = 'Add New License';
cancelEditBtn.style.display = 'none';
licenseForm.querySelector('button[type="submit"]').textContent = 'Save License';
}
// --- PDF GENERATION ---
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', function() {
const { jsPDF } = window.jspdf;
const contentToCapture = document.getElementById('dashboard-table-for-pdf');
if (!contentToCapture) {
console.error("Dashboard content for PDF not found.");
return;
}
html2canvas(contentToCapture, { scale: 2, useCORS: true }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'landscape',
unit: 'px',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
let imgWidth = pdfWidth - 40;
let imgHeight = imgWidth / ratio;
if (imgHeight > pdfHeight - 60) {
imgHeight = pdfHeight - 60;
imgWidth = imgHeight * ratio;
}
let position = 20;
pdf.setFontSize(20);
pdf.setFont('helvetica', 'bold');
pdf.text("Business License Renewal Report", pdfWidth / 2, position + 10, { align: 'center' });
position += 30;
pdf.addImage(imgData, 'PNG', (pdfWidth - imgWidth) / 2, position, imgWidth, imgHeight);
const pageCount = pdf.internal.getNumberOfPages();
for(let i = 1; i <= pageCount; i++) {
pdf.setPage(i);
pdf.setFontSize(10);
pdf.setTextColor(150);
pdf.text('Page ' + i + ' of ' + pageCount, pdfWidth - 40, pdfHeight - 10);
pdf.text(`Report Generated: ${new Date().toLocaleDateString()}`, 20, pdfHeight - 10);
}
pdf.save('Business-License-Report.pdf');
});
});
}
// --- START THE APP ---
initializeApp();
});