<%# Requisitions approved by the Operations Manager, ready to issue. Shown to people who may issue stock
    other than Admin & Logistics (Front Office, and people granted stock issuing on their account). %>
<% if (Array.isArray(locals.dispatchQueue)) { const queue = locals.dispatchQueue; const owners = locals.dispatchOwners || {}; %>
<section class="logi-panel staff-dispatch" id="dispatch-queue">
	<div class="logi-panel-head">
		
		<div><h2>Ready to dispatch</h2><p>Hardware requisitions approved by the Operations Manager. Pick the units and issue them.</p></div>
		<span class="logi-count blue"><%= queue.length %> waiting</span>
	</div>
	<% if (!queue.length) { %>
	<div class="logi-empty"><strong>Nothing to dispatch</strong><p>No approved requisitions are waiting for hardware.</p></div>
	<% } %>
	<ul class="logi-list">
	<% queue.forEach((request) => { const owner = owners[request.user_id]; const stockChecks = request.stockChecks || []; const matched = stockChecks.length && stockChecks.every((line) => line.matches.length); const allAvailable = matched && stockChecks.every((line) => line.available >= line.quantity); %>
		<li class="logi-requisition">
			<span class="logi-avatar blue" aria-hidden="true"><%= initials(owner?.full_name || '?') %></span>
			<div class="logi-item-main">
				<div class="logi-item-title"><strong><%= request.details?.item_requested || 'Hardware requisition' %></strong><span class="logi-stage ready"><%= (() => { const lines = request.details?.items || []; const issued = lines.reduce((sum, line) => sum + (line.issued_serial_numbers?.length || 0) + Number(line.issued_quantity || 0), 0); const total = lines.reduce((sum, line) => sum + Number(line.quantity || 0), 0); return issued ? `Partly issued · ${issued} of ${total}` : 'Ready to issue'; })() %></span></div>
				<p><%= stockChecks.map((line) => `${line.stockType} × ${line.quantity}`).join(' · ') || `${Number(request.details?.quantity || 0)} units` %></p>
				<small><a href="/requests/<%= request.id %>">#RQ-<%= String(request.id).padStart(3, '0') %></a> · <%= owner?.full_name || 'Unknown' %> · <%= requestWhere(request.details) || owner?.department || '' %></small>
			</div>
			<div class="logi-stock">
				<% if (matched) { %>
				<span class="logi-stock-pill <%= allAvailable ? 'ok' : 'low' %>"><%= allAvailable ? 'In stock' : 'Short stock' %></span>
				<small><%= stockChecks.map((line) => `${line.available} available / ${line.quantity} needed`).join(', ') %></small>
				<% } else { %>
				<span class="logi-stock-pill low">No catalogue match</span>
				<% } %>
			</div>
			<div class="logi-item-action"><button class="button small primary" type="button" data-queue-review="<%= request.id %>">Review &amp; issue</button></div>
		</li>
	<% }) %>
	</ul>
</section>
<%- include('requisition-review-modal') %>
<% } %>
