| <!doctype html> |
| <html lang="en"> |
| <head> |
| <meta charset="utf-8"/> |
| <meta name="viewport" content="width=device-width, initial-scale=1"/> |
| <title>Route Optimizer</title> |
| <link rel="stylesheet" href="/static/suite.css"/> |
| <style> |
| .mono{font-family:ui-monospace,Menlo,Consolas,monospace} |
| .grid.two-col{grid-template-columns:1fr} |
| @media (min-width:1000px){.grid.two-col{grid-template-columns:1fr 1fr}} |
| .stops{display:grid;gap:12px} |
| .actions{display:flex;gap:12px;flex-wrap:wrap;margin-top:12px} |
| .stats{display:grid;gap:12px;grid-template-columns:1fr 1fr; margin:8px 0} |
| @media (min-width:900px){.stats{grid-template-columns:repeat(4,1fr)}} |
| .pill{background:#fff;border:1px solid var(--border);border-radius:12px;padding:10px} |
| .pill .k{font-weight:800;font-size:18px} |
| .pill .l{color:var(--muted);font-size:12px;margin-top:4px} |
| .pill.good{border-color:#22c55e33;background:#ecfdf5} |
| .stop-remove{appearance:none;border:0;background:#f3f4f6;color:#111827;padding:8px 10px;border-radius:8px;font-weight:700;cursor:pointer} |
| .stop-remove:hover{background:#e5e7eb} |
| </style> |
| </head> |
| <body> |
| <div id="suite-shared-header"></div> |
| <div class="container"> |
| <h1>Route Optimizer</h1> |
| <p class="muted">Enter origin and destinations to get the fastest route, ETAs, and savings.</p> |
| <div class="grid" id="grid"> |
| <div class="card"> |
| <div class="form-row"> |
| <div> |
| <label for="originLabel">Origin label</label> |
| <input id="originLabel" type="text" placeholder="Warehouse"/> |
| </div> |
| <div> |
| <label for="origin">Origin address</label> |
| <input id="origin" type="text" placeholder="1600 Amphitheatre Pkwy, Mountain View, CA"/> |
| </div> |
| </div> |
| <label for="fuel">Fuel cost per km</label> |
| <input id="fuel" type="number" min="0" step="0.01" value="0.2"/> |
| <h2>Destinations</h2> |
| <div id="stops" class="stops"></div> |
| <div class="actions"> |
| <button id="addStop" class="btn" type="button">Add destination</button> |
| <button id="optimize" class="btn" type="button">Optimize</button> |
| </div> |
| </div> |
| <div id="out" class="card" style="display:none"></div> |
| </div> |
| </div> |
| <script src="/static/header.js"></script> |
| <script> |
| const stopsEl = document.getElementById('stops'); |
| const addStopBtn = document.getElementById('addStop'); |
| const out = document.getElementById('out'); |
| const grid = document.getElementById('grid'); |
| function stopRow(i){ |
| const wrap = document.createElement('div'); |
| wrap.className='form-row'; |
| wrap.innerHTML = ` |
| <div> |
| <label>Label</label> |
| <input type="text" placeholder="Stop ${i+1}"/> |
| </div> |
| <div> |
| <label>Address</label> |
| <input type="text" placeholder="address line"/> |
| </div> |
| <div style="align-self:flex-end;"> |
| <button class="stop-remove" type="button">Remove</button> |
| </div> |
| `; |
| wrap.querySelector('.stop-remove').addEventListener('click', ()=>{ |
| wrap.remove(); |
| }); |
| return wrap; |
| } |
| function addStop(){ |
| stopsEl.appendChild(stopRow(stopsEl.children.length)); |
| } |
| addStopBtn.addEventListener('click', ()=>{ |
| addStop(); |
| }); |
| // seed 5 |
| for(let i=0;i<5;i++) addStop(); |
| |
| document.getElementById('optimize').addEventListener('click', async ()=>{ |
| const origin = { |
| label: document.getElementById('originLabel').value || 'Origin', |
| address: document.getElementById('origin').value || '' |
| }; |
| const dests = Array.from(stopsEl.children).map(row=>{ |
| const inputs = row.querySelectorAll('input'); |
| return { label: inputs[0].value || 'Stop', address: inputs[1].value || '' }; |
| }).filter(d=>d.address.trim().length>0); |
| const fuel = parseFloat(document.getElementById('fuel').value || '0.2'); |
| out.style.display='block'; |
| grid.classList.add('two-col'); |
| out.innerHTML = '<div class="muted">(optimizing...)</div>'; |
| try{ |
| const res = await fetch('/route/optimize', {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({origin, destinations:dests, fuel_cost_per_km:fuel})}); |
| if(!res.ok) throw new Error(await res.text()); |
| const data = await res.json(); |
| const legs = (data.route||[]).map(s=>`<tr><td>${s.order}</td><td>${s.label}</td><td>${s.address}</td><td>${s.distance_km.toFixed(2)} km</td><td>${s.eta_minutes} min</td></tr>`).join(''); |
| // Visual: simple SVG polyline map approximation using normalized lat/lng |
| let svg = ''; |
| try{ |
| const pts = (data.points||[]); |
| const ord = (data.order||[]); |
| if(pts.length && ord.length){ |
| const lats = ord.map(i=>pts[i].lat); |
| const lngs = ord.map(i=>pts[i].lng); |
| const minLat=Math.min(...lats), maxLat=Math.max(...lats); |
| const minLng=Math.min(...lngs), maxLng=Math.max(...lngs); |
| const pad=12, W=440, H=280; |
| const norm=(lat,lng)=>[ |
| pad + ( (lng-minLng)/(maxLng-minLng||1) )*(W-2*pad), |
| pad + ( 1-((lat-minLat)/(maxLat-minLat||1)) )*(H-2*pad) |
| ]; |
| const pointsStr = ord.map(i=>{ |
| const p = norm(pts[i].lat, pts[i].lng); |
| return p[0].toFixed(1)+','+p[1].toFixed(1); |
| }).join(' '); |
| const markers = ord.map((i,idx)=>{ |
| const p = norm(pts[i].lat, pts[i].lng); |
| return `<circle cx="${p[0].toFixed(1)}" cy="${p[1].toFixed(1)}" r="4" fill="#111827"/><text x="${(p[0]+6).toFixed(1)}" y="${(p[1]-6).toFixed(1)}" font-size="10" fill="#111827">${idx}</text>`; |
| }).join(''); |
| svg = `<svg viewBox="0 0 ${W} ${H}" style="width:100%;max-width:520px;border:1px solid var(--border);border-radius:8px;background:#fff"> |
| <polyline points="${pointsStr}" fill="none" stroke="#3b82f6" stroke-width="2"/> |
| ${markers} |
| </svg>`; |
| } |
| }catch(_){ svg=''; } |
| |
| const stats = ` |
| <div class="stats"> |
| <div class="pill"><div class="k">${data.total_distance_km.toFixed(1)} km</div><div class="l">Distance</div></div> |
| <div class="pill"><div class="k">${data.total_time_minutes} min</div><div class="l">Time</div></div> |
| <div class="pill"><div class="k">$${data.estimated_fuel_cost.toFixed(2)}</div><div class="l">Fuel</div></div> |
| <div class="pill good"><div class="k">$${data.estimated_savings.toFixed(2)}</div><div class="l">Savings</div></div> |
| </div>`; |
| |
| out.innerHTML = ` |
| <div><strong>Summary</strong></div> |
| <p style="white-space:pre-wrap;">${(data.summary||'').replace(/</g,'<').replace(/>/g,'>')}</p> |
| ${stats} |
| ${svg ? `<div style=\"margin:8px 0;\"><strong>Route preview</strong></div>${svg}` : ''} |
| ${data.maps_embed_url ? `<div style=\"margin-top:8px;\"><strong>Google Maps</strong></div><iframe title=\"Route\" width=\"100%\" height=\"360\" style=\"border:0;border-radius:10px;\" loading=\"lazy\" referrerpolicy=\"no-referrer-when-downgrade\" src=\"${data.maps_embed_url}\"></iframe>` : (data.maps_url ? `<div style=\"margin-top:8px;\"><a class=\"btn\" href=\"${data.maps_url}\" target=\"_blank\" rel=\"noopener\">Open in Google Maps</a></div>` : '')} |
| <table class="table" style="margin-top:8px;"> |
| <thead><tr><th>#</th><th>Label</th><th>Address</th><th>Distance</th><th>ETA</th></tr></thead> |
| <tbody>${legs}</tbody> |
| </table> |
| <div style="margin-top:8px;"> |
| <strong>Total:</strong> ${data.total_distance_km.toFixed(2)} km, ${data.total_time_minutes} min |
| </div> |
| <div><strong>Fuel cost:</strong> $${data.estimated_fuel_cost.toFixed(2)} | <strong>Estimated savings:</strong> $${data.estimated_savings.toFixed(2)}</div> |
| `; |
| }catch(e){ |
| out.innerHTML = `<div style="color:#b91c1c;">Error: ${String(e)}</div>`; |
| } |
| }); |
| </script> |
| </body> |
| </html> |
|
|
|
|
|
|