<%- include('partials/header', { title, user }) %>
<% const reference = `RQ-${String(request.id).padStart(3, '0')}`; %>
<section class="hero">
	<div>
		<p class="kicker"><a href="/requests/<%= request.id %>"><%= reference %></a><%= requestWhere(request.details) ? ` · ${requestWhere(request.details)}` : '' %></p>
		<h1>Where the issued units went</h1>
		<p class="muted">Assign or correct the vehicle, holding site or branch of any unit already issued. The requisition, each unit's record and the issued hardware report are all updated, and every change is logged.</p>
	</div>
</section>
<section class="panel">
	<form method="post" action="/requests/<%= request.id %>/destinations">
		<% if (vehicles.length) { %><datalist id="requisition-vehicles"><% vehicles.forEach((reg) => { %><option value="<%= reg %>"><% }) %></datalist><% } %>
		<div class="table-wrap"><table class="destinations-table">
			<thead><tr><th>Unit</th><th>Issued</th><th>Goes to</th></tr></thead>
			<tbody>
			<% units.forEach((entry, index) => { %>
				<tr>
					<td><strong><%= entry.product %></strong><br><span class="mono"><%= entry.numbers %></span></td>
					<td>Round <%= entry.round %><br><small class="muted"><%= entry.roundAt %></small></td>
					<td>
						<input type="hidden" name="units[<%= index %>][item]" value="<%= entry.unit.item_id %>">
						<div class="destination-cell" data-destination>
							<label><span class="visually-hidden">Destination</span><select name="units[<%= index %>][mode]" data-mode>
								<option value="vehicles" <%= entry.mode === 'vehicles' ? 'selected' : '' %>>Vehicle</option>
								<option value="site_backup" <%= entry.mode === 'site_backup' ? 'selected' : '' %>>Pending vehicle assignment</option>
								<option value="branch" <%= entry.mode === 'branch' ? 'selected' : '' %>>Another branch</option>
							</select></label>
							<label data-kind-wrap><span class="visually-hidden">Identified by</span><select name="units[<%= index %>][kind]">
								<option value="plate" <%= entry.kind === 'plate' ? 'selected' : '' %>>Number plate</option>
								<option value="chassis" <%= entry.kind === 'chassis' ? 'selected' : '' %>>Chassis number</option>
							</select></label>
							<label><span class="visually-hidden">Vehicle, site or branch</span><input name="units[<%= index %>][value]" value="<%= entry.value %>" maxlength="120" autocomplete="off" spellcheck="false" data-value <%= vehicles.length ? 'list=requisition-vehicles' : '' %>></label>
						</div>
					</td>
				</tr>
			<% }) %>
			</tbody>
		</table></div>
		<footer class="destinations-actions"><a class="button outline" href="/requests/<%= request.id %>">Back to <%= reference %></a><button class="button primary" type="submit">Save changes</button></footer>
	</form>
</section>
<script>
// Each row: vehicles need "plate or chassis" and a vehicle; a site or branch needs only its name.
document.querySelectorAll('[data-destination]').forEach((row) => {
	const mode = row.querySelector('[data-mode]');
	const kindWrap = row.querySelector('[data-kind-wrap]');
	const value = row.querySelector('[data-value]');
	const sync = () => {
		const vehicle = mode.value === 'vehicles';
		kindWrap.hidden = !vehicle;
		value.placeholder = vehicle ? 'Plate or chassis number (blank: none)' : mode.value === 'branch' ? 'Branch, e.g. Kisumu branch' : 'Site, e.g. Mombasa yard';
		value.required = !vehicle;
	};
	mode.addEventListener('change', sync);
	sync();
});
</script>
<%- include('partials/footer') %>
