<?php $title = 'Loan Management'; ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-coins mr-2"></i>Loan Management</h1>
    <a href="<?= $baseUrl ?>/loans/create" class="glass-btn glass-btn-primary"><i class="fas fa-plus mr-1"></i>New Loan</a>
</div>
<div class="glass-card mb-4">
    <form method="GET" class="form-inline">
        <div class="input-group mr-2" style="max-width:300px;">
            <input type="text" name="search" class="form-control glass-input" placeholder="Search member..." value="<?= htmlspecialchars($search) ?>">
            <div class="input-group-append"><button class="glass-btn glass-btn-primary" type="submit"><i class="fas fa-search"></i></button></div>
        </div>
        <select name="status" class="form-control glass-input mr-2" onchange="this.form.submit()">
            <option value="">All Statuses</option>
            <option value="pending" <?= $currentStatus === 'pending' ? 'selected' : '' ?>>Pending</option>
            <option value="approved" <?= $currentStatus === 'approved' ? 'selected' : '' ?>>Approved</option>
            <option value="rejected" <?= $currentStatus === 'rejected' ? 'selected' : '' ?>>Rejected</option>
            <option value="disbursed" <?= $currentStatus === 'disbursed' ? 'selected' : '' ?>>Disbursed</option>
            <option value="completed" <?= $currentStatus === 'completed' ? 'selected' : '' ?>>Completed</option>
            <option value="defaulted" <?= $currentStatus === 'defaulted' ? 'selected' : '' ?>>Defaulted</option>
        </select>
        <?php if ($search || $currentStatus): ?>
        <a href="<?= $baseUrl ?>/loans" class="glass-btn btn-sm"><i class="fas fa-times mr-1"></i>Clear</a>
        <?php endif; ?>
    </form>
</div>
<div class="glass-table">
    <table class="table">
        <thead><tr><th>#</th><th>Member</th><th>Type</th><th>Amount</th><th>Payable</th><th>Duration</th><th>Frequency</th><th>Installment</th><th>Status</th><th>Date</th><th>Actions</th></tr></thead>
        <tbody>
            <?php if (!empty($loans)): ?>
                <?php foreach ($loans as $l): ?>
                <tr>
                    <td><?= $l->id ?></td>
                    <td><?= htmlspecialchars($l->first_name ? trim($l->first_name . ' ' . $l->last_name) : $l->user_name) ?></td>
                    <td><?= htmlspecialchars($l->loan_type_name) ?></td>
                    <td>GHS <?= number_format($l->amount, 2) ?></td>
                    <td>GHS <?= number_format($l->total_payable, 2) ?></td>
                    <td><?= $l->duration_months ?> mo</td>
                    <td><span style="color:rgba(255,255,255,0.7);"><?= $l->frequency_label ?? ucfirst($l->repayment_frequency ?? 'monthly') ?></span></td>
                    <td>GHS <?= number_format($l->monthly_payment, 2) ?> <small style="color:rgba(255,255,255,0.4);">×<?= $l->installment_count ?? $l->duration_months ?></small></td>
                    <td>
                        <?php $statusColors = ['pending'=>'warning','approved'=>'info','rejected'=>'secondary','disbursed'=>'primary','completed'=>'success','defaulted'=>'danger']; ?>
                        <span class="badge badge-<?= $statusColors[$l->status] ?? 'secondary' ?>"><?= ucfirst($l->status) ?></span>
                    </td>
                    <td><?= date('M j, Y', strtotime($l->created_at)) ?></td>
                    <td><a href="<?= $baseUrl ?>/loans/<?= $l->id ?>" class="glass-btn btn-sm"><i class="fas fa-eye"></i></a></td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="11" class="text-center" style="color:rgba(255,255,255,0.5);">No loans found</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
<?php if ($pagination && $pagination->lastPage > 1): ?>
<div class="d-flex justify-content-center mt-4">
    <nav><ul class="pagination" style="gap:4px;">
        <?php if ($pagination->page > 1): ?>
        <li class="page-item"><a class="glass-btn btn-sm" href="?<?= http_build_query(array_merge($_GET, ['page' => 1])) ?>"><i class="fas fa-angle-double-left"></i></a></li>
        <li class="page-item"><a class="glass-btn btn-sm" href="?<?= http_build_query(array_merge($_GET, ['page' => $pagination->page - 1])) ?>"><i class="fas fa-angle-left"></i></a></li>
        <?php endif; ?>
        <?php $start = max(1, $pagination->page - 2); $end = min($pagination->lastPage, $pagination->page + 2); for ($i = $start; $i <= $end; $i++): ?>
        <li class="page-item <?= $i == $pagination->page ? 'active' : '' ?>"><a class="glass-btn btn-sm" href="?<?= http_build_query(array_merge($_GET, ['page' => $i])) ?>"><?= $i ?></a></li>
        <?php endfor; ?>
        <?php if ($pagination->page < $pagination->lastPage): ?>
        <li class="page-item"><a class="glass-btn btn-sm" href="?<?= http_build_query(array_merge($_GET, ['page' => $pagination->page + 1])) ?>"><i class="fas fa-angle-right"></i></a></li>
        <li class="page-item"><a class="glass-btn btn-sm" href="?<?= http_build_query(array_merge($_GET, ['page' => $pagination->lastPage])) ?>"><i class="fas fa-angle-double-right"></i></a></li>
        <?php endif; ?>
    </ul></nav>
</div>
<?php endif; ?>
