<?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');
$section = $section ?? 'kazanclar';
?>
<div class="panel zpanel finance-page">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>
    <section class="panel-body">
        <div class="finance-head">
            <div><h1>Kazanclar</h1><p>Platform geliri, creator netleri, kesintiler ve ledger hareketleri.</p></div>
        </div>
        <?php if (!empty($loadError)): ?><div class="finance-alert"><?= e($loadError) ?></div><?php endif; ?>

        <div class="finance-stat-grid">
            <article><span>Toplam platform geliri</span><strong><?= e($money($summary['platform_net_total'] ?? 0)) ?></strong></article>
            <article><span>Toplam creator net</span><strong><?= e($money($summary['creator_net_total'] ?? 0)) ?></strong></article>
            <article><span>Toplam komisyon</span><strong><?= e($money($summary['commission_total'] ?? 0)) ?></strong></article>
            <article><span>Toplam vergi</span><strong><?= e($money($summary['tax_total'] ?? 0)) ?></strong></article>
            <article><span>Provider fee</span><strong><?= e($money($summary['provider_fee_total'] ?? 0)) ?></strong></article>
        </div>

        <section class="finance-card">
            <div class="finance-card-head"><h2>Creator Bazli Ozet</h2><span><?= count($creators ?? []) ?> creator</span></div>
            <div class="table-wrap"><table class="finance-table">
                <thead><tr><th>Creator</th><th>Satis</th><th>Brut</th><th>Creator Net</th><th>Komisyon</th><th>Vergi</th><th>Provider</th><th>Cekilebilir</th><th>Cekilmis</th></tr></thead>
                <tbody>
                <?php foreach (($creators ?? []) as $row): ?>
                    <tr>
                        <td><?= e($row['creator_name'] ?? $row['username'] ?? '-') ?></td>
                        <td><?= e($row['sale_count'] ?? 0) ?></td>
                        <td><?= e($money($row['gross_total'] ?? 0)) ?></td>
                        <td><?= e($money($row['creator_net_total'] ?? 0)) ?></td>
                        <td><?= e($money($row['commission_total'] ?? 0)) ?></td>
                        <td><?= e($money($row['tax_total'] ?? 0)) ?></td>
                        <td><?= e($money($row['provider_fee_total'] ?? 0)) ?></td>
                        <td><?= e($money($row['available_balance'] ?? 0)) ?></td>
                        <td><?= e($money($row['withdrawn_balance'] ?? 0)) ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($creators)): ?><tr><td colspan="9">Creator kazanc kaydi yok.</td></tr><?php endif; ?>
                </tbody>
            </table></div>
        </section>

        <div class="finance-grid">
            <section class="finance-card">
                <div class="finance-card-head"><h2>Son Satislar</h2></div>
                <div class="table-wrap"><table class="finance-table compact">
                    <thead><tr><th>Siparis</th><th>Creator</th><th>Paket</th><th>Abone</th><th>Brut</th><th>Net</th><th>Durum</th><th>Tarih</th></tr></thead>
                    <tbody>
                    <?php foreach (($sales ?? []) as $sale): ?>
                        <tr><td><?= e($sale['order_no'] ?? '') ?></td><td><?= e($sale['creator_name'] ?? '-') ?></td><td><?= e($sale['package_name'] ?? '-') ?></td><td><?= e($sale['subscriber_name'] ?? '-') ?></td><td><?= e($money($sale['gross_amount'] ?? 0, $sale['currency'] ?? 'TRY')) ?></td><td><?= e($money($sale['creator_net_amount'] ?? 0, $sale['currency'] ?? 'TRY')) ?></td><td><?= e($sale['status'] ?? '') ?></td><td><?= e($sale['created_at'] ?? '') ?></td></tr>
                    <?php endforeach; ?>
                    <?php if (empty($sales)): ?><tr><td colspan="8">Satis yok.</td></tr><?php endif; ?>
                    </tbody>
                </table></div>
            </section>
            <section class="finance-card">
                <div class="finance-card-head"><h2>Ledger Hareketleri</h2></div>
                <div class="table-wrap"><table class="finance-table compact">
                    <thead><tr><th>Creator</th><th>Tip</th><th>Tutar</th><th>Once</th><th>Sonra</th><th>Aciklama</th><th>Tarih</th></tr></thead>
                    <tbody>
                    <?php foreach (($ledger ?? []) as $entry): ?>
                        <tr><td><?= e($entry['creator_name'] ?? $entry['username'] ?? '-') ?></td><td><?= e($entry['type'] ?? '') ?></td><td><?= e($money($entry['amount'] ?? 0, $entry['currency'] ?? 'TRY')) ?></td><td><?= e($money($entry['balance_before'] ?? 0, $entry['currency'] ?? 'TRY')) ?></td><td><?= e($money($entry['balance_after'] ?? 0, $entry['currency'] ?? 'TRY')) ?></td><td><?= e($entry['description'] ?? '') ?></td><td><?= e($entry['created_at'] ?? '') ?></td></tr>
                    <?php endforeach; ?>
                    <?php if (empty($ledger)): ?><tr><td colspan="7">Ledger yok.</td></tr><?php endif; ?>
                    </tbody>
                </table></div>
            </section>
        </div>
    </section>
</div>
