<?php $__env->startSection('title', 'Transfers'); ?>
<?php $__env->startSection('page-title', 'Transfer Center'); ?>
<?php $__env->startSection('page-subtitle', 'Review internal member transfers and bank payout requests'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $canApproveTransfers = auth()->user()->hasPermission('transfers.approve');
    $canUseTransferApproval = $canApproveTransfers && $transferApprovalRequired;
    $currency = auth()->user()->tenant->settings['currency'] ?? 'NGN';
    $compactNumber = function ($amount) {
        $amount = (float) $amount;
        $absolute = abs($amount);
        $format = fn ($value, $suffix) => rtrim(rtrim(number_format($value, 1), '0'), '.').$suffix;

        if ($absolute >= 1000000000) return $format($amount / 1000000000, 'b');
        if ($absolute >= 1000000) return $format($amount / 1000000, 'm');
        if ($absolute >= 1000) return $format($amount / 1000, 'k');

        return number_format($amount, 2);
    };
    $compactMoney = fn ($amount) => $currency.' '.$compactNumber($amount);
    $money = fn ($amount) => $currency.' '.number_format((float) $amount, 2);
    $historyFiltersActive = filled($search) || filled($transferType) || filled($status) || filled($initiatedBy) || filled($approvedBy) || filled($from) || filled($to);
    $cards = [
        ['Transferred today', $compactMoney($transferStats['today_amount'] ?? 0), $compactNumber($transferStats['today_count'] ?? 0).' posted today', 'bi-arrow-left-right', 'teal'],
        ['Pending approval', $compactMoney($transferStats['pending_amount'] ?? 0), $compactNumber($transferStats['pending_count'] ?? 0).' waiting', 'bi-hourglass-split', 'amber'],
        ['Bank payouts', $compactMoney($transferStats['bank_pending_amount'] ?? 0), $compactNumber($transferStats['bank_pending_count'] ?? 0).' pending bank request', 'bi-bank', 'blue'],
        ['This month', $compactMoney($transferStats['month_amount'] ?? 0), $compactNumber($transferStats['month_count'] ?? 0).' posted', 'bi-calendar2-week', 'green'],
    ];
?>

<?php if($errors->any()): ?>
    <div class="alert alert-warning border-0"><?php echo e($errors->first()); ?></div>
<?php endif; ?>

<section class="finance-hero finance-hero-deposits mb-4">
    <div class="finance-hero-copy">
        <span>Transfer workspace</span>
        <h2>Approve member transfers without opening each savings account.</h2>
        <p>
            Members can move money to another member inside the company or request a bank payout. Every posted transfer creates savings transactions, accounting entries, and audit history.
        </p>
    </div>
    <div class="finance-metric-grid">
        <?php $__currentLoopData = $cards; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value, $hint, $icon, $tone]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="finance-metric-card <?php echo e($tone); ?>">
                <i class="bi <?php echo e($icon); ?>"></i>
                <span><?php echo e($label); ?></span>
                <strong><?php echo e($value); ?></strong>
                <small><?php echo e($hint); ?></small>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>
</section>

<div class="card border-0 shadow-sm finance-workspace">
    <div class="finance-tabbar">
        <ul class="nav nav-pills finance-tabs" role="tablist">
            <?php if($canUseTransferApproval): ?>
                <li class="nav-item" role="presentation">
                    <button class="<?php echo \Illuminate\Support\Arr::toCssClasses(['nav-link', 'active' => ! $historyFiltersActive]); ?>" data-bs-toggle="tab" data-bs-target="#pending-transfers" type="button">
                        <i class="bi bi-hourglass-split me-1"></i> Pending approval
                        <?php if(($transferStats['pending_count'] ?? 0) > 0): ?>
                            <span class="badge text-bg-warning ms-1"><?php echo e($compactNumber($transferStats['pending_count'])); ?></span>
                        <?php endif; ?>
                    </button>
                </li>
            <?php endif; ?>
            <li class="nav-item" role="presentation">
                <button class="<?php echo \Illuminate\Support\Arr::toCssClasses(['nav-link', 'active' => $historyFiltersActive || ! $canUseTransferApproval]); ?>" data-bs-toggle="tab" data-bs-target="#transfer-history" type="button">
                    <i class="bi bi-clock-history me-1"></i> History
                </button>
            </li>
        </ul>
    </div>

    <div class="tab-content">
        <?php if($canUseTransferApproval): ?>
            <div class="<?php echo \Illuminate\Support\Arr::toCssClasses(['tab-pane fade', 'show active' => ! $historyFiltersActive]); ?>" id="pending-transfers">
                <div class="finance-pane-title">
                    <div>
                        <span>Member requests</span>
                        <h3>Transfers waiting for review</h3>
                        <p>Approve internal transfers to post both sides immediately. Bank transfers debit the member savings balance after review.</p>
                    </div>
                </div>
                <?php if($pendingTransfers->isNotEmpty()): ?>
                    <div class="table-responsive">
                        <table class="table align-middle mb-0">
                            <thead>
                                <tr>
                                    <th>Member</th>
                                    <th>Source</th>
                                    <th>Destination</th>
                                    <th>Type</th>
                                    <th class="text-end">Amount</th>
                                    <th>Initiated</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $pendingTransfers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transfer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td>
                                            <strong><?php echo e($transfer->sender?->fullName() ?? 'Member'); ?></strong>
                                            <span class="d-block small text-muted"><?php echo e($transfer->sender?->account_number); ?></span>
                                        </td>
                                        <td>
                                            <strong><?php echo e($transfer->sourceAccount?->account_number); ?></strong>
                                            <span class="d-block small text-muted"><?php echo e($money($transfer->sourceAccount?->balance ?? 0)); ?> available</span>
                                        </td>
                                        <td>
                                            <strong><?php echo e($transfer->recipient_account_name ?: 'Bank recipient'); ?></strong>
                                            <span class="d-block small text-muted">
                                                <?php echo e($transfer->recipient_bank_name ? $transfer->recipient_bank_name.' - ' : ''); ?><?php echo e($transfer->recipient_account_number); ?>

                                            </span>
                                        </td>
                                        <td><span class="badge text-bg-light text-dark"><?php echo e($transfer->typeLabel()); ?></span></td>
                                        <td class="text-end fw-bold"><?php echo e($money($transfer->total_amount)); ?></td>
                                        <td>
                                            <?php echo e(app_datetime($transfer->created_at)); ?>

                                            <span class="d-block small text-muted"><?php echo e($transfer->initiator?->name ?? 'Member portal'); ?></span>
                                        </td>
                                        <td class="text-end">
                                            <div class="dropdown">
                                                <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">Actions</button>
                                                <ul class="dropdown-menu dropdown-menu-end">
                                                    <li><button class="dropdown-item" type="button" data-bs-toggle="modal" data-bs-target="#transferView<?php echo e($transfer->id); ?>"><i class="bi bi-eye me-2"></i>View details</button></li>
                                                    <li>
                                                        <form method="POST" action="<?php echo e(route('tenant.transfers.approve', $transfer)); ?>">
                                                            <?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 class="dropdown-item text-danger" type="button" data-bs-toggle="collapse" data-bs-target="#rejectTransfer<?php echo e($transfer->id); ?>"><i class="bi bi-x-circle me-2"></i>Reject</button></li>
                                                </ul>
                                            </div>
                                            <form method="POST" action="<?php echo e(route('tenant.transfers.reject', $transfer)); ?>" class="collapse mt-2" id="rejectTransfer<?php echo e($transfer->id); ?>">
                                                <?php echo csrf_field(); ?>
                                                <textarea name="reason" class="form-control form-control-sm mb-2" rows="2" placeholder="Reason"></textarea>
                                                <button class="btn btn-sm btn-danger w-100">Submit rejection</button>
                                            </form>
                                        </td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="text-center text-muted py-5">
                        <i class="bi bi-arrow-left-right h1 d-block"></i>
                        No transfer request is waiting for approval.
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <div class="<?php echo \Illuminate\Support\Arr::toCssClasses(['tab-pane fade', 'show active' => $historyFiltersActive || ! $canUseTransferApproval]); ?>" id="transfer-history">
            <div class="finance-pane-title">
                <div>
                    <span>Transfer history</span>
                    <h3>Posted, rejected, canceled, and failed transfers</h3>
                    <p>Filter by type, status, staff action, date, member, reference, bank, or account number.</p>
                </div>
            </div>

            <div class="p-3 border-bottom">
                <form method="GET" class="row g-2 align-items-end">
                    <div class="col-lg-3">
                        <label class="form-label">Search</label>
                        <input name="q" value="<?php echo e($search); ?>" class="form-control" placeholder="Reference, member, bank, account">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">Type</label>
                        <select name="transfer_type" class="form-select">
                            <option value="">All</option>
                            <option value="internal" <?php if($transferType === 'internal'): echo 'selected'; endif; ?>>Internal</option>
                            <option value="bank" <?php if($transferType === 'bank'): echo 'selected'; endif; ?>>Bank</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">Status</label>
                        <select name="status" class="form-select">
                            <option value="">All</option>
                            <?php $__currentLoopData = ['posted' => 'Posted', 'rejected' => 'Rejected', 'canceled' => 'Canceled', 'failed' => 'Failed']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($value); ?>" <?php if($status === $value): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">Initiated by</label>
                        <select name="initiated_by" class="form-select">
                            <option value="">All staff</option>
                            <?php $__currentLoopData = $usersForFilter; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $staff): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($staff->id); ?>" <?php if((string) $initiatedBy === (string) $staff->id): echo 'selected'; endif; ?>><?php echo e($staff->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">Approved by</label>
                        <select name="approved_by" class="form-select">
                            <option value="">All staff</option>
                            <?php $__currentLoopData = $usersForFilter; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $staff): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($staff->id); ?>" <?php if((string) $approvedBy === (string) $staff->id): echo 'selected'; endif; ?>><?php echo e($staff->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">From</label>
                        <input name="from" type="date" value="<?php echo e($from); ?>" class="form-control">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">To</label>
                        <input name="to" type="date" value="<?php echo e($to); ?>" class="form-control">
                    </div>
                    <div class="col-md-2">
                        <button class="btn btn-primary w-100">Filter</button>
                    </div>
                    <div class="col-md-2">
                        <a class="btn btn-outline-secondary w-100" href="<?php echo e(route('tenant.transfers.index')); ?>">Clear</a>
                    </div>
                </form>
            </div>

            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Reference</th>
                            <th>Member</th>
                            <th>Destination</th>
                            <th>Status</th>
                            <th class="text-end">Amount</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $transfers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transfer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <tr>
                                <td><?php echo e(app_datetime($transfer->posted_at ?? $transfer->created_at)); ?></td>
                                <td>
                                    <strong><?php echo e($transfer->reference); ?></strong>
                                    <span class="d-block small text-muted"><?php echo e($transfer->typeLabel()); ?></span>
                                </td>
                                <td>
                                    <?php echo e($transfer->sender?->fullName() ?? '-'); ?>

                                    <span class="d-block small text-muted"><?php echo e($transfer->sourceAccount?->account_number); ?></span>
                                </td>
                                <td>
                                    <?php echo e($transfer->recipient_account_name ?: '-'); ?>

                                    <span class="d-block small text-muted"><?php echo e($transfer->recipient_account_number ?: '-'); ?></span>
                                </td>
                                <td><span class="badge text-bg-<?php echo e($transfer->statusBadge()); ?>"><?php echo e($transfer->statusLabel()); ?></span></td>
                                <td class="text-end fw-bold"><?php echo e($money($transfer->total_amount)); ?></td>
                                <td class="text-end">
                                    <div class="dropdown">
                                        <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">Actions</button>
                                        <ul class="dropdown-menu dropdown-menu-end">
                                            <li><button class="dropdown-item" type="button" data-bs-toggle="modal" data-bs-target="#transferView<?php echo e($transfer->id); ?>"><i class="bi bi-eye me-2"></i>View details</button></li>
                                            <?php if($canApproveTransfers && $transfer->status === \App\Models\MemberTransfer::STATUS_PENDING): ?>
                                                <li>
                                                    <form method="POST" action="<?php echo e(route('tenant.transfers.cancel', $transfer)); ?>">
                                                        <?php echo csrf_field(); ?>
                                                        <button class="dropdown-item text-secondary"><i class="bi bi-slash-circle me-2"></i>Cancel</button>
                                                    </form>
                                                </li>
                                            <?php endif; ?>
                                        </ul>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr><td colspan="7" class="text-center text-muted py-5">No transfer history found.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <div class="p-3"><?php echo e($transfers->links()); ?></div>
        </div>
    </div>
</div>

<?php $__currentLoopData = $pendingTransfers->merge($transfers->getCollection())->unique('id'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transfer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <div class="modal fade" id="transferView<?php echo e($transfer->id); ?>" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content">
                <div class="modal-header border-0">
                    <div>
                        <h5 class="modal-title"><?php echo e($transfer->reference); ?></h5>
                        <div class="text-muted small"><?php echo e($transfer->typeLabel()); ?> - <?php echo e($transfer->statusLabel()); ?></div>
                    </div>
                    <button class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-4"><div class="text-muted small">Sender</div><strong><?php echo e($transfer->sender?->fullName() ?? '-'); ?></strong><div class="small"><?php echo e($transfer->sender?->account_number); ?></div></div>
                        <div class="col-md-4"><div class="text-muted small">Source savings</div><strong><?php echo e($transfer->sourceAccount?->account_number ?? '-'); ?></strong><div class="small"><?php echo e($transfer->sourceAccount?->product_name); ?></div></div>
                        <div class="col-md-4"><div class="text-muted small">Amount</div><strong><?php echo e($money($transfer->amount)); ?></strong><div class="small">Fee: <?php echo e($money($transfer->fee)); ?></div></div>
                        <div class="col-md-4"><div class="text-muted small">Recipient name</div><strong><?php echo e($transfer->recipient_account_name ?: '-'); ?></strong></div>
                        <div class="col-md-4"><div class="text-muted small">Recipient account</div><strong><?php echo e($transfer->recipient_account_number ?: '-'); ?></strong></div>
                        <div class="col-md-4"><div class="text-muted small">Bank</div><strong><?php echo e($transfer->recipient_bank_name ?: 'Internal transfer'); ?></strong></div>
                        <div class="col-md-4"><div class="text-muted small">Initiated by</div><strong><?php echo e($transfer->initiator?->name ?? 'Member portal'); ?></strong><div class="small"><?php echo e(app_datetime($transfer->created_at)); ?></div></div>
                        <div class="col-md-4"><div class="text-muted small">Approved by</div><strong><?php echo e($transfer->approver?->name ?? '-'); ?></strong><div class="small"><?php echo e(app_datetime($transfer->approved_at, false, '-')); ?></div></div>
                        <div class="col-md-4"><div class="text-muted small">Posted</div><strong><?php echo e(app_datetime($transfer->posted_at, false, '-')); ?></strong></div>
                        <div class="col-12"><div class="text-muted small">Narration</div><div><?php echo e($transfer->narration ?: '-'); ?></div></div>
                        <?php if($transfer->rejected_reason || $transfer->canceled_reason): ?>
                            <div class="col-12"><div class="text-muted small">Reason</div><div><?php echo e($transfer->rejected_reason ?: $transfer->canceled_reason); ?></div></div>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="modal-footer border-0">
                    <button class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?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\tenant\transfers\index.blade.php ENDPATH**/ ?>