<?php
/*
|--------------------------------------------------------------------------
| RadyoHost.BiZ / Profesyonel Radyo Hosting ve Web Tasarım Hizmetleri
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| DUYURU YÖNETİM EKRANI
|--------------------------------------------------------------------------
| announcements.php
| Okey Platformu Duyuru Listeleme ve Yönetim Modülü
| Developed By VoLKaN75
|--------------------------------------------------------------------------
*/

declare(strict_types=1);

$pageTitle = 'Duyuru Yönetimi';
$pageDescription = 'Okey Platformu duyurularını görüntüleyin, yönetin ve düzenleyin.';

require_once __DIR__ . '/includes/admin-layout.php';

$announcements = [];

$stats = [
    'total'   => 0,
    'active'  => 0,
    'passive' => 0,
    'month'   => 0,
];

$columns = [];

try {
    if (isset($pdo) && $pdo instanceof PDO) {
        $columnQuery = $pdo->query("SHOW COLUMNS FROM announcements");

        while ($row = $columnQuery->fetch(PDO::FETCH_ASSOC)) {
            $columns[] = (string)$row['Field'];
        }

        $stats['total'] = (int)$pdo->query("SELECT COUNT(*) FROM announcements")->fetchColumn();

        if (in_array('status', $columns, true)) {
            $stats['active'] = (int)$pdo->query("
                SELECT COUNT(*) FROM announcements 
                WHERE status IN ('active','aktif','1')
            ")->fetchColumn();

            $stats['passive'] = (int)$pdo->query("
                SELECT COUNT(*) FROM announcements 
                WHERE status IN ('passive','pasif','0')
            ")->fetchColumn();
        }

        if (in_array('created_at', $columns, true)) {
            $stats['month'] = (int)$pdo->query("
                SELECT COUNT(*) FROM announcements 
                WHERE MONTH(created_at) = MONTH(CURDATE())
                AND YEAR(created_at) = YEAR(CURDATE())
            ")->fetchColumn();
        }

        $announcements = $pdo->query("
            SELECT * FROM announcements
            ORDER BY id DESC
            LIMIT 100
        ")->fetchAll(PDO::FETCH_ASSOC);
    }
} catch (Throwable $e) {
    error_log('Duyuru yönetimi hatası: ' . $e->getMessage());
}

function announcement_value(array $row, array $keys, string $default = '-'): string
{
    foreach ($keys as $key) {
        if (isset($row[$key]) && trim((string)$row[$key]) !== '') {
            return (string)$row[$key];
        }
    }

    return $default;
}

function announcement_image(array $row): string
{
    $image = announcement_value($row, ['image', 'photo', 'cover'], '');

    if ($image === '' || $image === '-') {
        return '../assets/images/admin-default.png';
    }

    $image = trim(str_replace('\\', '/', $image));

    if (str_starts_with($image, 'http://') || str_starts_with($image, 'https://')) {
        return $image;
    }

    $imageName = basename($image);

    $paths = [
        [
            'disk' => __DIR__ . '/../uploads/announcements/' . $imageName,
            'url'  => '../uploads/announcements/' . $imageName,
        ],
        [
            'disk' => __DIR__ . '/../uploads/announcement/' . $imageName,
            'url'  => '../uploads/announcement/' . $imageName,
        ],
        [
            'disk' => __DIR__ . '/../uploads/' . $imageName,
            'url'  => '../uploads/' . $imageName,
        ],
    ];

    foreach ($paths as $path) {
        if (is_file($path['disk'])) {
            return $path['url'];
        }
    }

    return '../assets/images/admin-default.png';
}
?>

<style>
.announcement-stats{
    display:grid;
    grid-template-columns:repeat(4,1fr);
    gap:18px;
    margin-bottom:24px;
}

.announcement-stat{
    position:relative;
    overflow:hidden;
    padding:24px;
    border-radius:28px;
    background:rgba(255,255,255,.055);
    border:1px solid rgba(255,255,255,.09);
    box-shadow:0 22px 64px rgba(0,0,0,.18);
}

.announcement-stat:after{
    content:attr(data-icon);
    position:absolute;
    right:18px;
    bottom:8px;
    font-size:54px;
    opacity:.12;
}

.announcement-stat span{
    display:block;
    color:#9aa3b2;
    font-size:13px;
    font-weight:900;
}

.announcement-stat strong{
    display:block;
    margin-top:12px;
    color:#f5b642;
    font-size:38px;
}

.announcement-toolbar{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:16px;
    margin-bottom:20px;
    padding:22px;
    border-radius:28px;
    background:rgba(255,255,255,.052);
    border:1px solid rgba(255,255,255,.09);
}

.announcement-toolbar h2{
    margin:0;
    color:#f5b642;
    font-size:22px;
}

.announcement-toolbar p{
    margin:7px 0 0;
    color:#9aa3b2;
    font-size:13px;
    font-weight:700;
}

.announcement-table-wrap{
    overflow:auto;
    border-radius:28px;
    border:1px solid rgba(255,255,255,.09);
    background:rgba(255,255,255,.045);
    box-shadow:0 24px 70px rgba(0,0,0,.18);
}

.announcement-table{
    width:100%;
    min-width:980px;
    border-collapse:collapse;
}

.announcement-table th,
.announcement-table td{
    padding:16px;
    border-bottom:1px solid rgba(255,255,255,.07);
    text-align:left;
    color:#d7dce6;
    font-size:14px;
}

.announcement-table th{
    color:#f5b642;
    background:rgba(0,0,0,.22);
    font-weight:900;
}

.announcement-info{
    display:flex;
    align-items:center;
    gap:12px;
}

.announcement-img{
    width:58px;
    height:58px;
    border-radius:16px;
    padding:2px;
    background:linear-gradient(135deg,#f5b642,#b90000);
    flex:0 0 auto;
}

.announcement-img img{
    width:100%;
    height:100%;
    border-radius:14px;
    object-fit:cover;
    background:#111;
}

.announcement-info strong{
    display:block;
    color:#fff;
    margin-bottom:4px;
}

.announcement-info span{
    color:#9aa3b2;
    font-size:12px;
}

.badge{
    display:inline-flex;
    padding:7px 11px;
    border-radius:999px;
    font-size:12px;
    font-weight:900;
    background:rgba(245,182,66,.13);
    color:#ffd47d;
    border:1px solid rgba(245,182,66,.28);
}

.badge.active{
    background:rgba(0,170,80,.16);
    color:#b8ffd7;
    border-color:rgba(0,255,140,.25);
}

.badge.passive{
    background:rgba(185,0,0,.18);
    color:#ffdada;
    border-color:rgba(255,80,80,.30);
}

.action-group{
    display:flex;
    gap:8px;
    flex-wrap:wrap;
}

.action-link{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    min-height:36px;
    padding:0 12px;
    border-radius:12px;
    text-decoration:none;
    color:#fff;
    background:rgba(255,255,255,.07);
    border:1px solid rgba(255,255,255,.10);
    font-size:12px;
    font-weight:900;
}

.action-link:hover{
    color:#f5b642;
    border-color:#f5b642;
}

.empty-announcements{
    padding:30px;
    text-align:center;
    color:#9aa3b2;
    font-weight:800;
}

@media(max-width:1000px){
    .announcement-stats{
        grid-template-columns:repeat(2,1fr);
    }

    .announcement-toolbar{
        flex-direction:column;
        align-items:flex-start;
    }
}

@media(max-width:560px){
    .announcement-stats{
        grid-template-columns:1fr;
    }
}
</style>

<section class="announcement-stats">
    <div class="announcement-stat" data-icon="📢">
        <span>Toplam Duyuru</span>
        <strong><?= admin_e((string)$stats['total']) ?></strong>
    </div>

    <div class="announcement-stat" data-icon="✅">
        <span>Aktif Duyuru</span>
        <strong><?= admin_e((string)$stats['active']) ?></strong>
    </div>

    <div class="announcement-stat" data-icon="⛔">
        <span>Pasif Duyuru</span>
        <strong><?= admin_e((string)$stats['passive']) ?></strong>
    </div>

    <div class="announcement-stat" data-icon="🆕">
        <span>Bu Ay Eklenen</span>
        <strong><?= admin_e((string)$stats['month']) ?></strong>
    </div>
</section>

<section class="announcement-toolbar">
    <div>
        <h2>Duyuru Listesi</h2>
        <p>Oyunculara gösterilecek duyuruları buradan kontrol edebilirsiniz.</p>
    </div>

    <a href="announcement-view.php" class="admin-btn">+ Yeni Duyuru Oluştur</a>
</section>

<section class="announcement-table-wrap">
    <table class="announcement-table">
        <thead>
            <tr>
                <th>Duyuru</th>
                <th>Durum</th>
                <th>Görüntülenme</th>
                <th>Oluşturma</th>
                <th>Güncelleme</th>
                <th>İşlem</th>
            </tr>
        </thead>

        <tbody>
            <?php if ($announcements): ?>
                <?php foreach ($announcements as $announcement): ?>
                    <?php
                    $status = strtolower(announcement_value($announcement, ['status'], 'active'));
                    $statusClass = in_array($status, ['active', 'aktif', '1'], true) ? 'active' : 'passive';
                    ?>
                    <tr>
                        <td>
                            <div class="announcement-info">
                                <div class="announcement-img">
                                    <img src="<?= admin_e(announcement_image($announcement)) ?>" alt="Duyuru">
                                </div>
                                <div>
                                    <strong><?= admin_e(announcement_value($announcement, ['title', 'subject'], 'Duyuru')) ?></strong>
                                    <span><?= admin_e(announcement_value($announcement, ['summary', 'short_description'], 'Açıklama yok')) ?></span>
                                </div>
                            </div>
                        </td>

                        <td>
                            <span class="badge <?= admin_e($statusClass) ?>">
                                <?= admin_e(announcement_value($announcement, ['status'], 'active')) ?>
                            </span>
                        </td>

                        <td><?= admin_e(announcement_value($announcement, ['views'], '0')) ?></td>

                        <td><?= admin_e(announcement_value($announcement, ['created_at'], '-')) ?></td>

                        <td><?= admin_e(announcement_value($announcement, ['updated_at'], '-')) ?></td>

                        <td>
                            <div class="action-group">
                                <a href="announcement-edit.php?id=<?= (int)($announcement['id'] ?? 0) ?>" class="action-link">Düzenle</a>
                                <a href="announcement-view.php?id=<?= (int)($announcement['id'] ?? 0) ?>" class="action-link">Detay</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="6">
                        <div class="empty-announcements">
                            Henüz kayıtlı duyuru bulunamadı.
                        </div>
                    </td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</section>

<?php require_once __DIR__ . '/includes/admin-footer.php'; ?>