@extends('layouts.app')
@section('title','Arrears '.$loan->loan_number)
@section('content')
<x-ui.page-toolbar :title="'Arrears · '.$loan->loan_number" :description="$loan->customer->full_name.' · '.$loan->customer->phone" :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Arrears','url'=>route('arrears.index')],['label'=>$loan->loan_number]]" />

<div class="mb-4 grid gap-2 sm:grid-cols-2 lg:grid-cols-5">
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Days overdue</div><div class="mt-1 text-lg font-semibold">{{ $loan->days_overdue }}</div><div class="text-xs text-muted-foreground">{{ $loan->arrears_bucket }} days</div></div>
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Principal overdue</div><div class="mt-1 text-lg font-semibold">{{ number_format((float)$loan->overdue_principal,2) }}</div></div>
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Interest + fees</div><div class="mt-1 text-lg font-semibold">{{ number_format((float)$loan->overdue_interest + (float)$loan->overdue_fees,2) }}</div></div>
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Penalties</div><div class="mt-1 text-lg font-semibold">{{ number_format((float)$loan->overdue_penalties,2) }}</div></div>
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Total overdue</div><div class="mt-1 text-lg font-semibold">{{ number_format((float)$loan->total_overdue,2) }}</div></div>
</div>

<div class="mb-4 grid gap-4 lg:grid-cols-3">
    <div class="mf-card mf-card-body lg:col-span-2">
        <h2 class="font-semibold">Loan & collection details</h2>
        <div class="mt-3 grid gap-3 sm:grid-cols-2 lg:grid-cols-3 text-sm">
            <div><div class="text-xs text-muted-foreground">Customer</div><div>{{ $loan->customer->full_name }}</div><div class="text-xs text-muted-foreground">{{ $loan->customer->customer_number }} · {{ $loan->customer->phone }}</div></div>
            <div><div class="text-xs text-muted-foreground">Branch</div><div>{{ $loan->branch->name }}</div></div>
            <div><div class="text-xs text-muted-foreground">Loan officer</div><div>{{ $loan->creator?->name ?? '—' }}</div></div>
            <div><div class="text-xs text-muted-foreground">Outstanding principal</div><div>{{ number_format((float)$loan->principal_outstanding,2) }}</div></div>
            <div><div class="text-xs text-muted-foreground">Outstanding interest</div><div>{{ number_format((float)$loan->interest_outstanding,2) }}</div></div>
            <div><div class="text-xs text-muted-foreground">Outstanding fees</div><div>{{ number_format((float)$loan->fees_outstanding,2) }}</div></div>
            <div><div class="text-xs text-muted-foreground">Outstanding penalties</div><div>{{ number_format((float)$loan->penalties_outstanding,2) }}</div></div>
            <div><div class="text-xs text-muted-foreground">Total outstanding</div><div>{{ number_format((float)$loan->total_outstanding,2) }}</div></div>
            <div><div class="text-xs text-muted-foreground">Penalty rule</div><div>{{ str($loan->late_payment_penalty_type)->replace('_',' ')->title() }} · {{ $loan->late_payment_penalty_value }}</div></div>
        </div>
        <div class="mt-4 border-t pt-3"><div class="text-xs text-muted-foreground">Collateral</div><div class="mt-2 flex flex-wrap gap-2">@forelse($loan->collaterals as $item)<a href="{{ route('collateral.show',$item) }}" class="mf-badge mf-badge-muted">{{ $item->code }} · {{ $item->item }}</a>@empty<span class="text-sm text-muted-foreground">No collateral linked.</span>@endforelse</div></div>
    </div>

    @if(auth()->user()->can('arrears.follow-up'))
    <form method="POST" action="{{ route('arrears.follow-ups.store',$loan) }}" class="mf-card mf-card-body">
        @csrf
        <h2 class="font-semibold">Record follow-up</h2>
        <div class="mt-3 space-y-3">
            <div><label class="mf-label">Action</label><select class="mf-input" name="action_type" required><option value="">Select action</option>@foreach(['called'=>'Called','sms'=>'SMS','visit'=>'Visit','promise_to_pay'=>'Promise to Pay','guarantor_contacted'=>'Guarantor Contacted','demand_notice'=>'Demand Notice','other'=>'Other'] as $value=>$label)<option value="{{ $value }}" @selected(old('action_type')===$value)>{{ $label }}</option>@endforeach</select></div>
            <div><label class="mf-label">Promise date</label><input type="date" class="mf-input" name="promise_date" value="{{ old('promise_date') }}"><div class="mt-1 text-xs text-muted-foreground">Required for Promise to Pay.</div></div>
            <div><label class="mf-label">Notes</label><textarea class="mf-input min-h-24" name="notes" maxlength="5000">{{ old('notes') }}</textarea></div>
            <button class="mf-btn mf-btn-primary w-full">Save follow-up</button>
        </div>
    </form>
    @endif
</div>

<div class="mf-card mb-4">
<div class="border-b p-4"><h2 class="font-semibold">Overdue installments</h2></div>
<div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>#</th><th>Due date</th><th>Principal</th><th>Interest</th><th>Fees</th><th>Penalty due</th><th>Paid</th><th>Remaining</th><th>Status</th></tr></thead><tbody>
@foreach($loan->installments->filter(fn($i)=>$i->remaining > 0 && $i->due_date->isBefore(today())) as $installment)
<tr><td>{{ $installment->installment_number }}</td><td>{{ $installment->due_date->format('d M Y') }}</td><td>{{ number_format((float)$installment->principal,2) }}</td><td>{{ number_format((float)$installment->interest,2) }}</td><td>{{ number_format((float)$installment->fees,2) }}</td><td>{{ number_format((float)$installment->penalty_due,2) }}</td><td>{{ number_format((float)$installment->amount_paid,2) }}</td><td class="font-semibold">{{ number_format((float)$installment->remaining,2) }}</td><td><span class="mf-badge mf-badge-destructive">{{ str($installment->status)->title() }}</span></td></tr>
@endforeach
</tbody></table></div></div>

<div class="grid gap-4 lg:grid-cols-2">
    <div class="mf-card">
        <div class="border-b p-4"><h2 class="font-semibold">Penalty history</h2><p class="mt-1 text-xs text-muted-foreground">Permanent assessed penalty records. Penalties do not compound on previous penalties.</p></div>
        <div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Date</th><th>Installment</th><th>Type</th><th>Base</th><th>Amount</th></tr></thead><tbody>
        @forelse($loan->penaltyCharges as $charge)<tr><td>{{ $charge->effective_date->format('d M Y') }}</td><td>#{{ $charge->installment->installment_number }}</td><td>{{ str($charge->penalty_type)->replace('_',' ')->title() }}</td><td>{{ number_format((float)$charge->base_amount,2) }}</td><td class="font-semibold">{{ number_format((float)$charge->amount,2) }}</td></tr>@empty<tr><td colspan="5" class="text-center text-muted-foreground">No penalties assessed.</td></tr>@endforelse
        </tbody></table></div>
    </div>

    <div class="mf-card">
        <div class="border-b p-4"><h2 class="font-semibold">Collection follow-up history</h2><p class="mt-1 text-xs text-muted-foreground">Permanent staff actions and promise-to-pay dates.</p></div>
        <div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Date</th><th>Action</th><th>Employee</th><th>Promise</th><th>Notes</th></tr></thead><tbody>
        @forelse($loan->collectionFollowUps as $followUp)<tr><td>{{ $followUp->action_at->format('d M Y H:i') }}</td><td>{{ str($followUp->action_type)->replace('_',' ')->title() }}</td><td>{{ $followUp->employee?->name ?? 'System' }}</td><td>{{ $followUp->promise_date?->format('d M Y') ?? '—' }}</td><td class="max-w-xs">{{ $followUp->notes ?: '—' }}</td></tr>@empty<tr><td colspan="5" class="text-center text-muted-foreground">No collection follow-ups recorded.</td></tr>@endforelse
        </tbody></table></div>
    </div>
</div>
@endsection
