Delivery Timeline Breakdown
Order Placed:
${formatDate(calculationResult.orderDate)}
Processing Starts:
${formatDate(calculationResult.processingStartDate)}
Estimated Ship Date (+${calculationResult.totalProcessingDays} processing days):
${formatDate(calculationResult.shipDate)}
Estimated Delivery (+${calculationResult.totalMinTransitDays}-${calculationResult.totalMaxTransitDays} transit days):
${formatDate(calculationResult.minDeliveryDate)} - ${formatDate(calculationResult.maxDeliveryDate)}
`;
}
dashboardContent.innerHTML = `
Calculate Delivery
${resultHtml}
`;
document.getElementById('calculate-btn').addEventListener('click', () => {
appData.currentOrder.orderDateTime = document.getElementById('order-datetime').value;
appData.currentOrder.shippingMethodName = document.getElementById('shipping-method').value;
runCalculation();
renderDashboard();
});
if (calculationResult) {
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
}
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-config');
if (!configContent) return;
const methodsHtml = appData.shippingMethods.map((method, index) => `
`).join('');
const holidaysHtml = appData.holidays.map((holiday, index) => `
`).join('');
configContent.innerHTML = `
`;
attachConfigListeners();
};
const attachConfigListeners = () => {
document.getElementById('add-method-btn').addEventListener('click', () => {
appData.shippingMethods.push({ name: 'New Method', minDays: 0, maxDays: 0 });
renderDataConfig();
lucide.createIcons();
});
document.getElementById('add-holiday-btn').addEventListener('click', () => {
appData.holidays.push(new Date().toISOString().slice(0, 10));
renderDataConfig();
lucide.createIcons();
});
document.getElementById('methods-container').addEventListener('click', e => {
if (e.target.closest('.remove-method-btn')) {
appData.shippingMethods.splice(parseInt(e.target.closest('.remove-method-btn').dataset.index), 1);
renderDataConfig();
lucide.createIcons();
}
});
document.getElementById('holidays-container').addEventListener('click', e => {
if (e.target.closest('.remove-holiday-btn')) {
appData.holidays.splice(parseInt(e.target.closest('.remove-holiday-btn').dataset.index), 1);
renderDataConfig();
lucide.createIcons();
}
});
document.getElementById('update-data-btn').addEventListener('click', handleConfigUpdate);
};
const handleConfigUpdate = () => {
appData.processingDays = parseInt(document.getElementById('processingDays').value) || 1;
appData.cutoffTime = document.getElementById('cutoffTime').value;
appData.workingDays = Array.from(document.querySelectorAll('.working-day-cb:checked')).map(cb => parseInt(cb.value));
appData.shippingMethods = Array.from(document.querySelectorAll('.method-row')).map(row => ({
name: row.querySelector('[data-field="name"]').value,
minDays: parseInt(row.querySelector('[data-field="minDays"]').value) || 0,
maxDays: parseInt(row.querySelector('[data-field="maxDays"]').value) || 0,
}));
appData.holidays = Array.from(document.querySelectorAll('.holiday-row input')).map(input => input.value).filter(Boolean);
calculationResult = null; // Invalidate old result
renderDashboard();
alert('Configuration saved!');
switchTab(0);
};
const generatePdf = () => {
if (!calculationResult) {
alert('Please run a calculation first.');
return;
}
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content-area');
const pdfHeader = document.getElementById('pdf-header');
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
pdfHeader.classList.remove('hidden');
html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.95);
const pdf = new jsPDF({ orientation: 'portrait', 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('Delivery-Estimate-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: 'Delivery Calculator', id: 'dashboard' },
{ name: 'Configuration', id: 'data-config' }
];
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);
};
initializeUI();
lucide.createIcons();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});