@extends('layouts.app')
@section('title','Early Settlement '.$settlement->settlement_number)
@section('content')
<x-ui.page-toolbar :title="$settlement->settlement_number" description="Permanent early-settlement record and posting details." :breadcrumbs="[['label'=>'Loan Adjustments','url'=>route('loan-adjustments.index')],['label'=>$settlement->settlement_number]]" />

<div class="grid gap-4 lg:grid-cols-3">
    <div class="mf-card mf-card-body lg:col-span-2">
        <h2 class="font-semibold">Settlement breakdown</h2>
        <div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
            @foreach([
                'Principal'=>$settlement->principal_amount,'Interest'=>$settlement->interest_amount,'Fees'=>$settlement->fees_amount,
                'Penalties'=>$settlement->penalties_amount,'Settlement adjustment'=>$settlement->settlement_adjustment,'Final amount'=>$settlement->final_amount,
            ] as $label=>$value)
                <div class="rounded-md border p-3"><div class="text-xs text-muted-foreground">{{ $label }}</div><div class="mt-1 font-semibold">{{ number_format((float)$value,2) }}</div></div>
            @endforeach
        </div>
        @if($settlement->adjustment_reason)<div class="mt-4 rounded-md border p-3"><div class="text-xs text-muted-foreground">Adjustment reason</div><div class="mt-1 text-sm">{{ $settlement->adjustment_reason }}</div></div>@endif
        @if($settlement->notes)<div class="mt-3 text-sm"><span class="text-muted-foreground">Notes:</span> {{ $settlement->notes }}</div>@endif
    </div>

    <div class="mf-card mf-card-body">
        <h2 class="font-semibold">Posting</h2>
        <dl class="mt-3 space-y-2 text-sm">
            <div><dt class="text-xs text-muted-foreground">Loan</dt><dd><a class="text-primary hover:underline" href="{{ route('loans.show',$settlement->loan) }}">{{ $settlement->loan->loan_number }}</a></dd></div>
            <div><dt class="text-xs text-muted-foreground">Customer</dt><dd>{{ $settlement->loan->customer->full_name }}</dd></div>
            <div><dt class="text-xs text-muted-foreground">Status</dt><dd><span class="mf-badge mf-badge-success">Fully Paid</span></dd></div>
            <div><dt class="text-xs text-muted-foreground">Date</dt><dd>{{ $settlement->settlement_date->format('d M Y') }}</dd></div>
            <div><dt class="text-xs text-muted-foreground">Account</dt><dd>{{ $settlement->financialAccount->name }}</dd></div>
            <div><dt class="text-xs text-muted-foreground">Reference</dt><dd>{{ $settlement->reference ?: '—' }}</dd></div>
            <div><dt class="text-xs text-muted-foreground">Settled by</dt><dd>{{ $settlement->settler?->name ?? '—' }}</dd></div>
            <div><dt class="text-xs text-muted-foreground">Account transaction</dt><dd>{{ $settlement->accountTransaction?->transaction_number ?? '—' }}</dd></div>
        </dl>
    </div>
</div>

@if($settlement->loan->collaterals->count())
<div class="mf-card mt-4">
    <div class="border-b p-4"><h2 class="font-semibold">Linked collateral</h2><p class="mt-1 text-xs text-muted-foreground">Settlement marks non-terminal linked collateral Ready for Release; physical release remains a separate custody action.</p></div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Code</th><th>Item</th><th>Category</th><th>Status</th><th></th></tr></thead><tbody>
    @foreach($settlement->loan->collaterals as $collateral)<tr><td>{{ $collateral->code }}</td><td>{{ $collateral->item }}</td><td>{{ $collateral->category?->name }}</td><td><span class="mf-badge mf-badge-muted">{{ str($collateral->status)->replace('_',' ')->title() }}</span></td><td class="text-right"><a class="mf-btn" href="{{ route('collateral.show',$collateral) }}">Open</a></td></tr>@endforeach
    </tbody></table></div>
</div>
@endif
@endsection
