<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-file-invoice mr-2"></i>Loan #<?= $loan->id ?> Details</h1>
    <div>
        <a href="<?= $baseUrl ?>/loans/<?= $isAdmin ? '' : 'my' ?>" class="glass-btn btn-sm"><i class="fas fa-arrow-left mr-1"></i>Back</a>
        <?php if ($isAdmin && $loan->status === 'pending'): ?>
            <button class="glass-btn glass-btn-success btn-sm" data-toggle="modal" data-target="#approveModal"><i class="fas fa-check mr-1"></i>Approve</button>
            <button class="glass-btn btn-sm" style="border-color:rgba(231,74,59,0.3);color:#e74a3b;" data-toggle="modal" data-target="#rejectModal"><i class="fas fa-times mr-1"></i>Reject</button>
        <?php endif; ?>
        <?php if ($isAdmin && $loan->status === 'approved'): ?>
            <form method="POST" action="<?= $baseUrl ?>/loans/<?= $loan->id ?>/disburse" style="display:inline;" onsubmit="return confirm('Mark this loan as disbursed?');">
                <button type="submit" class="glass-btn glass-btn-primary btn-sm"><i class="fas fa-hand-holding-usd mr-1"></i>Mark Disbursed</button>
            </form>
        <?php endif; ?>
        <?php if ($isAdmin && $loan->status === 'disbursed' && $totals->total_paid >= $totals->total_due): ?>
            <form method="POST" action="<?= $baseUrl ?>/loans/<?= $loan->id ?>/complete" style="display:inline;" onsubmit="return confirm('Mark this loan as completed?');">
                <button type="submit" class="glass-btn glass-btn-success btn-sm"><i class="fas fa-check-circle mr-1"></i>Mark Completed</button>
            </form>
        <?php endif; ?>
        <?php if ($isAdmin && $loan->status === 'disbursed'): ?>
            <button class="glass-btn btn-sm" style="border-color:rgba(231,74,59,0.3);color:#e74a3b;" onclick="if(confirm('Mark this loan as defaulted?')){window.location='<?= $baseUrl ?>/loans/<?= $loan->id ?>/default'}">
                <i class="fas fa-ban mr-1"></i>Mark Defaulted
            </button>
        <?php endif; ?>
    </div>
</div>

<div class="row">
    <div class="col-lg-4 mb-4">
        <div class="glass-card text-center">
            <img src="<?= $baseUrl ?>/img/undraw_profile.svg" class="rounded-circle mb-3" style="width:80px;height:80px;object-fit:cover;background:rgba(255,255,255,0.1);padding:5px;">
            <h5 class="font-weight-bold"><?= htmlspecialchars($loan->first_name ? trim($loan->first_name . ' ' . $loan->last_name) : $loan->user_name) ?></h5>
            <p style="color:rgba(255,255,255,0.7);"><?= htmlspecialchars($loan->email) ?></p>
            <div class="mt-3" style="border-top:1px solid rgba(255,255,255,0.1);padding-top:15px;">
                <span class="badge badge-<?= ['pending'=>'warning','approved'=>'info','rejected'=>'secondary','disbursed'=>'primary','completed'=>'success','defaulted'=>'danger'][$loan->status] ?? 'secondary' ?>" style="font-size:1rem;"><?= ucfirst($loan->status) ?></span>
            </div>
        </div>
        <div class="glass-card mt-3">
            <h6 class="font-weight-bold text-white"><i class="fas fa-info-circle mr-2"></i>Loan Summary</h6>
            <table class="table table-sm" style="margin:0;">
                <tr><td style="color:rgba(255,255,255,0.5);">Type</td><td class="text-right text-white"><?= htmlspecialchars($loan->loan_type_name) ?></td></tr>
                <tr><td style="color:rgba(255,255,255,0.5);">Principal</td><td class="text-right text-white">GHS <?= number_format($loan->amount, 2) ?></td></tr>
                <tr><td style="color:rgba(255,255,255,0.5);">Interest Rate</td><td class="text-right text-white"><?= number_format($loan->interest_rate, 1) ?>%</td></tr>
                <tr><td style="color:rgba(255,255,255,0.5);">Total Payable</td><td class="text-right text-success font-weight-bold">GHS <?= number_format($loan->total_payable, 2) ?></td></tr>
                <tr><td style="color:rgba(255,255,255,0.5);">Repayment</td><td class="text-right text-white"><?= htmlspecialchars($loan->frequency_label ?? ucfirst($loan->repayment_frequency ?? 'monthly')) ?></td></tr>
                <tr><td style="color:rgba(255,255,255,0.5);">Installments</td><td class="text-right text-white"><?= $loan->installment_count ?? $loan->duration_months ?> × GHS <?= number_format($loan->monthly_payment, 2) ?></td></tr>
                <tr><td style="color:rgba(255,255,255,0.5);">Duration</td><td class="text-right text-white"><?= $loan->duration_months ?> months</td></tr>
                <tr><td style="color:rgba(255,255,255,0.5);">Applied</td><td class="text-right text-white"><?= date('M j, Y', strtotime($loan->application_date)) ?></td></tr>
                <?php if ($loan->approval_date): ?><tr><td style="color:rgba(255,255,255,0.5);">Approved</td><td class="text-right text-white"><?= date('M j, Y', strtotime($loan->approval_date)) ?></td></tr><?php endif; ?>
                <?php if ($loan->disbursement_date): ?><tr><td style="color:rgba(255,255,255,0.5);">Disbursed</td><td class="text-right text-white"><?= date('M j, Y', strtotime($loan->disbursement_date)) ?></td></tr><?php endif; ?>
                <?php if ($loan->approver_name): ?><tr><td style="color:rgba(255,255,255,0.5);">Approved By</td><td class="text-right text-white"><?= htmlspecialchars($loan->approver_name) ?></td></tr><?php endif; ?>
            </table>
        </div>
        <?php if ($loan->purpose): ?>
        <div class="glass-card mt-3">
            <h6 class="font-weight-bold text-white"><i class="fas fa-pencil-alt mr-2"></i>Purpose</h6>
            <p style="color:rgba(255,255,255,0.7);"><?= nl2br(htmlspecialchars($loan->purpose)) ?></p>
        </div>
        <?php endif; ?>
        <?php if ($loan->notes): ?>
        <div class="glass-card mt-3">
            <h6 class="font-weight-bold text-white"><i class="fas fa-sticky-note mr-2"></i>Notes</h6>
            <p style="color:rgba(255,255,255,0.7);"><?= nl2br(htmlspecialchars($loan->notes)) ?></p>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-lg-8 mb-4">
        <div class="glass-card">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="font-weight-bold mb-0"><i class="fas fa-calendar-check mr-2"></i>Repayment Schedule</h5>
                <span style="color:rgba(255,255,255,0.5);font-size:0.85rem;">
                    Paid: <strong style="color:#1cc88a;">GHS <?= number_format($totals->total_paid, 2) ?></strong> /
                    Total: <strong style="color:#fff;">GHS <?= number_format($totals->total_due, 2) ?></strong>
                    (<?= $totals->paid_count ?>/<?= $totals->total_installments ?> installments)
                </span>
            </div>
            <?php if ($repayments): ?>
            <div class="table-responsive">
                <table class="table table-sm">
                    <thead><tr><th>#</th><th>Due Date</th><th>Amount</th><th>Paid</th><th>Balance</th><th>Status</th><?php if ($isAdmin && in_array($loan->status, ['approved','disbursed'])): ?><th>Action</th><?php endif; ?></tr></thead>
                    <tbody>
                        <?php foreach ($repayments as $r): ?>
                        <?php $balance = (float)$r->amount - (float)$r->paid_amount; ?>
                        <tr class="<?= $r->status === 'overdue' ? 'table-danger' : ($r->status === 'paid' ? '' : '') ?>">
                            <td><?= $r->installment_number ?></td>
                            <td><?= date('M j, Y', strtotime($r->due_date)) ?></td>
                            <td>GHS <?= number_format($r->amount, 2) ?></td>
                            <td>GHS <?= number_format($r->paid_amount, 2) ?></td>
                            <td>GHS <?= number_format(max(0, $balance), 2) ?></td>
                            <td>
                                <?php $sc = ['pending'=>'info','paid'=>'success','partial'=>'warning','overdue'=>'danger']; ?>
                                <span class="badge badge-<?= $sc[$r->status] ?? 'secondary' ?>"><?= ucfirst($r->status) ?></span>
                            </td>
                            <?php if ($isAdmin && in_array($loan->status, ['approved','disbursed']) && $r->status !== 'paid'): ?>
                            <td>
                                <button class="glass-btn btn-sm" data-toggle="modal" data-target="#paymentModal<?= $r->id ?>"><i class="fas fa-money-bill"></i></button>
                                <div class="modal fade" id="paymentModal<?= $r->id ?>" tabindex="-1">
                                    <div class="modal-dialog"><div class="modal-content" style="background:#1a2a4a;color:#fff;">
                                        <div class="modal-header"><h5 class="modal-title">Record Payment - Installment #<?= $r->installment_number ?></h5><button type="button" class="close text-white" data-dismiss="modal">&times;</button></div>
                                        <form method="POST" action="<?= $baseUrl ?>/loans/repayments/<?= $r->id ?>/pay">
                                            <div class="modal-body">
                                                <input type="hidden" name="loan_id" value="<?= $loan->id ?>">
                                                <div class="form-group">
                                                    <label>Amount (GHS) *</label>
                                                    <input type="number" step="0.01" name="amount" class="form-control glass-input"
                                                        max="<?= max(0, $balance) ?>" value="<?= number_format($balance, 2, '.', '') ?>" required>
                                                    <small style="color:rgba(255,255,255,0.4);">Remaining: GHS <?= number_format(max(0, $balance), 2) ?></small>
                                                </div>
                                                <div class="form-group">
                                                    <label>Payment Method</label>
                                                    <select name="payment_method" class="form-control glass-input">
                                                        <option value="cash">Cash</option>
                                                        <option value="bank_transfer">Bank Transfer</option>
                                                        <option value="mobile_money">Mobile Money</option>
                                                        <option value="cheque">Cheque</option>
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label>Transaction Reference</label>
                                                    <input type="text" name="transaction_ref" class="form-control glass-input" placeholder="Optional">
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="glass-btn" data-dismiss="modal">Cancel</button>
                                                <button type="submit" class="glass-btn glass-btn-success"><i class="fas fa-check mr-1"></i>Record Payment</button>
                                            </div>
                                        </form>
                                    </div></div>
                                </div>
                            </td>
                            <?php endif; ?>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <p style="color:rgba(255,255,255,0.5);">No repayment schedule generated yet</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php if ($isAdmin && $loan->status === 'pending'): ?>
<div class="modal fade" id="approveModal" tabindex="-1">
    <div class="modal-dialog"><div class="modal-content" style="background:#1a2a4a;color:#fff;">
        <div class="modal-header"><h5 class="modal-title">Approve Loan #<?= $loan->id ?></h5><button type="button" class="close text-white" data-dismiss="modal">&times;</button></div>
        <form method="POST" action="<?= $baseUrl ?>/loans/<?= $loan->id ?>/approve">
            <div class="modal-body">
                <p>This will approve the loan and generate the repayment schedule.</p>
                <div class="form-group">
                    <label>Admin Notes (optional)</label>
                    <textarea name="notes" class="form-control glass-input" rows="2" placeholder="Any notes about this approval..."></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="glass-btn" data-dismiss="modal">Cancel</button>
                <button type="submit" class="glass-btn glass-btn-success"><i class="fas fa-check mr-1"></i>Approve Loan</button>
            </div>
        </form>
    </div></div>
</div>
<div class="modal fade" id="rejectModal" tabindex="-1">
    <div class="modal-dialog"><div class="modal-content" style="background:#1a2a4a;color:#fff;">
        <div class="modal-header"><h5 class="modal-title">Reject Loan #<?= $loan->id ?></h5><button type="button" class="close text-white" data-dismiss="modal">&times;</button></div>
        <form method="POST" action="<?= $baseUrl ?>/loans/<?= $loan->id ?>/reject">
            <div class="modal-body">
                <div class="form-group">
                    <label>Reason for rejection</label>
                    <textarea name="notes" class="form-control glass-input" rows="3" placeholder="Explain why..." required></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="glass-btn" data-dismiss="modal">Cancel</button>
                <button type="submit" class="glass-btn btn-sm" style="border-color:rgba(231,74,59,0.3);color:#e74a3b;"><i class="fas fa-times mr-1"></i>Reject Loan</button>
            </div>
        </form>
    </div></div>
</div>
<?php endif; ?>
