@extends('layouts.app')
@section('title','Loan Payments')
@section('content')
<x-ui.page-toolbar title="Loan Payments" description="Find an active loan, receive a payment, and review recent branch collections." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Payments']]" />

<form method="GET" class="mf-card mf-card-body mb-4">
    <label class="mf-field-label">Find active loan</label>
    <div class="flex gap-2"><input class="mf-input flex-1" name="search" value="{{ $search }}" placeholder="Loan number, customer number, name or phone"><button class="mf-btn mf-btn-primary">Search</button></div>
</form>

@if($search!=='')
<div class="mf-card mb-4">
<div class="border-b p-4"><h2 class="font-semibold">Search results</h2><p class="mt-1 text-xs text-muted-foreground">Only active loans in your assigned branches can receive payments.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Loan</th><th>Customer</th><th>Branch</th><th>Outstanding</th><th></th></tr></thead><tbody>
@forelse($loans as $loan)<tr><td><a class="font-medium text-primary hover:underline" href="{{ route('loan-payments.statement',$loan) }}">{{ $loan->loan_number }}</a><div class="text-xs text-muted-foreground">{{ $loan->product->name }}</div></td><td>{{ $loan->customer->full_name }}<div class="text-xs text-muted-foreground">{{ $loan->customer->customer_number }} · {{ $loan->customer->phone }}</div></td><td>{{ $loan->branch->name }}</td><td class="font-medium">{{ number_format((float)$loan->total_outstanding,2) }}</td><td class="text-right"><a class="mf-btn mf-btn-primary" href="{{ route('loan-payments.create',$loan) }}">Receive payment</a></td></tr>@empty<tr><td colspan="5" class="text-center text-muted-foreground">No active loan matched this search.</td></tr>@endforelse
</tbody></table></div></div>
@endif

<div class="mf-card">
<div class="border-b p-4"><h2 class="font-semibold">Recent payments</h2><p class="mt-1 text-xs text-muted-foreground">Permanent office payment history for your accessible branches.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Receipt</th><th>Loan / customer</th><th>Date</th><th>Method</th><th>Account</th><th>Amount</th><th></th></tr></thead><tbody>
@forelse($recent as $payment)<tr><td class="font-medium">{{ $payment->receipt_number }}</td><td><a class="text-primary hover:underline" href="{{ route('loan-payments.statement',$payment->loan) }}">{{ $payment->loan->loan_number }}</a><div class="text-xs text-muted-foreground">{{ $payment->loan->customer->full_name }}</div></td><td>{{ $payment->payment_date->format('d M Y') }}</td><td>{{ str($payment->method)->replace('_',' ')->title() }}</td><td>{{ $payment->financialAccount->name }}</td><td class="font-medium">{{ number_format((float)$payment->amount,2) }}</td><td class="text-right"><a class="text-primary hover:underline" href="{{ route('loan-payments.receipt',$payment) }}">Receipt</a></td></tr>@empty<tr><td colspan="7" class="text-center text-muted-foreground">No loan payments recorded yet.</td></tr>@endforelse
</tbody></table></div></div>
<div class="mt-4">{{ $recent->links() }}</div>
@endsection
