import{i as e}from"./chunk-aKtaBQYM.js";import{Ft as t,G as n,Nt as r,vt as i}from"./mui-BgU4WrXF.js";import{d as a,l as o}from"./IconButton-oialqkv3.js";import{a as s,r as c,t as l}from"./Select-Bu2vmaew.js";import{n as u}from"./MenuItem-BNgF7Xqv.js";import{n as d,r as f,t as ee}from"./ReportFilter-CtusU-vs.js";import{t as p}from"./TableHead-DZPVxfXH.js";import{B as m,Dt as h,K as g,St as _,T as v,U as y,W as b,ct as x,gt as te,lt as S,pt as C,q as w,vt as T,w as E,wt as D,xt as O}from"./index-bel1r0Sr.js";import{t as k}from"./deviceEquality-4aERFrOn.js";import{t as A}from"./ReportsMenu-CwjT__h7.js";import{t as j}from"./ColumnSelect-CwYQdTe9.js";import{t as M}from"./TableShimmer-CBuxG-nx.js";import{t as N}from"./scheduleReport-DKXQL3XB.js";import{t as P}from"./exportExcel-DStci2wm.js";var F=e(t(),1),I=i(),L=[[`startTime`,`reportStartDate`],[`distance`,`sharedDistance`],[`startOdometer`,`reportStartOdometer`],[`endOdometer`,`reportEndOdometer`],[`averageSpeed`,`reportAverageSpeed`],[`maxSpeed`,`reportMaximumSpeed`],[`engineHours`,`reportEngineHours`],[`startHours`,`reportStartEngineHours`],[`endHours`,`reportEndEngineHours`],[`spentFuel`,`reportSpentFuel`]],R=new Map(L),z=()=>{let e=o(),{classes:t}=f(),i=C(),z=n(),[B,V]=a(),H=r(e=>e.devices.items,k([`id`,`name`])),U=v(`distanceUnit`),W=v(`speedUnit`),G=v(`volumeUnit`),[K,q]=te(`summaryColumns`,[`startTime`,`distance`,`averageSpeed`]),J=B.get(`daily`)===`true`,[Y,X]=(0,F.useState)([]),[Z,Q]=(0,F.useState)(!1),ne=S(async({deviceIds:e,groupIds:t,from:n,to:r})=>{let i=new URLSearchParams({from:n,to:r,daily:J});e.forEach(e=>i.append(`deviceId`,e)),t.forEach(e=>i.append(`groupId`,e)),Q(!0);try{X(await(await E(`/api/reports/summary?${i.toString()}`,{headers:{Accept:`application/json`}})).json())}finally{Q(!1)}},[J]),re=x(async()=>{let e=[],t=i(`sharedDevice`);if(Y.forEach(n=>{let r={[t]:H[n.deviceId].name};K.forEach(e=>{let t=i(R.get(e));r[t]=$(n,e)}),e.push(r)}),e.length===0)return;let n=i(J?`reportDaily`:`reportSummary`);await P(n,`summary.xlsx`,new Map([[n,e]]),z)}),ie=x(async(t,n,r)=>{r.type=`summary`,r.attributes.daily=J,await N(t,n,r),e(`/reports/scheduled`)}),$=(e,t)=>{let n=e[t];switch(t){case`deviceId`:return H[n].name;case`startTime`:return g(n,`date`);case`startOdometer`:case`endOdometer`:case`distance`:return m(n,U,i);case`averageSpeed`:case`maxSpeed`:return n>0?b(n,W,i):null;case`engineHours`:case`startHours`:case`endHours`:return n>0?y(n,i):null;case`spentFuel`:return n>0?w(n,G,i):null;default:return n}};return(0,I.jsxs)(u,{menu:(0,I.jsx)(A,{}),breadcrumbs:[`reportTitle`,`reportSummary`],children:[(0,I.jsx)(`div`,{className:t.header,children:(0,I.jsxs)(ee,{onShow:ne,onExport:re,onSchedule:ie,deviceType:`multiple`,loading:Z,formats:[`xlsx`],children:[(0,I.jsx)(`div`,{className:t.filterItem,children:(0,I.jsxs)(s,{fullWidth:!0,children:[(0,I.jsx)(c,{children:i(`sharedType`)}),(0,I.jsxs)(l,{label:i(`sharedType`),value:J,onChange:e=>d(B,V,`daily`,[String(e.target.value)]),children:[(0,I.jsx)(h,{value:!1,children:i(`reportSummary`)}),(0,I.jsx)(h,{value:!0,children:i(`reportDaily`)})]})]})}),(0,I.jsx)(j,{columns:K,setColumns:q,columnsArray:L})]})}),(0,I.jsxs)(D,{children:[(0,I.jsx)(p,{children:(0,I.jsxs)(T,{children:[(0,I.jsx)(O,{children:i(`sharedDevice`)}),K.map(e=>(0,I.jsx)(O,{children:i(R.get(e))},e))]})}),(0,I.jsx)(_,{children:Z?(0,I.jsx)(M,{columns:K.length+1}):Y.map(e=>(0,I.jsxs)(T,{children:[(0,I.jsx)(O,{children:H[e.deviceId].name}),K.map(t=>(0,I.jsx)(O,{children:$(e,t)},t))]},`${e.deviceId}_${Date.parse(e.startTime)}`))})]})]})};export{z as default};