

<?php $__env->startSection('page-title', 'Payments'); ?>
<?php $__env->startSection('page-description', 'Manage and monitor all payment transactions'); ?>

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


<div class="payment-page">

    <div class="payment-heading">
        <div>
            <h2>Payment Management</h2>
            <p>View online payments and record manual client payments.</p>
        </div>
    </div>

    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-label">Total Payments</div>
            <div class="stat-value"><?php echo e($totalPayments ?? 0); ?></div>
            <div class="stat-description">All payment transactions</div>
        </div>
        <div class="stat-card stat-green">
            <div class="stat-label">Successful</div>
            <div class="stat-value"><?php echo e($successfulPayments ?? 0); ?></div>
            <div class="stat-description">Completed payments</div>
        </div>
        <div class="stat-card stat-orange">
            <div class="stat-label">Pending</div>
            <div class="stat-value"><?php echo e($pendingPayments ?? 0); ?></div>
            <div class="stat-description">Awaiting payment</div>
        </div>
        <div class="stat-card stat-red">
            <div class="stat-label">Failed</div>
            <div class="stat-value"><?php echo e($failedPayments ?? 0); ?></div>
            <div class="stat-description">Failed or cancelled</div>
        </div>
    </div>

    <div class="total-card">
        <div>
            <div class="total-label">Total Successful Payment Amount</div>
            <div class="total-value"><?php echo e(money((float)($totalPaymentAmount ?? $totalAmount ?? 0))); ?></div>
        </div>
        <div class="total-icon">💰</div>
    </div>

    <div class="filter-card">
        <div class="filter-title">Search & Filter Payments</div>
        <form method="GET" action="<?php echo e(route('admin.payments')); ?>" class="filter-form">
            <div class="field">
                <label>Search</label>
                <input type="text" name="search" value="<?php echo e($search ?? ''); ?>" placeholder="Name, email, mobile, payment ID, loan number...">
            </div>
            <div class="field">
                <label>Status</label>
                <select name="status">
                    <option value="">All Status</option>
                    <?php $__currentLoopData = ['paid'=>'Paid','pending'=>'Pending','created'=>'Created','failed'=>'Failed','cancelled'=>'Cancelled','cancelled_by_user'=>'Cancelled by User']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value=>$label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($value); ?>" <?php echo e(($status ?? '') === $value ? 'selected' : ''); ?>><?php echo e($label); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div class="field">
                <label>Payment Method</label>
                <select name="payment_method">
                    <option value="">All Methods</option>
                    <option value="online" <?php echo e(($paymentMethod ?? '') === 'online' ? 'selected' : ''); ?>>Online</option>
                    <option value="cash" <?php echo e(($paymentMethod ?? '') === 'cash' ? 'selected' : ''); ?>>Cash</option>
                    <option value="upi" <?php echo e(($paymentMethod ?? '') === 'upi' ? 'selected' : ''); ?>>UPI</option>
                    <option value="bank_transfer" <?php echo e(($paymentMethod ?? '') === 'bank_transfer' ? 'selected' : ''); ?>>Bank Transfer</option>
                </select>
            </div>
            <button type="submit" class="search-btn">🔎 Search</button>
            <a href="<?php echo e(route('admin.payments')); ?>" class="reset-btn">Reset</a>
        </form>
    </div>

    <div class="table-card">
        <div class="table-head">
            <div>
                <h3>Payment Transactions</h3>
                <p>Online and manually recorded payments</p>
            </div>
            
        </div>

        <div class="table-wrap">
            <?php if($payments->count()): ?>
                <div class="payments-table-scroll">
<table>
                    <thead>
                        <tr>
                            <th>Client</th>
                            <th>Loan / EMI</th>
                            <th>Payment ID</th>
                            <th>Amount</th>
                            <th>Method</th>
                            <th>Status</th>
                            <th>Date</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php $__currentLoopData = $payments; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $payment): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $loan = $payment->loan;
                            $installment = $payment->installment;
                        ?>
                        <tr>
                            <td>
                                <div class="client-name"><?php echo e($payment->user->name ?? 'Unknown'); ?></div>
                                <div class="client-sub"><?php echo e($payment->user->mobile ?? ($payment->user->email ?? '—')); ?></div>
                            </td>
                            <td>
                                <?php if($loan): ?>
                                    <div class="loan-number"><?php echo e($loan->loan_number ?? 'Loan #'.$loan->id); ?></div>
                                    <?php if($installment): ?>
                                        <div class="emi-number">EMI #<?php echo e($installment->installment_number); ?></div>
                                    <?php else: ?>
                                        <div class="emi-number">Loan balance payment</div>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <span style="color:#9aa6b6">—</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div style="font-family:monospace;font-size:8px"><?php echo e($payment->payment_id ?? '—'); ?></div>
                                <div class="client-sub"><?php echo e($payment->order_id ?? ''); ?></div>
                            </td>
                            <td><span class="amount"><?php echo e(money((float)($payment->amount ?? 0))); ?></span></td>
                            <td>
                                <span class="method">
                                    <?php if($payment->payment_method): ?>
                                        <?php echo e(str_replace('_',' ', $payment->payment_method)); ?>

                                    <?php elseif($payment->is_manual): ?>
                                        Manual
                                    <?php else: ?>
                                        Online
                                    <?php endif; ?>
                                </span>
                            </td>
                            <td>
                                <?php if($payment->status === 'paid'): ?>
                                    <span class="badge badge-paid">● Paid</span>
                                <?php elseif(in_array($payment->status,['pending','created'],true)): ?>
                                    <span class="badge badge-pending">● <?php echo e(ucfirst($payment->status)); ?></span>
                                <?php elseif(in_array($payment->status,['failed','cancelled','cancelled_by_user'],true)): ?>
                                    <span class="badge badge-failed">● <?php echo e(ucfirst(str_replace('_',' ',$payment->status))); ?></span>
                                <?php else: ?>
                                    <span class="badge badge-gray">● <?php echo e(ucfirst($payment->status ?? 'Unknown')); ?></span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo e($payment->paid_at?->format('d M Y, h:i A') ?? $payment->created_at?->format('d M Y, h:i A') ?? '—'); ?></td>
                            <td>
                                <a href="<?php echo e(route('admin.payments.show',$payment)); ?>" class="view-btn">View</a>
                                <?php if($loan): ?>
                                    <a href="<?php echo e(route('admin.loans.show',$loan)); ?>" class="loan-link">Loan</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table>
</div>
            <?php else: ?>
                <div class="empty">💳 No payment transactions found.</div>
            <?php endif; ?>
        </div>

        <?php if($payments->hasPages()): ?>

    <div class="pagination-area">

        <div class="pagination-info">
            Page <?php echo e($payments->currentPage()); ?>

            of <?php echo e($payments->lastPage()); ?>

        </div>

        <div class="pagination-links">

            
            <?php if($payments->onFirstPage()): ?>

                <span class="pagination-btn disabled">
                    ‹
                </span>

            <?php else: ?>

                <a
                    href="<?php echo e($payments->previousPageUrl()); ?>"
                    class="pagination-btn"
                    aria-label="Previous page"
                >
                    ‹
                </a>

            <?php endif; ?>


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

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

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

                    </span>

                <?php else: ?>

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

                    </a>

                <?php endif; ?>

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


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

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

            <?php else: ?>

                <span class="pagination-btn disabled">
                    ›
                </span>

            <?php endif; ?>

        </div>

    </div>

<?php endif; ?>
    </div>
</div>

<div id="manualPaymentModal" class="modal-overlay">
    <div class="modal" onclick="event.stopPropagation()">
        <div class="modal-header">
            <div>
                <h3>Add Manual Payment</h3>
                <p>Record a payment received directly from the client.</p>
            </div>
            <button type="button" class="modal-close" onclick="closeManualPaymentModal()">×</button>
        </div>

        <form method="POST" action="<?php echo e(route('admin.payments.manual')); ?>">
            <?php echo csrf_field(); ?>
            <div class="modal-body">
                <div class="modal-info">This option is for administrators. The payment will be recorded as <strong>Paid</strong>.</div>

                <div class="form-group">
                    <label>Client *</label>
                    <select name="user_id" class="form-control" required>
                        <option value="">Select Client</option>
                        <?php $__currentLoopData = \App\Models\User::where('role','client')->orderBy('name')->get(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $client): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($client->id); ?>"><?php echo e($client->name); ?><?php echo e($client->mobile ? ' — '.$client->mobile : ''); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>

                <div class="form-group">
                    <label>Amount *</label>
                    <input type="number" name="amount" class="form-control" min="0.01" step="0.01" required>
                </div>

                <div class="form-group">
                    <label>Payment Method *</label>
                    <select name="payment_method" class="form-control" required>
                        <option value="">Select Payment Method</option>
                        <option value="cash">Cash</option>
                        <option value="upi">UPI</option>
                        <option value="bank_transfer">Bank Transfer</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Reference Number</label>
                    <input type="text" name="reference_number" class="form-control" maxlength="255">
                </div>

                <div class="form-group">
                    <label>Paid At *</label>
                    <input type="datetime-local" name="paid_at" class="form-control" value="<?php echo e(now()->format('Y-m-d\TH:i')); ?>" required>
                </div>

                <div class="form-group">
                    <label>Remarks</label>
                    <textarea name="remarks" class="form-control" maxlength="2000"></textarea>
                </div>

                <div class="modal-actions">
                    <button type="button" class="cancel-btn" onclick="closeManualPaymentModal()">Cancel</button>
                    <button type="submit" class="save-btn">✓ Save Payment</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
function openManualPaymentModal(){
    const modal=document.getElementById('manualPaymentModal');
    if(modal){modal.style.display='flex';document.body.style.overflow='hidden';}
}
function closeManualPaymentModal(){
    const modal=document.getElementById('manualPaymentModal');
    if(modal){modal.style.display='none';document.body.style.overflow='';}
}
document.getElementById('manualPaymentModal')?.addEventListener('click',function(e){
    if(e.target===this) closeManualPaymentModal();
});
document.addEventListener('keydown',function(e){
    if(e.key==='Escape') closeManualPaymentModal();
});
</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/payments/index.blade.php ENDPATH**/ ?>