<?php include app()->basePath('app/Views/partials/header.php'); ?>

<section class="panel">
    <div class="page-head">
        <div>
            <p class="eyebrow">Financeiro</p>
            <h1>Levantamentos</h1>
            <p>Aprove, rejeite ou marque como pagos os pedidos de saque de instrutores e instituicoes.</p>
        </div>
    </div>

    <form method="get" action="/admin/withdrawals" class="filterbar simple">
        <input name="search" placeholder="Pesquisar referencia, vendedor ou destino" value="<?= e($filters['search']) ?>">
        <select name="status">
            <option value="">Todos estados</option>
            <option value="requested" <?= $filters['status'] === 'requested' ? 'selected' : '' ?>>Solicitados</option>
            <option value="approved" <?= $filters['status'] === 'approved' ? 'selected' : '' ?>>Aprovados</option>
            <option value="rejected" <?= $filters['status'] === 'rejected' ? 'selected' : '' ?>>Rejeitados</option>
            <option value="paid" <?= $filters['status'] === 'paid' ? 'selected' : '' ?>>Pagos</option>
        </select>
        <button class="button" type="submit">Filtrar</button>
    </form>

    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Pedido</th>
                    <th>Vendedor</th>
                    <th>Valor</th>
                    <th>Destino</th>
                    <th>Estado</th>
                    <th>Accoes</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$withdrawals): ?>
                    <tr><td colspan="6">Nenhum pedido de levantamento encontrado.</td></tr>
                <?php endif; ?>
                <?php foreach ($withdrawals as $withdrawal): ?>
                    <tr>
                        <td><strong><?= e($withdrawal['reference']) ?></strong><span class="muted"><?= e(format_date($withdrawal['created_at'])) ?></span></td>
                        <td><?= e($withdrawal['seller_name']) ?><span class="muted"><?= e($withdrawal['seller_email']) ?></span></td>
                        <td><?= e(money_mzn($withdrawal['amount'])) ?></td>
                        <td><?= e($withdrawalsService->methodLabel($withdrawal['method'])) ?><span class="muted"><?= e($withdrawal['destination_name']) ?> - <?= e($withdrawal['destination_number']) ?></span></td>
                        <td><span class="badge <?= e($withdrawal['status']) ?>"><?= e($withdrawalsService->statusLabel($withdrawal['status'])) ?></span></td>
                        <td class="row-actions">
                            <?php if (($withdrawal['status'] ?? '') !== 'paid'): ?>
                                <?php foreach (['approved' => 'Aprovar', 'paid' => 'Pago', 'rejected' => 'Rejeitar'] as $status => $label): ?>
                                    <form method="post" action="/admin/withdrawals/status">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="withdrawal_id" value="<?= e((string) $withdrawal['id']) ?>">
                                        <input type="hidden" name="status" value="<?= e($status) ?>">
                                        <input type="hidden" name="admin_note" value="<?= e($label) ?>">
                                        <button class="mini-button <?= $status === 'rejected' ? 'danger' : ($status === 'paid' ? 'good' : '') ?>" type="submit"><?= e($label) ?></button>
                                    </form>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <span class="muted">Pago em <?= e(format_date($withdrawal['paid_at'] ?? '')) ?></span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<?php include app()->basePath('app/Views/partials/footer.php'); ?>
