`;
dashboardContainer.appendChild(card);
});
downloadPdfBtn.disabled = false;
};
const renderConfigTable = () => {
configTableBody.innerHTML = '';
orders.forEach(order => {
const row = document.createElement('tr');
row.dataset.id = order.id;
row.innerHTML = `
`;
configTableBody.appendChild(row);
});
};
// --- UI & EVENT HANDLERS ---
const switchTab = (tabId) => {
currentTab = tabId;
Object.values(tabPanes).forEach(pane => pane.classList.add('hidden'));
tabPanes[tabId].classList.remove('hidden');
Object.values(tabButtons).forEach(btn => btn.classList.replace('tab-active', 'tab-inactive'));
tabButtons[tabId].classList.replace('tab-inactive', 'tab-active');
updateNavButtons();
};
const navigateTabs = (direction) => {
const currentIndex = tabs.indexOf(currentTab);
const newIndex = direction === 'next' ? currentIndex + 1 : currentIndex - 1;
if (newIndex >= 0 && newIndex < tabs.length) {
// When moving from config to dashboard, refresh dashboard data
if (tabs[newIndex] === 'dashboard') renderDashboard();
switchTab(tabs[newIndex]);
}
};
const updateNavButtons = () => {
const currentIndex = tabs.indexOf(currentTab);
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === tabs.length - 1;
prevBtn.classList.toggle('opacity-50', prevBtn.disabled);
nextBtn.classList.toggle('opacity-50', nextBtn.disabled);
};
const handlePdfDownload = () => {
const pdfContent = document.getElementById('pdf-content');
const titleEl = document.createElement('h2');
titleEl.className = 'text-2xl font-bold text-gray-800 text-center mb-6';
titleEl.textContent = 'Supplier Order Status Report';
pdfContent.insertBefore(titleEl, pdfContent.firstChild);
html2canvas(pdfContent, { scale: 2, backgroundColor: '#ffffff' }).then(canvas => {
pdfContent.removeChild(titleEl);
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), margin = 40;
const contentWidth = pdfWidth - margin * 2;
const canvasAspectRatio = canvas.width / canvas.height;
const contentHeight = contentWidth / canvasAspectRatio;
pdf.addImage(imgData, 'PNG', margin, margin, contentWidth, contentHeight);
pdf.save('Order-Status-Report.pdf');
});
};
const simulateStatusUpdates = () => {
orders.forEach(order => {
if (order.status !== 'Delivered' && Math.random() > 0.6) { // 40% chance to update
const currentIndex = orderStatuses.indexOf(order.status);
if (currentIndex < orderStatuses.length - 1) {
order.status = orderStatuses[currentIndex + 1];
}
}
});
renderDashboard();
// also update the config table in the background
renderConfigTable();
};
// --- EVENT LISTENERS ---
window.switchTab = switchTab;
window.navigateTabs = navigateTabs;
searchInput.addEventListener('input', renderDashboard);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
refreshStatusesBtn.addEventListener('click', simulateStatusUpdates);
addOrderBtn.addEventListener('click', () => {
const newOrder = {
id: nextId++,
orderId: `US-NEW-${nextId}`,
product: 'New Product',
supplier: 'Default Supplier',
status: 'Processing'
};
orders.push(newOrder);
renderConfigTable();
});
configTableBody.addEventListener('input', e => {
if (e.target.classList.contains('config-input')) {
const id = parseInt(e.target.closest('tr').dataset.id);
const prop = e.target.dataset.prop;
const order = orders.find(o => o.id === id);
if (order) order[prop] = e.target.value;
}
});
configTableBody.addEventListener('click', e => {
if (e.target.classList.contains('remove-row-btn')) {
const idToRemove = parseInt(e.target.closest('tr').dataset.id);
orders = orders.filter(o => o.id !== idToRemove);
renderConfigTable();
}
});
// --- INITIALIZATION ---
renderDashboard();
renderConfigTable();
updateNavButtons();
switchTab('dashboard');
});
