<?php
use function App\Core\{e, logo_asset, url};

$money = static fn (mixed $amount, string $currency = 'TRY'): string => number_format((float) $amount, 2, ',', '.') . ' ' . $currency;
$zpanelLogo = logo_asset('zpanel');
$statuses = ['pending', 'approved', 'processing', 'sent', 'rejected', 'cancelled'];
?>
<div class="panel zpanel finance-page" data-zpanel-withdrawals data-csrf="<?= e($csrfToken ?? '') ?>">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>
    <section class="panel-body">
        <div class="finance-head">
            <div><h1>Para Cekme Talepleri</h1><p>Manuel onay, isleme alma, gonderildi ve red aksiyonlari.</p></div>
        </div>
        <?php if (!empty($loadError)): ?><div class="finance-alert"><?= e($loadError) ?></div><?php endif; ?>
        <nav class="finance-tabs">
            <a class="<?= $activeStatus === '' ? 'is-active' : '' ?>" href="<?= e(url('/zpanel/para-cekme-talepleri')) ?>">Tum</a>
            <?php foreach ($statuses as $status): ?>
                <a class="<?= $activeStatus === $status ? 'is-active' : '' ?>" href="<?= e(url('/zpanel/para-cekme-talepleri?status=' . $status)) ?>"><?= e($status) ?></a>
            <?php endforeach; ?>
        </nav>
        <section class="finance-card">
            <div class="table-wrap"><table class="finance-table">
                <thead><tr><th>ID</th><th>Kullanici</th><th>Banka</th><th>IBAN</th><th>Tutar</th><th>Durum</th><th>Not/Referans</th><th>Tarih</th><th>Aksiyonlar</th></tr></thead>
                <tbody>
                <?php foreach (($withdrawals ?? []) as $item): ?>
                    <?php $status = (string) ($item['status'] ?? 'pending'); ?>
                    <tr>
                        <td>#<?= e($item['id'] ?? '') ?></td>
                        <td><?= e($item['creator_name'] ?? $item['username'] ?? '-') ?></td>
                        <td><?= e($item['user_bank_name'] ?? '-') ?><br><small><?= e($item['user_account_holder'] ?? '') ?></small></td>
                        <td><?= e($item['user_iban'] ?? '') ?></td>
                        <td><?= e($money($item['amount'] ?? 0, $item['currency'] ?? 'TRY')) ?></td>
                        <td><span class="status-pill status-<?= e($status) ?>"><?= e($status) ?></span></td>
                        <td><?= e($item['admin_note'] ?? '') ?><br><small><?= e($item['transaction_reference'] ?? '') ?></small></td>
                        <td><?= e($item['requested_at'] ?? $item['created_at'] ?? '') ?></td>
                        <td>
                            <form class="zpanel-action-form" data-zpanel-finance-form>
                                <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                                <input type="hidden" name="id" value="<?= e($item['id'] ?? '') ?>">
                                <input name="admin_note" maxlength="1000" placeholder="Admin notu">
                                <input name="transaction_reference" maxlength="190" placeholder="Islem referansi">
                                <div class="toolbar">
                                    <?php if ($status === 'pending'): ?>
                                        <button class="pill primary" type="button" data-zpanel-endpoint="/api/zpanel/withdrawals/approve">Onayla</button>
                                        <button class="pill danger" type="button" data-zpanel-endpoint="/api/zpanel/withdrawals/reject" data-confirm="Bu talep reddedilsin mi?">Reddet</button>
                                    <?php elseif ($status === 'approved'): ?>
                                        <button class="pill primary" type="button" data-zpanel-endpoint="/api/zpanel/withdrawals/processing">Isleme Al</button>
                                        <button class="pill danger" type="button" data-zpanel-endpoint="/api/zpanel/withdrawals/reject" data-confirm="Bu talep reddedilsin mi?">Reddet</button>
                                    <?php elseif ($status === 'processing'): ?>
                                        <button class="pill primary" type="button" data-zpanel-endpoint="/api/zpanel/withdrawals/mark-sent">Gonderildi</button>
                                        <button class="pill danger" type="button" data-zpanel-endpoint="/api/zpanel/withdrawals/reject" data-confirm="Bu talep reddedilsin mi?">Reddet</button>
                                    <?php else: ?>
                                        <span class="muted">Kapali</span>
                                    <?php endif; ?>
                                </div>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($withdrawals)): ?><tr><td colspan="9">Talep bulunamadi.</td></tr><?php endif; ?>
                </tbody>
            </table></div>
        </section>
    </section>
</div>
