<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body text-center">
                <h4 class="card-title mb-4">Pending Withdrawals</h4>
                <?php
                if (isset($_SESSION['message'])) {
                    echo $_SESSION['message'];
                }
                ?>
                
                <!-- First table: Grouped withdrawals with Accept All button -->
                <div class="card mb-4">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">Multiple Withdrawals (Same Wallet)</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th scope="col">Username</th>
                                        <th scope="col">Status</th>
                                        <th scope="col">Method</th>
                                        <th scope="col">Ip Address</th>
                                        <th scope="col">Amount</th>
                                        <th scope="col">Wallet</th>
                                        <th scope="col">Date</th>
                                        <th scope="col">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $groupedWithdrawals = [];
                                    foreach ($pendingWithdrawal as $value) {
                                        $groupedWithdrawals[$value['wallet']][] = $value;
                                    }
                                    
                                    foreach ($groupedWithdrawals as $wallet => $withdrawals) {
                                        // Skip wallets with only one withdrawal - we'll show those in the second table
                                        if (count($withdrawals) <= 1) {
                                            continue;
                                        }
                                        
                                        $totalAmount = array_sum(array_column($withdrawals, 'amount'));
                                        foreach ($withdrawals as $withdrawal) {
                                    ?>
                                        <tr>
                                            <th scope="row"><a target="_blank" href="<?= site_url('/admin/users/details/' . $withdrawal['user_id']) ?>"><?= $withdrawal['username'] ?></a></th>
                                            <td><?= $withdrawal['user_status'] ?></td>
                                            <td><?= $withdrawal['name'] ?></td>
                                            <td><?= $withdrawal['ip_address'] ?></td>
                                            <td><?= $withdrawal['amount'] ?></td>
                                            <td><?= $withdrawal['wallet'] ?></td>
                                            <td><?= timespan($withdrawal['claim_time'], time(), 2) ?> ago</td>
                                            <td>
                                                <a href="<?= site_url("/admin/withdraw/accept/" . $withdrawal['id']) ?>" class="btn btn-success btn-sm" style="margin-right: 5px;">Accept</a>
                                                <a href="<?= site_url("/admin/withdraw/deny/" . $withdrawal['id']) ?>" class="btn btn-danger btn-sm">Deny</a>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                        <tr class="table-info">
                                            <td colspan="4" class="text-right"><strong>Wallet: <?= $wallet ?></strong></td>
                                            <td colspan="3" class="text-right"><strong>Total Amount: <?= $totalAmount ?></strong></td>
                                            <td>
                                                <form method="POST" action="<?= site_url('/admin/withdraw/accept_all') ?>">
                                                    <?php foreach ($withdrawals as $withdrawal): ?>
                                                        <input type="hidden" name="ids[]" value="<?= $withdrawal['id'] ?>">
                                                    <?php endforeach; ?>
                                                    <button type="submit" class="btn btn-primary">Accept All</button>
                                                </form>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <!-- Second table: Single withdrawals -->
                <div class="card">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">Single Withdrawals</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th scope="col">Username</th>
                                        <th scope="col">Status</th>
                                        <th scope="col">Method</th>
                                        <th scope="col">Ip Address</th>
                                        <th scope="col">Amount</th>
                                        <th scope="col">Wallet</th>
                                        <th scope="col">Date</th>
                                        <th scope="col">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    foreach ($groupedWithdrawals as $wallet => $withdrawals) {
                                        // Only display wallets with exactly one withdrawal
                                        if (count($withdrawals) > 1) {
                                            continue;
                                        }
                                        
                                        $withdrawal = $withdrawals[0]; // Get the single withdrawal
                                    ?>
                                        <tr>
                                            <th scope="row"><a target="_blank" href="<?= site_url('/admin/users/details/' . $withdrawal['user_id']) ?>"><?= $withdrawal['username'] ?></a></th>
                                            <td><?= $withdrawal['user_status'] ?></td>
                                            <td><?= $withdrawal['name'] ?></td>
                                            <td><?= $withdrawal['ip_address'] ?></td>
                                            <td><?= $withdrawal['amount'] ?></td>
                                            <td><?= $withdrawal['wallet'] ?></td>
                                            <td><?= timespan($withdrawal['claim_time'], time(), 2) ?> ago</td>
                                            <td>
                                                <a href="<?= site_url("/admin/withdraw/accept/" . $withdrawal['id']) ?>" class="btn btn-success btn-sm" style="margin-right: 5px;">Accept</a>
                                                <a href="<?= site_url("/admin/withdraw/deny/" . $withdrawal['id']) ?>" class="btn btn-danger btn-sm">Deny</a>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>