@extends('layouts.app')
@section('title','Loan Disbursement')
@section('content')
@php
$filters=[];
if(request()->filled('search')) $filters[]=['label'=>'Search','value'=>request('search'),'removeUrl'=>route('loan-disbursements.index',request()->except(['search','ready_page','history_page']))];
@endphp
<x-ui.page-toolbar title="Loan Disbursement" description="Release approved loans from branch financial accounts with permanent accounting and receipt records." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Disbursement']]" />
<x-ui.table-toolbar table-id="ready-disbursements-table" search-placeholder="Search loan, customer, phone or reference…" :columns="['loan'=>'Loan','customer'=>'Customer','branch'=>'Branch','principal'=>'Principal','approved'=>'Approved']" export-filename="ready-for-disbursement.csv" />
<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('loan-disbursements.index')" />

<div class="mf-card mb-4">
<div class="border-b p-4"><h2 class="font-semibold">Ready for Disbursement</h2><p class="mt-1 text-xs text-muted-foreground">Only approved loans in this state can release funds.</p></div>
<div class="mf-table-frame border-0"><table id="ready-disbursements-table" class="mf-table" data-standard-table><thead><tr><th data-table-column="loan">Loan</th><th data-table-column="customer">Customer</th><th data-table-column="branch">Branch</th><th data-table-column="principal">Principal</th><th data-table-column="approved">Approved</th><th data-no-export></th></tr></thead><tbody>
@forelse($ready as $loan)<tr><td data-table-column="loan"><a class="font-medium text-primary hover:underline" href="{{ route('loans.show',$loan) }}">{{ $loan->loan_number }}</a><div class="text-xs text-muted-foreground">{{ $loan->product->name }}</div></td><td data-table-column="customer">{{ $loan->customer->full_name }}<div class="text-xs text-muted-foreground">{{ $loan->customer->customer_number }} · {{ $loan->customer->phone }}</div></td><td data-table-column="branch">{{ $loan->branch->name }}</td><td data-table-column="principal" class="font-medium">{{ number_format((float)$loan->principal,2) }}</td><td data-table-column="approved">{{ $loan->approved_at?->format('d M Y H:i') ?? '—' }}</td><td data-no-export class="text-right"><a class="mf-btn mf-btn-primary" href="{{ route('loan-disbursements.create',$loan) }}">Disburse</a></td></tr>@empty<tr><td colspan="6" class="text-center text-muted-foreground">No approved loans are waiting for disbursement.</td></tr>@endforelse
</tbody></table></div>
@if($ready->hasPages())<div class="p-4 border-t">{{ $ready->links() }}</div>@endif
</div>

<div class="mf-card">
<div class="border-b p-4"><h2 class="font-semibold">Disbursement History</h2><p class="mt-1 text-xs text-muted-foreground">Permanent posted disbursements. Financial records are not deleted.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Disbursement</th><th>Loan</th><th>Customer</th><th>Account</th><th>Date</th><th>Amount</th><th>Disbursed by</th><th></th></tr></thead><tbody>
@forelse($history as $item)<tr><td class="font-medium">{{ $item->disbursement_number }}<div class="text-xs text-muted-foreground">{{ $item->reference ?: 'No external reference' }}</div></td><td><a class="text-primary hover:underline" href="{{ route('loans.show',$item->loan) }}">{{ $item->loan->loan_number }}</a></td><td>{{ $item->loan->customer->full_name }}</td><td>{{ $item->financialAccount->name }}<div class="text-xs text-muted-foreground">{{ $item->branch->name }}</div></td><td>{{ $item->disbursement_date->format('d M Y') }}</td><td class="font-medium">{{ number_format((float)$item->amount,2) }}</td><td>{{ $item->user?->name ?? 'System' }}</td><td class="text-right"><a class="text-primary hover:underline" href="{{ route('loan-disbursements.receipt',$item) }}">Receipt</a></td></tr>@empty<tr><td colspan="8" class="text-center text-muted-foreground">No loans have been disbursed yet.</td></tr>@endforelse
</tbody></table></div>
@if($history->hasPages())<div class="p-4 border-t">{{ $history->links() }}</div>@endif
</div>
@endsection
