@extends('layouts.app')
@section('title','Disburse '.$loan->loan_number)
@section('content')
@php
    $netDisbursement=$loan->processing_fee_payment_mode==='deducted' ? number_format(max(0,(float)$loan->principal-(float)$loan->processing_fee),2,'.','') : number_format((float)$loan->principal,2,'.','');
    $cashFeeRequired=$loan->processing_fee_payment_mode==='cash' && (float)$loan->processing_fee>0;
@endphp
<x-ui.form-shell :title="'Disburse '.$loan->loan_number" description="Release the approved customer amount and apply the product processing-fee policy." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Disbursement','url'=>route('loan-disbursements.index')],['label'=>$loan->loan_number]]" narrow>
<form method="POST" action="{{ route('loan-disbursements.store',$loan) }}" class="mf-form">@csrf
<x-ui.form-section title="Approved loan" description="These approved financial terms are locked for this disbursement.">
<x-ui.form-grid>
<div class="mf-col-6"><label class="mf-field-label">Customer</label><div class="mf-input flex items-center bg-muted/30">{{ $loan->customer->full_name }} · {{ $loan->customer->customer_number }}</div></div>
<div class="mf-col-6"><label class="mf-field-label">Branch</label><div class="mf-input flex items-center bg-muted/30">{{ $loan->branch->name }}</div></div>
<div class="mf-col-3"><label class="mf-field-label">Loan product</label><div class="mf-input flex items-center bg-muted/30">{{ $loan->product->name }}</div></div>
<div class="mf-col-3"><label class="mf-field-label">Approved principal</label><div class="mf-input flex items-center bg-muted/30">{{ number_format((float)$loan->principal,2) }}</div></div>
<div class="mf-col-3"><label class="mf-field-label">Processing fee</label><div class="mf-input flex items-center bg-muted/30">{{ number_format((float)$loan->processing_fee,2) }}</div><p class="mf-field-help">{{ $loan->processingFeePaymentModeLabel() }}</p></div>
<div class="mf-col-3"><label class="mf-field-label">Customer receives</label><div class="mf-input flex items-center bg-muted/30 font-semibold">{{ number_format((float)$netDisbursement,2) }}</div></div>
</x-ui.form-grid>
</x-ui.form-section>

@if($loan->branch->business->require_signed_contract_before_disbursement && ! $loan->hasSignedContract())
<div class="rounded-md border p-3 text-sm"><strong>Signed contract required.</strong><div class="mt-1 text-muted-foreground">This business requires a signed contract before disbursement. Upload the signed contract on the loan page first.</div><a class="mt-2 inline-block text-primary hover:underline" href="{{ route('loans.show',$loan) }}">Open loan documents</a></div>
@endif

<x-ui.form-section title="Disbursement details" description="The amount is calculated automatically from the approved principal and processing-fee policy.">
<x-ui.form-grid>
<div class="mf-col-6"><label class="mf-field-label">Disbursement account *</label><select name="financial_account_id" class="mf-input" 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() }} · Balance {{ 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">Amount to customer *</label><input name="amount" class="mf-input bg-muted/30" inputmode="decimal" value="{{ old('amount',$netDisbursement) }}" readonly required>@error('amount')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-3"><label class="mf-field-label">Date *</label><input type="date" name="date" class="mf-input" value="{{ old('date',now()->toDateString()) }}" required>@error('date')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
@if($cashFeeRequired)
<div class="mf-col-6"><label class="mf-field-label">Processing fee cash account *</label><select name="processing_fee_account_id" class="mf-input" required><option value="">Select cash account</option>@foreach($cashAccounts as $account)<option value="{{ $account->id }}" @selected((int)old('processing_fee_account_id')===$account->id)>{{ $account->name }} · Balance {{ number_format((float)$account->current_balance,2) }}</option>@endforeach</select><p class="mf-field-help">Confirm the cash processing fee of {{ number_format((float)$loan->processing_fee,2) }} was received into this account.</p>@error('processing_fee_account_id')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
@endif
<div class="mf-col-6"><label class="mf-field-label">Reference</label><input name="reference" class="mf-input" value="{{ old('reference') }}" placeholder="Bank/M-Pesa/cash reference if available">@error('reference')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-6"><label class="mf-field-label">Notes</label><textarea name="notes" class="mf-input" rows="3" placeholder="Optional disbursement notes">{{ old('notes') }}</textarea>@error('notes')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
</x-ui.form-grid>
</x-ui.form-section>

@error('loan')<div class="rounded-md border p-3 text-sm text-destructive">{{ $message }}</div>@enderror
<x-ui.form-actions><a class="mf-btn" href="{{ route('loan-disbursements.index') }}">Cancel</a><button class="mf-btn mf-btn-primary" @disabled($accounts->isEmpty() || ($cashFeeRequired && $cashAccounts->isEmpty()) || ($loan->branch->business->require_signed_contract_before_disbursement && ! $loan->hasSignedContract()))>Post Disbursement</button></x-ui.form-actions>
</form>
</x-ui.form-shell>
@endsection
