<!doctype html>
<html lang="en">
<head>
  <%- include('partials/head', { title }) %>
</head>
<body class="print-body">
<main class="print-sheet pick-list">
  <% const reference = `RQ-${String(request.id).padStart(3, '0')}`; %>
  <% if (flash) { %><div class="alert <%= flash.type %> no-print" role="status"><%= flash.text %></div><% } %>
  <header class="print-header">
    <img src="/images/logo.png" alt="GAPTECH Solutions Ltd">
    <div>
      <p class="kicker">Pick list · <%= reference %> · Round <%= round.round %><%= rounds > 1 ? ` of ${rounds}` : '' %></p>
      <h1>Hardware to release</h1>
      <p class="muted">For <strong><%= owner?.full_name || 'Unknown' %></strong><%= requestWhere(request.details) ? ` · ${requestWhere(request.details)}` : '' %><%= request.details?.vehicles?.length ? ` · Vehicles${request.details.vehicle_id_type === 'chassis' ? ' (chassis no.)' : ''}: ${request.details.vehicles.join(', ')}` : '' %> · Issued by <%= round.by_name %> on <%= fmtDate(round.at) %></p>
    </div>
  </header>

  <section>
    <% if (round.backup_site) { %><p><% if (round.destination === 'branch') { %><strong>Dispatch to <%= round.backup_site %></strong>: stock sent to another branch, not assigned to vehicles.<% } else { %><strong>Held at <%= round.backup_site %></strong>: pending vehicle assignment.<% } %></p><% } %>
    <h2>Units in this round (<%= round.units.length + (round.quantities || []).reduce((sum, entry) => sum + Number(entry.quantity || 0), 0) %>)</h2>
    <table>
      <thead><tr><th>#</th><th>Product</th><th>Serial</th><th>IMEI</th><th>Vehicle</th><th>Picked</th></tr></thead>
      <tbody>
        <% round.units.forEach((unit, index) => { const numbers = roundUnitNumbers(unit); %>
        <tr><td><%= index + 1 %></td><td><%= unit.product %></td><td class="pick-number"><%= numbers.serial || '—' %></td><td class="pick-number"><%= numbers.imei || '—' %></td><td class="pick-number"><%= numbers.vehicle ? `${numbers.vehicleKind === 'chassis' ? 'Chassis ' : ''}${numbers.vehicle}` : (numbers.site ? `${numbers.destination === 'branch' ? 'Branch' : 'Awaiting vehicle'}: ${numbers.site}` : '—') %></td><td><span class="pick-box" aria-hidden="true"></span></td></tr>
        <% }) %>
        <% (round.quantities || []).forEach((entry, index) => { %>
        <tr><td><%= round.units.length + index + 1 %></td><td><%= entry.product %></td><td colspan="3" class="pick-number">Quantity: <strong><%= entry.quantity %></strong> (no serial numbers)</td><td><span class="pick-box" aria-hidden="true"></span></td></tr>
        <% }) %>
      </tbody>
    </table>
  </section>

  <section>
    <h2>Requisition progress</h2>
    <table>
      <thead><tr><th>Product</th><th>Requested</th><th>Issued so far</th><th>Still to issue</th></tr></thead>
      <tbody>
        <% progress.forEach((line) => { %>
        <tr><td><%= line.product %></td><td><%= line.requested %></td><td><%= line.issued %></td><td><%= Math.max(0, line.requested - line.issued) %></td></tr>
        <% }) %>
      </tbody>
    </table>
    <% if (round.notes) { %><p><strong>Note:</strong> <%= round.notes %></p><% } %>
  </section>

  <section class="pick-signatures">
    <div><span>Released by (store)</span><p><%= round.by_name %></p></div>
    <div><span>Received by (technician)</span><p>&nbsp;</p></div>
    <div><span>Date</span><p>&nbsp;</p></div>
  </section>

  <footer class="print-footer"><span>GAPTECH Solutions Ltd · Fleet Management Systems &amp; IT Solutions</span><span>Printed <%= fmtDate(new Date()) %></span></footer>
  <div class="pick-actions no-print">
    <a class="button outline" href="<%= back %>">Back</a>
    <button class="button primary" type="button" onclick="window.print()">Print pick list</button>
  </div>
</main>
</body>
</html>
