@extends('layouts.app')
@section('title','Receive Loan Payment')
@section('content')
<x-ui.form-shell title="Receive Loan Payment" :description="'Post a payment for '.$loan->loan_number.' · '.$loan->customer->full_name" :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Payments','url'=>route('loan-payments.index')],['label'=>$loan->loan_number]]" narrow>
<form method="POST" action="{{ route('loan-payments.store',$loan) }}" class="mf-form">@csrf
<x-ui.form-section title="Loan & outstanding balance" description="The payment is allocated automatically to the oldest unpaid installments.">
<div class="mf-form-grid">
<div class="mf-col-4"><label class="mf-field-label">Loan</label><input class="mf-input" value="{{ $loan->loan_number }}" readonly></div>
<div class="mf-col-4"><label class="mf-field-label">Customer</label><input class="mf-input" value="{{ $loan->customer->full_name }}" readonly></div>
<div class="mf-col-4"><label class="mf-field-label">Outstanding</label><input class="mf-input" value="{{ number_format((float)$loan->total_outstanding,2) }}" readonly></div>
<div class="mf-col-3"><label class="mf-field-label">Principal outstanding</label><input class="mf-input" value="{{ number_format((float)$loan->principal_outstanding,2) }}" readonly></div>
<div class="mf-col-3"><label class="mf-field-label">Interest outstanding</label><input class="mf-input" value="{{ number_format((float)$loan->interest_outstanding,2) }}" readonly></div>
<div class="mf-col-3"><label class="mf-field-label">Fees outstanding</label><input class="mf-input" value="{{ number_format((float)$loan->fees_outstanding,2) }}" readonly></div>
<div class="mf-col-3"><label class="mf-field-label">Penalties outstanding</label><input class="mf-input" value="{{ number_format((float)$loan->penalties_outstanding,2) }}" readonly></div>
</div>
</x-ui.form-section>

@if($loan->branch->business->payment_deadline_penalties_enabled && $loan->payment_deadline_enabled)
<x-ui.form-section title="Payment deadline" :description="'Each scheduled installment must be paid by '.substr((string)$loan->payment_deadline_time,0,5).'. Late and missed-day penalties are charged once per installment unless an excuse is recorded.'">
<div class="mb-3 grid gap-3 md:grid-cols-2 text-sm">
<div class="rounded-md border p-3"><div class="text-xs text-muted-foreground">Late on due date</div><div class="font-medium">{{ ucfirst($loan->deadline_late_penalty_type) }} · {{ $loan->deadline_late_penalty_value }}{{ $loan->deadline_late_penalty_type==='percentage'?'%':'' }}</div><div class="text-xs text-muted-foreground">{{ $loan->deadline_late_penalty_settlement==='next_payment'?'Collect with next payment':'Add to loan balance' }}</div></div>
<div class="rounded-md border p-3"><div class="text-xs text-muted-foreground">Missed schedule day</div><div class="font-medium">{{ ucfirst($loan->missed_schedule_penalty_type) }} · {{ $loan->missed_schedule_penalty_value }}{{ $loan->missed_schedule_penalty_type==='percentage'?'%':'' }}</div><div class="text-xs text-muted-foreground">{{ $loan->missed_schedule_penalty_settlement==='next_payment'?'Collect with next payment':'Add to loan balance' }}</div></div>
</div>
<div class="mf-table-frame"><table class="mf-table"><thead><tr><th>#</th><th>Due</th><th>Deadline</th><th>Remaining</th><th>Excuse</th></tr></thead><tbody>
@foreach($loan->installments as $installment)
<tr>
<td>{{ $installment->installment_number }}</td>
<td>{{ $installment->due_date->format('d M Y') }}</td>
<td>{{ substr((string)$loan->payment_deadline_time,0,5) }}</td>
<td>{{ number_format((float)$installment->remaining,2) }}</td>
<td>
@if($installment->excuse)
<div class="text-xs"><strong>Excused</strong> · {{ $installment->excuse->reason }}<div class="text-muted-foreground">{{ $installment->excuse->user?->name ?? 'Staff' }} · {{ $installment->excuse->excused_at->format('d M H:i') }}</div></div>
@if(auth()->user()->can('loans.update'))<form method="POST" action="{{ route('loans.installments.excuse.destroy',[$loan,$installment]) }}" class="mt-1">@csrf @method('DELETE')<button type="submit" class="text-xs text-destructive hover:underline">Remove excuse</button></form>@endif
@elseif(auth()->user()->can('loans.update') && $installment->status!=='paid')
<form method="POST" action="{{ route('loans.installments.excuse.store',[$loan,$installment]) }}" class="flex gap-2">@csrf<input class="mf-input" name="reason" placeholder="Reason for excuse" required><button type="submit" class="mf-btn">Excuse</button></form>
@else<span class="text-xs text-muted-foreground">None</span>@endif
</td>
</tr>
@endforeach
</tbody></table></div>
</x-ui.form-section>
@endif

<x-ui.form-section title="Payment details" description="Posting is atomic: account, schedule, statement, ledger and receipt update together.">
<div class="mf-form-grid">
<div class="mf-col-4"><label class="mf-field-label">Amount *</label><input class="mf-input" name="amount" inputmode="decimal" value="{{ old('amount') }}" required>@error('amount')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-4"><label class="mf-field-label">Payment date & time *</label><input type="datetime-local" class="mf-input" name="date" value="{{ old('date',now()->format('Y-m-d\TH:i')) }}" required>@error('date')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-4"><label class="mf-field-label">Method *</label><select class="mf-input" name="method" required>@foreach(['cash'=>'Cash','mobile_money'=>'Mobile Money','bank'=>'Bank','transfer'=>'Transfer','other'=>'Other'] as $value=>$label)<option value="{{ $value }}" @selected(old('method')===$value)>{{ $label }}</option>@endforeach</select></div>
<div class="mf-col-6"><label class="mf-field-label">Receiving 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((int)old('financial_account_id')===$account->id)>{{ $account->name }} · {{ str($account->account_type)->replace('_',' ')->title() }} · {{ 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-6"><label class="mf-field-label">Reference</label><input class="mf-input" name="reference" value="{{ old('reference') }}" placeholder="Optional external reference"></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>
</div>
</x-ui.form-section>
<x-ui.form-actions><a class="mf-btn" href="{{ route('loan-payments.index') }}">Cancel</a><button class="mf-btn mf-btn-primary" @disabled($accounts->isEmpty())>Post payment</button></x-ui.form-actions>
@if($accounts->isEmpty())<p class="text-sm text-destructive">No active receiving account exists for this branch. Create or activate a financial account before collecting a payment.</p>@endif
</form>
</x-ui.form-shell>
@endsection
