<%- include('partials/header', { title, user }) %>
<% const identifiers = Object.entries(item.identifiers || {}).filter(([, value]) => value); const identifierLabels = { serial: 'Serial', imei: 'IMEI', sim: 'SIM / ICCID', msisdn: 'MSISDN', mac: 'MAC', flsId: 'FLS ID', vid: 'VID' }; %>
<section class="hero">
	<div><p class="kicker"><a href="/admin/stock?product=<%= encodeURIComponent(item.product_id || '') %>"><%= item.category %></a></p><h1><%= item.primaryIdentifier %></h1><p class="muted"><%= item.model ? `Model ${item.model} · ` : '' %>Last updated <%= fmtDate(item.stock_updated_at || item.imported_at || item.created_at) %></p></div>
	<span class="stock-status large <%= item.stock_status %>"><%= statusLabels[item.stock_status] || item.stock_status %></span>
</section>
<section class="detail-grid stock-item-grid">
	<article class="panel">
		<h2>Unit details</h2>
		<dl class="detail-list">
			<% identifiers.forEach(([key, value]) => { %><div><dt><%= identifierLabels[key] || key %></dt><dd><%= value %></dd></div><% }) %>
			<div><dt>Client</dt><dd><%= item.client || '—' %></dd></div>
			<div><dt>Issued to</dt><dd><%= item.issuedTo || '—' %></dd></div>
			<% if (linkedRequest) { %><div><dt><%= item.issued_request_id ? 'Issued on requisition' : 'Last issued on' %></dt><dd><a href="/requests/<%= linkedRequest.id %>">#<%= linkedRequest.id %></a><%= linkedOwner ? ` · ${linkedOwner.full_name}` : '' %><%= requestWhere(linkedRequest.details) ? ` · ${requestWhere(linkedRequest.details)}` : '' %></dd></div><% } %>
			<div><dt>Origin</dt><dd><%= item.source === 'portal' ? 'Received in portal' : 'Imported from master workbook' %><%= item.rowNumber ? ` (row ${item.rowNumber})` : '' %></dd></div>
		</dl>
	</article>
	<% if (canManageStock) { %>
	<article class="panel">
		<h2>Change status</h2>
		<% if (!transitions.length) { %><p class="muted">No manual changes are available for this unit.</p><% } else { %>
		<p class="muted small">Issuing happens through an approved requisition. Use this to record returns, deployments to clients, and write-offs.</p>
		<form method="post" action="/admin/stock/items/<%= encodeURIComponent(item._id) %>/status" class="stack" data-status-form>
			<fieldset class="status-choices"><legend>New status</legend>
				<% transitions.forEach((status, index) => { %><label><input type="radio" name="stock_status" value="<%= status %>" <%= index === 0 ? 'checked' : '' %>> <%= status === 'in_stock' && item.stock_status === 'issued' ? 'Returned to stock' : statusLabels[status] %></label><% }) %>
			</fieldset>
			<div class="grid two" data-assign-fields>
				<label>Client<input name="client" maxlength="120" value="<%= item.client || '' %>"></label>
				<label>Issued to (person / vehicle)<input name="issued_to" maxlength="120" value="<%= item.issuedTo || '' %>"></label>
			</div>
			<label>Note<input name="note" maxlength="300" placeholder="Reason, delivery note, vehicle registration…"><span class="hint">Required when retiring a unit.</span></label>
			<div class="actions"><button class="button primary" type="submit">Save status</button></div>
		</form>
		<script>
		(() => {
			const form = document.querySelector('[data-status-form]');
			const assign = form?.querySelector('[data-assign-fields]');
			if (!form || !assign) return;
			const sync = () => { assign.hidden = form.querySelector('input[name="stock_status"]:checked')?.value !== 'assigned'; };
			form.addEventListener('change', sync);
			sync();
		})();
		</script>
		<% } %>
	</article>
	<% } %>
	<article class="panel">
		<h2>Movement history</h2>
		<% if (!movements.length) { %><p class="muted">No movements recorded since stock moved into the portal database.</p><% } else { %>
		<ul class="movement-list"><% movements.forEach((movement) => { %>
			<li><time><%= fmtDate(movement.at) %></time><span class="movement-action <%= movement.action %>"><%= movement.action %></span><span><%= movement.from ? `${statusLabels[movement.from] || movement.from} → ` : '' %><%= statusLabels[movement.to] || movement.to %> · <%= movement.by_name || 'System' %><% if (movement.request_id) { %> · <a href="/requests/<%= movement.request_id %>">Requisition #<%= movement.request_id %></a><% } %></span><% if (movement.note) { %><small><%= movement.note %></small><% } %></li>
		<% }) %></ul>
		<% } %>
	</article>
</section>
<p><a href="/admin/stock">&larr; Back to Master Database</a></p>
<%- include('partials/footer') %>
