<%- include('partials/header', { title, user }) %>
<%
const spendMax = Math.max(...spendSeries.map((month) => month.total), 0);
const openCount = stats.pending + stats.computed;
const pct = (value) => stats.total ? Math.round(value / stats.total * 1000) / 10 : 0;
const approvedPct = pct(stats.approved);
const openPct = pct(openCount);
const rejectedPct = pct(stats.rejected);
// The Head Technician deals only with technicians, so payout figures and company-wide charts are hidden.
const isHeadTech = isHeadTechnician(user);
%>
<section class="dashboard-heading">
	<div><% if (!['director', 'manager'].includes(user.position_level)) { %><p class="kicker">Approver dashboard</p><% } %><h1><%= greeting %>, <%= user.full_name.split(' ')[0] %>.</h1></div>
	<% if (user.can_request_items || isHeadTech) { %><div class="dashboard-request-actions"><a class="button primary" href="/requests/new?type=petty_cash">Request petty cash</a><a class="button outline" href="/requests/new?type=requisition">Request hardware</a></div><% } %>
</section>
<%- include('partials/cover-requests') %>
<%- include('partials/dispatch-queue') %>
<div class="dashboard-metrics<%= isHeadTech ? ' three' : '' %>">
	<article><span>Cash balance</span><strong>KES <%= money(dashboard.balance) %></strong></article>
	<% if (!isHeadTech) { %><article><span>Disbursed this month</span><strong>KES <%= money(dashboard.monthSpend) %></strong></article><% } %>
	<article class="metric-alert"><span>Pending action</span><strong><%= dashboard.pendingAction %> <small>items</small></strong></article>
	<article><span>On leave today</span><strong><%= dashboard.onLeave %> <small><%= isHeadTech ? 'technicians' : 'staff members' %></small></strong></article>
</div>
<section class="action-panel">
	<div class="action-heading"><span class="pulse" aria-hidden="true"></span><div><h2>Needs your attention</h2><p>Requests and accounts waiting on you</p></div><strong><%= dashboard.pendingAction %></strong></div>
	<% if (!awaitingApproval.length && !pendingUsers.length) { %><p class="empty">You're all caught up. Nothing needs your attention right now.</p><% } %>
	<% awaitingApproval.forEach((request) => { const owner = ownerMap[request.user_id]; const isLeave = request.request_type === 'leave'; %>
		<div class="action-row"><div><b>#<%= request.id %> · <%= prettyType(request.request_type) %></b><span><%= owner?.full_name || 'Unknown staff member' %> · <%= isLeave ? `${request.details?.days_requested || 0} days (${request.details?.leave_type || 'leave'})` : `KES ${money(request.details?.amount_requested || request.details?.amount_approved || 0)}` %></span></div><div class="actions"><a class="button small outline" href="/requests/<%= request.id %>">Review</a><form method="post" action="/requests/<%= request.id %>/decision"><button class="button small success" name="status" value="approved">Approve</button><button class="button small danger" name="status" value="rejected">Reject</button></form></div></div>
	<% }) %>
	<% pendingUsers.forEach((person) => { %>
		<div class="action-row"><div><b>Account activation</b><span><%= person.full_name %> · <%= positionLabel(person.position_level) %> · <%= person.department || 'No department' %></span></div><form method="post" action="/admin/users/<%= person.id %>/activate" class="activate-form"><input type="hidden" name="return_to" value="/dashboard"><% if (protectedPositions.includes(person.position_level)) { %><button class="button small success" type="submit">Activate</button><% } else { %><%- include('partials/supervisor-select', { selectName: 'supervisor_id', options: supervisorOptions, person, allowNone: false, required: true }) %><button class="button small success" type="submit">Activate</button><% } %></form></div>
	<% }) %>
</section>
<% if (!isHeadTech) { %>
<div class="dashboard-columns">
	<section class="chart-panel">
		<div class="panel-title"><h2>Petty cash disbursed</h2><span>Last 6 months · KES</span></div>
		<div class="bar-chart" role="img" aria-label="Petty cash disbursed per month: <%= spendSeries.map((month) => `${month.label} KES ${money(month.total)}`).join(', ') %>">
			<% spendSeries.forEach((month, index) => { const height = spendMax ? Math.max(4, Math.round(month.total / spendMax * 100)) : 4; %>
				<div title="<%= month.label %>: KES <%= money(month.total) %>"><b><%= month.total ? (month.total >= 1000 ? `${Math.round(month.total / 100) / 10}k` : Math.round(month.total)) : '' %></b><i class="<%= index === spendSeries.length - 1 ? 'current' : '' %>" style="height: <%= height %>%"></i><span><%= month.label %></span></div>
			<% }) %>
		</div>
	</section>
	<section class="chart-panel">
		<div class="panel-title"><h2>Requests by status</h2><span><%= stats.total %> total</span></div>
		<div class="request-mix">
			<div class="donut" role="img" aria-label="<%= stats.approved %> approved, <%= openCount %> open, <%= stats.rejected %> rejected" style="--approved: <%= approvedPct %>%; --open: <%= approvedPct + openPct %>%; --rejected: <%= approvedPct + openPct + rejectedPct %>%"><strong><%= stats.total %></strong><span>requests</span></div>
			<div class="legend"><span><i class="legend-green"></i>Approved <b><%= stats.approved %></b></span><span><i class="legend-amber"></i>Open <b><%= openCount %></b></span><span><i class="legend-red"></i>Rejected <b><%= stats.rejected %></b></span></div>
		</div>
	</section>
</div>
<% } %>
<section class="panel recent-panel"><div class="panel-head"><h2>Recent requests</h2><a href="/requests">View all &rarr;</a></div><% if (!recent.length) { %><p class="empty">No requests yet.</p><% } else { %><div class="table-wrap"><table><thead><tr><th>Reference</th><th>Type</th><th>Requester</th><th>Status</th><th>Submitted</th></tr></thead><tbody><% recent.forEach((request) => { %><tr><td><a class="request-id" href="/requests/<%= request.id %>">#<%= request.id %></a></td><td class="capitalize"><%= prettyType(request.request_type) %></td><td><% if (request.user_id === user.id) { %><strong>You</strong><% } else { %><%= ownerMap[request.user_id]?.full_name || '—' %><% if (ownerMap[request.user_id]?.deleted_at) { %> <small class="muted">(account removed)</small><% } %><% } %></td><td><span class="status status-label <%= requestStatusTone(request) %>"><%= requestStatusLabel(request) %></span></td><td><%= fmtDate(request.submitted_at) %></td></tr><% }) %></tbody></table></div><% } %></section>
<%- include('partials/footer') %>
