@extends('layouts.app')
@section('title',($settlement->exists?'Edit':'New').' Driver Settlement')
@section('page_title','Driver Advance Settlement')
@section('content')
<div class="page-head"><div><h1>{{ $settlement->exists?'Edit':'Create' }} Settlement</h1><p>{{ $advance->advance_number }} · {{ $advance->driver?->name }} · Open AED {{ number_format((float)$open,2) }}</p></div><a class="btn btn-secondary" href="{{ $settlement->exists?route('driver-advances.settlements.show',[$advance,$settlement]):route('driver-advances.show',$advance) }}">Back</a></div>
<form method="post" action="{{ $settlement->exists?route('driver-advances.settlements.update',[$advance,$settlement]):route('driver-advances.settlements.store',$advance) }}">@csrf @if($settlement->exists)@method('put')@endif
<div class="card"><div class="card-body"><div class="form-grid">
<div class="field"><label>Settlement Date *</label><input class="control" type="date" name="settlement_date" value="{{ old('settlement_date',optional($settlement->settlement_date)->format('Y-m-d')) }}" required></div><div class="field"><label>Maximum Open Balance</label><input class="control" value="AED {{ number_format((float)$open,2) }}" readonly></div>
<div class="field"><label>Supported Expenses</label><input class="control settlement-calc" type="number" min="0" step="0.0001" name="expense_amount" value="{{ old('expense_amount',$settlement->expense_amount??0) }}"></div><div class="field"><label>Cash Refunded by Driver</label><input class="control settlement-calc" type="number" min="0" step="0.0001" name="cash_refund_amount" value="{{ old('cash_refund_amount',$settlement->cash_refund_amount??0) }}"></div>
<div class="field"><label>Expense Account</label><select class="control" name="expense_account_id"><option value="">Required when expenses are entered</option>@foreach($accounts as $account)<option value="{{ $account->id }}" @selected(old('expense_account_id',$settlement->expense_account_id)==$account->id)>{{ $account->code }} - {{ $account->name }}</option>@endforeach</select></div><div class="field"><label>Cash / Bank Account</label><select class="control" name="cash_bank_account_id"><option value="">Required when cash is refunded</option>@foreach($accounts as $account)<option value="{{ $account->id }}" @selected(old('cash_bank_account_id',$settlement->cash_bank_account_id)==$account->id)>{{ $account->code }} - {{ $account->name }}</option>@endforeach</select></div>
<div class="field"><label>Total Settlement</label><input class="control" id="settlement-total" readonly></div><div class="field"><label>Remaining After Settlement</label><input class="control" id="settlement-remaining" readonly></div>
<div class="field full"><label>Description</label><textarea class="control" rows="3" name="description">{{ old('description',$settlement->description) }}</textarea></div>
</div></div><div class="card-head" style="justify-content:flex-end"><button class="btn btn-primary">Save Draft</button></div></div></form>
@endsection
@push('scripts')<script>const settlementOpen={{ json_encode((string)$open) }};function settlementCalc(){let expense=Number(document.querySelector('[name=expense_amount]').value||0),refund=Number(document.querySelector('[name=cash_refund_amount]').value||0),total=expense+refund;document.getElementById('settlement-total').value=total.toFixed(2);document.getElementById('settlement-remaining').value=(Number(settlementOpen)-total).toFixed(2)}document.querySelectorAll('.settlement-calc').forEach(i=>i.addEventListener('input',settlementCalc));settlementCalc();</script>@endpush
