@extends('layouts.app')
@section('title','Loan Top-Up '.$topUp->top_up_number)
@section('content')
<x-ui.page-toolbar :title="$topUp->top_up_number" description="Permanent loan top-up relationship and financial posting details." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Top-Ups','url'=>route('loan-top-ups.index')],['label'=>$topUp->top_up_number]]" />

<div class="grid gap-4 lg:grid-cols-2">
<div class="mf-card">
    <div class="border-b p-4"><h2 class="font-semibold">Loan relationship</h2></div>
    <div class="p-4 grid gap-3 sm:grid-cols-2 text-sm">
        <div><div class="text-xs text-muted-foreground">Customer</div><div class="font-medium">{{ $topUp->previousLoan->customer->full_name }}</div></div>
        <div><div class="text-xs text-muted-foreground">Branch</div><div class="font-medium">{{ $topUp->branch->name }}</div></div>
        <div><div class="text-xs text-muted-foreground">Previous loan</div><a class="font-medium text-primary hover:underline" href="{{ route('loans.show',$topUp->previousLoan) }}">{{ $topUp->previousLoan->loan_number }}</a><div class="text-xs text-muted-foreground">{{ str($topUp->previousLoan->status)->replace('_',' ')->title() }}</div></div>
        <div><div class="text-xs text-muted-foreground">Replacement loan</div><a class="font-medium text-primary hover:underline" href="{{ route('loans.show',$topUp->newLoan) }}">{{ $topUp->newLoan->loan_number }}</a><div class="text-xs text-muted-foreground">{{ str($topUp->newLoan->status)->replace('_',' ')->title() }}</div></div>
        <div><div class="text-xs text-muted-foreground">Executed by</div><div>{{ $topUp->executor?->name ?? '—' }}</div></div>
        <div><div class="text-xs text-muted-foreground">Date</div><div>{{ $topUp->top_up_date->format('d M Y') }}</div></div>
    </div>
</div>

<div class="mf-card">
    <div class="border-b p-4"><h2 class="font-semibold">Financial summary</h2></div>
    <div class="p-4 grid gap-3 sm:grid-cols-2 text-sm">
        <div><div class="text-xs text-muted-foreground">Previous outstanding</div><div class="font-semibold">{{ number_format((float)$topUp->previous_outstanding,2) }}</div></div>
        <div><div class="text-xs text-muted-foreground">Settled internally</div><div class="font-semibold">{{ number_format((float)$topUp->amount_settled_internally,2) }}</div></div>
        <div><div class="text-xs text-muted-foreground">New principal</div><div>{{ number_format((float)$topUp->new_principal,2) }}</div></div>
        <div><div class="text-xs text-muted-foreground">New interest</div><div>{{ number_format((float)$topUp->new_interest,2) }}</div></div>
        <div><div class="text-xs text-muted-foreground">New fees</div><div>{{ number_format((float)$topUp->new_fees,2) }}</div></div>
        <div><div class="text-xs text-muted-foreground">New total repayment</div><div>{{ number_format((float)$topUp->new_total_repayment,2) }}</div></div>
        <div class="sm:col-span-2"><div class="text-xs text-muted-foreground">Cash actually given to customer</div><div class="text-lg font-semibold">{{ number_format((float)$topUp->cash_given_to_customer,2) }}</div></div>
    </div>
</div>
</div>

<div class="mf-card mt-4">
<div class="border-b p-4"><h2 class="font-semibold">Internal settlement components</h2><p class="mt-1 text-xs text-muted-foreground">These old-loan balances were settled from the new principal. They did not enter the branch cash account as a customer payment.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Principal</th><th>Interest</th><th>Fees</th><th>Penalties</th><th>Total</th></tr></thead><tbody><tr><td>{{ number_format((float)$topUp->previous_principal,2) }}</td><td>{{ number_format((float)$topUp->previous_interest,2) }}</td><td>{{ number_format((float)$topUp->previous_fees,2) }}</td><td>{{ number_format((float)$topUp->previous_penalties,2) }}</td><td class="font-semibold">{{ number_format((float)$topUp->previous_outstanding,2) }}</td></tr></tbody></table></div>
</div>

<div class="mf-card mt-4">
<div class="border-b p-4"><h2 class="font-semibold">Posting details</h2></div>
<div class="p-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-4 text-sm">
    <div><div class="text-xs text-muted-foreground">Funding account</div><div>{{ $topUp->financialAccount->name }}</div></div>
    <div><div class="text-xs text-muted-foreground">Reference</div><div>{{ $topUp->reference ?: '—' }}</div></div>
    <div><div class="text-xs text-muted-foreground">Account transaction</div><div>{{ $topUp->accountTransaction?->transaction_number ?? '—' }}</div></div>
    <div><div class="text-xs text-muted-foreground">Top-up number</div><div>{{ $topUp->top_up_number }}</div></div>
    @if($topUp->notes)<div class="sm:col-span-2 lg:col-span-4"><div class="text-xs text-muted-foreground">Notes</div><div>{{ $topUp->notes }}</div></div>@endif
</div>
</div>
@endsection