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

$money = static fn (mixed $amount, string $currency = 'TRY'): string => number_format((float) $amount, 2, ',', '.') . ' ' . $currency;
$currency = (string) ($balance['currency'] ?? $settings['currency'] ?? 'TRY');
?>
<div class="panel creator-shell"><?php require BASE_PATH . '/app/Views/partials/studio-sidebar.php'; ?>
<section class="panel-body finance-page" data-studio-withdrawals data-csrf="<?= e($csrfToken ?? '') ?>">
    <div class="finance-head">
        <div>
            <h1>Para Cek</h1>
            <p>Cekilebilir bakiyenden manuel admin onayli para cekme talebi olustur.</p>
        </div>
        <a class="pill" href="<?= url('/studyo/bilgilerim') ?>">Banka Bilgilerim</a>
    </div>

    <?php if (!empty($loadError)): ?><div class="finance-alert"><?= e($loadError) ?></div><?php endif; ?>

    <div class="finance-stat-grid">
        <article><span>Cekilebilir</span><strong><?= e($money($balance['available_balance'] ?? 0, $currency)) ?></strong></article>
        <article><span>Bekleyen</span><strong><?= e($money($balance['pending_balance'] ?? 0, $currency)) ?></strong></article>
        <article><span>Minimum cekim</span><strong><?= e($money($settings['minimum_withdrawal_amount'] ?? 100, $currency)) ?></strong></article>
        <article><span>Gonderim suresi</span><strong><?= e((string) ($settings['payout_delay_minutes'] ?? 20)) ?> dk</strong></article>
    </div>

    <div class="finance-grid">
        <section class="finance-card">
            <div class="finance-card-head"><h2>Talep Olustur</h2><span>payout_mode: <?= e($settings['payout_mode'] ?? 'manual') ?></span></div>
            <?php if (empty($bankAccounts)): ?>
                <div class="finance-alert">Banka hesabi olmadan para cekme talebi olusturamazsin.</div>
            <?php endif; ?>
            <form class="finance-form" data-finance-form data-endpoint="/api/studio/withdrawals/create">
                <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                <label>Tutar
                    <input type="number" name="amount" step="0.01" min="<?= e($settings['minimum_withdrawal_amount'] ?? 100) ?>" max="<?= e($balance['available_balance'] ?? 0) ?>" required>
                </label>
                <label>Banka hesabi
                    <select name="user_bank_account_id" required>
                        <?php foreach (($bankAccounts ?? []) as $account): ?>
                            <option value="<?= e($account['id']) ?>" <?= !empty($account['is_default']) ? 'selected' : '' ?>>
                                <?= e(($account['bank_name'] ?? '-') . ' - ' . ($account['iban_masked'] ?? '')) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </label>
                <label>Not
                    <textarea name="user_note" maxlength="500" placeholder="Opsiyonel not"></textarea>
                </label>
                <button class="pill primary" type="submit" <?= empty($bankAccounts) ? 'disabled' : '' ?>>Talep Olustur</button>
                <p class="muted">Onaylandiktan sonra yaklasik <?= e((string) ($settings['payout_delay_minutes'] ?? 20)) ?> dakika icinde manuel olarak gonderilir.</p>
            </form>
        </section>

        <section class="finance-card">
            <div class="finance-card-head"><h2>Cekim Gecmisi</h2><span><?= count($withdrawals ?? []) ?> talep</span></div>
            <div class="table-wrap">
                <table class="finance-table">
                    <thead><tr><th>ID</th><th>Banka</th><th>Tutar</th><th>Durum</th><th>Admin notu</th><th>Referans</th><th>Tarih</th><th>Aksiyon</th></tr></thead>
                    <tbody>
                    <?php foreach (($withdrawals ?? []) as $item): ?>
                        <tr>
                            <td>#<?= e($item['id'] ?? '') ?></td>
                            <td><?= e($item['user_bank_name'] ?? '-') ?></td>
                            <td><?= e($money($item['amount'] ?? 0, $item['currency'] ?? $currency)) ?></td>
                            <td><span class="status-pill status-<?= e($item['status'] ?? 'pending') ?>"><?= e($item['status'] ?? '-') ?></span></td>
                            <td><?= e($item['admin_note'] ?? '') ?></td>
                            <td><?= e($item['transaction_reference'] ?? '') ?></td>
                            <td><?= e($item['requested_at'] ?? $item['created_at'] ?? '') ?></td>
                            <td>
                                <?php if (($item['status'] ?? '') === 'pending'): ?>
                                    <button type="button" class="pill" data-finance-action="/api/studio/withdrawals/cancel" data-id="<?= e($item['id']) ?>" data-confirm="Bu para cekme talebi iptal edilsin mi?">Iptal Et</button>
                                <?php elseif (in_array(($item['status'] ?? ''), ['approved', 'processing'], true)): ?>
                                    <span class="muted">Yaklasik <?= e((string) ($settings['payout_delay_minutes'] ?? 20)) ?> dk</span>
                                <?php else: ?>
                                    <span class="muted">-</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($withdrawals)): ?><tr><td colspan="8">Henuz para cekme talebi yok.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </div>
</section></div>
