

<?php $__env->startSection('content'); ?>

<style>
    .loan-page{min-height:calc(100vh - 70px);background:#f5f8fc;padding:28px;color:#14243a}
    .loan-container{max-width:1500px;margin:auto}
    .loan-header{display:flex;justify-content:space-between;gap:20px;margin-bottom:24px}
    .loan-title h1{margin:0;font-size:28px;font-weight:800;letter-spacing:-.7px}
    .loan-title p{margin:7px 0 0;color:#738399;font-size:13px}
    .loan-actions{display:flex;gap:9px;flex-wrap:wrap}
    .loan-btn{border:0;border-radius:10px;padding:10px 14px;font-size:12px;font-weight:800;text-decoration:none;cursor:pointer;display:inline-flex;align-items:center;gap:7px;transition:.18s}
    .loan-btn:hover{transform:translateY(-1px)}
    .loan-btn-light{background:#fff;color:#40536c;border:1px solid #e1e8f1}
    .loan-btn-dark{background:#142b4a;color:#fff}
    .loan-stats{display:grid;grid-template-columns:repeat(6,1fr);gap:13px;margin-bottom:20px}
    .loan-stat{background:#fff;border:1px solid #e3eaf2;border-radius:16px;padding:17px;box-shadow:0 8px 24px rgba(24,52,86,.045)}
    .loan-stat-label{color:#7a8ba0;font-size:10px;font-weight:800;text-transform:uppercase;letter-spacing:.5px}
    .loan-stat-value{margin-top:7px;font-size:23px;font-weight:850}
    .loan-stat-meta{margin-top:5px;font-size:10px;color:#94a1b2}
    .blue{color:#1769e0}.orange{color:#d97706}.green{color:#198754}.red{color:#dc3545}.purple{color:#7057d8}
    .loan-main-card{background:#fff;border:1px solid #e1e8f1;border-radius:18px;box-shadow:0 10px 30px rgba(24,52,86,.055);overflow:hidden}
    .loan-toolbar{padding:17px;border-bottom:1px solid #edf1f5;display:flex;gap:13px;align-items:center;flex-wrap:wrap}
    .loan-search{flex:1;min-width:240px}.loan-search input,.loan-filter select{width:100%;height:40px;border:1px solid #dfe6ee;border-radius:9px;background:#fbfcfe;padding:0 12px;font-size:12px;outline:none}
    .loan-filter{width:180px}
    .loan-table-wrap{overflow-x:auto}.loan-table{width:100%;min-width:1080px;border-collapse:collapse}
    .loan-table th{background:#f8fafc;color:#7a8a9d;text-align:left;font-size:9px;text-transform:uppercase;letter-spacing:.65px;padding:12px 15px;border-bottom:1px solid #e9eef4;white-space:nowrap}
    .loan-table td{padding:14px 15px;border-bottom:1px solid #edf1f5;vertical-align:middle;font-size:12px;color:#334860}
    .loan-table tbody tr:hover{background:#fbfdff}
    .loan-client{display:flex;align-items:center;gap:10px;min-width:180px}.loan-avatar{width:36px;height:36px;border-radius:11px;display:flex;align-items:center;justify-content:center;background:#edf4ff;color:#1769e0;font-weight:900}
    .loan-client-name{font-weight:800;color:#203650}.loan-client-sub,.loan-submoney{margin-top:3px;color:#8795a7;font-size:10px}
    .loan-number{color:#1769e0;font-weight:850;white-space:nowrap}.loan-money{color:#1d3048;font-weight:850;white-space:nowrap}
    .loan-zero{display:inline-flex;padding:5px 7px;border-radius:7px;background:#ecf9f1;color:#198754;font-size:9px;font-weight:900;white-space:nowrap}
    .loan-status{display:inline-flex;align-items:center;gap:5px;padding:6px 8px;border-radius:50px;font-size:9px;font-weight:900;white-space:nowrap}
    .loan-status:before{content:"";width:5px;height:5px;border-radius:50%;background:currentColor}
    .status-pending{color:#b36b00;background:#fff5df}.status-approved{color:#1769e0;background:#eaf3ff}.status-disbursed,.status-active{color:#198754;background:#eaf8f0}.status-overdue{color:#dc3545;background:#fff0f1}.status-completed{color:#7057d8;background:#f0edff}.status-rejected,.status-cancelled{color:#7a8797;background:#f0f2f5}
    .loan-row-actions{display:flex;align-items:center;gap:6px}.loan-icon-btn{width:31px;height:31px;border-radius:8px;display:inline-flex;align-items:center;justify-content:center;border:1px solid #e1e7ef;background:#fff;color:#51647c;cursor:pointer;text-decoration:none;font-size:11px}
    .loan-icon-btn:hover{background:#f4f8fd;color:#1769e0}.loan-mini-form{display:inline}
    .loan-pagination{padding:14px 17px;display:flex;justify-content:space-between;align-items:center;color:#7c8b9e;font-size:10px}
    .loan-empty{padding:65px 20px;text-align:center;color:#8291a4}.loan-empty-icon{width:54px;height:54px;margin:0 auto 12px;border-radius:16px;display:flex;align-items:center;justify-content:center;background:#f0f5fb;color:#7e91aa;font-size:20px;font-weight:900}
    .loan-empty strong{display:block;color:#40536c;font-size:14px;margin-bottom:5px}.loan-alert{margin-bottom:16px;padding:11px 13px;border-radius:10px;font-size:12px;font-weight:700}.loan-alert-success{color:#146c43;background:#dff5e9;border:1px solid #c4ebd5}.loan-alert-error{color:#a52834;background:#fde4e7;border:1px solid #f7c9cf}
    @media(max-width:1200px){.loan-stats{grid-template-columns:repeat(3,1fr)}}
    @media(max-width:760px){.loan-page{padding:17px 12px 30px}.loan-header{flex-direction:column}.loan-stats{grid-template-columns:repeat(2,1fr)}.loan-search,.loan-filter{width:100%;min-width:0}.loan-pagination{flex-direction:column;align-items:flex-start}}
    @media(max-width:430px){.loan-stats{grid-template-columns:1fr}.loan-title h1{font-size:24px}}
</style>

<div class="loan-page">
<div class="loan-container">

<?php if(session('success')): ?>
<div class="loan-alert loan-alert-success"><?php echo e(session('success')); ?></div>
<?php endif; ?>

<?php if(session('error')): ?>
<div class="loan-alert loan-alert-error"><?php echo e(session('error')); ?></div>
<?php endif; ?>

<div class="loan-header">
    <div class="loan-title">
        <h1>Loan Management</h1>
        <p>Review applications, manage approvals and monitor active loans.</p>
    </div>
    <div class="loan-actions">
        <form method="POST" action="<?php echo e(route('admin.loans.update-overdue')); ?>" class="loan-mini-form" onsubmit="return confirm('Update overdue loan statuses?')">
            <?php echo csrf_field(); ?>
            <button type="submit" class="loan-btn loan-btn-light">↻ Update Overdue</button>
        </form>
    </div>
</div>

<div class="loan-stats">
    <div class="loan-stat"><div class="loan-stat-label">Total Loans</div><div class="loan-stat-value blue"><?php echo e(number_format($totalLoans)); ?></div><div class="loan-stat-meta">All applications</div></div>
    <div class="loan-stat"><div class="loan-stat-label">Pending</div><div class="loan-stat-value orange"><?php echo e(number_format($pendingLoans)); ?></div><div class="loan-stat-meta">Awaiting review</div></div>
    <div class="loan-stat"><div class="loan-stat-label">Approved</div><div class="loan-stat-value blue"><?php echo e(number_format($approvedLoans)); ?></div><div class="loan-stat-meta">Ready to disburse</div></div>
    <div class="loan-stat"><div class="loan-stat-label">Active</div><div class="loan-stat-value green"><?php echo e(number_format($activeLoans)); ?></div><div class="loan-stat-meta">Currently running</div></div>
    <div class="loan-stat"><div class="loan-stat-label">Overdue</div><div class="loan-stat-value red"><?php echo e(number_format($overdueLoans)); ?></div><div class="loan-stat-meta">Needs attention</div></div>
    <div class="loan-stat"><div class="loan-stat-label">Completed</div><div class="loan-stat-value purple"><?php echo e(number_format($completedLoans)); ?></div><div class="loan-stat-meta">Successfully closed</div></div>
</div>

<div class="loan-stats" style="grid-template-columns:repeat(2,1fr);margin-bottom:20px">
    <div class="loan-stat"><div class="loan-stat-label">Total Disbursed</div><div class="loan-stat-value green">₹<?php echo e(number_format((float)$totalDisbursed,2)); ?></div><div class="loan-stat-meta">Disbursed, active and completed loans</div></div>
    <div class="loan-stat"><div class="loan-stat-label">Total Outstanding</div><div class="loan-stat-value orange">₹<?php echo e(number_format((float)$totalOutstanding,2)); ?></div><div class="loan-stat-meta">Current outstanding balance</div></div>
</div>

<div class="loan-main-card">

<form method="GET" action="<?php echo e(route('admin.loans')); ?>" class="loan-toolbar">
    <div class="loan-search">
        <input type="search" name="search" value="<?php echo e(request('search')); ?>" placeholder="Search loan number, client name, mobile or email...">
    </div>
    <div class="loan-filter">
        <select name="status" onchange="this.form.submit()">
            <option value="">All Status</option>
            <?php $__currentLoopData = ['pending'=>'Pending','approved'=>'Approved','disbursed'=>'Disbursed','active'=>'Active','overdue'=>'Overdue','completed'=>'Completed','rejected'=>'Rejected','cancelled'=>'Cancelled']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value=>$label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($value); ?>" <?php if(request('status') === $value): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
    </div>
    <button type="submit" class="loan-btn loan-btn-dark">Search</button>
    <?php if(request()->filled('search') || request()->filled('status')): ?>
        <a href="<?php echo e(route('admin.loans')); ?>" class="loan-btn loan-btn-light">Clear</a>
    <?php endif; ?>
</form>

<?php if($loans->count()): ?>

<div class="loan-table-wrap">
<table class="loan-table">
<thead>
<tr>
<th>Loan</th><th>Client</th><th>Loan Amount</th><th>Charges</th><th>EMI</th><th>Tenure</th><th>Outstanding</th><th>Status</th><th>Applied</th><th>Action</th>
</tr>
</thead>
<tbody>

<?php $__currentLoopData = $loans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $loan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php
    $clientName = $loan->user->name ?? 'Unknown Client';
    $initial = strtoupper(substr($clientName,0,1));
    $statusClass = match($loan->status) {
        'pending'=>'status-pending','approved'=>'status-approved','disbursed'=>'status-disbursed','active'=>'status-active',
        'overdue'=>'status-overdue','completed'=>'status-completed','rejected'=>'status-rejected','cancelled'=>'status-cancelled',
        default=>'status-pending'
    };
?>

<tr>
<td>
    <div class="loan-number"><?php echo e($loan->loan_number); ?></div>
    <div class="loan-client-sub">Card: <?php echo e($loan->emiCard->card_number ?? '—'); ?></div>
</td>

<td>
    <div class="loan-client">
        <div class="loan-avatar"><?php echo e($initial); ?></div>
        <div>
            <div class="loan-client-name"><?php echo e($clientName); ?></div>
            <div class="loan-client-sub"><?php echo e($loan->user->mobile ?? $loan->user->email ?? '—'); ?></div>
        </div>
    </div>
</td>

<td>
    <div class="loan-money">₹<?php echo e(number_format((float)$loan->loan_amount,2)); ?></div>
    <div class="loan-submoney">Total: ₹<?php echo e(number_format((float)$loan->total_payable,2)); ?></div>
</td>

<td>
    <span class="loan-zero"><?php echo e(number_format((float)$loan->interest_rate,2)); ?>% Interest</span>
    <div class="loan-submoney">Fee: ₹<?php echo e(number_format((float)$loan->processing_fee,2)); ?></div>
</td>

<td>
    <div class="loan-money">₹<?php echo e(number_format((float)$loan->emi_amount,2)); ?></div>
    <div class="loan-submoney">Monthly</div>
</td>

<td>
    <div class="loan-money"><?php echo e($loan->tenure_months); ?></div>
    <div class="loan-submoney">months</div>
</td>

<td><div class="loan-money">₹<?php echo e(number_format((float)$loan->outstanding_amount,2)); ?></div></td>

<td><span class="loan-status <?php echo e($statusClass); ?>"><?php echo e(ucfirst($loan->status)); ?></span></td>

<td>
    <div class="loan-money"><?php echo e(optional($loan->applied_at)->format('d M Y') ?? optional($loan->created_at)->format('d M Y')); ?></div>
    <div class="loan-submoney"><?php echo e(optional($loan->created_at)->format('h:i A')); ?></div>
</td>

<td>
<div class="loan-row-actions">

<a href="<?php echo e(route('admin.loans.show',$loan)); ?>" class="loan-icon-btn" title="View Loan">↗</a>

<?php if($loan->status === 'pending'): ?>
<form method="POST" action="<?php echo e(route('admin.loans.approve',$loan)); ?>" class="loan-mini-form" onsubmit="return confirm('Approve this loan?')">
<?php echo csrf_field(); ?>
<button type="submit" class="loan-icon-btn" title="Approve">✓</button>
</form>

<form method="POST" action="<?php echo e(route('admin.loans.reject',$loan)); ?>" class="loan-mini-form" onsubmit="return rejectLoan(this)">
<?php echo csrf_field(); ?>
<input type="hidden" name="rejection_reason" value="">
<button type="submit" class="loan-icon-btn" title="Reject">×</button>
</form>

<?php elseif($loan->status === 'approved'): ?>
<form method="POST" action="<?php echo e(route('admin.loans.disburse',$loan)); ?>" class="loan-mini-form" onsubmit="return confirm('Disburse this loan and generate the EMI schedule?')">
<?php echo csrf_field(); ?>
<button type="submit" class="loan-icon-btn" title="Disburse">₹</button>
</form>
<?php endif; ?>

</div>
</td>
</tr>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

</tbody>
</table>
</div>

<div class="loan-pagination">
<div>Showing <?php echo e($loans->firstItem()); ?> - <?php echo e($loans->lastItem()); ?> of <?php echo e($loans->total()); ?></div>
<div><?php echo e($loans->links()); ?></div>
</div>

<?php else: ?>
<div class="loan-empty">
    <div class="loan-empty-icon">₹</div>
    <strong>No loans found</strong>
    <span>There are no loan records matching your current filters.</span>
</div>
<?php endif; ?>

</div>
</div>
</div>

<script>
function rejectLoan(form) {
    const reason = prompt('Enter rejection reason:');
    if (reason === null) return false;
    if (!reason.trim()) {
        alert('Rejection reason is required.');
        return false;
    }
    form.querySelector('input[name="rejection_reason"]').value = reason.trim();
    return true;
}
</script>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\emi-card\resources\views/admin/loans/index.blade.php ENDPATH**/ ?>