import{i as e}from"./chunk-aKtaBQYM.js";import{Vt as t,Y as n,wt as r,zt as i}from"./mui-B6lh3XSP.js";import{d as a,l as o}from"./IconButton-BEBOUWhl.js";import{n as s}from"./MenuItem-HQNtf0LG.js";import{n as c,r as l,t as u}from"./ReportFilter-DWQ_hjet.js";import{t as d}from"./TableHead-_KJlbpnj.js";import{$ as f,At as ee,Bt as p,Dt as m,Ft as h,Gt as g,K as _,Nt as v,Ot as y,Q as b,R as x,St as te,X as S,Y as C,ht as w,mt as T,w as E,wt as D,yt as O}from"./index-KVuxnkWj.js";import{t as k}from"./deviceEquality-4aERFrOn.js";import{t as A}from"./ReportsMenu-XWwKkI0p.js";import{t as j}from"./ColumnSelect-BAx72xpb.js";import{t as M}from"./TableShimmer-f7Cf4NTV.js";import{t as N}from"./scheduleReport-DHCgjQvQ.js";import{t as P}from"./exportExcel-D5CrW9S1.js";var F=e(t(),1),I=r(),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}=l(),r=O(),z=n(),[B,V]=a(),H=i(e=>e.devices.items,k([`id`,`name`])),U=x(`distanceUnit`),W=x(`speedUnit`),G=x(`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=w(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=T(async()=>{let e=[],t=r(`sharedDevice`);if(Y.forEach(n=>{let i={[t]:H[n.deviceId].name};K.forEach(e=>{let t=r(R.get(e));i[t]=$(n,e)}),e.push(i)}),e.length===0)return;let n=r(J?`reportDaily`:`reportSummary`);await P(n,`summary.xlsx`,new Map([[n,e]]),z)}),ie=T(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 b(n,`date`);case`startOdometer`:case`endOdometer`:case`distance`:return _(n,U,r);case`averageSpeed`:case`maxSpeed`:return n>0?S(n,W,r):null;case`engineHours`:case`startHours`:case`endHours`:return n>0?C(n,r):null;case`spentFuel`:return n>0?f(n,G,r):null;default:return n}};return(0,I.jsxs)(s,{menu:(0,I.jsx)(A,{}),breadcrumbs:[`reportTitle`,`reportSummary`],children:[(0,I.jsx)(`div`,{className:t.header,children:(0,I.jsxs)(u,{onShow:ne,onExport:re,onSchedule:ie,deviceType:`multiple`,loading:Z,formats:[`xlsx`],children:[(0,I.jsx)(`div`,{className:t.filterItem,children:(0,I.jsxs)(g,{fullWidth:!0,children:[(0,I.jsx)(p,{children:r(`sharedType`)}),(0,I.jsxs)(v,{label:r(`sharedType`),value:J,onChange:e=>c(B,V,`daily`,[String(e.target.value)]),children:[(0,I.jsx)(h,{value:!1,children:r(`reportSummary`)}),(0,I.jsx)(h,{value:!0,children:r(`reportDaily`)})]})]})}),(0,I.jsx)(j,{columns:K,setColumns:q,columnsArray:L})]})}),(0,I.jsxs)(ee,{children:[(0,I.jsx)(d,{children:(0,I.jsxs)(D,{children:[(0,I.jsx)(m,{children:r(`sharedDevice`)}),K.map(e=>(0,I.jsx)(m,{children:r(R.get(e))},e))]})}),(0,I.jsx)(y,{children:Z?(0,I.jsx)(M,{columns:K.length+1}):Y.map(e=>(0,I.jsxs)(D,{children:[(0,I.jsx)(m,{children:H[e.deviceId].name}),K.map(t=>(0,I.jsx)(m,{children:$(e,t)},t))]},`${e.deviceId}_${Date.parse(e.startTime)}`))})]})]})};export{z as default};