<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body text-center">
                <?php if ($this->session->flashdata('success')): ?>
                    <div class="alert alert-success"><?= $this->session->flashdata('success') ?></div>
                <?php endif; ?>
                <?php if ($this->session->flashdata('error')): ?>
                    <div class="alert alert-danger"><?= $this->session->flashdata('error') ?></div>
                <?php endif; ?>
                
                <!-- Single Withdrawals -->
                <h4 class="card-title mb-4">Single Withdrawals</h4>
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Username</th>
                                <th>Status</th>
                                <th>Method</th>
                                <th>IP Address</th>
                                <th>Amount</th>
                                <th>Wallet</th>
                                <th>Date</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($single_withdrawals as $withdrawal): ?>
                                <tr>
                                    <td><a href="<?= site_url('admin/users/details/' . $withdrawal['user_id']) ?>"><?= $withdrawal['username'] ?></a></td>
                                    <td><?= $withdrawal['status'] ?></td>
                                    <td><?= $withdrawal['method_name'] ?></td>
                                    <td><?= $withdrawal['ip_address'] ?></td>
                                    <td><?= $withdrawal['amount'] ?></td>
                                    <td><?= $withdrawal['wallet'] ?></td>
                                    <td><?= timespan($withdrawal['created_at'], time(), 2) ?> ago</td>
                                    <td>
                                        <a href="<?= site_url('admin/withdraw/accept/' . $withdrawal['id']) ?>" class="btn btn-success btn-sm">Accept</a>
                                        <a href="<?= site_url('admin/withdraw/deny/' . $withdrawal['id']) ?>" class="btn btn-danger btn-sm">Deny</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                
                <!-- Multiple Withdrawals (Same Wallet) -->
                <h4 class="card-title mb-4 mt-5">Multiple Withdrawals (Same Wallet)</h4>
                <?php foreach ($grouped_withdrawals as $wallet => $withdrawals): ?>
                    <?php 
                    $total_amount = array_sum(array_column($withdrawals, 'amount'));
                    $user_ids = array_unique(array_column($withdrawals, 'user_id'));
                    ?>
                    
                    <div class="wallet-group mb-4 border rounded">
                        <div class="wallet-header bg-light p-3 d-flex justify-content-between align-items-center">
                            <h5 class="mb-0 font-weight-bold">Wallet: <?= htmlspecialchars($wallet) ?></h5>
                            <div class="d-flex align-items-center">
                                <div class="total-info-box bg-white p-3 rounded border mr-4" style="min-width: 220px;">
                                    <div class="d-flex justify-content-between mb-2">
                                        <span class="font-weight-bold">Total Amount:</span>
                                        <span class="text-success font-weight-bold"><?= number_format($total_amount, 2) ?></span>
                                    </div>
                                    <div class="d-flex justify-content-between mb-2">
                                        <span class="font-weight-bold">Unique Users:</span>
                                        <span class="text-primary"><?= count($user_ids) ?></span>
                                    </div>
                                    <div class="d-flex justify-content-between">
                                        <span class="font-weight-bold">Withdrawals:</span>
                                        <span class="text-info"><?= count($withdrawals) ?></span>
                                    </div>
                                </div>
                                <a href="<?= site_url('admin/withdraw/accept_all/' . rawurlencode($wallet)) ?>" 
                                   class="btn btn-success btn-lg px-4 py-2"
                                   onclick="return confirm('Are you sure you want to approve ALL <?= count($withdrawals) ?> withdrawals for this wallet?')">
                                    Accept All
                                </a>
                            </div>
                        </div>
                        
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>Username</th>
                                        <th>Status</th>
                                        <th>Method</th>
                                        <th>IP Address</th>
                                        <th>Amount</th>
                                        <th>Date</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($withdrawals as $withdrawal): ?>
                                        <tr>
                                            <td><a href="<?= site_url('admin/users/details/' . $withdrawal['user_id']) ?>"><?= $withdrawal['username'] ?></a></td>
                                            <td><?= $withdrawal['status'] ?></td>
                                            <td><?= $withdrawal['method_name'] ?></td>
                                            <td><?= $withdrawal['ip_address'] ?></td>
                                            <td><?= $withdrawal['amount'] ?></td>
                                            <td><?= timespan($withdrawal['created_at'], time(), 2) ?> ago</td>
                                            <td>
                                                <a href="<?= site_url('admin/withdraw/accept/' . $withdrawal['id']) ?>" class="btn btn-success btn-sm">Accept</a>
                                                <a href="<?= site_url('admin/withdraw/deny/' . $withdrawal['id']) ?>" class="btn btn-danger btn-sm">Deny</a>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>