@extends('layouts.app')
@section('title','Request Loan Write-Off')
@section('content')
<x-ui.page-toolbar :title="'Request Write-Off · '.$loan->loan_number" :description="'Management write-off request for '.$loan->customer->full_name" :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Write-Offs','url'=>route('loan-write-offs.index')],['label'=>'Request']]" />

<div class="grid gap-4 lg:grid-cols-3">
    <div class="lg:col-span-2">
        <form method="POST" enctype="multipart/form-data" action="{{ route('loan-write-offs.store',$loan) }}" class="mf-card" data-ui="form-shell">
            @csrf
            <div class="border-b p-4">
                <h2 class="font-semibold">Write-off request</h2>
                <p class="mt-1 text-xs text-muted-foreground">Supporting evidence is mandatory. Submission does not change the loan until an authorized management decision is recorded.</p>
            </div>
            <div class="p-4 space-y-4">
                <div class="rounded-md border p-3 text-sm">
                    <strong>Accounting control:</strong>
                    <span class="text-muted-foreground">Step 15 writes off the full current loan balance. Principal is removed from the accounting receivable only after approval; interest, fees and penalties remain preserved in the write-off snapshot for recovery tracking.</span>
                </div>
                <div class="grid gap-3 md:grid-cols-2">
                    <div><label class="mf-field-label">Loan</label><input class="mf-input" value="{{ $loan->loan_number }}" disabled></div>
                    <div><label class="mf-field-label">Customer</label><input class="mf-input" value="{{ $loan->customer->full_name }}" disabled></div>
                    <div><label class="mf-field-label">Write-off amount</label><input class="mf-input" name="write_off_amount" value="{{ old('write_off_amount',$loan->total_outstanding) }}" readonly required><p class="mt-1 text-xs text-muted-foreground">Must equal the full current outstanding balance.</p></div>
                    <div><label class="mf-field-label">Branch</label><input class="mf-input" value="{{ $loan->branch->name }}" disabled></div>
                </div>
                <div>
                    <label class="mf-field-label">Reason</label>
                    <textarea class="mf-input" name="reason" rows="5" required placeholder="Explain why this loan should be written off, collection efforts made, and the management basis for the request.">{{ old('reason') }}</textarea>
                    @error('reason')<p class="mt-1 text-xs text-destructive">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="mf-field-label">Supporting documents</label>
                    <input type="file" class="mf-input" name="documents[]" multiple required accept=".jpg,.jpeg,.png,.webp,.pdf,.doc,.docx">
                    <p class="mt-1 text-xs text-muted-foreground">Required. Up to 10 files, maximum 8 MB each. Evidence is stored privately.</p>
                    @error('documents')<p class="mt-1 text-xs text-destructive">{{ $message }}</p>@enderror
                    @error('documents.*')<p class="mt-1 text-xs text-destructive">{{ $message }}</p>@enderror
                </div>
                <div class="flex justify-end gap-2">
                    <a class="mf-btn" href="{{ route('loan-write-offs.index') }}">Cancel</a>
                    <button class="mf-btn mf-btn-primary">Submit for management approval</button>
                </div>
            </div>
        </form>
    </div>

    <div class="space-y-4">
        <div class="mf-card">
            <div class="border-b p-4"><h2 class="font-semibold">Balance snapshot</h2></div>
            <div class="p-4 space-y-3 text-sm">
                @foreach([
                    'Original principal'=>$loan->principal,
                    'Original total repayment'=>$loan->total_repayment,
                    'Principal outstanding'=>$loan->principal_outstanding,
                    'Interest outstanding'=>$loan->interest_outstanding,
                    'Fees outstanding'=>$loan->fees_outstanding,
                    'Penalties outstanding'=>$loan->penalties_outstanding,
                    'Total outstanding'=>$loan->total_outstanding,
                    'Paid to date'=>$loan->total_paid,
                ] as $label=>$value)
                    <div class="flex justify-between gap-3"><span class="text-muted-foreground">{{ $label }}</span><span class="font-medium">{{ number_format((float)$value,2) }}</span></div>
                @endforeach
            </div>
        </div>
        <div class="mf-card">
            <div class="border-b p-4"><h2 class="font-semibold">History preserved</h2></div>
            <div class="p-4 space-y-2 text-sm">
                <div class="flex justify-between"><span class="text-muted-foreground">Payments</span><span>{{ $loan->payments->count() }}</span></div>
                <div class="flex justify-between"><span class="text-muted-foreground">Collateral items</span><span>{{ $loan->collaterals->count() }}</span></div>
                <div class="flex justify-between"><span class="text-muted-foreground">Collection follow-ups</span><span>{{ $loan->collectionFollowUps->count() }}</span></div>
                <p class="pt-2 text-xs text-muted-foreground">Write-off never deletes or rewrites these records. Collateral remains under its existing custody status.</p>
            </div>
        </div>
    </div>
</div>
@endsection
