@extends('layouts.app')
@section('title','Post Written-Off Loan Recovery')
@section('content')
<x-ui.page-toolbar :title="'Post Recovery · '.$writeOff->write_off_number" :description="'Written-off loan '.$writeOff->loan->loan_number.' · '.$writeOff->loan->customer->full_name" :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Write-Offs','url'=>route('loan-write-offs.index')],['label'=>$writeOff->write_off_number,'url'=>route('loan-write-offs.show',$writeOff)],['label'=>'Recovery']]" />

<div class="grid gap-4 lg:grid-cols-3">
    <div class="lg:col-span-2">
        <form method="POST" action="{{ route('loan-write-offs.recoveries.store',$writeOff) }}" class="mf-card" data-ui="form-shell">
            @csrf
            <div class="border-b p-4"><h2 class="font-semibold">Written-off loan recovery receipt</h2><p class="mt-1 text-xs text-muted-foreground">Recovery is posted to cash/accounting separately from normal loan payments and never rewrites the original write-off.</p></div>
            <div class="p-4 space-y-4">
                <div class="grid gap-3 md:grid-cols-2">
                    <div><label class="mf-field-label">Receiving account</label><select class="mf-input" name="financial_account_id" required><option value="">Select account</option>@foreach($accounts as $account)<option value="{{ $account->id }}" @selected(old('financial_account_id')==$account->id)>{{ $account->name }} · {{ number_format((float)$account->current_balance,2) }}</option>@endforeach</select></div>
                    <div><label class="mf-field-label">Amount</label><input class="mf-input" name="amount" value="{{ old('amount') }}" inputmode="decimal" required placeholder="0.00"><p class="mt-1 text-xs text-muted-foreground">Maximum {{ number_format((float)$writeOff->recoverable_balance,2) }}</p></div>
                    <div><label class="mf-field-label">Recovery date</label><input type="datetime-local" class="mf-input" name="date" value="{{ old('date',now()->format('Y-m-d\TH:i')) }}" required></div>
                    <div><label class="mf-field-label">Method</label><select class="mf-input" name="method" required>@foreach(['cash'=>'Cash','bank'=>'Bank','mobile_money'=>'Mobile money','other'=>'Other'] as $value=>$label)<option value="{{ $value }}" @selected(old('method')===$value)>{{ $label }}</option>@endforeach</select></div>
                    <div><label class="mf-field-label">Reference</label><input class="mf-input" name="reference" value="{{ old('reference') }}" placeholder="Optional external receipt/reference"></div>
                    <div><label class="mf-field-label">Notes</label><input class="mf-input" name="notes" value="{{ old('notes') }}" placeholder="Optional collection note"></div>
                </div>
                @if($errors->any())<div class="rounded-md border p-3 text-sm text-destructive">{{ $errors->first() }}</div>@endif
                <div class="flex justify-end gap-2"><a class="mf-btn" href="{{ route('loan-write-offs.show',$writeOff) }}">Cancel</a><button class="mf-btn mf-btn-primary">Post recovery</button></div>
            </div>
        </form>
    </div>
    <div class="space-y-4">
        <div class="mf-card"><div class="border-b p-4"><h2 class="font-semibold">Recovery position</h2></div><div class="p-4 space-y-3 text-sm">
            <div class="flex justify-between"><span class="text-muted-foreground">Original write-off</span><span>{{ number_format((float)$writeOff->write_off_amount,2) }}</span></div>
            <div class="flex justify-between"><span class="text-muted-foreground">Recovered to date</span><span>{{ number_format((float)$writeOff->recovered_amount,2) }}</span></div>
            <div class="flex justify-between border-t pt-3"><span class="font-medium">Remaining recoverable</span><span class="font-semibold">{{ number_format((float)$writeOff->recoverable_balance,2) }}</span></div>
        </div></div>
        <div class="mf-card mf-card-body text-sm"><strong>Accounting:</strong><p class="mt-1 text-muted-foreground">Cash is debited to the selected financial account and credited to dedicated Written-Off Loan Recovery income. The original loan payment allocation is not changed.</p></div>
    </div>
</div>
@endsection
