tracreport/src/reports/reportHtml.ts

167 lines
4.6 KiB
TypeScript

import { GroupedUnitReport, VehicleReport } from "./reports";
export function generateHTMLReport(units: GroupedUnitReport[], startDate: Date, endDate: Date): string {
const unitsWithReports = units.filter(unit =>
unit.vehicles.some(
v => v.saloonReports > 0 || v.cabReports > 0
)
);
const body = unitsWithReports.map(renderUnitSection).join('\n');
return wrapHtml(body, startDate, endDate);
}
function renderUnitSection(unit: GroupedUnitReport): string {
const total = unit.vehicles.length;
return `
<div class="train-container">
<h1 class="unit-number">${unit.unitNumber}</h1>
<div class="vehicle-container">
${unit.vehicles.map((v, i) => renderCoach(v, i, total)).join('\n')}
</div>
${renderComments(unit.comments)}
</div>`;
}
function renderCoach(vehicle: VehicleReport, index: number, total: number): string {
const cabMarkerHTML = vehicle.hasCabZone
? `<div class="cab-marker">${vehicle.cabReports}</div>`
: '';
const cabAbove = index === 0;
const cabBelow = index === total -1;
return `
<div class="coach">
<div class="vehicle-number">${vehicle.vehicleNumber}</div>
<div class="vehicle-box">
${cabAbove ? cabMarkerHTML : ''}
<div class="report-count">${vehicle.saloonReports}</div>
${cabBelow && !cabAbove ? cabMarkerHTML : ''}
</div>
</div>
`;
}
function renderComments(comments: string[]): string {
const renderedComments = comments.length
? comments.map(c => `<p>${c}</p>`).join('\n')
: '<p>No comments submitted.</p>';
return `
<div class="comments-box">
<h2>Comments</h2>
${renderedComments}
</div>
`;
}
function wrapHtml(content: string, startDate: Date, endDate: Date): string {
const formatDate = (d: Date) => d.toISOString().split('T')[0];
return `
<!DOCTYPE html>
<html lang="en">
<head>
<title>TrACreport - Report Output</title>
<style>
body {
font-family: sans-serif;
}
.preface {
text-align: center;
font-size: 0.9em;
}
.train-container {
width: 75%;
margin: 2em auto;
padding: 1em;
border-radius: 8px;
}
.unit-number {
text-align: center;
margin-bottom: 1em;
font-size: 1.5em;
}
.vehicle-container {
display: flex;
justify-content: space-between;
gap: 0.5em;
}
.coach {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
}
.vehicle-number {
margin-bottom: 0.5em;
font-weight: bold;
}
.vehicle-box {
width: 100%;
height: 50px;
background-color: #e0e0e0;
border: 2px solid black;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 6px;
font-size: 1.2em;
box-sizing: border-box;
}
.cab-marker {
width: 1.5em;
text-align: center;
font-weight: bold;
background-color: #d0d0f0;
border-radius: 4px;
padding: 2px;
}
.report-count {
flex: 1;
text-align: center;
}
.comments-box {
margin-top: 1.5em;
padding: 1em;
background-color: #f9f9f9;
border: 1px solid #aaa;
border-radius: 6px;
}
.comments-box h2 {
margin-top: 0;
font-size: 1.2em;
border-bottom: 1px solid #ccc;
padding-bottom: 0.5em;
}
.comments-box p {
margin: 0.5em 0;
}
</style>
</head>
<body>
<div class="preface">
<h1>TrACreport</h1>
<h2>Report period: ${formatDate(startDate)} - ${formatDate(endDate)}</h2>
<p>Total reports received for each vehicle in the West fleet</p>
<p>158 cabs are not recorded as cab cooling is disabled for Guards</p>
</div>
${content}
</body>
</html>`
}