Live Order Feed
| Order ID | Customer | Progress | Status | Last Update |
${tableRows}
`;
lucide.createIcons();
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-manage-orders');
if (!configContent) return;
const tableRows = appData.orders.map(o => `
|
|
|
|
`).join('');
configContent.innerHTML = `
Manage Orders
| Order ID | Customer | Status | |
${tableRows}
`;
attachConfigListeners();
};
// --- EVENT HANDLERS & REAL-TIME SIMULATION ---
const attachConfigListeners = () => {
document.querySelectorAll('.config-input').forEach(input => input.addEventListener('change', e => {
const id = parseInt(e.target.dataset.id);
const field = e.target.dataset.field;
const value = e.target.value;
const order = appData.orders.find(o => o.id === id);
if (order) order[field] = value;
appData.analysisResults = null;
}));
document.querySelectorAll('.remove-order-btn').forEach(btn => btn.addEventListener('click', e => {
const id = parseInt(e.currentTarget.dataset.id);
appData.orders = appData.orders.filter(o => o.id !== id);
appData.analysisResults = null;
renderDataConfig();
}));
document.getElementById('add-order-btn').addEventListener('click', () => {
const newId = appData.orders.length > 0 ? Math.max(...appData.orders.map(o => o.id)) + 1 : 1;
appData.orders.push({ id: newId, orderId: `US-NEW-${newId}`, customer: 'New Customer', status: 'Order Placed', lastUpdate: new Date(), history: [{ status: 'Order Placed', timestamp: new Date() }] });
renderDataConfig();
});
};
const startSimulation = () => {
if(simulationInterval) clearInterval(simulationInterval);
simulationInterval = setInterval(() => {
const activeOrders = appData.orders.filter(o => o.status !== 'Delivered');
if(activeOrders.length === 0) return;
const orderToUpdate = activeOrders[Math.floor(Math.random() * activeOrders.length)];
if (Math.random() < 0.1 && orderToUpdate.status !== 'Delayed' && orderToUpdate.status !== 'Delivered') {
orderToUpdate.status = STATUS_DELAYED;
} else {
const currentIndex = STATUS_OPTIONS.indexOf(orderToUpdate.status);
if (currentIndex !== -1 && currentIndex < STATUS_OPTIONS.length - 1) {
orderToUpdate.status = STATUS_OPTIONS[currentIndex + 1];
} else if (orderToUpdate.status === STATUS_DELAYED) { // If delayed, revert to previous non-delayed status
const lastNonDelayedStatus = orderToUpdate.history.filter(h => h.status !== STATUS_DELAYED).pop()?.status || 'Processing';
const lastStatusIndex = STATUS_OPTIONS.indexOf(lastNonDelayedStatus);
if(lastStatusIndex < STATUS_OPTIONS.length -1) {
orderToUpdate.status = STATUS_OPTIONS[lastStatusIndex + 1];
}
}
}
orderToUpdate.lastUpdate = new Date();
orderToUpdate.history.push({ status: orderToUpdate.status, timestamp: new Date() });
appData.analysisResults = null;
if(activeTabIndex === 0) renderDashboard();
}, 5000); // Update every 5 seconds
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleDateString('en-US');
const pdfHeader = document.getElementById('pdf-header');
pdfHeader.classList.remove('hidden');
const fullContent = document.getElementById('pdf-content-area');
html2canvas(fullContent, { scale: 2, useCORS: true, logging: false, windowWidth: 1200 })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.9);
const pdf = new jsPDF({ orientation: 'landscape', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight);
pdf.save('OrderStatus-Report.pdf');
loadingOverlay.style.display = 'none';
}).catch(err => {
console.error("PDF generation failed:", err);
pdfHeader.classList.add('hidden');
loadingOverlay.style.display = 'none';
alert('An error occurred generating the PDF.');
});
};
// --- TAB NAVIGATION & INITIALIZATION ---
const switchTab = (tabIndex) => {
activeTabIndex = tabIndex;
document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex));
document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex));
updateNavButtons();
};
const updateNavButtons = () => {
prevTabBtn.disabled = activeTabIndex === 0;
nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1;
};
const initializeUI = () => {
const tabs = [
{ name: 'Tracking Dashboard', id: 'tracking-dashboard' },
{ name: 'Manage Orders', id: 'manage-orders' }
];
tabIdentifiers = tabs.map(t => t.id);
tabsContainer.innerHTML = tabs.map(tab => `
`).join('');
mainContent.innerHTML = tabs.map(tab => `
`).join('');
tabs.forEach((tab, index) => {
document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index));
});
renderDashboard();
renderDataConfig();
switchTab(0);
startSimulation();
};
initializeUI();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});