<?php
use function App\Core\e;

$section = $section ?? 'dashboard';
$money = static fn (mixed $amount): string => number_format((float) $amount, 2, ',', '.') . ' TRY';
$stats = $stats ?? [];
?>
<div class="panel zpanel zpanel-app">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>
    <section class="panel-body zpanel-main">
        <div class="zpanel-head">
            <div>
                <p class="eyebrow">ZPanel</p>
                <h1>Dashboard</h1>
                <p class="muted">Platform sagligi, bekleyen isler ve son hareketler.</p>
            </div>
        </div>

        <?php if (!empty($testMode)): ?>
            <div class="dev-alert zpanel-dev-alert"><strong>Test Modu Aktif</strong><span>FEATURE_TEST_MODE local ortamda acik.</span></div>
        <?php endif; ?>

        <div class="zpanel-stat-grid">
            <article><span>Toplam kullanici</span><strong><?= e((string) (int) ($stats['total_users'] ?? 0)) ?></strong></article>
            <article><span>Toplam mod</span><strong><?= e((string) (int) ($stats['total_mods'] ?? 0)) ?></strong></article>
            <article><span>Toplam muzik</span><strong><?= e((string) (int) ($stats['total_music'] ?? 0)) ?></strong></article>
            <article><span>Toplam odeme</span><strong><?= e((string) (int) ($stats['total_payments'] ?? 0)) ?></strong></article>
            <article><span>Toplam kazanc</span><strong><?= e($money($stats['total_earnings'] ?? 0)) ?></strong></article>
            <article><span>Bekleyen modlar</span><strong><?= e((string) (int) ($stats['pending_mods'] ?? 0)) ?></strong></article>
            <article><span>Bekleyen muzikler</span><strong><?= e((string) (int) ($stats['pending_music'] ?? 0)) ?></strong></article>
            <article><span>Bekleyen cekimler</span><strong><?= e((string) (int) ($stats['pending_withdrawals'] ?? 0)) ?></strong></article>
            <article><span>Guvenlik uyarilari</span><strong><?= e((string) (int) ($stats['security_alerts'] ?? 0)) ?></strong></article>
        </div>

        <section class="zpanel-card">
            <div class="zpanel-card-head"><h2>Hizli aksiyonlar</h2></div>
            <div class="quick-actions">
                <a class="pill primary" href="/zpanel/sayfa-ekle">Yeni Sayfa Ekle</a>
                <a class="pill" href="/zpanel/kategoriler">Kategori Yonet</a>
                <a class="pill" href="/zpanel/settings">Site Ayarlari</a>
                <a class="pill" href="/zpanel/media">Medya Yonetimi</a>
                <?php if (!empty($testMode)): ?><a class="pill" href="/zpanel/test-araclari">Test Araclari</a><?php endif; ?>
            </div>
        </section>

        <div class="zpanel-grid two">
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Son modlar</h2><a href="/zpanel/modlar">Modlar</a></div>
                <div class="zpanel-list">
                    <?php foreach (($latestMods ?? []) as $mod): ?>
                        <article><strong><?= e($mod['title'] ?? '-') ?></strong><span><?= e(($mod['status'] ?? '-') . ' - ' . ($mod['created_at'] ?? '')) ?></span></article>
                    <?php endforeach; ?>
                    <?php if (empty($latestMods)): ?><p class="muted">Mod kaydi yok.</p><?php endif; ?>
                </div>
            </section>
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Son muzikler</h2><a href="/zpanel/muzikler">Muzikler</a></div>
                <div class="zpanel-list">
                    <?php foreach (($latestMusic ?? []) as $track): ?>
                        <article><strong><?= e($track['title'] ?? '-') ?></strong><span><?= e(($track['artist_name'] ?? '-') . ' - ' . ($track['status'] ?? '-')) ?></span></article>
                    <?php endforeach; ?>
                    <?php if (empty($latestMusic)): ?><p class="muted">Muzik kaydi yok.</p><?php endif; ?>
                </div>
            </section>
        </div>

        <div class="zpanel-grid two">
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Son admin islemleri</h2><a href="/zpanel/loglar">Tum loglar</a></div>
                <div class="zpanel-list">
                    <?php foreach (($adminLogs ?? []) as $log): ?>
                        <article><strong><?= e($log['action'] ?? '-') ?></strong><span><?= e(($log['admin_username'] ?? 'admin') . ' - ' . ($log['created_at'] ?? '')) ?></span></article>
                    <?php endforeach; ?>
                    <?php if (empty($adminLogs)): ?><p class="muted">Admin logu henuz yok.</p><?php endif; ?>
                </div>
            </section>
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Guvenlik botu son loglari</h2><a href="/zpanel/link-denetimi">Incele</a></div>
                <div class="zpanel-list">
                    <?php foreach (array_slice($securityLogs ?? [], 0, 8) as $log): ?>
                        <article><strong>Risk <?= e((string) ($log['risk_score'] ?? 0)) ?> / <?= e($log['status'] ?? $log['result_status'] ?? '-') ?></strong><span><?= e(($log['scan_type'] ?? '-') . ' - ' . ($log['created_at'] ?? '')) ?></span></article>
                    <?php endforeach; ?>
                    <?php if (empty($securityLogs)): ?><p class="muted">Guvenlik logu yok.</p><?php endif; ?>
                </div>
            </section>
        </div>

        <div class="zpanel-grid two">
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Son odemeler</h2><a href="/zpanel/odemeler">Odemeler</a></div>
                <div class="table-scroll"><table class="zpanel-table compact">
                    <thead><tr><th>Order</th><th>Kullanici</th><th>Tutar</th><th>Status</th><th>Tarih</th></tr></thead>
                    <tbody>
                    <?php foreach (($payments ?? []) as $payment): ?>
                        <tr><td><?= e($payment['order_no'] ?? '-') ?></td><td><?= e($payment['username'] ?? '-') ?></td><td><?= e($money($payment['total'] ?? 0)) ?></td><td><span class="status-pill"><?= e($payment['status'] ?? '-') ?></span></td><td><?= e($payment['created_at'] ?? '') ?></td></tr>
                    <?php endforeach; ?>
                    <?php if (empty($payments)): ?><tr><td colspan="5">Odeme kaydi yok.</td></tr><?php endif; ?>
                    </tbody>
                </table></div>
            </section>
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Son kayit olan kullanicilar</h2><a href="/zpanel/kullanicilar">Kullanicilar</a></div>
                <div class="table-scroll"><table class="zpanel-table compact">
                    <thead><tr><th>Kullanici</th><th>Email</th><th>Role</th><th>Status</th></tr></thead>
                    <tbody>
                    <?php foreach (($users ?? []) as $user): ?>
                        <tr><td><?= e($user['username'] ?? '-') ?></td><td><?= e($user['email'] ?? '-') ?></td><td><?= e($user['role'] ?? '-') ?></td><td><span class="status-pill"><?= e($user['status'] ?? '-') ?></span></td></tr>
                    <?php endforeach; ?>
                    <?php if (empty($users)): ?><tr><td colspan="4">Kullanici kaydi yok.</td></tr><?php endif; ?>
                    </tbody>
                </table></div>
            </section>
        </div>
    </section>
</div>
