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

/*
|--------------------------------------------------------------------------
| İNDİRME YÖNETİM EKRANI
|--------------------------------------------------------------------------
| downloads.php
| Okey Platformu İndirme Dosyaları Listeleme ve Yönetim Modülü
| Developed By VoLKaN75
|--------------------------------------------------------------------------
*/

declare(strict_types=1);

$pageTitle = 'İndirme Yönetimi';
$pageDescription = 'Oyun EXE dosyaları, sürüm bilgileri ve indirme kayıtlarını yönetin.';

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

$downloads = [];

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

$columns = [];

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

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

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

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

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

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

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

function download_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 download_status_class(string $status): string
{
    $status = strtolower($status);

    return in_array($status, ['active', 'aktif', '1'], true) ? 'active' : 'passive';
}
?>

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

.download-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);
}

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

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

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

.download-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);
}

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

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

.download-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);
}

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

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

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

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

.file-info span{
    color:#9aa3b2;
    font-size:12px;
    word-break:break-all;
}

.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-downloads{
    padding:30px;
    text-align:center;
    color:#9aa3b2;
    font-weight:800;
}

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

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

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

<section class="download-stats">
    <div class="download-stat" data-icon="⬇️">
        <span>Toplam Kayıt</span>
        <strong><?= admin_e((string)$stats['total']) ?></strong>
    </div>

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

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

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

<section class="download-toolbar">
    <div>
        <h2>İndirme Listesi</h2>
        <p>Okey oyun dosyaları, EXE bağlantıları ve sürüm kayıtlarını buradan kontrol edebilirsiniz.</p>
    </div>

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

<section class="download-table-wrap">
    <table class="download-table">
        <thead>
            <tr>
                <th>Dosya</th>
                <th>Sürüm</th>
                <th>Boyut</th>
                <th>Durum</th>
                <th>İndirme</th>
                <th>Tarih</th>
                <th>İşlem</th>
            </tr>
        </thead>

        <tbody>
            <?php if ($downloads): ?>
                <?php foreach ($downloads as $download): ?>
                    <?php
                    $status = download_value($download, ['status'], 'active');
                    $statusClass = download_status_class($status);
                    ?>
                    <tr>
                        <td>
                            <div class="file-info">
                                <strong><?= admin_e(download_value($download, ['title', 'name', 'file_name'], 'İndirme Dosyası')) ?></strong>
                                <span><?= admin_e(download_value($download, ['file_path', 'file_url', 'url'], 'Dosya yolu yok')) ?></span>
                            </div>
                        </td>

                        <td><?= admin_e(download_value($download, ['version'], '-')) ?></td>

                        <td><?= admin_e(download_value($download, ['file_size', 'size'], '-')) ?></td>

                        <td>
                            <span class="badge <?= admin_e($statusClass) ?>">
                                <?= admin_e($status) ?>
                            </span>
                        </td>

                        <td><?= admin_e(download_value($download, ['download_count', 'downloads', 'count'], '0')) ?></td>

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

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

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