@extends('layouts.app')
@section('title','Loan Write-Offs')
@section('content')
<x-ui.page-toolbar title="Loan Write-Offs" description="Controlled management write-offs with permanent loan history and dedicated recovery tracking." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Write-Offs']]" />

<form method="GET" class="mf-card mf-card-body mb-4">
    <div class="flex items-center gap-2">
        <input class="mf-input min-w-0 flex-1" name="search" value="{{ request('search') }}" placeholder="Loan, write-off, recovery, customer, phone or reference">
        <button class="mf-btn mf-btn-primary shrink-0" type="submit">Search</button>
    </div>
</form>

<div class="mf-card mb-4">
    <div class="border-b p-4">
        <h2 class="font-semibold">Eligible active loans</h2>
        <p class="mt-1 text-xs text-muted-foreground">A write-off request captures the full current outstanding balance. The loan remains active until management approval.</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>Principal outstanding</th><th>Other outstanding</th><th>Total write-off balance</th><th></th></tr></thead>
        <tbody>
        @forelse($eligible as $loan)
            <tr>
                <td><a class="font-medium text-primary hover:underline" href="{{ route('loans.show',$loan) }}">{{ $loan->loan_number }}</a><div class="text-xs text-muted-foreground">{{ $loan->product->name }}</div></td>
                <td>{{ $loan->customer->full_name }}<div class="text-xs text-muted-foreground">{{ $loan->customer->customer_number }} · {{ $loan->customer->phone }}</div></td>
                <td>{{ $loan->branch->name }}</td>
                <td>{{ number_format((float)$loan->principal_outstanding,2) }}</td>
                <td>{{ number_format((float)($loan->interest_outstanding+$loan->fees_outstanding+$loan->penalties_outstanding),2) }}</td>
                <td class="font-semibold">{{ number_format((float)$loan->total_outstanding,2) }}</td>
                <td class="text-right">@if(auth()->user()->can('loans.writeoff.request'))<a class="mf-btn mf-btn-primary" href="{{ route('loan-write-offs.create',$loan) }}">Request write-off</a>@endif</td>
            </tr>
        @empty<tr><td colspan="7" class="text-center text-muted-foreground">No active loans are currently eligible for write-off.</td></tr>@endforelse
        </tbody>
    </table></div>
</div>
<div class="mb-4">{{ $eligible->links() }}</div>

<div class="mf-card mb-4">
    <div class="border-b p-4">
        <h2 class="font-semibold">Write-off requests & history</h2>
        <p class="mt-1 text-xs text-muted-foreground">Rejected, pending and approved records remain permanently visible.</p>
    </div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table>
        <thead><tr><th>Write-off</th><th>Loan</th><th>Customer</th><th>Amount</th><th>Recovered</th><th>Recoverable</th><th>Status</th><th>Requested</th><th></th></tr></thead>
        <tbody>
        @forelse($writeOffs as $item)
            <tr>
                <td class="font-medium">{{ $item->write_off_number }}</td>
                <td>{{ $item->loan->loan_number }}</td>
                <td>{{ $item->loan->customer->full_name }}</td>
                <td>{{ number_format((float)$item->write_off_amount,2) }}</td>
                <td>{{ number_format((float)$item->recovered_amount,2) }}</td>
                <td class="font-semibold">{{ number_format((float)$item->recoverable_balance,2) }}</td>
                <td><span class="mf-badge {{ $item->status==='approved'?'mf-badge-success':($item->status==='rejected'?'mf-badge-danger':'mf-badge-warning') }}">{{ ucfirst($item->status) }}</span></td>
                <td>{{ $item->requested_at?->format('d M Y H:i') }}<div class="text-xs text-muted-foreground">{{ $item->requester?->name }}</div></td>
                <td class="text-right"><a class="mf-btn" href="{{ route('loan-write-offs.show',$item) }}">Open</a></td>
            </tr>
        @empty<tr><td colspan="9" class="text-center text-muted-foreground">No write-off requests have been recorded.</td></tr>@endforelse
        </tbody>
    </table></div>
</div>
<div class="mb-4">{{ $writeOffs->links() }}</div>

<div class="mf-card">
    <div class="border-b p-4">
        <h2 class="font-semibold">Written-off loan recoveries</h2>
        <p class="mt-1 text-xs text-muted-foreground">Cash collected after write-off is recorded separately from normal loan payments.</p>
    </div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table>
        <thead><tr><th>Recovery</th><th>Write-off</th><th>Loan</th><th>Customer</th><th>Amount</th><th>Account</th><th>Date</th><th>Received by</th></tr></thead>
        <tbody>
        @forelse($recoveries as $item)
            <tr>
                <td class="font-medium">{{ $item->recovery_number }}</td>
                <td><a class="text-primary hover:underline" href="{{ route('loan-write-offs.show',$item->writeOff) }}">{{ $item->writeOff->write_off_number }}</a></td>
                <td>{{ $item->loan->loan_number }}</td>
                <td>{{ $item->loan->customer->full_name }}</td>
                <td class="font-semibold">{{ number_format((float)$item->amount,2) }}</td>
                <td>{{ $item->financialAccount->name }}</td>
                <td>{{ $item->recovery_date->format('d M Y H:i') }}</td>
                <td>{{ $item->receiver?->name ?? '—' }}</td>
            </tr>
        @empty<tr><td colspan="8" class="text-center text-muted-foreground">No written-off loan recoveries have been posted.</td></tr>@endforelse
        </tbody>
    </table></div>
</div>
<div class="mt-4">{{ $recoveries->links() }}</div>
@endsection
