@extends('layouts.app')
@section('title','Loan Top-Ups')
@section('content')
<x-ui.page-toolbar title="Loan Top-Ups" description="Create controlled replacement loans, settle old balances internally and disburse only the net cash after normal contract and approval checks." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Top-Ups']]" />

<form method="GET" class="mf-card mf-card-body mb-4">
    <div class="mf-form-inline">
        <input class="mf-input" name="search" value="{{ request('search') }}" placeholder="Loan number, top-up number, customer, customer number or phone">
        <button class="mf-btn mf-btn-primary">Search</button>
    </div>
</form>

@if($pending->count())
<div class="mf-card mb-4">
<div class="border-b p-4"><h2 class="font-semibold">Top-up applications in progress</h2><p class="mt-1 text-xs text-muted-foreground">These replacement loans still follow the normal contract and approval workflow.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Replacement loan</th><th>Previous loan</th><th>Customer</th><th>Principal</th><th>Eligibility at application</th><th>Status</th><th></th></tr></thead><tbody>
@foreach($pending as $loan)
<tr>
<td class="font-medium">{{ $loan->loan_number }}</td>
<td><a class="text-primary hover:underline" href="{{ route('loans.show',$loan->topUpSourceLoan) }}">{{ $loan->topUpSourceLoan?->loan_number }}</a></td>
<td>{{ $loan->customer->full_name }}</td>
<td>{{ number_format((float)$loan->principal,2) }}</td>
<td>{{ $loan->top_up_paid_percentage_at_application }}% / {{ $loan->top_up_eligibility_percentage }}%@if($loan->top_up_override_used)<div><span class="mf-badge mf-badge-warning">Override</span></div>@endif</td>
<td><span class="mf-badge mf-badge-muted">{{ str($loan->status)->replace('_',' ')->title() }}</span></td>
<td class="text-right"><a class="mf-btn" href="{{ route('loans.show',$loan) }}">Open</a>@if($loan->status==='ready_for_disbursement' && auth()->user()->can('loans.top-up.execute')) <a class="mf-btn mf-btn-primary" href="{{ route('loan-top-ups.execute-form',$loan) }}">Execute</a>@endif</td>
</tr>
@endforeach
</tbody></table></div></div>
<div class="mt-4 mb-4">{{ $pending->links() }}</div>
@endif

<div class="mf-card mb-4">
<div class="border-b p-4"><h2 class="font-semibold">Active loans</h2><p class="mt-1 text-xs text-muted-foreground">Eligibility is based on the repaid share of the original scheduled principal, interest and fees; penalties do not inflate eligibility.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Loan</th><th>Customer</th><th>Branch</th><th>Outstanding</th><th>Repaid</th><th>Threshold</th><th>Eligibility</th><th></th></tr></thead><tbody>
@forelse($active as $loan)
@php($eligibility=$loan->top_up_eligibility)
<tr>
<td><div class="font-medium">{{ $loan->loan_number }}</div><div class="text-xs text-muted-foreground">{{ $loan->product->name }}</div></td>
<td><div>{{ $loan->customer->full_name }}</div><div class="text-xs text-muted-foreground">{{ $loan->customer->phone }}</div></td>
<td>{{ $loan->branch->name }}</td>
<td>{{ number_format((float)$loan->total_outstanding,2) }}</td>
<td>{{ $eligibility['paid_percentage'] }}%</td>
<td>{{ $eligibility['threshold'] }}%</td>
<td>@if($eligibility['eligible'])<span class="mf-badge mf-badge-success">Eligible</span>@else<span class="mf-badge mf-badge-warning">Below threshold</span>@endif</td>
<td class="text-right">@if(auth()->user()->can('loans.top-up.create'))<a class="mf-btn mf-btn-primary" href="{{ route('loan-top-ups.create',$loan) }}">Start top-up</a>@else<a class="mf-btn" href="{{ route('loans.show',$loan) }}">View</a>@endif</td>
</tr>
@empty<tr><td colspan="8" class="text-center text-muted-foreground">No active loans match this search.</td></tr>@endforelse
</tbody></table></div></div>
<div class="mt-4 mb-4">{{ $active->links() }}</div>

<div class="mf-card">
<div class="border-b p-4"><h2 class="font-semibold">Completed top-ups</h2><p class="mt-1 text-xs text-muted-foreground">Permanent relationship and financial history for executed top-ups.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Top-up</th><th>Customer</th><th>Previous loan</th><th>New loan</th><th>Settled internally</th><th>Net cash</th><th>Date</th><th></th></tr></thead><tbody>
@forelse($history as $topUp)
<tr>
<td class="font-medium">{{ $topUp->top_up_number }}</td>
<td>{{ $topUp->previousLoan->customer->full_name }}</td>
<td>{{ $topUp->previousLoan->loan_number }}</td>
<td>{{ $topUp->newLoan->loan_number }}</td>
<td>{{ number_format((float)$topUp->amount_settled_internally,2) }}</td>
<td class="font-semibold">{{ number_format((float)$topUp->cash_given_to_customer,2) }}</td>
<td>{{ $topUp->top_up_date->format('d M Y') }}</td>
<td class="text-right"><a class="mf-btn" href="{{ route('loan-top-ups.show',$topUp) }}">Open</a></td>
</tr>
@empty<tr><td colspan="8" class="text-center text-muted-foreground">No completed top-ups yet.</td></tr>@endforelse
</tbody></table></div></div>
<div class="mt-4">{{ $history->links() }}</div>
@endsection