@extends('layouts.app')
@section('title',$loan->loan_number.' Statement')
@section('content')
@php($business=$loan->branch->business)
<x-ui.page-toolbar :title="$loan->loan_number.' Statement'" :description="'Customer statement for '.$loan->customer->full_name" :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Payments','url'=>route('loan-payments.index')],['label'=>$loan->loan_number]]"><x-slot:actions><button class="mf-btn" onclick="window.print()">Print statement</button><a class="mf-btn" href="{{ route('documents.loan-statement.pdf',$loan) }}">Download PDF</a>@if($loan->status==='active' && auth()->user()->can('payments.receive'))<a class="mf-btn mf-btn-primary" href="{{ route('loan-payments.create',$loan) }}">Receive payment</a>@endif</x-slot:actions></x-ui.page-toolbar>
<div class="mf-card mf-card-body mb-4 statement-brand"><div class="flex justify-between gap-4"><div class="flex gap-3">@if($business->logo_path)<img src="{{ route('document-branding.asset',[$business,'logo']) }}" alt="Company logo" class="h-14 w-20 object-contain">@endif<div><div class="text-lg font-semibold">{{ $business->name }}</div><div class="text-xs text-muted-foreground">{{ collect([$business->address,$business->phone,$business->email])->filter()->join(' · ') }}</div><div class="text-xs text-muted-foreground">{{ $loan->branch->name }} · Customer Loan Statement</div></div></div><div class="text-right text-xs text-muted-foreground">Reg {{ $business->registration_number ?: 'N/A' }}<br>TIN {{ $business->tin ?: 'N/A' }}</div></div></div>
<div class="grid gap-4 lg:grid-cols-4 mb-4"><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Status</div><div class="mt-1 font-medium">{{ str($loan->status)->replace('_',' ')->title() }}</div></div><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Original total</div><div class="mt-1 font-semibold">{{ number_format((float)$loan->total_repayment,2) }}</div></div><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Paid</div><div class="mt-1 font-semibold">{{ number_format((float)$loan->total_paid,2) }}</div></div><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Outstanding</div><div class="mt-1 font-semibold">{{ number_format((float)$loan->total_outstanding,2) }}</div></div></div>
<div class="mf-card mb-4"><div class="border-b p-4"><h2 class="font-semibold">Customer statement</h2><p class="mt-1 text-xs text-muted-foreground">Loan obligation and all posted payment credits.</p></div><div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Date</th><th>Reference</th><th>Description</th><th>Debit</th><th>Credit</th><th>Balance</th></tr></thead><tbody>@forelse($loan->statementEntries as $entry)<tr><td>{{ $entry->entry_date->format('d M Y') }}</td><td>{{ $entry->reference }}</td><td>{{ $entry->description }}</td><td>{{ number_format((float)$entry->debit,2) }}</td><td>{{ number_format((float)$entry->credit,2) }}</td><td class="font-medium">{{ number_format((float)$entry->running_balance,2) }}</td></tr>@empty<tr><td colspan="6" class="text-center text-muted-foreground">No statement entries recorded.</td></tr>@endforelse</tbody></table></div></div>
<div class="mf-card mb-4"><div class="border-b p-4"><h2 class="font-semibold">Repayment schedule</h2><p class="mt-1 text-xs text-muted-foreground">Component allocations are stored against each installment.</p></div><div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>#</th><th>Due date</th><th>Principal paid</th><th>Interest paid</th><th>Fees paid</th><th>Penalty paid</th><th>Remaining</th><th>Status</th></tr></thead><tbody>@foreach($loan->installments as $i)<tr><td>{{ $i->installment_number }}</td><td>{{ $i->due_date->format('d M Y') }}</td><td>{{ number_format((float)$i->principal_paid,2) }}</td><td>{{ number_format((float)$i->interest_paid,2) }}</td><td>{{ number_format((float)$i->fees_paid,2) }}</td><td>{{ number_format((float)$i->penalty_paid,2) }}</td><td>{{ number_format((float)$i->remaining,2) }}</td><td>{{ str($i->status)->title() }}</td></tr>@endforeach</tbody></table></div></div>
<div class="mf-card"><div class="border-b p-4"><h2 class="font-semibold">Payment receipts</h2></div><div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Receipt</th><th>Date</th><th>Method</th><th>Amount</th><th>Received by</th><th></th></tr></thead><tbody>@forelse($loan->payments as $payment)<tr><td>{{ $payment->receipt_number }}</td><td>{{ $payment->payment_date->format('d M Y') }}</td><td>{{ str($payment->method)->replace('_',' ')->title() }}</td><td>{{ number_format((float)$payment->amount,2) }}</td><td>{{ $payment->receiver?->name ?? 'System' }}</td><td class="text-right"><a class="text-primary hover:underline" href="{{ route('loan-payments.receipt',$payment) }}">Receipt</a></td></tr>@empty<tr><td colspan="6" class="text-center text-muted-foreground">No payments recorded yet.</td></tr>@endforelse</tbody></table></div></div>
@if($business->stamp_path)<div class="mt-4 flex justify-end print-stamp"><img src="{{ route('document-branding.asset',[$business,'stamp']) }}" alt="Company stamp" class="max-h-20 max-w-28 object-contain"></div>@endif
@endsection
@push('styles')<style>@media print{.mf-sidebar,.mf-topbar{display:none!important}.mf-content{margin:0!important}.mf-page{padding:0!important}.mf-card{box-shadow:none!important}}</style>@endpush
