@extends('layouts.app')
@section('title','Loan Approvals')
@section('content')
@php
    $filters=[];
    if(request()->filled('search')) $filters[]=['label'=>'Search','value'=>request('search'),'removeUrl'=>route('loans.approvals.index',request()->except(['search','page']))];
    if(request()->filled('status')) $filters[]=['label'=>'Status','value'=>str(request('status'))->replace('_',' ')->title(),'removeUrl'=>route('loans.approvals.index',request()->except(['status','page']))];
    if(request()->filled('branch')) {
        $selectedBranch=$branches->firstWhere('id',(int)request('branch'));
        if($selectedBranch) $filters[]=['label'=>'Branch','value'=>$selectedBranch->name,'removeUrl'=>route('loans.approvals.index',request()->except(['branch','page']))];
    }
    $tabs=array_merge(
        [['label'=>'Pending Review','value'=>'']],
        collect($statuses)->reject(fn($status)=>$status==='under_review')->map(fn($status)=>['label'=>str($status)->replace('_',' ')->title()->toString(),'value'=>$status])->values()->all()
    );
@endphp

<x-ui.page-toolbar title="Loan Approvals" description="Review submitted loan applications, record decisions and track items returned for correction, documents or collateral." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Approvals']]" />
<x-ui.quick-status-tabs :tabs="$tabs" />
<x-ui.table-toolbar table-id="approval-table" search-placeholder="Search loan, customer or phone…" :filter-names="['branch']" :columns="['loan'=>'Loan','customer'=>'Customer','branch'=>'Branch','product'=>'Product','principal'=>'Principal','status'=>'Status','creator'=>'Created by']" export-filename="loan-approvals.csv">
    <x-slot:filters>
        <select class="mf-input mf-quick-filter" name="branch"><option value="">All branches</option>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected((int)request('branch')===$branch->id)>{{ $branch->name }}</option>@endforeach</select>
    </x-slot:filters>
</x-ui.table-toolbar>
<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('loans.approvals.index')" />

<div class="mf-table-frame">
<table id="approval-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="product">Product</th><th data-table-column="principal">Principal</th><th data-table-column="status">Status</th><th data-table-column="creator">Created by</th><th data-no-export></th></tr></thead>
<tbody>
@forelse($loans 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></td>
<td data-table-column="customer"><span class="font-medium">{{ $loan->customer->full_name }}</span><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="product">{{ $loan->product->name }}</td>
<td data-table-column="principal">{{ number_format((float)$loan->principal,2) }}</td>
<td data-table-column="status"><span class="mf-badge {{ $loan->status==='ready_for_disbursement'?'mf-badge-success':'mf-badge-muted' }}">{{ str($loan->status)->replace('_',' ')->title() }}</span></td>
<td data-table-column="creator">{{ $loan->creator?->name ?? 'System' }}</td>
<td data-no-export class="mf-table-row-actions"><a class="text-sm text-primary hover:underline" href="{{ route('loans.show',$loan) }}">Review</a></td>
</tr>
@empty<tr><td colspan="8" class="text-center text-muted-foreground">No approval records found for this view.</td></tr>@endforelse
</tbody>
</table>
</div>
<div class="mt-4">{{ $loans->links() }}</div>
@endsection
