1import{r as l,j as e,u as He,M as _e,T as Ge,P as qe,C as he,f as fe}from"./react-vendor-pafHdoJx.js";import{a as Fe,d as Se,c as p,r as We,b as Ze,Q as K,S as J,T as ne,a8 as Qe,n as je,M as Ke,a9 as De,o as Je,aa as Xe}from"./index-Db0fDLGN.js";import{z as Ye,r as et,a as tt}from"./firebase-vendor-D3UzkAhn.js";import{H as $e,P as st}from"./HelpTooltip-DKugySwl.js";import{an as Ne,X as Re,a8 as oe,aG as pe,u as X,y as ke,a_ as at,a$ as rt,ba as lt,z as nt,U as Pe,a6 as Oe,N as Ae,o as Be,I as ot,L as ze,as as it,aB as ge,r as Le,at as Me,aR as ct,aF as dt,s as Ve,l as Te,a4 as xt,D as mt,n as ut,d1 as pt,d2 as ht,a5 as Ee,F as ft,d3 as gt,Z as bt}from"./ui-vendor-DLXJn6s3.js";import{c as vt,d as jt,g as Nt,h as le,e as yt,a as wt}from"./export-utils-BHX8wKd9.js";import{E as St}from"./ExportMenu-kt6fodhv.js";import{E as kt}from"./ErrorState-B9i6eo2M.js";import"./map-vendor-Dr8zbd0c.js";import Ct from"./TrackingPage-BxX1D6w_.js";function Dt(c){if(!(c instanceof HTMLElement))return!1;const r=c.tagName;return!!(r==="INPUT"||r==="TEXTAREA"||r==="SELECT"||c.isContentEditable)}function Lt(c,r={}){const{enabled:g=!0,allowInInputs:a=!1,onEnter:S,onExit:j}=r,[$,R]=l.useState(!1);return l.useEffect(()=>{if(!g){$&&(R(!1),j==null||j());return}const E=c.toLowerCase();function k(N){!a&&Dt(N.target)||N.key.toLowerCase()===E&&!N.repeat&&R(A=>(A||S==null||S(),!0))}function C(N){N.key.toLowerCase()===E&&R(A=>(A&&(j==null||j()),!1))}function q(){R(N=>(N&&(j==null||j()),!1))}return window.addEventListener("keydown",k),window.addEventListener("keyup",C),window.addEventListener("blur",q),()=>{window.removeEventListener("keydown",k),window.removeEventListener("keyup",C),window.removeEventListener("blur",q)}},[c,g,a,S,j,$]),$}const Mt=300*1e3,Tt=900*1e3,Ie={street:{url:"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",attribution:"© OpenStreetMap contributors"},satellite:{url:"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}",attribution:"© Esri, Maxar, Earthstar Geographics"}};function ye(c){const r=Date.now()-c;return r<6e4?"agora":r<36e5?`${Math.floor(r/6e4)} min`:r<864e5?`${Math.floor(r/36e5)}h`:`${Math.floor(r/864e5)}d`}function we(c){return`${Math.round(c*3.6)} km/h`}function Ue(c){return["â","â","â","â","â","â","â","â"][Math.round(c/45)%8]}function Q(c){const r=Date.now()-c.timestamp;return r>Tt?"offline":r>Mt?"stale":c.isMoving?"moving":"stopped"}const ue={moving:{label:"Em movimento",color:"#10B981",dotCls:"bg-emerald-400",textCls:"text-emerald-400"},stopped:{label:"Parado",color:"#F59E0B",dotCls:"bg-amber-400",textCls:"text-amber-400"},stale:{label:"Sem sinal recente",color:"#F97316",dotCls:"bg-orange-400",textCls:"text-orange-400"},offline:{label:"Offline",color:"#6B7280",dotCls:"bg-slate-500",textCls:"text-slate-500"}};function Et({driver:c,name:r,onClose:g}){const a=Q(c),S=ue[a];return e.jsxs("div",{className:"absolute bottom-4 left-4 right-4 md:left-auto md:right-4 md:bottom-4 md:w-80 bg-slate-900/95 backdrop-blur-md border border-slate-700 rounded-2xl shadow-2xl z-[1000] overflow-hidden",children:[e.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-slate-800",children:[e.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[e.jsx("div",{className:"w-10 h-10 rounded-full flex items-center justify-center font-bold text-sm flex-shrink-0",style:{background:S.color,color:"#ffffff"},children:(r[0]||"M").toUpperCase()}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-bold text-white truncate",children:r}),e.jsx("span",{className:p("text-xs font-medium",S.textCls),children:S.label})]})]}),e.jsx("button",{onClick:g,className:"p-1 rounded-lg hover:bg-slate-800 text-slate-500 hover:text-slate-300 transition-colors",children:e.jsx(Re,{className:"w-4 h-4"})})]}),e.jsxs("div",{className:"p-4 space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"p-2.5 rounded-xl bg-slate-800/50 border border-slate-700/50",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wider mb-1",children:"Velocidade"}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(pe,{className:"w-3.5 h-3.5 text-blue-400"}),e.jsx("span",{className:"text-sm font-bold",style:{color:"var(--page-text)"},children:we(c.speed)})]})]}),e.jsxs("div",{className:"p-2.5 rounded-xl bg-slate-800/50 border border-slate-700/50",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wider mb-1",children:"Direção"}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(Ae,{className:"w-3.5 h-3.5 text-teal-400"}),e.jsxs("span",{className:"text-sm font-bold",style:{color:"var(--page-text)"},children:[Ue(c.heading)," ",Math.round(c.heading),"°"]})]})]}),e.jsxs("div",{className:"p-2.5 rounded-xl bg-slate-800/50 border border-slate-700/50",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wider mb-1",children:"Ãltima atualização"}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(Be,{className:"w-3.5 h-3.5 text-amber-400"}),e.jsx("span",{className:"text-sm font-bold",style:{color:"var(--page-text)"},children:ye(c.timestamp)})]})]}),e.jsxs("div",{className:"p-2.5 rounded-xl bg-slate-800/50 border border-slate-700/50",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wider mb-1",children:"Precisão GPS"}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(ke,{className:"w-3.5 h-3.5 text-purple-400"}),e.jsxs("span",{className:"text-sm font-bold",style:{color:"var(--page-text)"},children:[Math.round(c.accuracy||0),"m"]})]})]})]}),c.tripId&&e.jsxs("div",{className:"flex items-center gap-2 p-2.5 rounded-xl bg-brand-600/10 border border-brand-500/20",children:[e.jsx(X,{className:"w-4 h-4 text-brand-400"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-brand-400",children:"Em viagem"}),e.jsxs("p",{className:"text-[10px] text-slate-500 font-mono",children:[c.tripId.slice(0,20),"..."]})]})]}),e.jsxs("div",{className:"text-[10px] text-slate-600 flex items-center gap-1.5 pt-1",children:[e.jsx(ot,{className:"w-3 h-3"}),"Coordenadas: ",c.latitude.toFixed(5),", ",c.longitude.toFixed(5)]})]})]})}function It(){const{effectiveOrgId:c}=Fe(),[r,g]=l.useState([]),[a,S]=l.useState(null),[j,$]=l.useState(!1),[R,E]=l.useState(!0),[k,C]=l.useState({}),[q,N]=l.useState(0),A=Lt(" "),[o,v]=l.useState(""),[b,I]=l.useState("all"),[P,V]=l.useState("street"),[U,te]=l.useState(!1),Y=l.useRef(null),M=l.useRef(null),h=l.useRef({}),T=l.useRef(null),D=l.useRef(null),ie=l.useRef(!1),w=l.useRef(!1);l.useEffect(()=>{c&&Se.fleetHealthDriverNames().then(s=>{const x={},i=new Set;for(const[u,f]of Object.entries(s))i.add(u),f&&f.name?x[u]=f.name:f&&f.email&&(x[u]=f.email);C(x),de(i),Z(!0)}).catch(s=>{Z(!0)})},[c]);const ce=l.useCallback(async()=>{var s;return(s=window.L)!=null&&s.markerClusterGroup?window.L:new Promise((x,i)=>{const u=setTimeout(()=>{i(new Error("Leaflet load timeout"))},1e4);function f(y){if(!document.querySelector(`link[href="${y}"]`)){const F=document.createElement("link");F.rel="stylesheet",F.href=y,document.head.appendChild(F)}}function H(y){return new Promise((F,_)=>{if(document.querySelector(`script[src="${y}"]`)){F();return}const G=document.createElement("script");G.src=y,G.onload=()=>F(),G.onerror=()=>
1_(new Error(`Failed to load: ${y}`)),document.head.appendChild(G)})}f("https://unpkg.com/[email protected]/dist/leaflet.css"),f("https://unpkg.com/[email protected]/dist/MarkerCluster.css"),f("https://unpkg.com/[email protected]/dist/MarkerCluster.Default.css");const O=()=>new Promise(y=>{if(window.L){y();return}let F=0;const _=setInterval(()=>{if(window.L){clearInterval(_),y();return}++F>100&&(clearInterval(_),y())},50)});H("https://unpkg.com/[email protected]/dist/leaflet.js").then(O).then(()=>H("https://unpkg.com/[email protected]/dist/leaflet.markercluster.js")).then(()=>{clearTimeout(u),x(window.L)}).catch(y=>{clearTimeout(u),i(y)})})},[]),B=l.useCallback(async()=>{if(!Y.current||M.current)return;let s;try{s=await ce()}catch{return}if(!s||!Y.current)return;const x=s.map(Y.current,{center:[-15.78,-47.93],zoom:5,zoomControl:!0,attributionControl:!0}),i=Ie.street;D.current=s.tileLayer(i.url,{attribution:i.attribution,maxZoom:19}).addTo(x);const u=s.markerClusterGroup({maxClusterRadius:50,spiderfyOnMaxZoom:!0,showCoverageOnHover:!1,iconCreateFunction:f=>{const H=f.getChildCount();return s.divIcon({html:`<div style=" 2 width: 36px; height: 36px; border-radius: 50%; 3 background: rgba(99, 102, 241, 0.9); border: 3px solid rgba(255,255,255,0.3); 4 display: flex; align-items: center; justify-content: center; 5 color: white; font-weight: 700; font-size: 12px; font-family: system-ui; 6 box-shadow: 0 2px 12px rgba(99, 102, 241, 0.4); 7 ">${H}</div>`,className:"custom-cluster",iconSize:[36,36],iconAnchor:[18,18]})}});x.addLayer(u),T.current=u,M.current=x,ie.current=!0},[ce]);l.useEffect(()=>(B(),()=>{var s;(s=M.current)==null||s.remove(),M.current=null}),[B]),l.useEffect(()=>{!R&&!M.current&&B()},[R,B]),l.useEffect(()=>{if(!M.current||!D.current)return;const s=window.L;if(!s)return;const x=Ie[P];M.current.removeLayer(D.current),D.current=s.tileLayer(x.url,{attribution:x.attribution,maxZoom:19}).addTo(M.current)},[P]),l.useEffect(()=>{const s=setInterval(()=>N(x=>x+1),3e4);return()=>clearInterval(s)},[]);const[W,de]=l.useState(new Set),[ee,Z]=l.useState(!1);l.useEffect(()=>{if(!c||!ee)return;let s=null,x=!1;return(async()=>{try{const f=Ye().currentUser;f&&await f.getIdToken(!0)}catch{}if(x)return;const u=et(We,"tracking/current");s=tt(u,f=>{if($(!0),E(!1),!f.exists()){g([]);return}const H=f.val(),O=[];Object.entries(H).forEach(([y,F])=>{const _=F.sharedWith||[];(W.has(y)||_.includes(c))&&O.push({...F,driverId:y})}),g(O)},f=>{$(!1)})})(),()=>{x=!0,s==null||s()}},[c,W,ee]),l.useEffect(()=>{if(!M.current||!ie.current||!T.current)return;const s=window.L;if(!s)return;const x=new Set(r.map(i=>i.driverId));if(Object.entries(h.current).forEach(([i,u])=>{x.has(i)||(T.current.removeLayer(u),delete h.current[i])}),r.forEach(i=>{const u=Q(i),f=ue[u].color,H=a===i.driverId,O=H?24:18,y=k[i.driverId]||i.driverId.slice(0,12)+"...",F=y.split(" ")[0],_=s.divIcon({className:"custom-marker",html:`<div style="display:flex;flex-direction:column;align-items:center;pointer-events:none;"> 8 <div style=" 9 width:${O}px;height:${O}px;border-radius:50%; 10 background:${f};border:3px solid white; 11 box-shadow:0 2px 10px rgba(0,0,0,0.5); 12 ${u==="moving"?"animation:pulse 2s infinite;":""} 13 ${H?"box-shadow:0 0 0 5px rgba(99,102,241,0.6),0 2px 10px rgba(0,0,0,0.5);":""} 14 pointer-events:auto; 15 "></div> 16 <div style=" 17 margin-top:3px;padding:1px 6px;border-radius:8px; 18 background:rgba(15,23,42,0.85);backdrop-filter:blur(4px); 19 border:1px solid rgba(255,255,255,0.15); 20 color:white;font-size:10px;font-weight:600;font-family:system-ui; 21 white-space:nowrap;pointer-events:auto; 22 ">${F}</div> 23 </div>`,iconSize:[80,O+22],iconAnchor:[40,O/2]}),G=`<div style="font-family: system-ui; min-width: 160px;"> 24 <b>${y}</b><br/> 25 <small style="color: ${f};">${ue[u].label}</small><br/> 26 <small>Velocidade: ${we(i.speed)}</small><br/> 27 <small>Direção: ${Ue(i.heading)}</small><br/> 28 <small>Atualizado: ${ye(i.timestamp)}</small> 29 ${i.tripId?'<br/><small style="color: #818cf8;">Em viagem</small>':""} 30 </div>`;if(h.current[i.driverId])h.current[i.driverId].setLatLng([i.latitude,i.longitude]).setIcon(_),h.current[i.driverId].setPo
30pupContent(G);else{const xe=s.marker([i.latitude,i.longitude],{icon:_}).bindPopup(G,{offset:[0,-(O/2+5)],autoPan:!1}).on("click",()=>S(i.driverId));T.current.addLayer(xe),h.current[i.driverId]=xe}}),!w.current&&r.length>0){w.current=!0;const i=r.filter(u=>u.latitude!==0&&u.longitude!==0);if(i.length>0){const u=s.latLngBounds(i.map(f=>[f.latitude,f.longitude]));u.isValid()&&M.current.fitBounds(u,{padding:[60,60],maxZoom:14})}}},[r,a,k,q]);const se=r.filter(s=>Q(s)==="moving").length,ae=r.filter(s=>Q(s)==="stopped").length,re=r.filter(s=>Q(s)==="stale").length,t=r.filter(s=>Q(s)==="offline").length,n=l.useMemo(()=>r.filter(s=>{if(b!=="all"&&Q(s)!==b)return!1;if(o){const x=o.toLowerCase();if(!(k[s.driverId]||s.driverId).toLowerCase().includes(x))return!1}return!0}),[r,b,o,k]),d=l.useMemo(()=>a&&r.find(s=>s.driverId===a)||null,[a,r]),L=()=>{if(!M.current||r.length===0)return;const s=window.L,x=r.filter(u=>u.latitude!==0&&u.longitude!==0);if(x.length===0)return;const i=s.latLngBounds(x.map(u=>[u.latitude,u.longitude]));i.isValid()&&M.current.fitBounds(i,{padding:[60,60],maxZoom:14})},z=s=>{const x=a===s?null:s;if(S(x),te(!1),x&&M.current){const i=r.find(u=>u.driverId===x);if(i&&i.latitude!==0&&i.longitude!==0){const u=M.current,f=window.L,H=f.latLng(i.latitude,i.longitude),O=16,y=u.project(H,O),F=window.innerWidth>=768?144:0,G=f.point(y.x-F,y.y-60),xe=u.unproject(G,O);u.flyTo(xe,O,{animate:!0,duration:.6}),setTimeout(()=>{const Ce=h.current[x];Ce&&Ce.openPopup()},700)}}},m=e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"p-3 border-b border-slate-800",children:e.jsxs("div",{className:"relative",children:[e.jsx(Ne,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-slate-500"}),e.jsx("input",{type:"text",placeholder:"Buscar motorista...",value:o,onChange:s=>v(s.target.value),className:"w-full bg-slate-800/50 border border-slate-700/50 rounded-lg pl-8 pr-3 py-2 text-xs text-white placeholder:text-slate-500 focus:outline-none focus:border-brand-500/40"}),o&&e.jsx("button",{onClick:()=>v(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-slate-500 hover:text-slate-300",children:e.jsx(Re,{className:"w-3 h-3"})})]})}),e.jsx("div",{className:"flex border-b border-slate-800 overflow-x-auto",children:[{key:"all",label:"Todos",count:r.length},{key:"moving",label:"Mov.",count:se},{key:"stopped",label:"Parado",count:ae},{key:"stale",label:"Sinal fraco",count:re},{key:"offline",label:"Off",count:t}].map(s=>e.jsxs("button",{onClick:()=>I(s.key),className:p("flex-1 px-2 py-2 text-[10px] font-medium text-center transition-colors whitespace-nowrap",b===s.key?"text-brand-400 border-b-2 border-brand-400 bg-brand-500/5":"text-slate-500 hover:text-slate-300"),children:[s.label," (",s.count,")"]},s.key))}),e.jsx("div",{className:"flex-1 overflow-y-auto",children:n.length===0?e.jsxs("div",{className:"p-6 text-center",children:[e.jsx(oe,{className:"w-8 h-8 text-slate-700 mx-auto mb-3"}),e.jsx("p",{className:"text-xs text-slate-500 font-medium mb-1",children:r.length===0?"Nenhum motorista rastreado":"Nenhum resultado para o filtro"}),e.jsx("p",{className:"text-[10px] text-slate-600 leading-relaxed",children:r.length===0?"Motoristas com o app Driver Copilot ativo aparecerão aqui em tempo real.":"Tente alterar o filtro de status ou a busca."})]}):n.map(s=>{const x=Q(s),i=ue[x],u=a===s.driverId;return e.jsxs("button",{onClick:()=>z(s.driverId),className:p("w-full text-left p-3 border-b border-slate-800/50 hover:bg-slate-800/50 transition-all",u&&"bg-brand-600/10 border-l-2 border-l-brand-500"),children:[e.jsxs("div",{className:"flex items-center gap-2.5",children:[e.jsxs("div",{className:"relative flex-shrink-0",children:[e.jsx("div",{className:p("w-8 h-8 rounded-full flex items-center justify-center font-bold text-[10px]"),style:{background:i.color,color:"#ffffff"},children:(k[s.driverId]||"M")[0].toUpperCase()}),x==="moving"&&e.jsx("span",{className:"absolute -top-0.5 -right-0.5 w-3 h-3 rounded-full bg-emerald-400 border-2 border-slate-900 animate-pulse"})]}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-slate-200 truncate",children:k[s.driverId]||s.driverId.slice(0,16)+"..."}),e.jsxs("div",{className:"flex items-center gap-2.5 mt-0.5",children:[e.jsx("span",{className:p("text-[10px] font-medium",i.textCls),children:i.label}),e.jsx("span",{className:"text-[10px] text-slate-600",children:"·"}),e.jsxs("span",{className:"text-[10px] text-slate-500 flex items-center gap-0.5",children:[e.jsx(pe,{className:"w-2.5 h-2.5"})," ",we(s.speed)]}),e.jsx("span",{className:"text-[10px] text-slate-600",children:"·"}),e.jsx("span",{className:"text-[10px] text-slate-500",children:ye(s.timestamp)})]})]})]}),s.tripId&&e.jsxs("span",{className:"mt-1.5 inline-flex items-center gap-1 text-[10px] px-1.5 py-0.5 rounded bg-brand-600/15 text-brand-400",children:[e.jsx(X,{className:"w-2.5 h-2.5"})," Em viagem"]})]},s.driverId)})})]});return l.useEffect(()=>{const s=setTimeout(()=>E(!1),5e3);return()=>clearTimeout(s)},[]),R?e.jsxs("div",{className:"flex flex-col items-center justify-center py-32",children:[e.jsx("div",{className:"w-8 h-8 border-2 border-t-transparent roun
30ded-full animate-spin mb-3",style:{borderColor:"var(--accent-brand)",borderTopColor:"transparent"}}),e.jsx("p",{className:"text-sm",style:{color:"var(--page-text-muted)"},children:"Conectando ao rastreamento em tempo real..."})]}):e.jsxs("div",{className:"flex flex-col -m-4 md:-m-6 lg:-m-8 overflow-hidden",style:{height:"100vh"},children:[e.jsxs("div",{className:"flex items-center justify-between px-4 md:px-6 py-3 bg-slate-900/80 backdrop-blur-sm border-b border-slate-800 z-10",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-9 h-9 rounded-xl bg-emerald-600/15 flex items-center justify-center",children:e.jsx(ke,{className:"w-5 h-5 text-emerald-400"})}),e.jsxs("div",{children:[e.jsxs("h1",{className:"text-lg font-bold text-white",children:["Mapa em Tempo Real ",e.jsx($e,{content:"Rastreamento em tempo real de todos os veÃculos ativos da frota.",position:"bottom"})]}),e.jsxs("p",{className:"text-xs",style:{color:"var(--page-text-secondary)"},children:[r.length," motorista",r.length!==1?"s":""," rastreado",r.length!==1?"s":""]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"hidden sm:flex items-center gap-3 mr-2",children:[e.jsxs("button",{onClick:()=>I(b==="moving"?"all":"moving"),className:p("flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-xs font-medium transition-all",b==="moving"?"bg-emerald-500/20 text-emerald-400 ring-1 ring-emerald-500/30":"text-emerald-400 hover:bg-emerald-500/10"),children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-emerald-400 animate-pulse"}),se]}),e.jsxs("button",{onClick:()=>I(b==="stopped"?"all":"stopped"),className:p("flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-xs font-medium transition-all",b==="stopped"?"bg-amber-500/20 text-amber-400 ring-1 ring-amber-500/30":"text-amber-400 hover:bg-amber-500/10"),children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-amber-400"}),ae]}),e.jsxs("button",{onClick:()=>I(b==="stale"?"all":"stale"),className:p("flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-xs font-medium transition-all",b==="stale"?"bg-orange-500/20 text-orange-400 ring-1 ring-orange-500/30":"text-orange-400 hover:bg-orange-500/10"),children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-orange-400"}),re]}),e.jsxs("button",{onClick:()=>I(b==="offline"?"all":"offline"),className:p("flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-xs font-medium transition-all",b==="offline"?"bg-slate-500/20 text-slate-400 ring-1 ring-slate-500/30":"text-slate-500 hover:bg-slate-500/10"),children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-slate-500"}),t]})]}),e.jsxs("div",{className:p("flex items-center gap-1 px-2 py-1 rounded-full text-xs font-medium",j?"bg-emerald-600/15 text-emerald-400":"bg-red-600/15 text-red-400"),children:[j?e.jsx(at,{className:"w-3 h-3"}):e.jsx(rt,{className:"w-3 h-3"}),e.jsx("span",{className:"hidden sm:inline",children:j?"Ao vivo":"Desconectado"})]}),e.jsx("button",{onClick:()=>V(s=>s==="street"?"satellite":"street"),className:p("p-2 rounded-lg transition-colors",P==="satellite"?"bg-brand-500/15 text-brand-400":"bg-slate-800 text-slate-300 hover:bg-slate-700"),title:P==="street"?"Modo satélite":"Modo mapa",children:P==="street"?e.jsx(lt,{className:"w-4 h-4"}):e.jsx(nt,{className:"w-4 h-4"})}),e.jsx("button",{onClick:L,disabled:r.length===0,className:"p-2 rounded-lg bg-slate-800 hover:bg-slate-700 text-slate-300 transition-colors disabled:opacity-30",title:"Centralizar todos",children:e.jsx(Pe,{className:"w-4 h-4"})}),e.jsxs("button",{onClick:()=>te(!U),className:"md:hidden p-2 rounded-lg bg-slate-800 hover:bg-slate-700 text-slate-300 transition-colors relative",children:[e.jsx(Oe,{className:"w-4 h-4"}),r.length>0&&e.jsx("span",{className:"absolute -top-1 -right-1 w-4 h-4 rounded-full bg-brand-500 text-white text-[9px] font-bold flex items-center justify-center",children:r.length})]})]})]}),e.jsxs("div",{className:"flex-1 flex relative min-h-0",children:[e.jsx("div",{ref:Y,className:"flex-1 min-h-0",style:{cursor:A?"grab":"default"},"aria-label":A?"Modo pan ativo (segurando espaço)":void 0}),A&&e.jsxs("div",{className:"pointer-events-none fixed bottom-6 left-1/2 -translate-x-1/2 px-3 py-1.5 rounded-full text-xs font-medium z-toast",style:{background:"var(--surface-primary)",border:"1px solid var(--border-primary)",color:"var(--page-text)",boxShadow:"var(--shadow-lg)"},children:[e.jsx("kbd",{className:"px-1.5 py-0.5 rounded text-[10px] font-mono mr-1.5",style:{background:"var(--surface-tertiary)"},children:"Espaço"}),"Modo arrastar"]}),e.jsx("style",{children:` 31 @keyframes pulse { 32 0%, 100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4); } 33 50% { box-shadow: 0 0 0 8px rgba(16, 185, 129, 0); } 34 } 35 .custom-marker { background: transparent !important; border: none !important; margin: 0 !important; padding: 0 !important; } 36 .custom-cluster { background: transparent !important; border: none !important; } 37 .theme-dark .leaflet-popup-content-wrapper { background: #1e293b !important; color: #e2e8f0 !important; border-radius: 12px !important; border: 1px solid #334155 !important; box-shadow: 0 8px 32px rgba(0,0,0,0.4) !important; } 38 .theme-dark .leaflet-popup-tip { background: #1e293b !important; } 39 .theme-dark .leaflet-popup-close-button { color: #94a3b8 !important; } 40 .theme-light .leaflet-popup-content-wrapper { background: #ffffff !important; color: #1e293b !important; border-radius: 12px !important; border: 1px solid #e2e8f0 !important; box-shadow: 0 8px 32px rgba(0,0,0,0.12) !important; } 41 .theme-light .leaflet-popup-tip { background: #ffffff !important; } 42 .theme-light .leaflet-popup-close-button { color: #64748b !important; } 43 .marker-cluster-small, .marker-cluster-medium, .marker-cluster-large { background: transparent !important; } 44 .marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div { background: transparent !important; } 45 `}),e.jsx("div",{className:"hidden md:flex flex-col w-72 bg-slate-900/95 backdrop-blur-sm border-l border-slate-800",children:m}),e.jsx("div",{className:p("md:hidden absolute left-0 right-0 bottom-0 bg-slate-900/98 backdrop-blur-md border-t border-slate-700 rounded-t-2xl shadow-2xl z-[999] transition-all duration-300 flex flex-col",U?"max-h-[70vh]":"max-h-0 border-t-0"),children:U&&e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>te(!1),className:"flex items-center justify-center py-2",children:e.jsx("div",{className:"w-10 h-1 rounded-full bg-slate-600"})}),m]})}),!U&&e.jsx("div",{className:"md:hidden absolute bottom-4 left-4 flex items-center gap-2 z-[998]",children:e.jsxs("div",{className:"flex items-center gap-2 bg-slate-900/90 backdrop-blur-sm rounded-xl px-3 py-2 border border-slate-700",children:[e.jsxs("span",{className:"flex items-center gap-1 text-xs text-emerald-400 font-medium",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-emerald-400 animate-pulse"})," ",se]}),e.jsx("span",{className:"text-slate-700",children:"|"}),e.jsxs("span",{className:"flex items-center gap-1 text-xs text-amber-400 font-medium",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-amber-400"})," ",ae]}),e.jsx("span",{className:"text-slate-700",children:"|"}),e.jsxs("span",{className:"flex items-center gap-1 text-xs text-orange-400 font-medium",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-orange-400"})," ",re]}),e.jsx("span",{className:"text-slate-700",children:"|"}),e.jsxs("span",{className:"flex items-center gap-1 text-xs text-slate-500 font-medium",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-slate-500"})," ",t]})]})}),d&&e.jsx(Et,{driver:d,name:k[d.driverId]||d.driverId.slice(0,16)+"...",onClose:()=>S(null)}),j&&r.length===0&&e.jsx("div",{className:"absolute inset-0 flex items-center justify-center pointer-events-none",children:e.jsxs("div",{className:"bg-slate-900/90 backdrop-blur-sm rounded-2xl p-8 text-center max-w-sm pointer-events-auto border border-slate-800",children:[e.jsx(oe,{className:"w-12 h-12 text-slate-600 mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-semibold text-slate-200 mb-2",children:"Nenhum motorista rastreado"}),e.jsx("p",{className:"text-sm text-slate-400 leading-relaxed",children:"Os motoristas vinculados à sua frota aparecerão aqui em tempo real quando o app Driver Copilot estiver transmitindo a localização."})]})})]})]})}
45function be(c){return c.toLocaleString("pt-BR",{style:"currency",currency:"BRL"})}function me({label:c,value:r,icon:g}){if(r==null)return null;const a=Math.min(r,100),S=r>=80?"bg-emerald-500":r>=60?"bg-amber-500":r>=40?"bg-orange-500":"bg-red-500";return e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(g,{className:"w-4 h-4 text-slate-500 flex-shrink-0"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between mb-1",children:[e.jsx("span",{className:"text-xs",style:{color:"var(--page-text-secondary)"},children:c}),e.jsx("span",{className:p("text-xs font-bold",ne(r)),children:r})]}),e.jsx("div",{className:"h-1.5 bg-slate-800 rounded-full overflow-hidden",children:e.jsx("div",{className:p("h-full rounded-full transition-all duration-500",S),style:{width:`${a}%`}})})]})]})}function ve({label:c,value:r,icon:g,accent:a}){return e.jsxs("div",{className:"card flex flex-col",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-3",children:[e.jsx("div",{className:p("w-10 h-10 rounded-xl flex items-center justify-center",a),children:e.jsx(g,{className:"w-5 h-5 text-white"})}),e.jsx("span",{className:"text-sm",style:{color:"var(--page-text-secondary)"},children:c})]}),e.jsx("span",{className:"text-2xl font-bold",style:{color:"var(--page-text)"},children:r})]})}function Ft({tripId:c,onClose:r}){var N,A;const g=He(),[a,S]=l.useState(null),[j,$]=l.useState(!0),[R,E]=l.useState("");l.useEffect(()=>{$(!0),E(""),Se.tripDetail(c).then(S).catch(o=>E(o.message)).finally(()=>$(!1))},[c]);const k=l.useMemo(()=>{var I;if(!((I=a==null?void 0:a.locations)!=null&&I.length))return null;const o=a.locations.filter(P=>P.lat&&P.lng);if(!o.length)return null;const v=o.reduce((P,V)=>P+V.lat,0)/o.length,b=o.reduce((P,V)=>P+V.lng,0)/o.length;return[v,b]},[a]),C=l.useMemo(()=>{var o;return(o=a==null?void 0:a.locations)!=null&&o.length?a.locations.filter(v=>v.lat&&v.lng).map(v=>[v.lat,v.lng]):[]},[a]),q=l.useMemo(()=>{var o;return(o=a==null?void 0:a.events)!=null&&o.length?a.events.filter(v=>v.lat&&v.lng):[]},[a]);return e.jsx(Ke,{open:!0,onClose:r,title:"Detalhes da Viagem",description:a?`${a.driverName} · ${je(a.startTime)}`:void 0,size:"xl",bodyClassName:"space-y-5",children:e.jsxs(e.Fragment,{children:[j&&e.jsx("div",{className:"flex items-center justify-center py-16",children:e.jsx(ze,{className:"w-8 h-8 text-brand-500 animate-spin"})}),R&&e.jsxs("div",{className:"text-center py-12",children:[e.jsx(Te,{className:"w-10 h-10 text-amber-400 mx-auto mb-3"}),e.jsx("p",{className:"text-slate-300",children:R})]}),a&&!j&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-slate-700/50 text-center",children:[e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"Distância"}),e.jsx("p",{className:"text-lg font-bold",style:{color:"var(--page-text)"},children:K(a.distance)})]}),e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-slate-700/50 text-center",children:[e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"Duração"}),e.jsx("p",{className:"text-lg font-bold",style:{color:"var(--page-text)"},children:J(a.duration)})]}),e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-slate-700/50 text-center",children:[e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"Score"}),e.jsx("p",{className:p("text-lg font-bold",ne(a.score)),children:a.score})]}),e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-slate-700/50 text-center",children:[e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"Eventos"}),e.jsx("p",{className:"text-lg font-bold",style:{color:"var(--page-text)"},children:((N=a.events)==null?void 0:N.length)||0})]})]}),a.dispatchId&&e.jsxs("button",{type:"button",onClick:()=>g(`/viagens?dispatch=${encodeURIComponent(a.dispatchId)}`),className:"card w-full flex items-center gap-3 text-left hover-surface",children:[e.jsx(xt,{className:"w-4 h-4 text-brand-400 shrink-0"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-xs text-slate-500",children:"DESPACHO DESTA VIAGEM"}),e.jsxs("p",{className:"text-sm font-semibold truncate",style:{color:"var(--page-text)"},children:[a.dispatchId,a.dispatchCargoWeight?` · ${a.dispatchCargoWeight} t`:""]})]}),e.jsx(Ve,{className:"w-4 h-4 text-slate-500 shrink-0"})]}),(a.speedScore!=null||a.accelerationScore!=null||a.brakingScore!=null||a.corneringScore!=null)&&e.jsxs("div",{className:"card",children:[e.jsxs("h3",{className:"text-sm font-semibold text-white mb-4 flex items-center gap-2",children:[e.jsx(mt,{className:"w-4 h-4 text-brand-400"}),"Breakdown de Scores"]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(me,{label:"Velocidade",value:a.speedScore,icon:pe}),e.jsx(me,{label:"Aceleração",value:a.accelerationScore,icon:ut}),e.jsx(me,{label:"Frenagem",value:a.brakingScore,icon:pt}),e.jsx(me,{label:"Curvas",value:a.corneringScore,icon:ht})]})]}),(a.fuelCost!=null||a.brakesCost!=null||a.totalCost!=null||a.ecoScore!=null)&&e.jsxs("div",{className:"card",children:[e.jsxs("h3",{className:"text-sm font-semibold text-white mb-4 flex items-center gap-2",children:[e.jsx(Ee,{className:"w-4 h-4 text-emerald-400"}),"Custo Econômico da Viagem"]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3 mb-4",children:[a.fuelCost!=null&&e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-slate-700/50 text-center",children:[e.jsx(ft,{className:"w-4 h-4 text-amber-400 mx-auto mb-1"}),e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"CombustÃvel"}),e.jsx("p",{className:"text-base font-bold",style:{color:"var(--page-text)"},children:be(a.fuelCost)})]}),a.brakesCost!=null&&e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-slate-700/50 text-center",children:[e.jsx(gt,{className:"w-4 h-4 text-red-400 mx-auto mb-1"}),e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"Desgaste Freios"}),e.jsx("p",{className:"text-base font-bold",style:{color:"var(--page-text)"},children:be(a.brakesCost)})]}),a.totalCost!=null&&e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-emerald-500/30 text-center",children:[e.jsx(Ee,{className:"w-4 h-4 text-emerald-400 mx-auto mb-1"}),e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"Custo Total"}),e.jsx("p",{className:"text-base font-bold text-emerald-400",children:be(a.totalCost)})]}),a.ecoScore!=null&&e.jsxs("div",{className:"p-3 rounded-xl bg-slate-800/40 border border-slate-700/50 text-center",children:[e.jsx(Oe,{className:"w-4 h-4 text-brand-400 mx-auto mb-1"}),e.jsx("p",{className:"text-xs text-slate-500 mb-1",children:"Eco-Score"}),e.jsx("p",{className:p("text-base font-bold",ne(a.ecoScore)),children:a.ecoScore})]})]}),(a.distSteadyState!=null||a.distAccState!=null||a.distDecState!=null)&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-slate-500 mb-2",children:"Perfil de Condução (distância por estado)"}),e.jsx("div",{className:"flex h-4 rounded-full overflow-hidden bg-slate-800",children:(()=>{const o=(a.distSteadyState||0)+(a.distAccState||0)+(a.distDecState||0);if(o===0)return null;const v=(a.distSteadyState||0)/o*100,b=(a.distAccState||0)/o*100,I=(a.distDecState||0)/o*100;return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"bg-emerald-500 transition-all",style:{width:`${v}%`},title:`Estável: ${v.toFixed(0)}%`}),e.jsx("div",{className:"bg-amber-500 transition-all",style:{width:`${b}%`},title:`Aceleração: ${b.toFixed(0)}%`}),e.jsx("div",{className:"bg-red-500 transition-all",style:{width:`${I}%`},title:`Frenagem: ${I.toFixed(0)}%`})]})})()}),e.jsxs("div",{className:"flex items-center gap-4 mt-2 text-[10px] text-slate-500",children:[e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-emerald-500 inline-block"})," Estável"]}),e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-amber-500 inline-block"})," Aceleração"]}),e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-red-500
45inline-block"})," Frenagem"]})]})]})]}),k&&C.length>1&&e.jsxs("div",{className:"card p-0 overflow-hidden",children:[e.jsx("div",{className:"px-4 py-3 border-b border-slate-800",children:e.jsxs("h3",{className:"text-sm font-semibold text-white flex items-center gap-2",children:[e.jsx(oe,{className:"w-4 h-4 text-brand-400"}),"Trajeto (",C.length," pontos)"]})}),e.jsx("div",{className:"h-[350px]",children:e.jsxs(_e,{center:k,zoom:13,scrollWheelZoom:!0,style:{height:"100%",width:"100%"},className:"rounded-b-2xl",children:[e.jsx(Ge,{attribution:'© <a href="https://www.openstreetmap.org/copyright">OSM</a>',url:"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"}),e.jsx(qe,{positions:C,pathOptions:{color:"#6366f1",weight:4,opacity:.8}}),e.jsx(he,{center:C[0],radius:8,pathOptions:{color:"#10B981",fillColor:"#10B981",fillOpacity:1},children:e.jsx(fe,{children:"InÃcio"})}),e.jsx(he,{center:C[C.length-1],radius:8,pathOptions:{color:"#EF4444",fillColor:"#EF4444",fillOpacity:1},children:e.jsx(fe,{children:"Fim"})}),q.map((o,v)=>{const b=o.severity==="high"||o.severity>=8?"#EF4444":o.severity==="medium"||o.severity>=5?"#F59E0B":"#3B82F6";return e.jsx(he,{center:[o.lat,o.lng],radius:6,pathOptions:{color:b,fillColor:b,fillOpacity:.8},children:e.jsxs(fe,{children:[e.jsx("strong",{children:De(o.eventType)}),e.jsx("br",{}),"Severidade: ",o.severity]})},v)})]})})]}),((A=a.events)==null?void 0:A.length)>0&&e.jsxs("div",{className:"card",children:[e.jsxs("h3",{className:"text-sm font-semibold text-white mb-3 flex items-center gap-2",children:[e.jsx(bt,{className:"w-4 h-4 text-amber-400"}),"Eventos da Viagem (",a.events.length,")"]}),e.jsx("div",{className:"space-y-2 max-h-[250px] overflow-y-auto",children:a.events.map((o,v)=>{const b=Je(o.confidence);return e.jsxs("div",{className:"flex items-center gap-3 py-2 border-b border-slate-800/50 last:border-0",children:[e.jsx(Te,{className:p("w-4 h-4 flex-shrink-0",Xe(o.severity))}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-slate-200",children:De(o.eventType)}),e.jsx("p",{className:"text-xs",style:{color:"var(--page-text-muted)"},children:je(o.timestamp)})]}),b&&e.jsx("span",{className:p("text-[10px] px-1.5 py-0.5 rounded",b.color),children:b.label}),e.jsx("span",{className:p("text-xs font-semibold px-2 py-0.5 rounded-lg",o.severity==="high"||o.severity>=8?"bg-red-500/15 text-red-400":o.severity==="medium"||o.severity>=5?"bg-amber-500/15 text-amber-400":"bg-blue-500/15 text-blue-400"),children:typeof o.severity=="string"?o.severity==="high"?"Alto":o.severity==="medium"?"Médio":"Baixo":o.severity})]},v)})})]}),(!a.locations||a.locations.length===0)&&(!a.events||a.events.length===0)&&e.jsxs("div",{className:"card text-center py-8",children:[e.jsx(oe,{className:"w-10 h-10 text-slate-600 mx-auto mb-3"}),e.jsx("p",{className:"text-slate-400",children:"Nenhum dado de trajeto ou evento disponÃvel para esta viagem."})]})]})]})})}function $t(){const{orgName:c,user:r}=Fe(),{data:g,loading:a,error:S,refetch:j}=Ze(()=>Se.trips({limit:200}),[]),[$,R]=l.useState("daily"),[E,k]=l.useState(""),[C,q]=l.useState(""),[N,A]=l.useState(""),[o,v]=l.useState(!1),[b,I]=l.useState(null),[P,V]=l.useState(1),[U,te]=l.useState(15),[Y,M]=l.useState(null),h=l.useMemo(()=>g?g.filter(t=>{var n;if(C&&t.driverId!==C)return!1;if(N){const d=t.score||0;if(N==="excellent"&&d<80||N==="good"&&(d<60||d>=80)||N==="regular"&&(d<40||d>=60)||N==="poor"&&d>=40)return!1}if(E){const d=E.toLowerCase();if(!((n=t.driverName)!=null&&n.toLowerCase().includes(d)))return!1}return!0}):[],[g,C,N,E]),T=Math.max(1,Math.ceil(h.length/U)),D=Math.min(P,T),ie=l.useMemo(()=>{const t=(D-1)*U;return h.slice(t,t+U)},[h,D,U]),w=l.useMemo(()=>{if(!h.length)return{count:0,distance:0,duration:0,avgScore:0};const t=h.length,n=h.reduce((m,s)=>m+(s.distance||0),0),d=h.reduce((m,s)=>m+(s.duration||0),0),L=h.filter(m=>m.score>0),z=L.length?Math.round(L.reduce((m,s)=>m+s.score,0)/L.length):0;return{count:t,distance:n,duration:d,avgScore:z}},[h]),ce=l.useMemo(()=>{if(!g)return[];const t=new Map;return g.forEach(n=>
45{n.driverId&&!t.has(n.driverId)&&t.set(n.driverId,n.driverName||"Motorista")}),Array.from(t.entries()).sort((n,d)=>n[1].localeCompare(d[1]))},[g]),B=C||N||E,W=l.useMemo(()=>{if(!h.length)return[];const t=new Map;for(const n of h){const d=new Date(n.startTime),L=`${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,"0")}-${String(d.getDate()).padStart(2,"0")}`,z=`${L}__${n.driverId||"unknown"}`;t.has(z)||t.set(z,{key:z,date:L,dateLabel:d.toLocaleDateString("pt-BR",{weekday:"short",day:"numeric",month:"short"}),driverId:n.driverId,driverName:n.driverName||"Motorista",trips:[],tripCount:0,totalDistance:0,totalDuration:0,scores:[],firstStart:n.startTime,lastEnd:n.startTime});const m=t.get(z);m.trips.push(n),m.tripCount++,m.totalDistance+=n.distance||0,m.totalDuration+=n.duration||0,n.score>0&&m.scores.push(n.score),n.startTime<m.firstStart&&(m.firstStart=n.startTime);const s=n.startTime+(n.duration||0);s>m.lastEnd&&(m.lastEnd=s)}return Array.from(t.values()).map(n=>({...n,avgScore:n.scores.length?Math.round(n.scores.reduce((d,L)=>d+L,0)/n.scores.length):0,minScore:n.scores.length?Math.min(...n.scores):0})).sort((n,d)=>d.firstStart-n.firstStart)},[h]),de=l.useMemo(()=>({totalDays:new Set(W.map(t=>t.date)).size,totalDrivers:new Set(W.map(t=>t.driverId)).size}),[W]),ee=["Motorista","Data/Hora","Distância","Duração","Score","Vel.","Acel.","Fren.","Curv."],Z=l.useCallback(()=>h.map(t=>[t.driverName||"â",vt(t.startTime),jt(t.distance),Nt(t.duration),le(t.score),le(t.speedScore),le(t.accelerationScore),le(t.brakingScore),le(t.corneringScore)]),[h]),se=l.useCallback(()=>{yt({filename:`viagens-${new Date().toISOString().slice(0,10)}`,sheetName:"Viagens",headers:ee,rows:Z()})},[Z]),ae=l.useCallback(()=>{const t=Z(),n=x=>x.includes(";")||x.includes('"')||x.includes(` 46`)?`"${x.replace(/"/g,'""')}"`:x,L="\uFEFF"+[ee.map(n).join(";"),...t.map(x=>x.map(i=>n(String(i??""))).join(";"))].join(`\r 47`),z=new Blob([L],{type:"text/csv;charset=utf-8;"}),m=URL.createObjectURL(z),s=document.createElement("a");s.href=m,s.download=`viagens-${new Date().toISOString().slice(0,10)}.csv`,document.body.appendChild(s),s.click(),document.body.removeChild(s),setTimeout(()=>URL.revokeObjectURL(m),1e3)},[Z]),re=l.useCallback(()=>{wt({filename:`viagens-${new Date().toISOString().slice(0,10)}`,title:"Relatório de Viagens",subtitle:`${h.length} viagens${B?" (filtrado)":""}`,orgName:c||void 0,headers:ee,rows:Z(),kpis:[{label:"Viagens",value:w.count},{label:"Distância Total",value:K(w.distance)},{label:"Tempo Total",value:J(w.duration)},{label:"Score Médio",value:w.avgScore||"â"}],generatedBy:(r==null?void 0:r.email)||void 0})},[h,w,c,r,B,Z]);return a?e.jsx("div",{className:"flex items-center justify-center h-[60vh]",children:e.jsx(ze,{className:"w-8 h-8 text-brand-500 animate-spin"})}):S?e.jsx("div",{className:"py-20",children:e.jsx(kt,{error:S,onRetry:j})}):e.jsxs("div",{className:"space-y-6 max-w-7xl",children:[e.jsx(st,{tips:["Cada viagem é registrada automaticamente pelo app do motorista.","Use os filtros para localizar viagens por motorista, data ou distância."],storageKey:"trips"}),e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-blue-500/15 flex items-center justify-center",children:e.jsx(X,{className:"w-5 h-5 text-blue-400"})}),e.jsxs("div",{children:[e.jsxs("h1",{className:"text-2xl font-bold",style:{color:"var(--page-text)"},children:["Viagens ",e.jsx($e,{content:"Registro de todas as viagens realizadas pela frota com detalhes de percurso e eventos.",position:"bottom"})]}),e.jsxs("p",{className:"text-slate-400 text-sm",children:[h.length," ",h.length===1?"viagem":"viagens",B?" (filtrado)":""]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(St,{onExportCsv:ae,onExportExcel:se,onExportPdf:re,onPrint:()=>window.print(),disabled:!h.length}),e.jsxs("button",{onClick:j,className:"btn-secondary flex items-center gap-2 text-sm",children:[e.jsx(it,{className:"w-4 h-4"})," Atualizar"]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 lg:grid-cols-4 gap-4",children:[e.jsx(ve,{label:"Viagens",value:w.count,icon:X,accent:"bg-blue-500/15"}),e.jsx(ve,{label:"Distância Total",value:K(w.distance),icon:oe,accent:"bg-emerald-500/15"}),e.jsx(ve,{label:"Tempo Total",value:J(w.duration),icon:Be,accent:"bg-amber-500/15"}),e.jsxs("div",{className:"card flex flex-col",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-3",children:[e.jsx("div",{className:p("w-10 h-10 rounded-xl flex items-center justify-center",w.avgScore>=80?"bg-emerald-500/15":w.avgScore>=60?"bg-amber-500/15":"bg-red-500/15"),children:e.jsx(pe,{className:"w-5 h-5 text-white"})}),e.jsx("span",{className:"text-sm",style:{color:"var(--page-text-secondary)"},children:"Score Médio"})]}),e.jsx("span",{className:p("text-2xl font-bold",w.avgScore>0?ne(w.avgScore):"text-slate-500"),children:w.avgScore>0?w.avgScore:"â"}),w.avgScore>0&&e.jsx("span",{className:p("text-xs mt-1",ne(w.avgScore)),children:Qe(w.avgScore)})]})]}),e.jsxs("div",{className:"flex border-b border-slate-700/50",children:[e.jsxs("button",{onClick:()=>R("daily"),className:p("flex items-center gap-2 px-5 py-3 text-sm font-medium border-b-2 transition-colors",$==="daily"?"border-brand-500 text-brand-400":"border-transparent text-slate-500 hover:text-slate-300"),children:[e.jsx(ge,{className:"w-4 h-4"}),"Jornadas Diárias",e.jsx("span",{className:"text-[10px] px-1.5 py-0.5 rounded bg-slate-700 text-slate-300",children:W.length})]}),e.jsxs("button",{onClick:()=>R("trips"),className:p("flex items-center gap-2 px-5 py-3 text-sm font-medium border-b-2 transition-colors",$==="trips"?"border-brand-500 text-brand-400":"border-transparent text-slate-500 hover:text-slate-300"),children:[e.jsx(X,{className:"w-4 h-4"}),"Viagens",e.jsx("span",{className:"text-[10px] px-1.5 py-0.5 rounded bg-slate-700 text-slate-300",children:h.length})]})]}),$==="daily"&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-4 text-xs",style:{color:"var(--page-text-muted)"},children:[e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(ge,{className:"w-3.5 h-3.5"}),de.totalDays," dias"]}),e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(Pe,{className:"w-3.5 h-3.5"}),de.totalDrivers," motoristas"]})]}),e.jsxs("div",{className:"relative max-w-md",children:[e.jsx(Ne,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-500"}),e.jsx("input",{type:"text",placeholder:"Buscar por motorista...",value:E,onChange:t=>k(t.target.value),className:"input pl-10 w-full"})]}),W.length===0?e.jsxs("div",{className:"card text-center py-16",children:[e.jsx(ge,{className:"w-12 h-12 text-slate-600 mx-auto mb-4"}),e.jsx("p",{className:"text-slate-400",children:"Nenhuma jornada registrada"})]}):e.jsx("div",{className:"space-y-2",children:W.map(t=>{var d,L,z;const n=Y===t.key;return e.jsxs("div",{className:"card overflow-hidden",children:[e.jsxs("div",{className:"flex items-center gap-4 p-4 cursor-pointer hover:bg-slate-800/30 transition-colors",onClick:()=>M(n?null:t.key),children:[e.jsxs("div",{className:"w-12 h-12 rounded-xl bg-slate-800 flex flex-col items-center justify-center shrink-0",children:[e.jsx("span",{className:"text-xs font-bold text-brand-400",children:(d=t.dateLabel.split(",")[0])==null?void 0:d.trim()}),e.jsx("span",{className:"text-[10px] text-slate-500",children:((L=t.dateLabel.split(",")[1])==null?void 0:L.trim())||t.dateLabel})]}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-6 h-6 rounded-full bg-brand-600/15 flex items-center justify-center text-brand-400 font-bold text-[10px] shrink-0",children:(((z=t.driverName)==null?void 0:z[0])||"M").toUpperCase()}),e.jsx("span",{className:"text-sm font-medium text-white truncate",children:t.driverName})]}),e.jsxs("div",{className:"flex items-center gap-4 mt-1 text-xs",style:{color:"var(--page-text-secondary)"},children:[e.jsxs("span",{children:[t.tripCount," ",t.tripCount===1?"viagem":"viagens"]}),e.jsx("span",{children:K(t.totalDistance)}),e.jsx("span",{children:J(t.totalDuration)})]})]}),e.jsxs("div",{className:"flex items-center gap-3 shrink-0",children:[t.avgScore>0&&e.jsxs("div",{className:"text-center",children:[e.jsx("span",{className:p("inline-flex items-center justify-center w-10 h-10 rounded-xl text-sm font-bold border",t.avgScore>=80?"bg-emerald-500/10 border-emerald-500/20 text-emerald-400":t.avgScore>=60?"bg-amber-500/10 border-amber-500/20 text-amber-400":t.avgScore>=40?"bg-orange-500/10 border-orange-500/20 text-orange-400":"bg-red-500/10 border-red-500/20 text-red-400"),children:t.avgScore}),e.jsx("p",{className:"text-[10px] text-slate-500 mt-0.5",children:"média"})]}),e.jsx(Le,{className:p("w-4 h-4 text-slate-500 transition-transform",n&&"rotate-180")}
47)]})]}),n&&e.jsxs("div",{className:"border-t border-slate-800/50",children:[e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"bg-slate-800/30",children:[e.jsx("th",{className:"text-left text-[10px] font-medium text-slate-500 uppercase tracking-wider px-4 py-2",children:"Hora"}),e.jsx("th",{className:"text-right text-[10px] font-medium text-slate-500 uppercase tracking-wider px-4 py-2",children:"Distância"}),e.jsx("th",{className:"text-right text-[10px] font-medium text-slate-500 uppercase tracking-wider px-4 py-2",children:"Duração"}),e.jsx("th",{className:"text-center text-[10px] font-medium text-slate-500 uppercase tracking-wider px-4 py-2",children:"Score"}),e.jsx("th",{className:"text-center text-[10px] font-medium text-slate-500 uppercase tracking-wider px-4 py-2 hidden md:table-cell",children:"Subscores"}),e.jsx("th",{className:"w-8"})]})}),e.jsx("tbody",{children:t.trips.sort((m,s)=>m.startTime-s.startTime).map(m=>e.jsxs("tr",{className:"border-b border-slate-800/30 last:border-0 hover:bg-slate-800/20 cursor-pointer transition-colors",onClick:s=>{s.stopPropagation(),I(m.id)},children:[e.jsx("td",{className:"px-4 py-2.5 text-sm",style:{color:"var(--page-text)"},children:new Date(m.startTime).toLocaleTimeString("pt-BR",{hour:"2-digit",minute:"2-digit"})}),e.jsx("td",{className:"px-4 py-2.5 text-sm text-slate-300 text-right tabular-nums",children:K(m.distance)}),e.jsx("td",{className:"px-4 py-2.5 text-sm text-slate-300 text-right tabular-nums",children:J(m.duration)}),e.jsx("td",{className:"px-4 py-2.5 text-center",children:e.jsx("span",{className:p("inline-flex items-center justify-center w-8 h-7 rounded text-xs font-bold",m.score>=80?"bg-emerald-500/10 text-emerald-400":m.score>=60?"bg-amber-500/10 text-amber-400":m.score>=40?"bg-orange-500/10 text-orange-400":"bg-red-500/10 text-red-400"),children:m.score||"â"})}),e.jsx("td",{className:"px-4 py-2.5 hidden md:table-cell",children:e.jsx("div",{className:"flex items-center justify-center gap-1",children:[m.speedScore,m.accelerationScore,m.brakingScore,m.corneringScore].map((s,x)=>s!=null?e.jsx("span",{className:p("text-[9px] font-bold w-6 h-5 rounded flex items-center justify-center",s>=80?"bg-emerald-500/15 text-emerald-400":s>=60?"bg-amber-500/15 text-amber-400":"bg-red-500/15 text-red-400"),children:s},x):null)})}),e.jsx("td",{className:"px-2 py-2.5",children:e.jsx(Me,{className:"w-3.5 h-3.5 text-slate-600"})})]},m.id))})]}),e.jsxs("div",{className:"flex items-center justify-between px-4 py-2 bg-slate-800/20 text-xs",style:{color:"var(--page-text-muted)"},children:[e.jsxs("span",{children:["Total do dia: ",t.tripCount," viagens"]}),e.jsxs("span",{children:[K(t.totalDistance)," ⢠",J(t.totalDuration)]}),t.minScore>0&&t.minScore!==t.avgScore&&e.jsxs("span",{children:["Score: ",t.minScore,"â",Math.max(...t.scores)," (média ",t.avgScore,")"]})]})]})]},t.key)})})]}),$==="trips"&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex gap-3 flex-wrap",children:[e.jsxs("div",{className:"relative flex-1 min-w-[240px]",children:[e.jsx(Ne,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-500"}),e.jsx("input",{type:"text",placeholder:"Buscar por motorista...",value:E,onChange:t=>k(t.target.value),className:"input pl-10 w-full"})]}),e.jsxs("button",{onClick:()=>v(!o),className:p("flex items-center gap-2 px-4 py-2.5 rounded-xl border text-sm font-medium transition-all",o||B?"border-brand-500/40 bg-brand-500/10 text-brand-400":"border-slate-700 text-slate-400 hover:border-slate-600"),children:[e.jsx(ct,{className:"w-4 h-4"}),"Filtros",B&&e.jsx("span",{className:"w-2 h-2 rounded-full bg-brand-400"}),e.jsx(Le,{className:p("w-3.5 h-3.5 transition-transform",o&&"rotate-180")})]})]}),o&&e.jsxs("div",{className:"flex gap-3 flex-wrap p-4 rounded-xl bg-slate-800/30 border border-slate-700/50",children:[e.jsxs("div",{className:"min-w-[200px]",children:[e.jsx("label",{className:"text-xs font-medium text-slate-400 mb-1.5 block",children:"Motorista"}),e.jsxs("select",{value:C,onChange:t=>q(t.target.value),className:"input w-full text-sm",children:[e.jsx("option",{value:"",children:"Todos"}),ce.map(([t,n])=>e.jsx("option",{value:t,children:n},t))]})]}),e.jsxs("div",{className:"min-w-[160px]",children:[e.jsx("label",{className:"text-xs font-medium text-slate-400 mb-1.5 block",children:"Score"}),e.jsxs("select",{value:N,onChange:t=>A(t.target.value),className:"input w-full text-sm",children:[e.jsx("option",{value:"",children:"Todos"}),e.jsx("option",{value:"excellent",children:"Excelente (80+)"}),e.jsx("option",{value:"good",children:"Bom (60-79)"}),e.jsx("option",{value:"regular",children:"Regular (40-59)"}),e.jsx("option",{value:"poor",children:"Ruim (<40)"})]})]}),B&&e.jsx("div",{className:"flex items-end",children:e.jsx("button",{onClick:()=>{q(""),A(""),k("")},className:"text-xs text-brand-400 hover:text-brand-300 font-medium px-3 py-2.5",children:"Limpar filtros"})})]})]}),h.length===0?e.jsxs("div",{className:"card text-center py-16",children:[e.jsx(X,{className:"w-12 h-12 text-slate-600 mx-auto mb-4"}),e.jsx("p",{className:"text-slate-400",children:B?"Nenhuma viagem encontrada com os filtros aplicad
47os":"Nenhuma viagem registrada ainda"}),e.jsx("p",{className:"text-slate-500 text-sm mt-2",children:B?"Tente ajustar os filtros ou a busca.":"Viagens aparecerão aqui conforme os motoristas usam o app DriverCopilot."})]}):e.jsxs("div",{className:"card overflow-hidden p-0",children:[e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-slate-800",children:[e.jsx("th",{className:"text-left text-xs font-medium uppercase tracking-wider px-5 py-3",children:"Motorista"}),e.jsx("th",{className:"text-left text-xs font-medium uppercase tracking-wider px-5 py-3",children:"Data"}),e.jsx("th",{className:"text-right text-xs font-medium uppercase tracking-wider px-5 py-3",children:"Distância"}),e.jsx("th",{className:"text-right text-xs font-medium uppercase tracking-wider px-5 py-3",children:"Duração"}),e.jsx("th",{className:"text-center text-xs font-medium uppercase tracking-wider px-5 py-3",children:"Score"}),e.jsx("th",{className:"text-center text-xs font-medium uppercase tracking-wider px-5 py-3 hidden lg:table-cell",children:"Subscores"}),e.jsx("th",{className:"text-center text-xs font-medium uppercase tracking-wider px-5 py-3 w-12"})]})}),e.jsx("tbody",{children:ie.map(t=>{var n;return e.jsxs("tr",{className:"border-b border-slate-800/50 hover:bg-slate-800/30 transition-colors cursor-pointer",onClick:()=>I(t.id),children:[e.jsx("td",{className:"px-5 py-3",children:e.jsxs("div",{className:"flex items-center gap-2.5",children:[e.jsx("div",{className:"w-7 h-7 rounded-full bg-brand-600/15 flex items-center justify-center text-brand-400 font-bold text-xs flex-shrink-0",children:(((n=t.driverName)==null?void 0:n[0])||"M").toUpperCase()}),e.jsx("span",{className:"text-sm font-medium text-slate-200 truncate max-w-[140px]",children:t.driverName})]})}),e.jsx("td",{className:"px-5 py-3 text-sm text-slate-300 whitespace-nowrap",children:je(t.startTime)}),e.jsx("td",{className:"px-5 py-3 text-sm text-slate-300 text-right tabular-nums",children:K(t.distance)}),e.jsx("td",{className:"px-5 py-3 text-sm text-slate-300 text-right tabular-nums",children:J(t.duration)}),e.jsx("td",{className:"px-5 py-3 text-center",children:e.jsx("span",{className:p("inline-flex items-center justify-center w-9 h-9 rounded-lg text-sm font-bold border",t.score>=80?"bg-emerald-500/10 border-emerald-500/20 text-emerald-400":t.score>=60?"bg-amber-500/10 border-amber-500/20 text-amber-400":t.score>=40?"bg-orange-500/10 border-orange-500/20 text-orange-400":"bg-red-500/10 border-red-500/20 text-red-400"),children:t.score||"â"})}),e.jsx("td",{className:"px-5 py-3 hidden lg:table-cell",children:t.speedScore!=null||t.accelerationScore!=null?e.jsx("div",{className:"flex items-center justify-center gap-1.5",children:[{v:t.speedScore,tip:"Vel"},{v:t.accelerationScore,tip:"Acel"},{v:t.brakingScore,tip:"Fren"},{v:t.corneringScore,tip:"Curv"}].map((d,L)=>d.v!=null?e.jsx("span",{title:d.tip,className:p("text-[10px] font-bold w-7 h-5 rounded flex items-center justify-center",d.v>=80?"bg-emerald-500/15 text-emerald-400":d.v>=60?"bg-amber-500/15 text-amber-400":d.v>=40?"bg-orange-500/15 text-orange-400":"bg-red-500/15 text-red-400"),children:d.v},L):null)}):e.jsx("span",{className:"text-xs text-slate-600",children:"â"})}),e.jsx("td",{className:"px-5 py-3 text-center",children:e.jsx(Me,{className:"w-4 h-4 text-slate-600 hover:text-slate-300 transition-colors mx-auto"})})]},t.id)})})]})}),h.length>0&&e.jsxs("div",{className:"flex items-center justify-between px-5 py-3 border-t border-slate-800",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("span",{className:"text-xs",style:{color:"var(--page-text-muted)"},children:[(D-1)*U+1,"â",Math.min(D*U,h.length)," de ",h.length]}),e.jsx("select",{value:U,onChange:t=>{te(Number(t.target.value)),V(1)},className:"bg-slate-800/50 border border-slate-700/50 rounded-lg px-2 py-1 text-xs text-slate-300 focus:outline-none focus:border-brand-500/40",children:[10,15,25,50,100].map(t=>e.jsxs("option",{value:t,children:[t," por página"]},t))})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("button",{onClick:()=>V(1),disabled:D<=1,className:"px-2 py-1 rounded-lg text-xs text-slate-400 hover:text-slate-200 hover:bg-slate-800 transition-colors disabled:opacity-30 disabled:cursor-not-allowed",children:"1"}),e.jsx("button",{onClick:()=>V(t=>Math.max(1,t-1)),disabled:D<=1,className:"p-1.5 rounded-lg text-slate-400 hover:text-slate-200 hover:bg-slate-800 transition-colors disabled:opacity-30 disabled:cursor-not-allowed",children:e.jsx(dt,{className:"w-4 h-4"})}),Array.from({length:Math.min(5,T)},(t,n)=>{let d;return T<=5||D<=3?d=n+1:D>=T-2?d=T-4+n:d=D-2+n,e.jsx("button",{onClick:()=>V(d),className:p("w-8 h-8 rounded-lg text-xs font-medium transition-colors",d===D?"bg-brand-500/15 text-brand-400 ring-1 ring-brand-500/30":"text-slate-400 hover:text-slate-200 hover:bg-slate-800"),children:d},d)}),e.jsx("button",{onClick:()=>V(t=>Math.min(T,t+1)),disabled:D>=T,className:"p-1.5 rounded-lg text-slate-400 hover:text-slate-200 hover:bg-slate-800 transition-colors disabled:opacity-30 disabled:cursor-not-allowed",children:e.jsx(Ve,{className:"w-4 h-4"})}),T>1&&e.jsx("button",{onClick:()=>V(T),disabled:D>=T,className:"px-2 py-1 rounded-lg text-xs text-slate-400 hover:text-slate-200 hover:bg-slate-800 transition-colors disabled:opacity-30 disabled:cursor-not-allowed",children:T})]})]})]})]}),b&&e.jsx(Ft,{tripId:b,onClose:()=>I(null)})]})}const Rt=[{key:"mapa",label:"Mapa ao Vivo",icon:ke},{key:"viagens",label:"Viagens",icon:X},{key:"rastreamento",label:"Rastreamento",icon:Ae}];function qt(){const[c,r]=l.useState("mapa");return e.jsxs("div",{className:"max-w-7xl",children:[e.jsx("div",{className:"flex items-center justify-between mb-4",children:e.jsx("h1",{className:"text-2xl font-bold",style:{color:"var(--page-text)"},children:"Rastrear"})}),e.jsx("div",{className:"flex gap-1 mb-6 overflow-x-auto pb-1 scrollbar-hide",children:Rt.map(g=>e.jsxs("button",{onClick:()=>r(g.key),className:p("flex items-center gap-1.5 px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap transition-colors",c===g.key?"shadow-sm":"hover-surface"),style:c===g.key?{background:"var(--accent-brand)",color:"#ffffff"}:{color:"var(--page-text-secondary)"},children:[e.jsx(g.icon,{className:"w-4 h-4"}),g.label]},g.key))}),c==="mapa"&&e.jsx(It,{}),c==="viagens"&&e.jsx($t,{}),c==="rastreamento"&&e.jsx(Ct,{})]})}
47export{qt as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.