@php $mode=$mode??'transfer'; @endphp
<div class="card-erp"><div class="card-erp-header"><span>Product lines</span><small class="text-muted">Search product or scan barcode</small></div><div class="card-erp-body"><input id="inventoryProductSearch" class="form-control" placeholder="Product code, barcode, English or Arabic name" autocomplete="off"><div id="inventoryProductResults" class="mt-1"></div></div><div class="table-responsive"><table class="table table-dense"><thead><tr><th>#</th><th>Product</th><th>Unit</th><th style="width:160px">Quantity</th>@if(in_array($mode,['opening','adjustment']))<th style="width:160px">Unit cost</th>@endif<th></th></tr></thead><tbody id="inventoryRows"><tr id="inventoryEmpty"><td colspan="6" class="text-center text-muted py-4">Add product lines.</td></tr></tbody></table></div></div>
@section('scripts')<script>let iri=0,irt;const invMode='{{ $mode }}',wh=()=>document.getElementById(invMode==='transfer'?'fromWarehouse':(invMode==='adjustment'?'adjustWarehouse':'stockWarehouse'))?.value||'';inventoryProductSearch.oninput=()=>{clearTimeout(irt);let q=inventoryProductSearch.value.trim();if(!q){inventoryProductResults.innerHTML='';return}irt=setTimeout(async()=>{let r=await fetch(`{{ route('api.products.search') }}?q=${encodeURIComponent(q)}&warehouse_id=${wh()}`,{headers:{Accept:'application/json'}}),a=await r.json();inventoryProductResults.innerHTML=a.map((p,i)=>`<button type="button" class="btn btn-sm btn-light border me-1 mb-1 inv-pick" data-i="${i}">${p.product_code} · ${p.name_en} · Stock ${p.current_stock}</button>`).join('')||'<span class="text-muted small">No products found</span>';document.querySelectorAll('.inv-pick').forEach(x=>x.onclick=()=>invAdd(a[x.dataset.i]))},160)};inventoryProductSearch.onkeydown=e=>{if(e.key==='Enter'){e.preventDefault();document.querySelector('.inv-pick')?.click()}};function invAdd(p){inventoryEmpty?.remove();let units=p.units?.length?p.units:[{id:p.sales_unit_id,code:p.base_unit?.code}],u=units.find(x=>x.id==p.sales_unit_id)||units[0],qtyName=invMode==='adjustment'?'adjustment_qty':(invMode==='transfer'?'qty':'qty'),cost=invMode==='opening'?(u.purchase_price||p.cost_price):(p.avg_cost||p.cost_price),tr=document.createElement('tr');tr.innerHTML=`<td class="irn"></td><td><b>${p.product_code} · ${p.name_en}</b><small class="d-block text-muted" dir="rtl">${p.name_ar||''}</small><input type="hidden" name="items[${iri}][product_id]" value="${p.id}"></td><td><select class="form-select" name="items[${iri}][unit_id]">${units.map(x=>`<option value="${x.id}" ${x.id==u.id?'selected':''}>${x.code}</option>`).join('')}</select></td><td><input class="form-control" name="items[${iri}][${qtyName}]" type="number" value="1" min=".0001" step=".0001"></td>${['opening','adjustment'].includes(invMode)?`<td><input class="form-control" name="items[${iri}][unit_cost]" type="number" value="${cost||0}" min="0" step=".0001"></td>`:''}<td><button type="button" class="btn btn-link text-danger"><i class="bi bi-x"></i></button></td>`;inventoryRows.appendChild(tr);tr.querySelector('button').onclick=()=>tr.remove();inventoryRows.querySelectorAll('.irn').forEach((x,i)=>x.textContent=i+1);iri++;inventoryProductSearch.value='';inventoryProductResults.innerHTML='';inventoryProductSearch.focus()}document.getElementById('stockForm').addEventListener('submit',e=>{if(!inventoryRows.querySelector('input[name*="product_id"]')){e.preventDefault();alert('Add at least one product.')}});</script>@endsection
