<?php
use function App\Core\e;

$shorten = static function (string $value): string {
    return function_exists('mb_strimwidth') ? mb_strimwidth($value, 0, 90, '...') : substr($value, 0, 90);
};
?>
<section class="zpanel-page zpanel-community-page" data-zpanel-community>
    <div class="zpanel-page-head">
        <div>
            <p class="eyebrow">ZPanel</p>
            <h1>Topluluk Yonetimi</h1>
            <p>Topluluk gonderilerini durum ve tur bazinda incele.</p>
        </div>
    </div>

    <div class="finance-stat-grid zpanel-stat-grid">
        <article><strong><?= e((string) (($summary['total'] ?? 0))) ?></strong><span>Toplam</span></article>
        <article><strong><?= e((string) (($summary['published'] ?? 0))) ?></strong><span>Yayinda</span></article>
        <article><strong><?= e((string) (($summary['pending'] ?? 0))) ?></strong><span>Incelemede</span></article>
        <article><strong><?= e((string) (($summary['blocked'] ?? 0))) ?></strong><span>Engelli</span></article>
    </div>

    <form class="zpanel-filters" method="get">
        <input name="q" value="<?= e($_GET['q'] ?? '') ?>" placeholder="Gonderi veya kullanici ara">
        <select name="status">
            <option value="">Aktif durumlar</option>
            <?php foreach (['published', 'pending_review', 'blocked', 'deleted'] as $status): ?>
                <option value="<?= e($status) ?>" <?= ($_GET['status'] ?? '') === $status ? 'selected' : '' ?>><?= e($status) ?></option>
            <?php endforeach; ?>
        </select>
        <select name="post_type">
            <option value="">Tum turler</option>
            <?php foreach (['text', 'image', 'poll'] as $type): ?>
                <option value="<?= e($type) ?>" <?= ($_GET['post_type'] ?? '') === $type ? 'selected' : '' ?>><?= e($type) ?></option>
            <?php endforeach; ?>
        </select>
        <button class="pill primary" type="submit">Filtrele</button>
    </form>

    <div class="table-wrap zpanel-card">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Kullanici</th>
                    <th>Tur</th>
                    <th>Icerik</th>
                    <th>Durum</th>
                    <th>Risk</th>
                    <th>Tarih</th>
                    <th>Aksiyon</th>
                </tr>
            </thead>
            <tbody>
                <?php if (($posts ?? []) === []): ?>
                    <tr><td colspan="8">Kayit bulunamadi.</td></tr>
                <?php else: ?>
                    <?php foreach ($posts as $post): ?>
                        <tr data-community-admin-row data-id="<?= (int) ($post['id'] ?? 0) ?>">
                            <td>#<?= (int) ($post['id'] ?? 0) ?></td>
                            <td><?= e((string) ($post['display_name'] ?? $post['username'] ?? 'User')) ?></td>
                            <td><?= e((string) ($post['post_type'] ?? 'text')) ?></td>
                            <td><?= e($shorten((string) (($post['content'] ?? '') ?: ($post['poll_question'] ?? '')))) ?></td>
                            <td><span class="status-pill status-<?= e((string) ($post['status'] ?? 'published')) ?>"><?= e((string) ($post['status'] ?? 'published')) ?></span></td>
                            <td><?= (int) ($post['risk_score'] ?? 0) ?></td>
                            <td><?= e((string) ($post['created_at'] ?? '')) ?></td>
                            <td>
                                <div class="zpanel-row-actions">
                                    <button class="pill" type="button" data-zpanel-action="/api/zpanel/community/approve" data-id="<?= (int) ($post['id'] ?? 0) ?>" data-reload-on-success="true">Onayla</button>
                                    <button class="pill" type="button" data-zpanel-action="/api/zpanel/community/block" data-id="<?= (int) ($post['id'] ?? 0) ?>" data-confirm="Bu gonderi engellensin mi?">Engelle</button>
                                    <button class="pill danger" type="button" data-zpanel-action="/api/zpanel/community/delete" data-id="<?= (int) ($post['id'] ?? 0) ?>" data-confirm="Bu gonderi silinsin mi?">Sil</button>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</section>
