<?php $__env->startSection('title', 'VTU Wallets'); ?>
<?php $__env->startSection('page-title', 'VTU Wallets'); ?>
<?php $__env->startSection('page-subtitle', 'Approve tenant VTU funding and monitor bill-payment wallet usage'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $money = fn ($amount) => 'NGN '.number_format((float) $amount, 2);
    $pendingTopups = $pendingTopupsCount ?? $topups->getCollection()->where('status', 'pending')->count();
?>

<section class="section-hero mb-4">
    <div class="section-hero-copy">
        <span>Platform VTU control</span>
        <h2>Tenants fund you before vending bills</h2>
        <p>Your VTU credentials remain platform-owned. Tenants pay into their VTU wallet, you approve the payment, and every VTpass bill purchase deducts provider cost from that wallet.</p>
    </div>
    <div class="section-hero-panel">
        <span>Total tenant VTU balance</span>
        <strong><?php echo e($money($walletTotals['balance'] ?? 0)); ?></strong>
        <small><?php echo e(number_format($pendingTopups)); ?> funding requests need review</small>
    </div>
</section>

<div class="section-stat-grid mb-4">
    <div class="section-stat-card">
        <i class="bi bi-wallet2"></i>
        <span>Total balance</span>
        <strong><?php echo e($money($walletTotals['balance'] ?? 0)); ?></strong>
        <small>All tenant VTU wallet balances</small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-arrow-down-circle"></i>
        <span>Total credited</span>
        <strong><?php echo e($money($walletTotals['credited'] ?? 0)); ?></strong>
        <small>Approved wallet funding</small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-phone-vibrate"></i>
        <span>Total used</span>
        <strong><?php echo e($money($walletTotals['used'] ?? 0)); ?></strong>
        <small>Provider cost consumed</small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-hourglass-split"></i>
        <span>Pending approvals</span>
        <strong><?php echo e(number_format($pendingTopups)); ?></strong>
        <small>Manual or online funding to verify</small>
    </div>
</div>

<div class="row g-4">
    <div class="col-xl-4">
        <div class="section-form-card">
            <div class="section-card-title">
                <span><i class="bi bi-plus-circle"></i></span>
                <div>
                    <strong>Credit tenant wallet</strong>
                    <small>Use for confirmed transfers, bonuses, corrections, or direct allocations.</small>
                </div>
            </div>
            <form method="POST" action="<?php echo e(route('super.vtu-wallets.credit')); ?>" class="row g-3">
                <?php echo csrf_field(); ?>
                <div class="col-12">
                    <label class="form-label">Tenant</label>
                    <select name="tenant_id" class="form-select" required>
                        <?php $__currentLoopData = $tenants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tenant): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($tenant->id); ?>"><?php echo e($tenant->name); ?> - <?php echo e($tenant->email); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-12">
                    <label class="form-label">Amount</label>
                    <div class="input-group">
                        <span class="input-group-text">NGN</span>
                        <input name="amount" type="number" min="100" step="100" value="5000" class="form-control" required>
                    </div>
                </div>
                <div class="col-12">
                    <label class="form-label">Notes</label>
                    <textarea name="notes" class="form-control" rows="3" placeholder="Transfer confirmation, bonus, or correction note"></textarea>
                </div>
                <div class="col-12 d-grid">
                    <button class="btn btn-primary"><i class="bi bi-check2-circle me-1"></i> Credit wallet now</button>
                </div>
            </form>
        </div>
    </div>

    <div class="col-xl-8">
        <div class="section-workspace">
            <div class="section-tabbar">
                <ul class="nav section-tabs" role="tablist">
                    <li class="nav-item" role="presentation"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#vtu-topups-pane" type="button">Funding requests</button></li>
                    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#vtu-wallets-pane" type="button">Wallets</button></li>
                    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#vtu-ledger-pane" type="button">Ledger</button></li>
                </ul>
            </div>
            <div class="tab-content">
                <div class="tab-pane fade show active" id="vtu-topups-pane" role="tabpanel" tabindex="0">
                    <div class="section-pane-head">
                        <div><span>Approvals</span><h3>Tenant VTU funding requests</h3></div>
                    </div>
                    <div class="table-responsive">
                        <table class="table section-table align-middle mb-0">
                            <thead><tr><th>Tenant</th><th>Reference</th><th>Amount</th><th>Channel</th><th>Status</th><th>Action</th></tr></thead>
                            <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $topups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $topup): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($topup->tenant?->name); ?></td>
                                    <td>
                                        <div class="fw-semibold"><?php echo e($topup->reference); ?></div>
                                        <small class="text-muted"><?php echo e(app_datetime($topup->created_at)); ?></small>
                                    </td>
                                    <td><?php echo e($money($topup->amount)); ?></td>
                                    <td><?php echo e(str_replace('_', ' ', ucfirst($topup->payment_channel))); ?></td>
                                    <td><span class="badge text-bg-<?php echo e($topup->status === 'approved' ? 'success' : ($topup->status === 'rejected' ? 'danger' : 'warning')); ?>"><?php echo e(ucfirst($topup->status)); ?></span></td>
                                    <td>
                                        <div class="dropdown">
                                            <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Action</button>
                                            <ul class="dropdown-menu dropdown-menu-end shadow-sm">
                                                <li>
                                                    <button type="button" class="dropdown-item" data-bs-toggle="modal" data-bs-target="#vtuTopupView<?php echo e($topup->id); ?>">
                                                        <i class="bi bi-eye me-2"></i> View details
                                                    </button>
                                                </li>
                                                <?php if($topup->status === 'pending'): ?>
                                                    <li><hr class="dropdown-divider"></li>
                                                    <li>
                                                        <form method="POST" action="<?php echo e(route('super.vtu-wallets.approve', $topup)); ?>">
                                                            <?php echo csrf_field(); ?>
                                                            <button class="dropdown-item text-success"><i class="bi bi-check2-circle me-2"></i> Approve</button>
                                                        </form>
                                                    </li>
                                                    <li>
                                                        <button type="button" class="dropdown-item text-danger" data-bs-toggle="modal" data-bs-target="#vtuTopupReject<?php echo e($topup->id); ?>">
                                                            <i class="bi bi-x-circle me-2"></i> Reject
                                                        </button>
                                                    </li>
                                                <?php endif; ?>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr><td colspan="6" class="text-center text-muted py-4">No VTU wallet funding requests yet.</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <?php $__currentLoopData = $topups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $topup): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $proofUrl = $topup->proof_path ? asset('uploads/'.$topup->proof_path) : null;
                            $proofExtension = strtolower(pathinfo((string) $topup->proof_path, PATHINFO_EXTENSION));
                            $proofIsImage = in_array($proofExtension, ['jpg', 'jpeg', 'png', 'webp'], true);
                        ?>
                        <div class="modal fade" id="vtuTopupView<?php echo e($topup->id); ?>" tabindex="-1" aria-labelledby="vtuTopupView<?php echo e($topup->id); ?>Label" aria-hidden="true">
                            <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <div>
                                            <h5 class="modal-title" id="vtuTopupView<?php echo e($topup->id); ?>Label">VTU wallet funding</h5>
                                            <div class="text-muted small"><?php echo e($topup->reference); ?> - <?php echo e($topup->tenant?->name); ?></div>
                                        </div>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <div class="modal-body">
                                        <div class="row g-3 mb-3">
                                            <div class="col-md-4">
                                                <div class="border rounded-3 p-3 h-100">
                                                    <span class="text-muted small">Amount</span>
                                                    <div class="fw-semibold"><?php echo e($money($topup->amount)); ?></div>
                                                </div>
                                            </div>
                                            <div class="col-md-4">
                                                <div class="border rounded-3 p-3 h-100">
                                                    <span class="text-muted small">Status</span>
                                                    <div class="fw-semibold"><?php echo e(ucfirst($topup->status)); ?></div>
                                                </div>
                                            </div>
                                            <div class="col-md-4">
                                                <div class="border rounded-3 p-3 h-100">
                                                    <span class="text-muted small">Channel</span>
                                                    <div class="fw-semibold"><?php echo e(str_replace('_', ' ', ucfirst($topup->payment_channel))); ?></div>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="border rounded-3 p-3 h-100">
                                                    <span class="text-muted small">Initiated by</span>
                                                    <div class="fw-semibold"><?php echo e($topup->creator?->name ?? 'System'); ?></div>
                                                    <small class="text-muted"><?php echo e(app_datetime($topup->created_at)); ?></small>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="border rounded-3 p-3 h-100">
                                                    <span class="text-muted small">Approved by</span>
                                                    <div class="fw-semibold"><?php echo e($topup->approver?->name ?? '-'); ?></div>
                                                    <small class="text-muted"><?php echo e($topup->processed_at ? app_datetime($topup->processed_at) : 'Not processed yet'); ?></small>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="mb-3">
                                            <div class="fw-semibold mb-2">Payment proof</div>
                                            <?php if($proofUrl && $proofIsImage): ?>
                                                <a href="<?php echo e($proofUrl); ?>" target="_blank" rel="noopener">
                                                    <img src="<?php echo e($proofUrl); ?>" alt="Payment proof for <?php echo e($topup->reference); ?>" class="img-fluid rounded-3 border">
                                                </a>
                                            <?php elseif($proofUrl): ?>
                                                <div class="border rounded-3 p-3 bg-light d-flex flex-wrap gap-2 justify-content-between align-items-center">
                                                    <div>
                                                        <div class="fw-semibold">Receipt attached</div>
                                                        <small class="text-muted"><?php echo e(strtoupper($proofExtension ?: 'FILE')); ?> payment proof</small>
                                                    </div>
                                                    <a href="<?php echo e($proofUrl); ?>" target="_blank" rel="noopener" class="btn btn-sm btn-outline-primary">
                                                        <i class="bi bi-box-arrow-up-right me-1"></i> Open receipt
                                                    </a>
                                                </div>
                                            <?php else: ?>
                                                <div class="alert alert-warning mb-0">No payment proof was attached to this funding request.</div>
                                            <?php endif; ?>
                                        </div>

                                        <?php if(filled($topup->notes)): ?>
                                            <div class="mb-3">
                                                <div class="fw-semibold mb-2">Tenant notes</div>
                                                <div class="border rounded-3 p-3 bg-light"><?php echo e($topup->notes); ?></div>
                                            </div>
                                        <?php endif; ?>

                                        <?php if(filled($topup->rejected_reason)): ?>
                                            <div>
                                                <div class="fw-semibold mb-2">Rejection reason</div>
                                                <div class="border rounded-3 p-3 bg-danger-subtle text-danger-emphasis"><?php echo e($topup->rejected_reason); ?></div>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
                                        <?php if($topup->status === 'pending'): ?>
                                            <form method="POST" action="<?php echo e(route('super.vtu-wallets.approve', $topup)); ?>">
                                                <?php echo csrf_field(); ?>
                                                <button class="btn btn-success"><i class="bi bi-check2-circle me-1"></i> Approve</button>
                                            </form>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <?php if($topup->status === 'pending'): ?>
                            <div class="modal fade" id="vtuTopupReject<?php echo e($topup->id); ?>" tabindex="-1" aria-labelledby="vtuTopupReject<?php echo e($topup->id); ?>Label" aria-hidden="true">
                                <div class="modal-dialog modal-dialog-centered">
                                    <div class="modal-content">
                                        <form method="POST" action="<?php echo e(route('super.vtu-wallets.reject', $topup)); ?>">
                                            <?php echo csrf_field(); ?>
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="vtuTopupReject<?php echo e($topup->id); ?>Label">Reject VTU funding</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                            </div>
                                            <div class="modal-body">
                                                <label class="form-label">Reason</label>
                                                <textarea name="rejected_reason" class="form-control" rows="4" required placeholder="Explain why this payment proof was rejected"></textarea>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
                                                <button class="btn btn-danger"><i class="bi bi-x-circle me-1"></i> Reject</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    <div class="p-3"><?php echo e($topups->links()); ?></div>
                </div>

                <div class="tab-pane fade" id="vtu-wallets-pane" role="tabpanel" tabindex="0">
                    <div class="section-pane-head">
                        <div><span>Balances</span><h3>Tenant VTU wallets</h3></div>
                    </div>
                    <div class="table-responsive">
                        <table class="table section-table align-middle mb-0">
                            <thead><tr><th>Tenant</th><th>Balance</th><th>Total credited</th><th>Total used</th><th>Refunded</th><th>Updated</th></tr></thead>
                            <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $wallets; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $wallet): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($wallet->tenant?->name); ?></td>
                                    <td class="fw-semibold"><?php echo e($money($wallet->balance)); ?></td>
                                    <td><?php echo e($money($wallet->total_credited)); ?></td>
                                    <td><?php echo e($money($wallet->total_used)); ?></td>
                                    <td><?php echo e($money($wallet->total_refunded)); ?></td>
                                    <td><?php echo e(app_datetime($wallet->updated_at)); ?></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr><td colspan="6" class="text-center text-muted py-4">No tenant VTU wallets yet.</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="tab-pane fade" id="vtu-ledger-pane" role="tabpanel" tabindex="0">
                    <div class="section-pane-head">
                        <div><span>Ledger</span><h3>VTU credits, usage, and refunds</h3></div>
                    </div>
                    <div class="table-responsive">
                        <table class="table section-table align-middle mb-0">
                            <thead><tr><th>Date</th><th>Tenant</th><th>Reference</th><th>Type</th><th>Amount</th><th>Balance</th><th>Description</th></tr></thead>
                            <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $transactions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transaction): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e(app_datetime($transaction->created_at)); ?></td>
                                    <td><?php echo e($transaction->tenant?->name); ?></td>
                                    <td><?php echo e($transaction->reference); ?></td>
                                    <td><span class="badge text-bg-light border"><?php echo e(ucfirst($transaction->type)); ?></span></td>
                                    <td class="<?php echo e($transaction->direction === 'debit' ? 'text-danger' : 'text-success'); ?>">
                                        <?php echo e($transaction->direction === 'debit' ? '-' : '+'); ?><?php echo e($money($transaction->amount)); ?>

                                    </td>
                                    <td><?php echo e($money($transaction->balance_after)); ?></td>
                                    <td><?php echo e($transaction->description); ?></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr><td colspan="7" class="text-center text-muted py-4">No VTU wallet activity yet.</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="p-3"><?php echo e($transactions->links()); ?></div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\Polite hp\Documents\Codex\2026-05-08\build-a-complete-production-ready-saas\microfinance-saas\resources\views\super\vtu-wallet\index.blade.php ENDPATH**/ ?>