

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

<?php $__env->startPush('styles'); ?>
<link rel="stylesheet" href="<?php echo e(asset('admin/css/loans.css')); ?>">
<?php $__env->stopPush(); ?>

<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">
        <a href="<?php echo e(route('admin.loans.create')); ?>" class="loan-btn loan-btn-dark">
            + Create Loan
        </a>
    </div>
</div>

<div class="loan-stats">
    <div class="loan-stat"><div class="loan-stat-label">Total Loans</div><div class="loan-stat-value loan-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 loan-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 loan-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 loan-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 loan-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 loan-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 loan-green"><?php echo e(money((float)$totalDisbursed)); ?></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 loan-orange"><?php echo e(money((float)$totalOutstanding)); ?></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(money((float)$loan->loan_amount)); ?></div>
    <div class="loan-submoney">Total: <?php echo e(money((float)$loan->total_payable)); ?></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(money((float)$loan->processing_fee)); ?></div>
</td>

<td>
    <div class="loan-money"><?php echo e(money((float)$loan->emi_amount)); ?></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(money((float)$loan->outstanding_amount)); ?></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>

<?php if($loans->hasPages()): ?>
<div class="loan-pagination">

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

    <div class="loan-pagination-controls">

        
        <?php if($loans->onFirstPage()): ?>
            <span class="loan-page-btn loan-page-disabled">
                ‹
            </span>
        <?php else: ?>
            <a
                href="<?php echo e($loans->previousPageUrl()); ?>"
                class="loan-page-btn"
                aria-label="Previous page"
            >
                ‹
            </a>
        <?php endif; ?>


        
        <?php $__currentLoopData = $loans->getUrlRange(
            max(1, $loans->currentPage() - 2),
            min($loans->lastPage(), $loans->currentPage() + 2)
        ); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $page => $url): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

            <?php if($page == $loans->currentPage()): ?>

                <span class="loan-page-btn loan-page-active">
                    <?php echo e($page); ?>

                </span>

            <?php else: ?>

                <a
                    href="<?php echo e($url); ?>"
                    class="loan-page-btn"
                >
                    <?php echo e($page); ?>

                </a>

            <?php endif; ?>

        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>


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

            <a
                href="<?php echo e($loans->nextPageUrl()); ?>"
                class="loan-page-btn"
                aria-label="Next page"
            >
                ›
            </a>

        <?php else: ?>

            <span class="loan-page-btn loan-page-disabled">
                ›
            </span>

        <?php endif; ?>

    </div>

</div>
<?php endif; ?>

<?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('admin.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**/ ?>