<div class="table-responsive finance-table-shell">
    <table class="table align-middle mb-0 finance-table repayment-table">
        <thead>
            <tr>
                <th>Loan</th>
                <th>Member</th>
                <th>Next collection</th>
                <th>Balance</th>
                <th>Collect</th>
            </tr>
        </thead>
        <tbody>
        @forelse($repaymentLoans as $loan)
            @php
                $nextSchedule = $loan->schedules->first();
                $nextAmount = $nextSchedule
                    ? max(0, (float) $nextSchedule->total_due - (float) $nextSchedule->paid_amount)
                    : (float) $loan->balance;
                $dueDate = $nextSchedule?->due_date ? \Illuminate\Support\Carbon::parse($nextSchedule->due_date)->startOfDay() : null;
                $daysDiff = $dueDate ? today()->startOfDay()->diffInDays($dueDate, false) : null;
                $daysState = $dueDate
                    ? ($daysDiff < 0 ? abs((int) $daysDiff).' day(s) late' : ((int) $daysDiff).' day(s) left')
                    : null;
                $dueTone = $dueDate && $daysDiff < 0 ? 'danger' : 'success';
            @endphp
            <tr>
                <td>
                    <div class="repayment-loan-cell">
                        <span class="finance-id-pill">{{ $loan->loan_number }}</span>
                        <a class="small" href="{{ route('tenant.loans.show', $loan) }}">Open loan</a>
                    </div>
                </td>
                <td>
                    <div class="fw-semibold">{{ $loan->customer?->fullName() }}</div>
                    <small class="text-muted d-block">{{ $loan->customer?->account_number }}</small>
                    @if($loan->customer?->phone)
                        <small class="text-muted d-block">{{ $loan->customer->phone }}</small>
                    @endif
                </td>
                <td>
                    @if($nextSchedule)
                        <div class="repayment-due-box">
                            <strong>{{ app_date($dueDate) }}</strong>
                            <span>{{ $money($nextAmount) }} due</span>
                            <small class="text-{{ $dueTone }}">{{ $daysState }}</small>
                        </div>
                    @else
                        <span class="text-muted">No open schedule</span>
                    @endif
                </td>
                <td>
                    <div class="repayment-balance">{{ $compactMoney($loan->balance) }}</div>
                    <small class="text-muted">{{ $money($loan->balance) }}</small>
                </td>
                @if(auth()->user()->hasPermission('repayments.record'))
                    <td>
                        <form method="POST" action="{{ route('tenant.loans.payments.store', $loan) }}" class="repayment-collect-form">
                            @csrf
                            <div class="amount-field">
                                <input name="amount" value="{{ number_format($nextAmount, 2, '.', '') }}" type="number" step="0.01" min="0.01" max="{{ $loan->balance }}" class="form-control form-control-sm" placeholder="Amount" required>
                            </div>
                            <div>
                                <select name="method" class="form-select form-select-sm">
                                    <option value="cash">Cash</option>
                                    <option value="transfer">Transfer</option>
                                    <option value="pos">POS</option>
                                </select>
                            </div>
                            <button class="btn btn-sm btn-primary">Submit</button>
                            <input name="notes" class="form-control form-control-sm note-field" placeholder="Optional note">
                        </form>
                    </td>
                @else
                    <td class="text-muted">View only</td>
                @endif
            </tr>
        @empty
            <tr><td colspan="5" class="text-center text-muted py-5">{{ $emptyMessage ?? 'No loans found.' }}</td></tr>
        @endforelse
        </tbody>
    </table>
</div>

@if(($showPagination ?? false) && method_exists($repaymentLoans, 'links'))
    <div class="card-body">{{ $repaymentLoans->links() }}</div>
@endif
