<%- include('partials/header', { title, user }) %>
<%
const isFinance = financeReports.includes(report);
const isPeople = peopleReports.includes(report);
// Reports without a from–to period: a lookup, a leave year and a calendar month.
const noPeriod = ['units', 'balances', 'absence'].includes(report);
// Filters only carry over to reports that use them; switching tabs keeps just the period.
const query = (overrides = {}) => {
	const own = report === 'issued'
		? { site: filters.site, product: filters.product, person: filters.person }
		: report === 'leave' ? { status: filters.status === 'approved' ? '' : filters.status, type: filters.type, dept: filters.department, person: filters.person }
		: report === 'balances' ? { year: data.year, dept: filters.department }
		: report === 'absence' ? { month: data.grid.month, dept: filters.department }
		: isFinance ? { dept: filters.department, category: filters.category, person: filters.person, status: filters.status === 'paid' ? '' : filters.status } : {};
	const period = noPeriod ? {} : { from: range.from, to: range.to };
	const params = new URLSearchParams(Object.entries({ report, ...period, ...own, ...overrides }).filter(([, value]) => value));
	return `?${params.toString()}`;
};
const tabLink = (key) => `/reports?${new URLSearchParams({ report: key, ...(['units', 'balances', 'absence'].includes(key) || noPeriod ? {} : { from: range.from, to: range.to }) }).toString()}`;
const leaveTypeLabel = (type) => String(type || '').replace(/^./, (character) => character.toUpperCase());
const day = (iso) => new Date(`${iso}T00:00:00Z`).toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' });
const kes = (value) => `KES ${money(value)}`;
const percent = (share) => `${Math.round(share * 1000) / 10}%`;
const showsPeople = isFinance && report !== 'treasury';
%>
<section class="report-hero">
	<div>
		<p class="report-kicker"><%= isFinance ? 'Finance reports' : isPeople ? 'People reports' : 'Stock reports' %></p>
		<h1><%= reports[report] %></h1>
		<% if (!noPeriod) { %><p class="report-period"><%= day(range.from) %> – <%= day(range.to) %></p><% } else if (range.label && report !== 'units') { %><p class="report-period"><%= range.label %></p><% } %>
	</div>
	<div class="report-actions no-print">
		<% if (!['units', 'absence'].includes(report)) { %><a class="button outline small" href="/reports/export<%= query() %>">Export to Excel</a><% } %>
		<button class="button outline small" type="button" onclick="window.print()">Print</button>
	</div>
</section>

<nav class="report-tabs no-print" aria-label="Reports">
	<% [['Finance', financeReports], ['Stock', stockReports], ['People', peopleReports]].filter(([, keys]) => keys.length).forEach(([group, keys]) => { %>
	<span class="report-tab-group"><%= group %></span>
	<% keys.forEach((key) => { %><a class="<%= key === report ? 'active' : '' %>" href="<%= tabLink(key) %>"><%= reports[key] %></a><% }) %>
	<% }) %>
</nav>

<% if (report === 'units') { %>
<form class="report-filters report-search no-print" method="get" action="/reports">
	<input type="hidden" name="report" value="units">
	<label>Serial, IMEI, SIM or MAC number<input name="q" value="<%= data.search %>" placeholder="e.g. 354518784007992" autocomplete="off" spellcheck="false" autofocus></label>
	<button class="button small primary" type="submit">Find unit</button>
</form>
<% } else if (report === 'balances') { %>
<form class="report-filters no-print" method="get" action="/reports">
	<input type="hidden" name="report" value="balances">
	<label>Leave year<select name="year"><% data.years.forEach((year) => { %><option <%= data.year === year ? 'selected' : '' %>><%= year %></option><% }) %></select></label>
	<label>Department<select name="dept"><option value="">All</option><% departments.forEach((department) => { %><option <%= filters.department === department ? 'selected' : '' %>><%= department %></option><% }) %></select></label>
	<button class="button small primary" type="submit">Apply</button>
</form>
<% } else if (report === 'absence') { %>
<form class="report-filters no-print" method="get" action="/reports">
	<input type="hidden" name="report" value="absence">
	<div class="report-month-nav"><a class="button outline small" href="/reports<%= query({ month: data.previousMonth }) %>" aria-label="Previous month">&larr;</a><strong><%= range.label %></strong><a class="button outline small" href="/reports<%= query({ month: data.nextMonth }) %>" aria-label="Next month">&rarr;</a></div>
	<label>Month<input type="month" name="month" value="<%= data.grid.month %>"></label>
	<label>Department<select name="dept"><option value="">All</option><% departments.forEach((department) => { %><option <%= filters.department === department ? 'selected' : '' %>><%= department %></option><% }) %></select></label>
	<button class="button small primary" type="submit">Apply</button>
</form>
<% } else { %>
<form class="report-filters no-print" method="get" action="/reports">
	<input type="hidden" name="report" value="<%= report %>">
	<div class="report-presets"><% presets.forEach((preset) => { %><a class="<%= range.preset === preset.key ? 'active' : '' %>" href="/reports<%= query({ from: preset.from, to: preset.to }) %>"><%= preset.label %></a><% }) %></div>
	<label>From<input type="date" name="from" value="<%= range.from %>"></label>
	<label>To<input type="date" name="to" value="<%= range.to %>"></label>
	<% if (report === 'leave') { %>
	<label>Status<select name="status"><option value="approved">Approved</option><option value="pending" <%= filters.status === 'pending' ? 'selected' : '' %>>Awaiting approval</option><option value="rejected" <%= filters.status === 'rejected' ? 'selected' : '' %>>Rejected</option><option value="all" <%= filters.status === 'all' ? 'selected' : '' %>>All</option></select></label>
	<label>Leave type<select name="type"><option value="">All types</option><% leaveTypes.forEach((type) => { %><option value="<%= type %>" <%= filters.type === type ? 'selected' : '' %>><%= leaveTypeLabel(type) %></option><% }) %></select></label>
	<label>Department<select name="dept"><option value="">All</option><% departments.forEach((department) => { %><option <%= filters.department === department ? 'selected' : '' %>><%= department %></option><% }) %></select></label>
	<label>Person<select name="person"><option value="">Everyone</option><% recipients.forEach((person) => { %><option value="<%= person.id %>" <%= String(filters.person) === String(person.id) ? 'selected' : '' %>><%= person.name %></option><% }) %></select></label>
	<% } %>
	<% if (report === 'issued') { %>
	<label>Site / client<select name="site"><option value="">All sites</option><% sites.forEach((site) => { %><option <%= filters.site === site ? 'selected' : '' %>><%= site %></option><% }) %></select></label>
	<label>Product<select name="product"><option value="">All products</option><% products.forEach((product) => { %><option value="<%= product._id %>" <%= filters.product === product._id ? 'selected' : '' %>><%= product.name %></option><% }) %></select></label>
	<label>Issued to<select name="person"><option value="">Everyone</option><% recipients.forEach((person) => { %><option value="<%= person.id %>" <%= String(filters.person) === String(person.id) ? 'selected' : '' %>><%= person.name %></option><% }) %></select></label>
	<% } %>
	<% if (showsPeople) { %>
	<label>Department<select name="dept"><option value="">All</option><% departments.forEach((department) => { %><option <%= filters.department === department ? 'selected' : '' %>><%= department %></option><% }) %></select></label>
	<label>Category<select name="category"><option value="">All</option><% categories.forEach((category) => { %><option <%= filters.category === category ? 'selected' : '' %>><%= category %></option><% }) %></select></label>
	<label>Person<select name="person"><option value="">Everyone</option><% recipients.forEach((person) => { %><option value="<%= person.id %>" <%= String(filters.person) === String(person.id) ? 'selected' : '' %>><%= person.name %></option><% }) %></select></label>
	<% } %>
	<% if (report === 'payouts') { %>
	<label>Status<select name="status"><option value="paid">Paid</option><option value="failed" <%= filters.status === 'failed' ? 'selected' : '' %>>Failed attempts</option><option value="all" <%= filters.status === 'all' ? 'selected' : '' %>>Paid and failed</option></select></label>
	<% } %>
	<button class="button small primary" type="submit">Apply</button>
</form>
<% } %>

<% if (report === 'issued') { const summary = data.summary; %>
<section class="report-tiles">
	<div><small>Units issued</small><strong><%= summary.units %></strong></div>
	<div><small>Requisitions</small><strong><%= summary.requisitions %></strong></div>
	<div><small>Sites / clients</small><strong><%= summary.sites %></strong></div>
</section>
<% if (!data.rows.length) { %><section class="report-card"><p class="report-empty">No hardware was issued in this period with these filters.</p></section><% } else { %>
<div class="report-grid">
	<% [['By site / client', summary.bySite, 'Site / client'], ['By product', summary.byProduct, 'Product']].forEach(([heading, groups, column]) => { %>
	<section class="report-card">
		<h2><%= heading %></h2>
		<table class="report-table report-breakdown">
			<thead><tr><th><%= column %></th><th class="num">Units</th><th>Share</th></tr></thead>
			<tbody><% groups.forEach((group) => { %><tr><td><%= group.key %></td><td class="num"><%= group.count %></td><td><span class="report-share"><span class="report-share-bar" aria-hidden="true"><span style="width: <%= Math.max(2, Math.round(group.share * 100)) %>%"></span></span><%= percent(group.share) %></span></td></tr><% }) %></tbody>
		</table>
	</section>
	<% }) %>
</div>
<section class="report-card report-spaced">
	<h2>Units issued</h2>
	<div class="table-wrap"><table class="report-table">
		<thead><tr><th>Date</th><th>Product</th><th>Qty</th><th>Serial</th><th>IMEI</th><th>Vehicle</th><th>Requisition</th><th>Site / client</th><th>Issued to</th><th>Issued by</th><% if (typeof canEditIssued !== 'undefined' && canEditIssued) { %><th><span class="visually-hidden">Edit</span></th><% } %></tr></thead>
		<tbody><% data.rows.forEach((row) => { %>
			<tr>
				<td><%= day(row.day) %></td>
				<td><%= row.product %></td>
				<td><%= row.quantity || 1 %></td>
				<td class="mono"><% if (row.serial) { %><a href="/reports?report=units&q=<%= encodeURIComponent(row.serial) %>"><%= row.serial %></a><% } else { %>—<% } %></td>
				<td class="mono"><% if (row.imei) { %><a href="/reports?report=units&q=<%= encodeURIComponent(row.imei) %>"><%= row.imei %></a><% } else { %>—<% } %></td>
				<td class="mono"><%= row.vehicle || '—' %></td>
				<td><% if (row.requestId) { %><a href="/requests/<%= row.requestId %>"><%= row.reference %></a><% } else { %>—<% } %></td>
				<td><%= row.site %></td>
				<td><strong><%= row.issuedTo %></strong><small><%= row.department || '—' %></small></td>
				<td><%= row.issuedBy || '—' %></td>
				<% if (typeof canEditIssued !== 'undefined' && canEditIssued) { %><td><% if (row.requestId && row.itemId) { %><a href="/requests/<%= row.requestId %>/destinations" title="Correct where this unit went">Edit</a><% } %></td><% } %>
			</tr>
		<% }) %></tbody>
	</table></div>
</section>
<% } %>
<% } %>

<% if (report === 'units') { %>
<% if (!data.search) { %>
<section class="report-card"><p class="report-empty">Enter a unit's serial, IMEI, SIM or MAC number to see where it has been: when it came into stock, which requisitions it was issued on, which site it went to, and any returns or status changes. Serial numbers in the Issued hardware report link here too.</p></section>
<% } else if (!data.units.length) { %>
<section class="report-card"><p class="report-empty">No unit has the number <strong><%= data.search %></strong>. Check it, or try the unit's other number (serial or IMEI).</p></section>
<% } else { data.units.forEach(({ item, timeline }) => { const ids = Object.entries(item.identifiers || {}).filter(([, value]) => value); %>
<section class="report-card unit-history">
	<header class="unit-history-head">
		<div>
			<h2><%= item.category %><% if (item.model) { %> <small><%= item.model %></small><% } %></h2>
			<dl class="unit-ids"><% ids.forEach(([key, value]) => { %><div><dt><%= { serial: 'Serial', imei: 'IMEI', sim: 'SIM', mac: 'MAC', flsId: 'FLS ID', vid: 'VID' }[key] || key %></dt><dd class="mono"><%= value %></dd></div><% }) %></dl>
		</div>
		<div class="unit-history-status">
			<span class="unit-status <%= item.stock_status %>"><%= statusLabels[item.stock_status] || item.stock_status %></span>
			<% if (item.client || item.issuedTo) { %><small><%= [item.client, item.issuedTo].filter(Boolean).join(' · ') %></small><% } %>
			<% if (canOpenUnits) { %><a class="button outline small no-print" href="/admin/stock/items/<%= encodeURIComponent(item._id) %>">Open in Master Database</a><% } %>
		</div>
	</header>
	<% if (!timeline.length) { %><p class="report-empty">No history recorded for this unit yet.</p><% } else { %>
	<ol class="unit-timeline">
		<% timeline.forEach((event) => { %>
		<li><time><%= fmtDate(event.at) %></time><strong><%= event.title %></strong><% if (event.detail) { %><small><%= event.detail %></small><% } %><% if (event.requestId) { %><a href="/requests/<%= event.requestId %>">View requisition</a><% } %></li>
		<% }) %>
	</ol>
	<% } %>
</section>
<% }) } %>
<% } %>

<% if (report === 'payouts') { const paid = data.rows.filter((row) => row.status === 'Paid'); const failed = data.rows.filter((row) => row.status === 'Failed'); %>
<section class="report-tiles">
	<div><small>Total paid</small><strong><%= kes(data.total) %></strong></div>
	<div><small>Payouts</small><strong><%= paid.length %></strong></div>
	<% if (filters.status !== 'paid') { %><div><small>Failed attempts</small><strong><%= failed.length %></strong><em>Nothing was deducted for these</em></div><% } %>
</section>
<section class="report-card">
	<% if (!data.rows.length) { %><p class="report-empty">No payouts match this period and these filters.</p><% } else { %>
	<div class="table-wrap"><table class="report-table">
		<thead><tr><th>Date</th><th>M-Pesa receipt</th><th>Request</th><th>Recipient</th><th>Phone</th><th>Category</th><th class="num">Amount (KES)</th><th>Approved by</th><th>Sent by</th><% if (filters.status !== 'paid') { %><th>Status</th><% } %></tr></thead>
		<tbody>
			<% data.rows.forEach((row) => { %>
			<tr class="<%= row.status === 'Failed' ? 'is-failed' : '' %>">
				<td><%= day(row.day) %></td>
				<td class="mono"><%= row.receipt || '—' %><% if (row.simulated) { %> <span class="report-tag">Test</span><% } %></td>
				<td><% if (row.requestId) { %><a href="/requests/<%= row.requestId %>"><%= row.reference %></a><% } else { %>—<% } %></td>
				<td><strong><%= row.recipient %></strong><small><%= row.department || '—' %></small></td>
				<td class="mono"><%= row.phone || '—' %></td>
				<td><%= row.category %><% if (row.purpose && row.purpose !== row.category) { %><small><%= row.purpose %></small><% } %></td>
				<td class="num"><%= money(row.amount) %></td>
				<td><%= row.approvedBy || '—' %></td>
				<td><%= row.sentBy || '—' %></td>
				<% if (filters.status !== 'paid') { %><td><span class="report-status <%= row.status === 'Paid' ? 'paid' : 'failed' %>"><%= row.status %></span><% if (row.failureReason) { %><small><%= row.failureReason %></small><% } %></td><% } %>
			</tr>
			<% }) %>
		</tbody>
		<tfoot><tr><th colspan="6">Total paid</th><th class="num"><%= money(data.total) %></th><th colspan="<%= filters.status !== 'paid' ? 3 : 2 %>"></th></tr></tfoot>
	</table></div>
	<% } %>
</section>
<% } %>

<% if (report === 'spend') { const change = data.change; %>
<section class="report-tiles">
	<div><small>Total paid out</small><strong><%= kes(data.total) %></strong>
		<em><% if (change === null) { %><%= data.previousTotal ? '' : 'Nothing paid in the previous period' %><% } else { %><span class="report-change <%= change > 0 ? 'up' : change < 0 ? 'down' : '' %>"><%= change > 0 ? '▲' : change < 0 ? '▼' : '' %> <%= Math.abs(Math.round(change * 100)) %>%</span> vs <%= day(data.previous.from) %> – <%= day(data.previous.to) %> (<%= kes(data.previousTotal) %>)<% } %></em></div>
	<div><small>Payouts</small><strong><%= data.count %></strong></div>
	<div><small>Average payout</small><strong><%= kes(Math.round(data.average)) %></strong></div>
</section>
<% if (!data.count) { %><section class="report-card"><p class="report-empty">No petty cash was paid out in this period.</p></section><% } else { %>
<div class="report-grid">
	<% [['By category', data.byCategory, 'Category'], ['By department', data.byDepartment, 'Department'], ['By person', data.byPerson, 'Person'], ['By month', data.byMonth.map((row) => ({ ...row, key: monthLabel(row.key) })), 'Month']].forEach(([heading, groups, column]) => { %>
	<section class="report-card">
		<h2><%= heading %></h2>
		<table class="report-table report-breakdown">
			<thead><tr><th><%= column %></th><th class="num">Amount (KES)</th><th class="num">Payouts</th><th>Share</th></tr></thead>
			<tbody><% groups.forEach((group) => { %><tr><td><%= group.key %></td><td class="num"><%= money(group.amount) %></td><td class="num"><%= group.count %></td><td><span class="report-share"><span class="report-share-bar" aria-hidden="true"><span style="width: <%= Math.max(2, Math.round(group.share * 100)) %>%"></span></span><%= percent(group.share) %></span></td></tr><% }) %></tbody>
		</table>
	</section>
	<% }) %>
</div>
<% } %>
<% } %>

<% if (report === 'treasury') { const totals = data.months.reduce((sum, month) => ({ fundsIn: sum.fundsIn + month.fundsIn, paidOut: sum.paidOut + month.paidOut, failed: sum.failed + month.failedAttempts }), { fundsIn: 0, paidOut: 0, failed: 0 }); %>
<section class="report-tiles">
	<div><small>Funds added</small><strong><%= kes(totals.fundsIn) %></strong></div>
	<div><small>Paid out</small><strong><%= kes(totals.paidOut) %></strong></div>
	<div><small>Net movement</small><strong><%= totals.fundsIn - totals.paidOut < 0 ? '−' : '' %><%= kes(Math.abs(totals.fundsIn - totals.paidOut)) %></strong></div>
	<div><small>Balance today</small><strong><%= kes(treasuryBalance) %></strong><em>As shown on the Wallet page</em></div>
</section>
<section class="report-card">
	<table class="report-table">
		<thead><tr><th>Month</th><th class="num">Funds added (KES)</th><th class="num">Paid out (KES)</th><th class="num">Payouts</th><th class="num">Failed attempts</th><th class="num">Net movement (KES)</th></tr></thead>
		<tbody><% data.months.forEach((month) => { %><tr><td><%= monthLabel(month.month) %></td><td class="num"><%= money(month.fundsIn) %></td><td class="num"><%= money(month.paidOut) %></td><td class="num"><%= month.payouts %></td><td class="num"><%= month.failedAttempts %><% if (month.failedAttempts) { %><small>KES <%= money(month.failedAmount) %> not deducted</small><% } %></td><td class="num"><%= month.net < 0 ? '−' : '' %><%= money(Math.abs(month.net)) %></td></tr><% }) %></tbody>
		<tfoot><tr><th>Total</th><th class="num"><%= money(totals.fundsIn) %></th><th class="num"><%= money(totals.paidOut) %></th><th class="num"><%= data.months.reduce((sum, month) => sum + month.payouts, 0) %></th><th class="num"><%= totals.failed %></th><th class="num"><%= totals.fundsIn - totals.paidOut < 0 ? '−' : '' %><%= money(Math.abs(totals.fundsIn - totals.paidOut)) %></th></tr></tfoot>
	</table>
	<p class="report-note">Funds added are the top-ups recorded in the portal. Money loaded straight into the B2C account in the M-Pesa organisation portal shows up in the balance, not here.</p>
</section>
<% } %>
<% if (report === 'leave') { const stats = data.stats; %>
<section class="report-tiles">
	<div><small>Applications</small><strong><%= stats.applications %></strong></div>
	<div><small>Working days</small><strong><%= stats.days %></strong><em>Full length of each application</em></div>
	<div><small>People</small><strong><%= stats.people %></strong></div>
</section>
<% if (!data.rows.length) { %><section class="report-card"><p class="report-empty">No leave matches this period and these filters.</p></section><% } else { %>
<div class="report-grid">
	<% [['By leave type', stats.byType, 'Leave type'], ['By department', stats.byDepartment, 'Department']].forEach(([heading, groups, column]) => { %>
	<section class="report-card">
		<h2><%= heading %></h2>
		<table class="report-table report-breakdown">
			<thead><tr><th><%= column %></th><th class="num">Days</th><th class="num">Applications</th><th>Share of days</th></tr></thead>
			<tbody><% groups.forEach((group) => { %><tr><td><%= group.key %></td><td class="num"><%= group.amount %></td><td class="num"><%= group.count %></td><td><span class="report-share"><span class="report-share-bar" aria-hidden="true"><span style="width: <%= Math.max(2, Math.round(group.share * 100)) %>%"></span></span><%= percent(group.share) %></span></td></tr><% }) %></tbody>
		</table>
	</section>
	<% }) %>
</div>
<section class="report-card report-spaced">
	<h2>Applications</h2>
	<div class="table-wrap"><table class="report-table">
		<thead><tr><th>Reference</th><th>Employee</th><th>Leave type</th><th>From</th><th>To</th><th class="num">Working days</th><th>Status</th><th>Decided by</th><th>Covering</th></tr></thead>
		<tbody><% data.rows.forEach((row) => { %>
			<tr>
				<td><a href="/requests/<%= row.id %>"><%= row.reference %></a></td>
				<td><strong><%= row.name %></strong><small><%= row.department || '—' %></small></td>
				<td><%= leaveTypeLabel(row.type) %></td>
				<td><%= day(row.start) %></td>
				<td><%= day(row.end) %></td>
				<td class="num"><%= row.days %></td>
				<td><span class="leave-report-status <%= row.status %>"><%= row.statusLabel %></span></td>
				<td><%= row.decidedBy || '—' %></td>
				<td><%= row.cover || '—' %></td>
			</tr>
		<% }) %></tbody>
	</table></div>
</section>
<% } %>
<% } %>

<% if (report === 'balances') { const allowanceTypes = Object.keys(data.rows[0]?.allowances || {}); %>
<section class="report-tiles">
	<div><small>People</small><strong><%= data.rows.length %></strong></div>
	<div><small>Annual days taken</small><strong><%= Math.round(data.rows.reduce((sum, row) => sum + row.taken, 0) * 10) / 10 %></strong></div>
	<div><small>Annual days available</small><strong><%= Math.round(data.rows.reduce((sum, row) => sum + row.available, 0) * 10) / 10 %></strong><em>Earned so far, plus carried forward, less taken</em></div>
</section>
<section class="report-card">
	<% if (!data.rows.length) { %><p class="report-empty">Nobody in this selection takes leave.</p><% } else { %>
	<div class="table-wrap"><table class="report-table">
		<thead>
			<tr><th rowspan="2">Employee</th><th colspan="6" class="report-colgroup">Annual leave (days)</th><th colspan="<%= allowanceTypes.length %>" class="report-colgroup">Other leave used / limit</th></tr>
			<tr><th class="num">Entitled</th><th class="num">Earned</th><th class="num">Carried</th><th class="num">Taken</th><th class="num">Pending</th><th class="num">Available</th><% allowanceTypes.forEach((type) => { %><th class="num"><%= leaveTypeLabel(type) %></th><% }) %></tr>
		</thead>
		<tbody><% data.rows.forEach((row) => { %>
			<tr>
				<td><strong><%= row.name %></strong><small><%= [row.positionLabel, row.department].filter(Boolean).join(' · ') %></small></td>
				<td class="num"><%= row.entitlement %></td>
				<td class="num"><%= row.earned %></td>
				<td class="num"><%= row.carried %></td>
				<td class="num"><%= row.taken %></td>
				<td class="num"><%= row.pending || '—' %></td>
				<td class="num"><strong class="<%= row.available < 0 ? 'balance-negative' : '' %>"><%= row.available %></strong></td>
				<% allowanceTypes.forEach((type) => { const allowance = row.allowances[type]; %><td class="num <%= allowance.used ? '' : 'muted' %>"><%= allowance.used %> / <%= allowance.limit %></td><% }) %>
			</tr>
		<% }) %></tbody>
	</table></div>
	<p class="report-note">Annual leave is earned month by month through the year. Other leave types have their own yearly limits, counted in working or calendar days as on the leave form.</p>
	<% } %>
</section>
<% } %>

<% if (report === 'absence') { const grid = data.grid; %>
<section class="report-tiles">
	<div><small>People with leave this month</small><strong><%= grid.people.length %></strong></div>
	<div><small>Off today</small><strong><%= grid.days.some((entry) => entry.today) ? grid.offToday : '—' %></strong><% if (!grid.days.some((entry) => entry.today)) { %><em>Today is not in this month</em><% } %></div>
</section>
<section class="report-card">
	<% if (!grid.people.length) { %><p class="report-empty">Nobody has approved or pending leave in <%= range.label %>.</p><% } else { %>
	<div class="absence-legend"><span><i class="absence-swatch approved"></i>Approved leave</span><span><i class="absence-swatch pending"></i>Awaiting approval</span><span><i class="absence-swatch weekend"></i>Weekend</span></div>
	<div class="table-wrap"><table class="absence-grid">
		<thead><tr><th class="absence-name">Employee</th><% grid.days.forEach((entry) => { %><th class="<%= entry.weekend ? 'is-weekend' : '' %> <%= entry.today ? 'is-today' : '' %>"><span><%= entry.letter %></span><%= entry.day %></th><% }) %></tr></thead>
		<tbody><% grid.people.forEach((person) => { %>
			<tr>
				<th class="absence-name"><strong><%= person.name %></strong><small><%= person.department || '—' %></small></th>
				<% person.cells.forEach((cell, index) => { const entry = grid.days[index]; %><td class="<%= entry.weekend ? 'is-weekend' : '' %> <%= entry.today ? 'is-today' : '' %>"><% if (cell) { %><span class="absence-mark <%= cell.state %>" title="<%= person.name %> · <%= leaveTypeLabel(cell.type) %> leave · <%= cell.state === 'approved' ? 'approved' : 'awaiting approval' %> · <%= day(cell.start) %> – <%= day(cell.end) %> (<%= cell.reference %>)"><span class="visually-hidden"><%= leaveTypeLabel(cell.type) %>, <%= cell.state === 'approved' ? 'approved' : 'awaiting approval' %></span></span><% } %></td><% }) %>
			</tr>
		<% }) %></tbody>
	</table></div>
	<p class="report-note no-print">Hover over a marked day to see the leave type, dates and reference.</p>
	<% } %>
</section>
<% } %>
<%- include('partials/footer') %>
