@extends('layouts.app')
@section('title','New Credit Note')
@section('crumbs', '<a href="'.route('sales-credit-notes.index').'">Credit Notes</a> · <b>New</b>')
@section('content')
<div class="card"><div class="card-h"><h2>New sales credit note</h2><div class="sp"><span class="bdg b-info">{{ $docNo }}</span></div></div>
<div class="card-b"><form method="POST" action="{{ route('sales-credit-notes.store') }}" data-doc>@csrf
<div class="frow">
<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 }}">{{ $c->display_name }}</option>@endforeach</select></div>
<div class="fld c4"><label>Against invoice</label><select name="invoice_id"><option value="">—</option>@foreach($invoices as $i)<option value="{{ $i->id }}" data-cur="{{ $i->currency_code }}">{{ $i->invoice_no }} ({{ $i->currency_code }}) — due {{ money($i->balance_due) }}</option>@endforeach</select></div>
<div class="fld c2"><label>Date *</label><input type="date" name="cn_date" value="{{ date('Y-m-d') }}" required></div>
@include('docs._fxfields', ['record' => $record, 'fx' => $fx, 'isNew' => true, 'lockCur' => !empty($record->invoice_id)])
<script>
(function(){var inv=document.querySelector('select[name=invoice_id]'),cur=document.querySelector('[data-fx-cur]');
if(!inv||!cur)return;
function sync(){var o=inv.options[inv.selectedIndex];var c=o&&o.dataset?o.dataset.cur:'';
var hid=document.querySelector('input[name=currency_code][type=hidden]');
if(c){cur.value=c;cur.dispatchEvent(new Event('change'));cur.disabled=true;
if(hid)hid.value=c;else{var h=document.createElement('input');h.type='hidden';h.name='currency_code';h.value=c;cur.parentNode.appendChild(h);}}
else{cur.disabled=false;if(hid)hid.remove();}}
inv.addEventListener('change',sync);sync();})();
</script>
<div class="fld c2"><label>Reason</label><input type="text" name="reason" placeholder="e.g. Short load"></div>
</div>
@include('docs._lines', ['id'=>'cnLines', 'rows'=>[], 'cols'=>[
  ['f'=>'description','l'=>'Description *','t'=>'text','req'=>1],
  ['f'=>'qty','l'=>'Qty','t'=>'number','role'=>'qty','w'=>'110px'],
  ['f'=>'rate','l'=>'Rate','t'=>'number','role'=>'rate','w'=>'130px'],
  ['f'=>'amount','l'=>'Amount','t'=>'number','role'=>'amount','ro'=>1,'w'=>'140px'],
]])
@include('docs._totals', ['tax'=>null])
<div style="margin-top:12px"><button class="btn btn-pri">Save draft</button> <a class="btn btn-ghost" href="{{ route('sales-credit-notes.index') }}">Cancel</a></div>
</form></div></div>
@endsection
