

<?php $__env->startSection('title', 'Withdrawal'); ?>
<?php $__env->startSection('page-title', 'Withdrawal Center'); ?>
<?php $__env->startSection('page-subtitle', 'Track matured savings, withdrawal activity, and upcoming maturity'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $canManageSavings = auth()->user()->hasPermission('savings.manage');
    $canApproveSavings = auth()->user()->hasPermission('savings.approve');
    $canUseWithdrawalApproval = $canApproveSavings && $withdrawalApprovalRequired;
    $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);
    };
    $money = fn ($amount) => $currency.' '.number_format((float) $amount, 2);
    $compactMoney = fn ($amount) => $currency.' '.$compactNumber($amount);
    $historyFiltersActive = filled($search) || filled($initiatedBy) || filled($approvedBy) || filled($from) || filled($to);
    $metricCards = [
        ['Withdrawal today', $compactMoney($withdrawalStats['today_amount'] ?? 0), $compactNumber($withdrawalStats['today_count'] ?? 0).' transaction'.(($withdrawalStats['today_count'] ?? 0) == 1 ? '' : 's'), 'bi-arrow-up-circle', 'rose'],
        ['Matured for withdrawal', $compactMoney($withdrawalStats['ready_amount'] ?? 0), $compactNumber($withdrawalStats['ready_count'] ?? 0).' account'.(($withdrawalStats['ready_count'] ?? 0) == 1 ? '' : 's').' ready', 'bi-check2-circle', 'green'],
        ['Upcoming maturity', $compactMoney($withdrawalStats['upcoming_amount'] ?? 0), 'Next 30 days', 'bi-calendar2-week', 'amber'],
        ['This month', $compactMoney($withdrawalStats['month_amount'] ?? 0), $compactNumber($withdrawalStats['month_count'] ?? 0).' withdrawal'.(($withdrawalStats['month_count'] ?? 0) == 1 ? '' : 's'), 'bi-graph-up-arrow', 'blue'],
    ];
?>

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

<section class="withdrawal-hero mb-4">
    <div class="withdrawal-hero-copy">
        <span>Withdrawal workspace</span>
        <h2>See what can be paid out today without digging through savings.</h2>
        <p>Monitor matured thrift, target, cooperative, and flexible savings balances, then open the member account or record eligible withdrawals.</p>
    </div>
    <div class="withdrawal-metric-grid">
        <?php $__currentLoopData = $metricCards; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value, $hint, $icon, $tone]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="withdrawal-metric <?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 withdrawal-workspace">
    <div class="withdrawal-tabbar">
        <ul class="nav nav-pills withdrawal-tabs" role="tablist">
            <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="#ready-withdrawals" type="button">
                    <i class="bi bi-check2-circle me-1"></i> Ready to withdraw
                    <?php if($readyAccounts->isNotEmpty()): ?>
                        <span class="badge text-bg-success ms-1"><?php echo e($compactNumber($readyAccounts->count())); ?></span>
                    <?php endif; ?>
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" data-bs-toggle="tab" data-bs-target="#upcoming-maturity" type="button">
                    <i class="bi bi-calendar2-check me-1"></i> Upcoming maturity
                    <?php if($upcomingMaturities->isNotEmpty()): ?>
                        <span class="badge text-bg-warning ms-1"><?php echo e($compactNumber($upcomingMaturities->count())); ?></span>
                    <?php endif; ?>
                </button>
            </li>
            <?php if($canUseWithdrawalApproval): ?>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" data-bs-toggle="tab" data-bs-target="#pending-withdrawals" type="button">
                        <i class="bi bi-hourglass-split me-1"></i> Pending approval
                        <?php if(($withdrawalStats['pending_count'] ?? 0) > 0): ?>
                            <span class="badge text-bg-warning ms-1"><?php echo e($compactNumber($withdrawalStats['pending_count'])); ?></span>
                        <?php endif; ?>
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" data-bs-toggle="tab" data-bs-target="#member-withdrawal-requests" type="button">
                        <i class="bi bi-person-check me-1"></i> Member request
                        <?php if(($withdrawalStats['member_request_count'] ?? 0) > 0): ?>
                            <span class="badge text-bg-info ms-1"><?php echo e($compactNumber($withdrawalStats['member_request_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]); ?>" data-bs-toggle="tab" data-bs-target="#withdrawal-history" type="button">
                    <i class="bi bi-clock-history me-1"></i> History
                </button>
            </li>
        </ul>
    </div>

    <div class="tab-content">
        <div class="<?php echo \Illuminate\Support\Arr::toCssClasses(['tab-pane fade', 'show active' => ! $historyFiltersActive]); ?>" id="ready-withdrawals">
            <div class="withdrawal-pane-title">
                <div>
                    <span>Matured for withdrawal</span>
                    <h3>Accounts that can be paid out now</h3>
                    <p>Flexible accounts are available immediately. Locked savings appear here once maturity or completion is reached.</p>
                </div>
            </div>
            <?php if($readyAccounts->isNotEmpty()): ?>
                <div class="table-responsive withdrawal-table-shell">
                    <table class="table align-middle mb-0 withdrawal-table">
                        <thead>
                            <tr>
                                <th>Member</th>
                                <th>Account</th>
                                <th>Product</th>
                                <th>Available since</th>
                                <th>Reason</th>
                                <th class="text-end">Balance</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $readyAccounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td>
                                        <strong><?php echo e($account->customer?->fullName() ?? 'Member'); ?></strong>
                                        <span><?php echo e($account->customer?->account_number ?? $account->customer?->phone); ?></span>
                                    </td>
                                    <td>
                                        <strong><?php echo e($account->account_number); ?></strong>
                                        <span><?php echo e(ucfirst($account->status)); ?></span>
                                    </td>
                                    <td>
                                        <strong><?php echo e($account->product_name); ?></strong>
                                        <span><?php echo e($account->product?->withdrawalPolicyLabel() ?? 'Flexible withdrawals'); ?></span>
                                    </td>
                                    <td><?php echo e(app_date($account->withdrawal_available_on, 'Any time')); ?></td>
                                    <td><span class="withdrawal-status-pill ready"><?php echo e($account->withdrawal_reason); ?></span></td>
                                    <td class="text-end fw-bold"><?php echo e($compactMoney($account->balance)); ?></td>
                                    <td class="text-end">
                                        <div class="dropdown">
                                            <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Actions</button>
                                            <ul class="dropdown-menu dropdown-menu-end">
                                                <li><a class="dropdown-item" href="<?php echo e(route('tenant.savings.show', $account)); ?>"><i class="bi bi-folder2-open me-2"></i>Open</a></li>
                                                <?php if($canManageSavings): ?>
                                                    <li><button class="dropdown-item text-primary" type="button" data-bs-toggle="modal" data-bs-target="#withdrawalModal<?php echo e($account->id); ?>"><i class="bi bi-cash me-2"></i>Withdraw</button></li>
                                                <?php endif; ?>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <div class="withdrawal-empty">
                    <i class="bi bi-wallet2"></i>
                    <h4>No account is matured for withdrawal yet</h4>
                    <p>When target, thrift / Isusu, cooperative, or flexible savings become available, they will appear here.</p>
                </div>
            <?php endif; ?>
        </div>

        <div class="tab-pane fade" id="upcoming-maturity">
            <div class="withdrawal-pane-title">
                <div>
                    <span>Upcoming maturity</span>
                    <h3>Accounts approaching payout date</h3>
                    <p>These savings accounts are expected to become withdrawable within the next 30 days.</p>
                </div>
            </div>
            <?php if($upcomingMaturities->isNotEmpty()): ?>
                <div class="table-responsive withdrawal-table-shell">
                    <table class="table align-middle mb-0 withdrawal-table">
                        <thead>
                            <tr>
                                <th>Member</th>
                                <th>Account</th>
                                <th>Product</th>
                                <th>Maturity date</th>
                                <th>Days left</th>
                                <th class="text-end">Expected balance</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $upcomingMaturities; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td>
                                        <strong><?php echo e($account->customer?->fullName() ?? 'Member'); ?></strong>
                                        <span><?php echo e($account->customer?->account_number ?? $account->customer?->phone); ?></span>
                                    </td>
                                    <td>
                                        <strong><?php echo e($account->account_number); ?></strong>
                                        <span><?php echo e(ucfirst($account->status)); ?></span>
                                    </td>
                                    <td>
                                        <strong><?php echo e($account->product_name); ?></strong>
                                        <span><?php echo e($account->product?->withdrawalPolicyLabel() ?? 'Flexible withdrawals'); ?></span>
                                    </td>
                                    <td><?php echo e(app_date($account->withdrawal_available_on)); ?></td>
                                    <td><span class="withdrawal-status-pill upcoming"><?php echo e($account->withdrawal_available_on?->diffForHumans(['parts' => 2, 'short' => true])); ?></span></td>
                                    <td class="text-end fw-bold"><?php echo e($compactMoney($account->balance)); ?></td>
                                    <td class="text-end">
                                        <div class="dropdown">
                                            <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Actions</button>
                                            <ul class="dropdown-menu dropdown-menu-end">
                                                <li><a class="dropdown-item" href="<?php echo e(route('tenant.savings.show', $account)); ?>"><i class="bi bi-folder2-open me-2"></i>Open</a></li>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <div class="withdrawal-empty">
                    <i class="bi bi-calendar2-week"></i>
                    <h4>No upcoming maturity in the next 30 days</h4>
                    <p>Locked savings approaching maturity will be listed here automatically.</p>
                </div>
            <?php endif; ?>
        </div>

        <?php if($canUseWithdrawalApproval): ?>
            <div class="tab-pane fade" id="pending-withdrawals">
                <div class="withdrawal-pane-title align-items-start">
                    <div>
                        <span>Pending approval</span>
                        <h3>Withdrawal requests waiting for review</h3>
                        <p>Approve only after confirming the member, savings account, amount, and payout method.</p>
                    </div>
                    <strong class="text-warning"><?php echo e($compactMoney($withdrawalStats['pending_amount'] ?? 0)); ?></strong>
                </div>
                <?php if($pendingWithdrawals->isNotEmpty()): ?>
                    <div class="table-responsive withdrawal-table-shell">
                        <table class="table align-middle mb-0 withdrawal-table">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Member</th>
                                    <th>Account</th>
                                    <th>Initiated by</th>
                                    <th class="text-end">Amount</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $pendingWithdrawals; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $withdrawal): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php
                                        $withdrawalFee = (float) ($withdrawal->processing_fee_amount ?? 0);
                                        $withdrawalNet = (float) ($withdrawal->net_amount ?? 0);
                                        if ($withdrawalNet <= 0 && $withdrawalFee < (float) $withdrawal->amount) {
                                            $withdrawalNet = (float) $withdrawal->amount - $withdrawalFee;
                                        }
                                    ?>
                                    <tr>
                                        <td>
                                            <strong><?php echo e(app_date($withdrawal->transacted_at) ?? '-'); ?></strong>
                                            <span><?php echo e($withdrawal->method); ?></span>
                                        </td>
                                        <td>
                                            <strong><?php echo e($withdrawal->customer?->fullName() ?? 'Member'); ?></strong>
                                            <span><?php echo e($withdrawal->customer?->account_number ?? $withdrawal->customer?->phone); ?></span>
                                        </td>
                                        <td>
                                            <strong><?php echo e($withdrawal->account?->account_number ?? '-'); ?></strong>
                                            <span><?php echo e($withdrawal->account?->product_name ?? 'Savings'); ?></span>
                                        </td>
                                        <td><?php echo e($withdrawal->creator?->name ?? '-'); ?></td>
                                        <td class="text-end">
                                            <strong><?php echo e($compactMoney($withdrawal->amount)); ?></strong>
                                            <?php if($withdrawalFee > 0): ?>
                                                <span>Fee <?php echo e($compactMoney($withdrawalFee)); ?> | Net <?php echo e($compactMoney($withdrawalNet)); ?></span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-end">
                                            <div class="dropdown">
                                                <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Actions</button>
                                                <ul class="dropdown-menu dropdown-menu-end">
                                                    <li><button class="dropdown-item" type="button" data-bs-toggle="modal" data-bs-target="#pendingWithdrawal<?php echo e($withdrawal->id); ?>"><i class="bi bi-eye me-2"></i>View</button></li>
                                                    <li>
                                                        <form method="POST" action="<?php echo e(route('tenant.savings.transactions.approve', $withdrawal)); ?>">
                                                            <?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="modal" data-bs-target="#rejectWithdrawal<?php echo e($withdrawal->id); ?>"><i class="bi bi-x-circle me-2"></i>Reject</button></li>
                                                </ul>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="withdrawal-empty">
                        <i class="bi bi-check2-circle"></i>
                        <h4>No pending withdrawal approval</h4>
                        <p>Withdrawal requests waiting for approval will appear here.</p>
                    </div>
                <?php endif; ?>
            </div>

            <div class="tab-pane fade" id="member-withdrawal-requests">
                <div class="withdrawal-pane-title align-items-start">
                    <div>
                        <span>Member request</span>
                        <h3>Member-initiated withdrawal approvals</h3>
                        <p>Withdrawal requests started from member self-service are kept separate from staff-entered withdrawals.</p>
                    </div>
                    <strong class="text-info"><?php echo e($compactMoney($withdrawalStats['member_request_amount'] ?? 0)); ?></strong>
                </div>
                <?php if($memberWithdrawalRequests->isNotEmpty()): ?>
                    <div class="table-responsive withdrawal-table-shell">
                        <table class="table align-middle mb-0 withdrawal-table">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Member</th>
                                    <th>Account</th>
                                    <th class="text-end">Amount</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $memberWithdrawalRequests; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $withdrawal): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td>
                                            <strong><?php echo e(app_date($withdrawal->transacted_at) ?? '-'); ?></strong>
                                            <span><?php echo e($withdrawal->method); ?></span>
                                        </td>
                                        <td>
                                            <strong><?php echo e($withdrawal->customer?->fullName() ?? 'Member'); ?></strong>
                                            <span><?php echo e($withdrawal->customer?->account_number ?? $withdrawal->customer?->phone); ?></span>
                                        </td>
                                        <td>
                                            <strong><?php echo e($withdrawal->account?->account_number ?? '-'); ?></strong>
                                            <span><?php echo e($withdrawal->account?->product_name ?? 'Savings'); ?></span>
                                        </td>
                                        <td class="text-end"><strong><?php echo e($compactMoney($withdrawal->amount)); ?></strong></td>
                                        <td class="text-end">
                                            <div class="dropdown">
                                                <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Actions</button>
                                                <ul class="dropdown-menu dropdown-menu-end">
                                                    <li>
                                                        <form method="POST" action="<?php echo e(route('tenant.savings.transactions.approve', $withdrawal)); ?>">
                                                            <?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="modal" data-bs-target="#rejectMemberWithdrawal<?php echo e($withdrawal->id); ?>"><i class="bi bi-x-circle me-2"></i>Reject</button></li>
                                                </ul>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                    <?php $__currentLoopData = $memberWithdrawalRequests; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $withdrawal): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="modal fade" id="rejectMemberWithdrawal<?php echo e($withdrawal->id); ?>" tabindex="-1" aria-labelledby="rejectMemberWithdrawal<?php echo e($withdrawal->id); ?>Label" aria-hidden="true">
                            <div class="modal-dialog modal-dialog-centered">
                                <div class="modal-content border-0 shadow">
                                    <form method="POST" action="<?php echo e(route('tenant.savings.transactions.reject', $withdrawal)); ?>">
                                        <?php echo csrf_field(); ?>
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="rejectMemberWithdrawal<?php echo e($withdrawal->id); ?>Label">Reject member withdrawal</h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">
                                            <label class="form-label">Rejection reason</label>
                                            <textarea name="rejected_reason" class="form-control" rows="3" required></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">Reject withdrawal</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <?php else: ?>
                    <div class="withdrawal-empty">
                        <i class="bi bi-person-check"></i>
                        <h4>No member withdrawal request</h4>
                        <p>Member withdrawal requests waiting for approval will appear here.</p>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <div class="<?php echo \Illuminate\Support\Arr::toCssClasses(['tab-pane fade', 'show active' => $historyFiltersActive]); ?>" id="withdrawal-history">
            <div class="withdrawal-pane-title align-items-start">
                <div>
                    <span>Withdrawal history</span>
                    <h3>Posted withdrawals</h3>
                    <p>Search withdrawal records by member, account number, product, reference, method, or note.</p>
                </div>
            </div>
            <form method="GET" class="withdrawal-filter row g-2 align-items-end">
                <div class="col-lg-3">
                    <label class="form-label">Search</label>
                    <input type="search" name="q" class="form-control" value="<?php echo e($search); ?>" placeholder="Member, account, reference, method">
                </div>
                <div class="col-md-3 col-lg-2">
                    <label class="form-label">Initiated by</label>
                    <select name="initiated_by" class="form-select">
                        <option value="">All</option>
                        <?php $__currentLoopData = $usersForFilter; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $filterUser): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($filterUser->id); ?>" <?php if((int) $initiatedBy === $filterUser->id): echo 'selected'; endif; ?>><?php echo e($filterUser->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-3 col-lg-2">
                    <label class="form-label">Approved by</label>
                    <select name="approved_by" class="form-select">
                        <option value="">All</option>
                        <?php $__currentLoopData = $usersForFilter; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $filterUser): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($filterUser->id); ?>" <?php if((int) $approvedBy === $filterUser->id): echo 'selected'; endif; ?>><?php echo e($filterUser->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-3 col-lg-2">
                    <label class="form-label">From</label>
                    <input type="date" name="from" class="form-control" value="<?php echo e($from); ?>">
                </div>
                <div class="col-md-3 col-lg-2">
                    <label class="form-label">To</label>
                    <input type="date" name="to" class="form-control" value="<?php echo e($to); ?>">
                </div>
                <div class="col-md-6 col-lg-3 d-flex gap-2">
                    <button class="btn btn-primary flex-fill" type="submit"><i class="bi bi-search me-1"></i> Filter</button>
                    <a class="btn btn-outline-secondary" href="<?php echo e(route('tenant.withdrawals.index')); ?>">Clear</a>
                </div>
            </form>
            <?php if($withdrawals->isNotEmpty()): ?>
                <div class="table-responsive withdrawal-table-shell mt-3">
                    <table class="table align-middle mb-0 withdrawal-table">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Member</th>
                                <th>Account</th>
                                <th>Initiated by</th>
                                <th class="text-end">Amount</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $withdrawals; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $withdrawal): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    $withdrawalFee = (float) ($withdrawal->processing_fee_amount ?? 0);
                                    $withdrawalNet = (float) ($withdrawal->net_amount ?? 0);
                                    if ($withdrawalNet <= 0 && $withdrawalFee < (float) $withdrawal->amount) {
                                        $withdrawalNet = (float) $withdrawal->amount - $withdrawalFee;
                                    }
                                ?>
                                <tr>
                                    <td>
                                        <strong><?php echo e(app_date($withdrawal->transacted_at) ?? '-'); ?></strong>
                                        <span><?php echo e($withdrawal->method); ?></span>
                                    </td>
                                    <td>
                                        <strong><?php echo e($withdrawal->customer?->fullName() ?? 'Member'); ?></strong>
                                        <span><?php echo e($withdrawal->customer?->account_number ?? $withdrawal->customer?->phone); ?></span>
                                    </td>
                                    <td>
                                        <strong><?php echo e($withdrawal->account?->account_number ?? '-'); ?></strong>
                                        <span><?php echo e($withdrawal->account?->product_name ?? 'Savings'); ?></span>
                                    </td>
                                    <td><?php echo e($withdrawal->creator?->name ?? '-'); ?></td>
                                    <td class="text-end">
                                        <strong><?php echo e($compactMoney($withdrawal->amount)); ?></strong>
                                        <?php if($withdrawalFee > 0): ?>
                                            <span>Fee <?php echo e($compactMoney($withdrawalFee)); ?> | Net <?php echo e($compactMoney($withdrawalNet)); ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="text-end">
                                        <div class="dropdown">
                                            <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Actions</button>
                                            <ul class="dropdown-menu dropdown-menu-end">
                                                <li><a class="dropdown-item" href="<?php echo e(route('tenant.savings.transactions.receipt', $withdrawal)); ?>"><i class="bi bi-receipt me-2"></i>Receipt</a></li>
                                                <?php if($withdrawal->account): ?>
                                                    <li><a class="dropdown-item" href="<?php echo e(route('tenant.savings.show', $withdrawal->account)); ?>"><i class="bi bi-folder2-open me-2"></i>Open account</a></li>
                                                <?php endif; ?>
                                                <?php if($canApproveSavings): ?>
                                                    <li><button class="dropdown-item text-danger" type="button" data-bs-toggle="modal" data-bs-target="#cancelWithdrawal<?php echo e($withdrawal->id); ?>"><i class="bi bi-slash-circle me-2"></i>Cancel</button></li>
                                                <?php endif; ?>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                    </table>
                </div>
                <div class="mt-3"><?php echo e($withdrawals->links()); ?></div>
            <?php else: ?>
                <div class="withdrawal-empty">
                    <i class="bi bi-clock-history"></i>
                    <h4>No withdrawal record found</h4>
                    <p>Posted withdrawals will appear here once they are recorded.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php if($canUseWithdrawalApproval): ?>
    <?php $__currentLoopData = $pendingWithdrawals; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $withdrawal): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <?php
            $pendingWithdrawalFee = (float) ($withdrawal->processing_fee_amount ?? 0);
            $pendingWithdrawalNet = (float) ($withdrawal->net_amount ?? 0);
            if ($pendingWithdrawalNet <= 0 && $pendingWithdrawalFee < (float) $withdrawal->amount) {
                $pendingWithdrawalNet = (float) $withdrawal->amount - $pendingWithdrawalFee;
            }
        ?>
        <div class="modal fade" id="pendingWithdrawal<?php echo e($withdrawal->id); ?>" tabindex="-1" aria-labelledby="pendingWithdrawal<?php echo e($withdrawal->id); ?>Label" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content border-0 shadow">
                    <div class="modal-header">
                        <div>
                            <h5 class="modal-title" id="pendingWithdrawal<?php echo e($withdrawal->id); ?>Label">Pending withdrawal details</h5>
                            <p class="text-muted small mb-0"><?php echo e($withdrawal->customer?->fullName()); ?> | <?php echo e($withdrawal->account?->account_number); ?></p>
                        </div>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <dl class="row small mb-0">
                            <dt class="col-5">Reference</dt><dd class="col-7"><?php echo e($withdrawal->reference); ?></dd>
                            <dt class="col-5">Member</dt><dd class="col-7"><?php echo e($withdrawal->customer?->fullName() ?? '-'); ?></dd>
                            <dt class="col-5">Savings account</dt><dd class="col-7"><?php echo e($withdrawal->account?->account_number ?? '-'); ?></dd>
                            <dt class="col-5">Product</dt><dd class="col-7"><?php echo e($withdrawal->account?->product_name ?? '-'); ?></dd>
                            <dt class="col-5">Amount</dt><dd class="col-7"><?php echo e($money($withdrawal->amount)); ?></dd>
                            <dt class="col-5">Processing fee</dt><dd class="col-7"><?php echo e($money($pendingWithdrawalFee)); ?></dd>
                            <dt class="col-5">Net payout</dt><dd class="col-7"><?php echo e($money($pendingWithdrawalNet)); ?></dd>
                            <dt class="col-5">Method</dt><dd class="col-7"><?php echo e(str($withdrawal->method)->headline()); ?></dd>
                            <dt class="col-5">Initiated by</dt><dd class="col-7"><?php echo e($withdrawal->creator?->name ?? '-'); ?></dd>
                            <dt class="col-5">Narration</dt><dd class="col-7"><?php echo e($withdrawal->description ?: '-'); ?></dd>
                        </dl>
                    </div>
                    <div class="modal-footer">
                        <form method="POST" action="<?php echo e(route('tenant.savings.transactions.approve', $withdrawal)); ?>">
                            <?php echo csrf_field(); ?>
                            <button class="btn btn-success"><i class="bi bi-check2-circle me-1"></i> Approve</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="rejectWithdrawal<?php echo e($withdrawal->id); ?>" tabindex="-1" aria-labelledby="rejectWithdrawal<?php echo e($withdrawal->id); ?>Label" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content border-0 shadow">
                    <form method="POST" action="<?php echo e(route('tenant.savings.transactions.reject', $withdrawal)); ?>">
                        <?php echo csrf_field(); ?>
                        <div class="modal-header">
                            <div>
                                <h5 class="modal-title" id="rejectWithdrawal<?php echo e($withdrawal->id); ?>Label">Reject withdrawal</h5>
                                <p class="text-muted small mb-0"><?php echo e($withdrawal->customer?->fullName()); ?> | <?php echo e($money($withdrawal->amount)); ?></p>
                            </div>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <label class="form-label">Rejection reason</label>
                            <textarea name="rejected_reason" class="form-control" rows="3" required></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">Reject withdrawal</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php endif; ?>

<?php if($canApproveSavings): ?>
    <?php $__currentLoopData = $withdrawals; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $withdrawal): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="modal fade" id="cancelWithdrawal<?php echo e($withdrawal->id); ?>" tabindex="-1" aria-labelledby="cancelWithdrawal<?php echo e($withdrawal->id); ?>Label" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content border-0 shadow">
                    <form method="POST" action="<?php echo e(route('tenant.savings.transactions.cancel', $withdrawal)); ?>">
                        <?php echo csrf_field(); ?>
                        <div class="modal-header">
                            <div>
                                <h5 class="modal-title" id="cancelWithdrawal<?php echo e($withdrawal->id); ?>Label">Cancel withdrawal</h5>
                                <p class="text-muted small mb-0"><?php echo e($withdrawal->reference); ?> | <?php echo e($money($withdrawal->amount)); ?></p>
                            </div>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <label class="form-label">Cancellation reason</label>
                            <textarea name="canceled_reason" class="form-control" rows="3" required></textarea>
                            <div class="form-text">The withdrawal will be reversed into the member savings balance.</div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
                            <button class="btn btn-danger">Cancel withdrawal</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php endif; ?>

<?php if($canManageSavings): ?>
    <?php $__currentLoopData = $readyAccounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="modal fade" id="withdrawalModal<?php echo e($account->id); ?>" tabindex="-1" aria-labelledby="withdrawalModal<?php echo e($account->id); ?>Label" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content border-0 shadow">
                    <form method="POST" action="<?php echo e(route('tenant.savings.transactions.store', $account)); ?>">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="type" value="withdrawal">
                        <div class="modal-header">
                            <div>
                                <h5 class="modal-title" id="withdrawalModal<?php echo e($account->id); ?>Label">Record withdrawal</h5>
                                <p class="text-muted small mb-0"><?php echo e($account->customer?->fullName()); ?> | <?php echo e($account->account_number); ?></p>
                            </div>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="withdrawal-modal-summary">
                                <span>Available balance</span>
                                <strong><?php echo e($money($account->balance)); ?></strong>
                                <small><?php echo e($account->withdrawal_reason); ?></small>
                            </div>
                            <?php if((float) $account->withdrawal_processing_fee_due > 0): ?>
                                <div class="alert alert-light border small mt-3 mb-0">
                                    Processing fee: <strong><?php echo e($money($account->withdrawal_processing_fee_due)); ?></strong>.
                                    Full balance payout after fee: <strong><?php echo e($money($account->withdrawal_full_net_payout)); ?></strong>.
                                </div>
                            <?php endif; ?>
                            <div class="row g-3 mt-1">
                                <div class="col-md-6">
                                    <label class="form-label">Amount</label>
                                    <input type="number" name="amount" class="form-control" step="0.01" min="0.01" max="<?php echo e($account->balance); ?>" value="<?php echo e(old('amount', $account->balance)); ?>" required>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Method</label>
                                    <select name="method" class="form-select" required>
                                        <option value="cash">Cash</option>
                                        <option value="bank_transfer">Bank transfer</option>
                                        <option value="pos">POS</option>
                                        <option value="mobile_money">Mobile money</option>
                                    </select>
                                </div>
                                <div class="col-12">
                                    <label class="form-label">Note</label>
                                    <textarea name="description" class="form-control" rows="3" placeholder="Optional withdrawal note"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary">Submit for approval</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php endif; ?>

<style>
.withdrawal-hero {
    display: grid;
    grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr);
    gap: 1rem;
    align-items: stretch;
}
.withdrawal-hero-copy,
.withdrawal-metric,
.withdrawal-workspace {
    border-radius: 18px;
}
.withdrawal-hero-copy {
    padding: 1.5rem;
    background: linear-gradient(135deg, #052e2b, #0f766e);
    color: #fff;
    min-height: 250px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
}
.withdrawal-hero-copy span,
.withdrawal-pane-title span {
    text-transform: uppercase;
    letter-spacing: .08em;
    font-size: .75rem;
    font-weight: 700;
}
.withdrawal-hero-copy h2 {
    max-width: 680px;
    margin: .45rem 0;
    font-weight: 800;
}
.withdrawal-hero-copy p {
    max-width: 680px;
    margin: 0;
    color: rgba(255, 255, 255, .8);
}
.withdrawal-metric-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: .85rem;
}
.withdrawal-metric {
    padding: 1rem;
    background: #fff;
    border: 1px solid #e5e7eb;
    box-shadow: 0 14px 30px rgba(15, 23, 42, .08);
}
.withdrawal-metric i {
    display: inline-flex;
    width: 2.4rem;
    height: 2.4rem;
    align-items: center;
    justify-content: center;
    border-radius: 14px;
    margin-bottom: .8rem;
    color: #fff;
}
.withdrawal-metric span,
.withdrawal-metric small,
.withdrawal-table td span {
    display: block;
    color: #64748b;
    font-size: .82rem;
}
.withdrawal-metric strong {
    display: block;
    font-size: clamp(1.35rem, 2vw, 1.95rem);
    color: #0f172a;
}
.withdrawal-metric.rose i { background: #e11d48; }
.withdrawal-metric.green i { background: #16a34a; }
.withdrawal-metric.amber i { background: #d97706; }
.withdrawal-metric.blue i { background: #2563eb; }
.withdrawal-tabbar {
    padding: 1rem 1rem 0;
    border-bottom: 1px solid #eef2f7;
    overflow-x: auto;
}
.withdrawal-tabs {
    flex-wrap: nowrap;
    gap: .5rem;
}
.withdrawal-tabs .nav-link {
    white-space: nowrap;
    border-radius: 999px;
    color: #475569;
}
.withdrawal-tabs .nav-link.active {
    background: #0f766e;
}
.withdrawal-pane-title {
    padding: 1.25rem;
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    border-bottom: 1px solid #eef2f7;
}
.withdrawal-pane-title span {
    color: #0f766e;
}
.withdrawal-pane-title h3 {
    margin: .25rem 0;
    font-size: 1.25rem;
    font-weight: 800;
}
.withdrawal-pane-title p {
    margin: 0;
    color: #64748b;
}
.withdrawal-table-shell {
    border-top: 0;
}
.withdrawal-table th {
    background: #f8fafc;
    color: #475569;
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .04em;
}
.withdrawal-table td,
.withdrawal-table th {
    padding: 1rem;
    vertical-align: middle;
}
.withdrawal-table td strong {
    display: block;
    color: #0f172a;
}
.withdrawal-status-pill {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: .35rem .65rem;
    font-size: .78rem;
    font-weight: 700;
}
.withdrawal-status-pill.ready {
    background: #dcfce7;
    color: #166534;
}
.withdrawal-status-pill.upcoming {
    background: #fef3c7;
    color: #92400e;
}
.withdrawal-empty {
    padding: 3rem 1.25rem;
    text-align: center;
}
.withdrawal-empty i {
    display: inline-flex;
    width: 3.25rem;
    height: 3.25rem;
    align-items: center;
    justify-content: center;
    border-radius: 18px;
    background: #ecfeff;
    color: #0f766e;
    font-size: 1.4rem;
    margin-bottom: 1rem;
}
.withdrawal-empty h4 {
    font-size: 1.1rem;
    font-weight: 800;
    margin-bottom: .35rem;
}
.withdrawal-empty p {
    color: #64748b;
    margin: 0;
}
.withdrawal-filter {
    padding: 0 1.25rem 1.25rem;
}
.withdrawal-modal-summary {
    border: 1px solid #dbeafe;
    background: #eff6ff;
    border-radius: 14px;
    padding: 1rem;
}
.withdrawal-modal-summary span,
.withdrawal-modal-summary small {
    display: block;
    color: #475569;
}
.withdrawal-modal-summary strong {
    display: block;
    font-size: 1.45rem;
    color: #0f172a;
}
@media (max-width: 991.98px) {
    .withdrawal-hero {
        grid-template-columns: 1fr;
    }
}
@media (max-width: 575.98px) {
    .withdrawal-metric-grid {
        grid-template-columns: 1fr;
    }
    .withdrawal-hero-copy {
        min-height: auto;
    }
}
</style>
<?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\withdrawals\index.blade.php ENDPATH**/ ?>