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

<section class="panel">
    <p class="eyebrow">Financeiro</p>
    <h1>Carteira e ganhos</h1>
    <p>Acompanhe o saldo disponivel, valores pendentes e pedidos de levantamento.</p>

    <div class="stats compact">
        <div class="stat"><span>Vendas</span><strong><?= e((string) $totals['count']) ?></strong></div>
        <div class="stat"><span>Valor bruto</span><strong><?= e(money_mzn($totals['gross'])) ?></strong></div>
        <div class="stat"><span>Comissao plataforma</span><strong><?= e(money_mzn($totals['platform'])) ?></strong></div>
        <div class="stat"><span>Saldo disponivel</span><strong><?= e(money_mzn($wallet['available'])) ?></strong><small><?= e(money_mzn($wallet['pending'])) ?> pendente</small></div>
    </div>
</section>

<section class="detail-grid">
    <div class="panel">
        <h2>Solicitar levantamento</h2>
        <form class="form" method="post" action="/withdrawals/request">
            <?= csrf_field() ?>
            <label>Valor a levantar
                <input name="amount" type="number" min="100" step="0.01" max="<?= e((string) $wallet['available']) ?>" placeholder="0.00" required>
            </label>
            <div class="grid two">
                <label>Metodo
                    <select name="method">
                        <option value="mpesa">M-Pesa</option>
                        <option value="emola">e-Mola</option>
                        <option value="mkesh">mKesh</option>
                        <option value="bank_transfer">Transferencia bancaria</option>
                    </select>
                </label>
                <label>Titular
                    <input name="destination_name" placeholder="Nome do titular" required>
                </label>
            </div>
            <label>Numero/conta de destino
                <input name="destination_number" placeholder="84xxxxxxx ou NIB/conta bancaria" required>
            </label>
            <label>Observacoes
                <textarea name="notes" rows="3" placeholder="Opcional"></textarea>
            </label>
            <button class="button" type="submit">Pedir levantamento</button>
        </form>
    </div>

    <div class="panel">
        <h2>Resumo da carteira</h2>
        <dl class="detail-list">
            <div><dt>Total ganho</dt><dd><?= e(money_mzn($wallet['seller'])) ?></dd></div>
            <div><dt>Disponivel</dt><dd><?= e(money_mzn($wallet['available'])) ?></dd></div>
            <div><dt>Pendente/aprovado</dt><dd><?= e(money_mzn($wallet['pending'])) ?></dd></div>
            <div><dt>Pago</dt><dd><?= e(money_mzn($wallet['paid'])) ?></dd></div>
            <div><dt>Pedidos</dt><dd><?= e((string) $wallet['withdrawals_count']) ?></dd></div>
        </dl>
    </div>
</section>

<section class="panel">
    <h2>Pedidos de levantamento</h2>
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Referencia</th>
                    <th>Valor</th>
                    <th>Metodo</th>
                    <th>Destino</th>
                    <th>Estado</th>
                    <th>Data</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$withdrawals): ?>
                    <tr><td colspan="6">Ainda nao existem pedidos de levantamento.</td></tr>
                <?php endif; ?>
                <?php foreach ($withdrawals as $withdrawal): ?>
                    <tr>
                        <td><strong><?= e($withdrawal['reference']) ?></strong><span class="muted"><?= e($withdrawal['admin_note'] ?? '') ?></span></td>
                        <td><?= e(money_mzn($withdrawal['amount'])) ?></td>
                        <td><?= e($withdrawalsService->methodLabel($withdrawal['method'])) ?></td>
                        <td><?= e($withdrawal['destination_name']) ?><span class="muted"><?= e($withdrawal['destination_number']) ?></span></td>
                        <td><span class="badge <?= e($withdrawal['status']) ?>"><?= e($withdrawalsService->statusLabel($withdrawal['status'])) ?></span></td>
                        <td><?= e(format_date($withdrawal['created_at'])) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="panel">
    <h2>Ganhos por venda</h2>
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Curso</th>
                    <th>Bruto</th>
                    <th>Comissao</th>
                    <th>Ganho</th>
                    <th>Data</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$commissions): ?>
                    <tr><td colspan="5">Ainda nao existem ganhos.</td></tr>
                <?php endif; ?>
                <?php foreach ($commissions as $commission): ?>
                    <tr>
                        <td><?= e($commission['course_title']) ?></td>
                        <td><?= e(money_mzn($commission['gross_amount'])) ?></td>
                        <td><?= e(money_mzn($commission['platform_amount'])) ?></td>
                        <td><strong><?= e(money_mzn($commission['seller_amount'])) ?></strong></td>
                        <td><?= e(format_date($commission['created_at'])) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

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