<?php $title = 'My Loans'; ?>
<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>My Loans</h1>
    <a href="<?= $baseUrl ?>/loans/apply" class="glass-btn glass-btn-primary"><i class="fas fa-plus mr-1"></i>Apply for Loan</a>
</div>
<div class="row mb-4">
    <div class="col-xl-3 col-md-6 mb-3">
        <div class="glass-card text-center py-3">
            <div style="font-size:1.8rem;font-weight:700;color:#fff;"><?= $summary->total_loans ?></div>
            <div style="color:rgba(255,255,255,0.5);font-size:0.85rem;">Total Loans</div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6 mb-3">
        <div class="glass-card text-center py-3">
            <div style="font-size:1.8rem;font-weight:700;color:#4e73df;">GHS <?= number_format($summary->total_approved, 2) ?></div>
            <div style="color:rgba(255,255,255,0.5);font-size:0.85rem;">Approved Amount</div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6 mb-3">
        <div class="glass-card text-center py-3">
            <div style="font-size:1.8rem;font-weight:700;color:#1cc88a;">GHS <?= number_format($summary->total_repaid, 2) ?></div>
            <div style="color:rgba(255,255,255,0.5);font-size:0.85rem;">Repaid</div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6 mb-3">
        <div class="glass-card text-center py-3">
            <div style="font-size:1.8rem;font-weight:700;color:#f6c23e;">GHS <?= number_format($summary->total_payable - $summary->total_repaid, 2) ?></div>
            <div style="color:rgba(255,255,255,0.5);font-size:0.85rem;">Outstanding</div>
        </div>
    </div>
</div>
<?php if (!empty($overdue)): ?>
<div class="glass-card mb-4" style="border-left:4px solid #e74a3b;">
    <h6 class="font-weight-bold" style="color:#e74a3b;"><i class="fas fa-exclamation-triangle mr-2"></i>Overdue Payments</h6>
    <div class="table-responsive">
        <table class="table table-sm">
            <thead><tr><th>Loan</th><th>Installment</th><th>Due Date</th><th>Amount</th><th>Paid</th><th>Status</th></tr></thead>
            <tbody>
                <?php foreach ($overdue as $o): ?>
                <tr>
                    <td><?= htmlspecialchars($o->loan_type_name) ?></td>
                    <td>#<?= $o->installment_number ?></td>
                    <td><?= date('M j, Y', strtotime($o->due_date)) ?></td>
                    <td>GHS <?= number_format($o->amount, 2) ?></td>
                    <td>GHS <?= number_format($o->paid_amount, 2) ?></td>
                    <td><span class="badge badge-danger">Overdue</span></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
<?php if (!empty($upcoming)): ?>
<div class="glass-card mb-4">
    <h6 class="font-weight-bold text-white"><i class="fas fa-calendar-alt mr-2"></i>Upcoming Payments</h6>
    <div class="table-responsive">
        <table class="table table-sm">
            <thead><tr><th>Loan</th><th>Installment</th><th>Due Date</th><th>Amount</th><th>Paid</th><th>Status</th></tr></thead>
            <tbody>
                <?php foreach ($upcoming as $u): ?>
                <tr>
                    <td><?= htmlspecialchars($u->loan_type_name) ?></td>
                    <td>#<?= $u->installment_number ?></td>
                    <td><?= date('M j, Y', strtotime($u->due_date)) ?></td>
                    <td>GHS <?= number_format($u->amount, 2) ?></td>
                    <td>GHS <?= number_format($u->paid_amount, 2) ?></td>
                    <td><span class="badge badge-<?= $u->status === 'partial' ? 'warning' : 'info' ?>"><?= ucfirst($u->status) ?></span></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
<div class="glass-table">
    <table class="table">
        <thead><tr><th>#</th><th>Type</th><th>Amount</th><th>Payable</th><th>Installment</th><th>Duration</th><th>Frequency</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->loan_type_name) ?></td>
                    <td>GHS <?= number_format($l->amount, 2) ?></td>
                    <td>GHS <?= number_format($l->total_payable, 2) ?></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><?= $l->duration_months ?> mo</td>
                    <td><span style="color:rgba(255,255,255,0.7);font-size:0.9rem;"><?= $l->frequency_label ?? ucfirst($l->repayment_frequency ?? 'monthly') ?></span></td>
                    <td>
                        <?php $sc = ['pending'=>'warning','approved'=>'info','rejected'=>'secondary','disbursed'=>'primary','completed'=>'success','defaulted'=>'danger']; ?>
                        <span class="badge badge-<?= $sc[$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="10" class="text-center" style="color:rgba(255,255,255,0.5);">No loans yet. <a href="<?= $baseUrl ?>/loans/apply" class="text-primary">Apply now</a></td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
