Smart Drone & Autonomous Delivery Cost Calculator

Smart Drone & Autonomous Delivery Cost Calculator

Analyze and compare the costs of next-generation delivery methods.

with ${summary.lowestCostMethod}

Monthly Cost Breakdown

${tableRows}
MethodCost/DeliveryCapitalMaintenanceEnergyLaborTotal
`; renderCostChart(breakdown); attachDashboardListeners(); }; const renderCostChart = (breakdown) => { const ctx = document.getElementById('cost-comparison-chart').getContext('2d'); if (charts.cost) charts.cost.destroy(); charts.cost = new Chart(ctx, { type: 'bar', data: { labels: Object.keys(breakdown), datasets: [ { label: 'Capital', data: Object.values(breakdown).map(d => d.capitalCost), backgroundColor: '#60a5fa' }, { label: 'Maintenance', data: Object.values(breakdown).map(d => d.maintenanceCost), backgroundColor: '#facc15' }, { label: 'Energy', data: Object.values(breakdown).map(d => d.energyCost), backgroundColor: '#fb923c' }, { label: 'Labor', data: Object.values(breakdown).map(d => d.laborCost), backgroundColor: '#f87171' }, ] }, options: { responsive: true, maintainAspectRatio: false, scales: { x: { stacked: true }, y: { stacked: true, ticks: { callback: (v) => formatCurrency(v, 0) } } }, plugins: { title: { display: true, text: 'Total Monthly Cost Comparison' } } } }); }; const renderDataConfig = () => { const configContent = document.getElementById('content-data-configuration'); if (!configContent) return; const methodsHtml = Object.entries(appData.methods).map(([name, data]) => `

${name}

`).join(''); configContent.innerHTML = `

General Cost Assumptions

Delivery Method Parameters

${methodsHtml}
`; attachConfigListeners(); }; // --- EVENT HANDLERS & LOGIC --- const attachDashboardListeners = () => { const handler = () => { appData.inputs.deliveriesPerMonth = parseFloat(document.getElementById('deliveries-per-month').value) || 0; appData.inputs.avgDistance = parseFloat(document.getElementById('avg-distance').value) || 0; appData.results = null; renderDashboard(); }; document.getElementById('deliveries-per-month').addEventListener('input', handler); document.getElementById('avg-distance').addEventListener('input', handler); document.getElementById('download-pdf-btn').addEventListener('click', generatePdf); }; const attachConfigListeners = () => { document.querySelectorAll('.config-input').forEach(input => { input.addEventListener('change', e => { const value = parseFloat(e.target.value); if(e.target.dataset.method) { const method = e.target.dataset.method; const field = e.target.dataset.field; appData.methods[method][field] = value; } else { if(e.target.id === 'energy-cost') appData.general.energyCostPerKWh = value; if(e.target.id === 'labor-cost') appData.general.laborCostPerHour = value; } appData.results = null; // force recalculation renderDashboard(); }); }); }; const generatePdf = () => { loadingOverlay.style.display = 'flex'; const { jsPDF } = window.jspdf; document.getElementById('pdf-generated-date').textContent = new Date().toLocaleString(); 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('Autonomous-Delivery-Cost-Analysis.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: 'Cost Dashboard', id: 'cost-dashboard' }, { name: 'Data Configuration', id: 'data-configuration' } ]; 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(); prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); }); nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); }); });
Scroll to Top