@extends('layouts.app')
@section('title','Execute Loan Top-Up')
@section('content')
<x-ui.form-shell
    title="Execute Loan Top-Up"
    description="Finalize the approved replacement loan. The system will settle the old balance internally and debit only the recalculated net cash from the selected branch account."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Top-Ups','url'=>route('loan-top-ups.index')],['label'=>$loan->loan_number],['label'=>'Execute']]"
    :narrow="true"
>
<form method="POST" action="{{ route('loan-top-ups.execute',$loan) }}" class="mf-form">
@csrf
<x-ui.form-section title="Top-up relationship" description="Both loans belong to the same customer and branch. Final old-loan penalties and outstanding are recalculated on the execution date.">
    <x-ui.form-grid>
        <div class="mf-col-6"><label class="mf-field-label">Previous loan</label><div class="mf-input bg-muted/30">{{ $loan->topUpSourceLoan->loan_number }} · Outstanding {{ number_format((float)$loan->topUpSourceLoan->total_outstanding,2) }}</div></div>
        <div class="mf-col-6"><label class="mf-field-label">Replacement loan</label><div class="mf-input bg-muted/30">{{ $loan->loan_number }} · Principal {{ number_format((float)$loan->principal,2) }}</div></div>
        <div class="mf-col-6"><label class="mf-field-label">Customer</label><div class="mf-input bg-muted/30">{{ $loan->customer->full_name }}</div></div>
        <div class="mf-col-6"><label class="mf-field-label">New total repayment</label><div class="mf-input bg-muted/30">{{ number_format((float)$loan->total_repayment,2) }}</div></div>
    </x-ui.form-grid>
</x-ui.form-section>

<x-ui.form-section title="Funding" description="Only net cash after internal settlement leaves this account. The transaction is rejected if funds are insufficient.">
    <x-ui.form-grid>
        <div class="mf-col-6"><label class="mf-field-label">Financial account</label><select class="mf-input" name="financial_account_id" required><option value="">Select account</option>@foreach($accounts as $account)<option value="{{ $account->id }}" @selected((string)old('financial_account_id')===(string)$account->id)>{{ $account->name }} · {{ number_format((float)$account->current_balance,2) }}</option>@endforeach</select>@error('financial_account_id')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
        <div class="mf-col-3"><label class="mf-field-label">Execution date</label><input type="date" class="mf-input" name="date" value="{{ old('date',now()->format('Y-m-d')) }}" required>@error('date')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
        <div class="mf-col-3"><label class="mf-field-label">Reference</label><input class="mf-input" name="reference" value="{{ old('reference') }}" placeholder="Optional"></div>
        <div class="mf-col-12"><label class="mf-field-label">Notes</label><textarea class="mf-input" name="notes" rows="3">{{ old('notes') }}</textarea></div>
    </x-ui.form-grid>
</x-ui.form-section>

<div class="mf-card mf-card-body text-sm"><strong>Final verification:</strong> submitting this form re-locks both loans, assesses penalties due on the selected date, verifies the approved schedule and signed-contract rule, recalculates the old settlement and net customer cash, then posts all loan, statement, account and accounting entries in one database transaction.</div>

<x-ui.form-actions><a class="mf-btn" href="{{ route('loan-top-ups.index') }}">Cancel</a><button class="mf-btn mf-btn-primary" type="submit">Execute top-up</button></x-ui.form-actions>
</form>
</x-ui.form-shell>
@endsection