import{e as C,a as U,r as c,aj as r,j as e,al as B,ao as G,a3 as V,d as _}from"./index-D4ZMoJFe.js";import{b as W}from"./formatters-jmx2pV9o.js";import{i as z}from"./trackingUtils-D2VhIKbr.js";import{R as J,C as Q,T as X}from"./generateCategoricalChart-3rcGS8p6.js";import{P as Y,a as Z}from"./PieChart-C9p1G6Gr.js";/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const q=C("Navigation",[["polygon",{points:"3 11 22 2 13 21 11 13 3 11",key:"1ltx0t"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ee=C("Signal",[["path",{d:"M2 20h.01",key:"4haj6o"}],["path",{d:"M7 20v-4",key:"j294jx"}],["path",{d:"M12 20v-8",key:"i3yub9"}],["path",{d:"M17 20V8",key:"1tkaf5"}],["path",{d:"M22 4v16",key:"sih9yq"}]]),R=Array.from({length:24},(h,o)=>o),N={online:"#10b981",moving:"#3b82f6",offline:"#94a3b8",disabled:"#64748b"},te=h=>{if(!h)return"Jamais";const o=new Date(h),d=Math.max(0,Math.floor((Date.now()-o.getTime())/1e3));if(d<60)return`il y a ${d}s`;const m=Math.floor(d/60);if(m<60)return`il y a ${m} min`;const u=Math.floor(m/60);return u<24?`il y a ${u} h`:W(h)},f=({icon:h,label:o,value:d,tone:m})=>e.jsx("article",{className:"card-panel p-5",children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold uppercase tracking-[0.18em] text-slate-500",children:o}),e.jsx("p",{className:"mt-3 text-3xl font-semibold text-slate-950",children:d})]}),e.jsx("div",{className:`flex h-12 w-12 items-center justify-center rounded-2xl ${m}`,children:e.jsx(h,{size:20})})]})});function re(){const{selectedCompanyId:h,selectedCompany:o,activeModule:d}=U(),[m,u]=c.useState([]),[O,y]=c.useState([]),[w,S]=c.useState({}),[H,k]=c.useState(!0),[D,A]=c.useState(""),[T,E]=c.useState(Date.now()),x=h||(o==null?void 0:o.id)||"",P=async()=>{const t=new Date;t.setHours(0,0,0,0);const[a,i]=await Promise.all([_.get("/tracking/devices/live-states",{params:{company_id:x}}),_.get("/tracking/alerts",{params:{company_id:x,status:"active"}})]),p=Array.isArray(a.data.items)?a.data.items:[];u(p),y(Array.isArray(i.data.items)?i.data.items:[]);const g=await Promise.all(p.map(async s=>{try{const n=await _.get(`/tracking/devices/${s.id}/locations`,{params:{company_id:x,from:t.toISOString(),to:new Date().toISOString(),limit:500}});return[s.id,n.data.items||[]]}catch{return[s.id,[]]}}));S(Object.fromEntries(g))};c.useEffect(()=>{if(d!=="TRACKING"||!x)return;let t=!1;return k(!0),A(""),P().then(()=>{}).catch(a=>{var i,p;t||A(((p=(i=a==null?void 0:a.response)==null?void 0:i.data)==null?void 0:p.message)||"Impossible de charger le tableau de bord tracking.")}).finally(()=>{t||k(!1)}),()=>{t=!0}},[d,x]),c.useEffect(()=>{const t=window.setInterval(()=>{E(Date.now())},5e3);return()=>{window.clearInterval(t)}},[]),c.useEffect(()=>{if(d!=="TRACKING"||!x)return;r.connect(),r.emit("company:subscribe",Number(x));const t=s=>{u(n=>n.map(l=>Number(l.id)===Number(s.device_id)?{...l,is_online:!0,last_lat:s.lat,last_lng:s.lng,last_speed:s.speed,last_battery:s.battery,last_seen:s.timestamp}:l)),S(n=>({...n,[s.device_id]:[...(n[s.device_id]||[]).slice(-499),{device_timestamp:s.timestamp,is_moving:s.is_moving}]}))},a=({device_id:s})=>{u(n=>n.map(l=>Number(l.id)===Number(s)?{...l,is_online:!0}:l))},i=({device_id:s,battery:n,timestamp:l})=>{u(L=>L.map(j=>Number(j.id)===Number(s)?{...j,is_online:!0,last_seen:l||new Date().toISOString(),last_battery:n??j.last_battery}:j))},p=({device_id:s})=>{u(n=>n.map(l=>Number(l.id)===Number(s)?{...l,is_online:!1}:l))},g=s=>{y(n=>[s,...n.filter(l=>Number(l.id)!==Number(s.id))])};return r.on("location:update",t),r.on("device:online",a),r.on("device:offline",p),r.on("device:heartbeat",i),r.on("tracking:alert",g),()=>{r.off("location:update",t),r.off("device:online",a),r.off("device:offline",p),r.off("device:heartbeat",i),r.off("tracking:alert",g),r.emit("company:unsubscribe",Number(x))}},[d,x]);const b=c.useMemo(()=>m.map(t=>({...t,ui_is_online:z(t,T)})),[m,T]),M=b.filter(t=>t.ui_is_online).length,$=b.filter(t=>Number(t.last_speed||0)>5&&t.ui_is_online).length,I=b.filter(t=>!t.ui_is_online).length,F=O.length,v=[{name:"En ligne",value:M,color:N.online},{name:"En mouvement",value:$,color:N.moving},{name:"Hors ligne",value:I,color:N.offline},{name:"Désactivé",value:b.filter(t=>t.is_active===!1).length,color:N.disabled}].filter(t=>t.value>0),K=c.useMemo(()=>b.map(t=>{const a=new Set((w[t.id]||[]).map(i=>new Date(i.device_timestamp).getHours()));return{device:t,hours:R.map(i=>({hour:i,active:a.has(i)}))}}),[w,b]);return e.jsxs("section",{className:"space-y-6",children:[e.jsxs("header",{className:"card-panel p-6",children:[e.jsx("p",{className:"tracking-panel-title",children:"Tracking · Tableau de bord"}),e.jsx("div",{className:"mt-3 flex flex-wrap items-center justify-between gap-4",children:e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-semibold text-slate-950",children:"Vue d'ensemble des téléphones"}),e.jsxs("p",{className:"mt-2 text-sm text-slate-500",children:["État temps réel du parc mobile de ",(o==null?void 0:o.name)||"la société","."]})]})})]}),H?e.jsx("div",{className:"grid gap-4 lg:grid-cols-5",children:Array.from({length:5}).map((t,a)=>e.jsx("div",{className:"card-panel h-32 animate-pulse bg-slate-100"},a))}):D?e.jsx("div",{className:"rounded-2xl border border-red-200 bg-red-50 p-5 text-red-700",children:D}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid gap-4 lg:grid-cols-5",children:[e.jsx(f,{icon:B,label:"Téléphones total",value:m.length,tone:"bg-emerald-50 text-emerald-700"}),e.jsx(f,{icon:ee,label:"En ligne maintenant",value:M,tone:"bg-emerald-50 text-emerald-700"}),e.jsx(f,{icon:q,label:"En mouvement",value:$,tone:"bg-blue-50 text-blue-700"}),e.jsx(f,{icon:G,label:"Hors ligne",value:I,tone:"bg-slate-100 text-slate-700"}),e.jsx(f,{icon:V,label:"Alertes actives",value:F,tone:"bg-red-50 text-red-700"})]}),e.jsxs("div",{className:"grid gap-6 xl:grid-cols-2",children:[e.jsxs("article",{className:"card-panel overflow-hidden",children:[e.jsxs("div",{className:"border-b border-slate-200 px-6 py-5",children:[e.jsx("p",{className:"tracking-panel-title",children:"Activité des téléphones aujourd'hui"}),e.jsx("h2",{className:"mt-2 text-xl font-semibold text-slate-950",children:"Heatmap horaire"})]}),e.jsx("div",{className:"overflow-x-auto px-6 py-5",children:e.jsx("div",{className:"min-w-[720px]",children:e.jsxs("div",{className:"grid grid-cols-[220px_repeat(24,minmax(24px,1fr))] gap-2 text-[11px] text-slate-500",children:[e.jsx("div",{}),R.map(t=>e.jsx("div",{className:"text-center",children:t.toString().padStart(2,"0")},t)),K.map(t=>e.jsxs(c.Fragment,{children:[e.jsx("div",{className:"py-2 text-sm font-medium text-slate-900",children:t.device.owner_name},`${t.device.id}-label`),t.hours.map(a=>e.jsx("div",{className:`h-8 rounded-lg border ${a.active?"border-emerald-200 bg-emerald-400/70":"border-slate-200 bg-slate-100"}`,title:`${t.device.owner_name} · ${a.hour}h`},`${t.device.id}-${a.hour}`))]},t.device.id))]})})})]}),e.jsxs("article",{className:"card-panel overflow-hidden",children:[e.jsxs("div",{className:"border-b border-slate-200 px-6 py-5",children:[e.jsx("p",{className:"tracking-panel-title",children:"Répartition par statut"}),e.jsx("h2",{className:"mt-2 text-xl font-semibold text-slate-950",children:"Statut instantané"})]}),e.jsxs("div",{className:"grid gap-6 px-6 py-6 lg:grid-cols-[1fr_180px]",children:[e.jsx("div",{className:"h-[260px]",children:e.jsx(J,{width:"100%",height:"100%",children:e.jsxs(Y,{children:[e.jsx(Z,{data:v,dataKey:"value",nameKey:"name",cx:"50%",cy:"50%",innerRadius:70,outerRadius:100,paddingAngle:3,children:v.map(t=>e.jsx(Q,{fill:t.color},t.name))}),e.jsx(X,{})]})})}),e.jsx("div",{className:"space-y-3",children:v.map(t=>e.jsxs("div",{className:"flex items-center justify-between rounded-2xl border border-slate-200 bg-slate-50 px-4 py-3",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("span",{className:"inline-block h-3 w-3 rounded-full",style:{backgroundColor:t.color}}),e.jsx("span",{className:"text-sm font-medium text-slate-700",children:t.name})]}),e.jsx("span",{className:"font-mono text-sm font-semibold text-slate-950",children:t.value})]},t.name))})]})]})]}),e.jsxs("article",{className:"table-shell overflow-hidden",children:[e.jsxs("div",{className:"border-b border-slate-200 px-6 py-5",children:[e.jsx("p",{className:"tracking-panel-title",children:"État actuel de tous les téléphones"}),e.jsx("h2",{className:"mt-2 text-xl font-semibold text-slate-950",children:"Tableau temps réel"})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsxs("tr",{children:[e.jsx("th",{className:"table-cell text-left font-semibold",children:"Téléphone"}),e.jsx("th",{className:"table-cell text-left font-semibold",children:"Propriétaire"}),e.jsx("th",{className:"table-cell text-left font-semibold",children:"N° Téléphone"}),e.jsx("th",{className:"table-cell text-left font-semibold",children:"Statut"}),e.jsx("th",{className:"table-cell text-left font-semibold",children:"Dernière position"}),e.jsx("th",{className:"table-cell text-left font-semibold",children:"Batterie"}),e.jsx("th",{className:"table-cell text-left font-semibold",children:"Dernière vue"})]})}),e.jsx("tbody",{children:b.map(t=>e.jsxs("tr",{className:"table-row",children:[e.jsxs("td",{className:"table-cell",children:[e.jsx("div",{className:"font-semibold text-slate-950",children:t.device_name}),e.jsx("div",{className:"text-xs text-slate-500",children:t.device_model||"Modèle inconnu"})]}),e.jsx("td",{className:"table-cell",children:t.owner_name}),e.jsx("td",{className:"table-cell",children:t.owner_phone||"—"}),e.jsx("td",{className:"table-cell",children:e.jsx("span",{className:`rounded-full px-2 py-1 text-xs font-semibold ${t.ui_is_online?"bg-emerald-100 text-emerald-700":"bg-slate-200 text-slate-600"}`,children:t.ui_is_online?"En ligne":"Hors ligne"})}),e.jsx("td",{className:"table-cell",children:t.last_lat!==null&&t.last_lng!==null?`${Number(t.last_lat).toFixed(5)}, ${Number(t.last_lng).toFixed(5)}`:"—"}),e.jsxs("td",{className:"table-cell font-mono",children:[t.last_battery??"—","%"]}),e.jsx("td",{className:"table-cell",children:te(t.last_seen)})]},t.id))})]})})]})]})]})}export{re as default};
