No maintenance items added yet. Go to the 'Data Configuration' tab.
`;
return;
}
// Group items by frequency
const groupedItems = FREQUENCY_OPTIONS.reduce((acc, freq) => {
acc[freq] = [];
return acc;
}, {});
pam_data.items.forEach(item => {
if(groupedItems[item.frequency]) {
groupedItems[item.frequency].push(item);
}
});
// Render each frequency group in order
FREQUENCY_OPTIONS.forEach(freq => {
if (groupedItems[freq].length > 0) {
const sectionDiv = document.createElement('div');
const h3Class = "text-xl font-semibold text-blue-800 border-b border-gray-300 pb-2 mb-3";
const tableClass = "w-full text-left border-collapse";
const thClass = "p-3 bg-gray-100 text-sm font-semibold text-gray-600 border-b border-gray-300";
const tdClass = "p-3 border-b border-gray-200 text-sm";
let itemsHTML = groupedItems[freq].map(item => `
| ${pam_escapeHTML(item.asset)} |
${pam_escapeHTML(item.location)} |
${pam_escapeHTML(item.type)} |
${pam_escapeHTML(item.task)} |
`).join('');
sectionDiv.innerHTML = `
${freq} Maintenance
| Asset / Artwork |
Location |
Type |
Task |
${itemsHTML}
`;
targetDiv.appendChild(sectionDiv);
}
});
}
/**
* Renders a clone for PDF generation
*/
function pam_renderPdfClone() {
pam_pdfRenderClone.innerHTML = `
Public Art Maintenance Schedule
`;
// Render data into the clone's content area
const contentArea = pam_pdfRenderClone.querySelector('.space-y-6');
pam_renderDashboard(contentArea, true);
}
/**
* Generates and downloads a PDF of the schedule
*/
async function pam_downloadPDF() {
if (pam_data.items.length === 0) {
alert("Please add some maintenance items before downloading.");
return;
}
if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') {
console.error("PAM Tool Error: jsPDF or html2canvas library not loaded.");
alert("Error: PDF libraries failed to load. Please check console.");
return;
}
pam_renderPdfClone(); // Create and populate the clone
const { jsPDF } = window.jspdf;
try {
// Target the entire clone div
const canvas = await html2canvas(pam_pdfRenderClone, {
scale: 1.5, // Increase resolution slightly
useCORS: true,
windowWidth: pam_pdfRenderClone.scrollWidth,
windowHeight: pam_pdfRenderClone.scrollHeight // Capture full height
});
const imgData = canvas.toDataURL('image/png');
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const pdf = new jsPDF({ orientation: 'l', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
// Scale image height to fit pdf width, handle multiple pages
const margin = 40;
const contentWidth = pdfWidth - (margin * 2);
const contentHeight = (contentWidth * imgHeight) / imgWidth;
let heightLeft = contentHeight;
let position = 0; // y-position of the image slice on the page
// Add the first page
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
// Add subsequent pages if needed
while (heightLeft > 0) {
position -= (pdfHeight - margin * 2); // Move the image's y-position up
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
}
pdf.save('Public_Art_Maintenance_Schedule.pdf');
} catch (error) {
console.error("PAM Tool Error: PDF generation failed.", error);
alert("An error occurred while generating the PDF. Please try again.");
}
}
// --- EVENT LISTENERS ---
// Tab link clicks
pam_tabLinks.forEach((link, index) => {
link.addEventListener('click', () => pam_switchTab(index));
});
// Next/Prev button clicks
if (pam_prevButton) {
pam_prevButton.addEventListener('click', () => {
if (pam_currentTab > 0) pam_switchTab(pam_currentTab - 1);
});
}
if (pam_nextButton) {
pam_nextButton.addEventListener('click', () => {
// If on the last tab (Config tab, index 1)
if (pam_currentTab === pam_tabLinks.length - 1) {
// Act as Submit: Save data and switch to Dashboard (index 0)
pam_updateDataFromConfig();
pam_switchTab(0);
} else {
// Otherwise, just go to the next tab
if (pam_currentTab < pam_tabLinks.length - 1) pam_switchTab(pam_currentTab + 1);
}
});
}
// PDF download
if (pam_downloadPdfButton) {
pam_downloadPdfButton.addEventListener('click', pam_downloadPDF);
}
// --- Config Tab Listeners ---
if (pam_addItemButton) {
pam_addItemButton.addEventListener('click', () => {
pam_itemsContainer.appendChild(pam_createItemInput());
});
}
if (pam_configTab) {
// Handle remove
pam_configTab.addEventListener('click', (e) => {
const removeButton = e.target.closest('.pam-remove-item');
if (removeButton) {
const entryDiv = removeButton.closest('.border[data-id]');
const idToRemove = parseInt(entryDiv.getAttribute('data-id'));
pam_data.items = pam_data.items.filter(s => s.id !== idToRemove); // Remove from data
entryDiv.remove(); // Remove from UI
// Prevent having zero rows if applicable
if(pam_itemsContainer.children.length === 0){
pam_itemsContainer.appendChild(pam_createItemInput());
}
}
});
}
// --- INITIALIZATION ---
pam_initSampleData();
pam_renderConfig(); // Populate config tab on load
pam_renderDashboard(); // Show initial schedule on dashboard
// Set initial tab state
pam_tabPanes.forEach((pane, index) => {
pane.classList.toggle('hidden', index !== 0);
pane.classList.toggle('pam-active', index === 0);
});
pam_tabLinks.forEach((link, index) => {
TAB_CLASSES.active.forEach(cls => link.classList.remove(cls));
TAB_CLASSES.inactive.forEach(cls => link.classList.remove(cls));
if (index === 0) {
TAB_CLASSES.active.forEach(cls => link.classList.add(cls));
} else {
TAB_CLASSES.inactive.forEach(cls => link.classList.add(cls));
}
});
});