@extends('layouts.app')
@section('title','New Dispatch (Delivery Note)')
@section('crumbs', '<a href="'.route('delivery-notes.index').'">Dispatch</a> · <b>New</b>')
@section('content')
<div class="card"><div class="card-h"><h2>New dispatch — posts stockpile issue immediately</h2><div class="sp"><span class="bdg b-info">{{ $docNo }}</span></div></div>
<div class="card-b"><form method="POST" action="{{ route('delivery-notes.store') }}" data-doc data-tax='{"apply":false}'>@csrf
<div class="frow">
<div class="fld c4"><label>Sales order</label><select name="sales_order_id" onchange="if(this.value)location='{{ route('delivery-notes.create') }}?from_order='+this.value"><option value="">— Ad-hoc (no order) —</option>@foreach($orders as $o)<option value="{{ $o->id }}" {{ $record->sales_order_id==$o->id?'selected':'' }}>{{ $o->order_no }} — {{ $o->customer->name ?? '' }}</option>@endforeach</select></div>
<div class="fld c4"><label>Customer *</label><select name="customer_id" required><option value="">—</option>@foreach($customers as $c)<option value="{{ $c->id }}" data-defcur="{{ $c->default_currency }}" {{ $record->customer_id==$c->id?'selected':'' }}>{{ $c->display_name }}</option>@endforeach</select></div>
<div class="fld c2"><label>Date *</label><input type="date" name="dn_date" value="{{ date('Y-m-d') }}" required></div>
@include('docs._fxfields', ['record' => $record, 'fx' => $fx, 'isNew' => $isNew])
<div class="fld c2"><label>Quarry</label><select name="quarry_id"><option value="">—</option>@foreach($quarries as $q)<option value="{{ $q->id }}" {{ $record->quarry_id==$q->id?'selected':'' }}>{{ $q->name }}</option>@endforeach</select></div>
<div class="fld c3"><label>Vehicle no.</label><input type="text" name="vehicle_no" style="text-transform:uppercase"></div>
<div class="fld c3"><label>Driver</label><input type="text" name="driver_name"></div>
<div class="fld c3"><label>Transporter</label><select name="transporter_id"><option value="">—</option>@foreach($transporters as $t)<option value="{{ $t->id }}">{{ $t->name }}</option>@endforeach</select></div>
<div class="fld c3"><label>Weighbridge ticket</label><select name="weighbridge_ticket_id"><option value="">—</option>@foreach($tickets as $t)<option value="{{ $t->id }}">{{ $t->ticket_no }} — {{ $t->vehicle_no }} ({{ qty($t->net_weight) }}t)</option>@endforeach</select></div>
</div>
@php
$preRows = [];
if(!empty($order)) foreach($order->lines as $l){ $bal = $l->qty_ordered - $l->qty_dispatched; if($bal > 0) $preRows[] = ['product_id'=>$l->product_id,'qty_tonnes'=>$bal,'rate'=>$l->rate,'amount'=>round($bal*$l->rate,2)]; }
@endphp
@include('docs._lines', ['id'=>'dnLines', 'rows'=>$preRows, 'cols'=>[
  ['f'=>'product_id','l'=>'Product *','t'=>'select','o'=>$products->pluck('display_name','id')->all(),'req'=>1],
  ['f'=>'stockpile_id','l'=>'Stockpile','t'=>'select','o'=>$piles->pluck('code','id')->all()],
  ['f'=>'qty_tonnes','l'=>'Qty (t) *','t'=>'number','role'=>'qty','req'=>1,'w'=>'110px'],
  ['f'=>'rate','l'=>'Rate *','t'=>'number','role'=>'rate','req'=>1,'w'=>'120px'],
  ['f'=>'amount','l'=>'Amount','t'=>'number','role'=>'amount','ro'=>1,'w'=>'130px'],
]])
@include('docs._totals', ['tax'=>null])
<div style="margin-top:12px"><button class="btn btn-pri">🚛 Dispatch now</button> <a class="btn btn-ghost" href="{{ route('delivery-notes.index') }}">Cancel</a></div>
</form></div></div>
@endsection
