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

/*
|--------------------------------------------------------------------------
| İNDİRME SAYFASI
|--------------------------------------------------------------------------
| downloads.php
| RadyoHost Okey Premium Oyun İndirme Sayfası
| Developed By VoLKaN75
|--------------------------------------------------------------------------
*/

declare(strict_types=1);

require_once __DIR__ . '/config/db.php';

function site_e($value): string
{
    return htmlspecialchars((string)$value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

function site_short($text, int $limit = 140): string
{
    $text = trim(strip_tags((string)$text));

    if ($text === '') {
        return 'İndirme dosyası açıklaması yakında yayınlanacaktır.';
    }

    if (mb_strlen($text, 'UTF-8') <= $limit) {
        return $text;
    }

    return mb_substr($text, 0, $limit, 'UTF-8') . '...';
}

function site_val(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 site_date_value(string $date): string
{
    if ($date === '') {
        return date('d.m.Y');
    }

    $time = strtotime($date);

    return $time ? date('d.m.Y', $time) : date('d.m.Y');
}

function download_file_url(array $row): string
{
    $file = site_val($row, ['file', 'file_path', 'download_file', 'url', 'link'], '#');

    if ($file === '#') {
        return '#';
    }

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

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

    return $file;
}

function download_image(array $row): string
{
    $image = site_val($row, ['image', 'image_path', 'photo', 'banner'], '');

    if ($image === '') {
        return 'uploads/site/downloads-hero.webp';
    }

    $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/downloads/' . $imageName,
            'url'  => 'uploads/downloads/' . $imageName,
        ],
        [
            'disk' => __DIR__ . '/uploads/site/' . $imageName,
            'url'  => 'uploads/site/' . $imageName,
        ],
        [
            'disk' => __DIR__ . '/uploads/' . $imageName,
            'url'  => 'uploads/' . $imageName,
        ],
    ];

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

    return $image;
}

$logoImage = 'uploads/site/logo.png';
$heroBgImage = 'uploads/site/downloads-hero.webp';

$downloads = [];
$totalDownloads = 0;
$latest = null;

try {
    if (isset($pdo) && $pdo instanceof PDO) {
        $columns = [];

        try {
            $columnQuery = $pdo->query("SHOW COLUMNS FROM downloads");
            while ($row = $columnQuery->fetch(PDO::FETCH_ASSOC)) {
                $columns[] = (string)$row['Field'];
            }
        } catch (Throwable $e) {}

        $where = '';
        if (in_array('status', $columns, true)) {
            $where = "WHERE status IN ('active','aktif','1')";
        }

        $totalDownloads = (int)$pdo->query("
            SELECT COUNT(*)
            FROM downloads
            {$where}
        ")->fetchColumn();

        $orderSql = in_array('sort_order', $columns, true)
            ? "ORDER BY sort_order ASC, id DESC"
            : "ORDER BY id DESC";

        $stmt = $pdo->query("
            SELECT *
            FROM downloads
            {$where}
            {$orderSql}
            LIMIT 50
        ");

        $downloads = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
        $latest = $downloads[0] ?? null;
    }
} catch (Throwable $e) {
    $downloads = [];
    $latest = null;
}
?>
<!doctype html>
<html lang="tr">
<head>
<meta charset="utf-8">
<title>Oyunu İndir | RadyoHost Okey</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="RadyoHost Okey oyun indirme merkezi. Güncel oyun dosyasını indir, kur ve premium okey masalarına katıl.">

<style>
*{box-sizing:border-box}
html{scroll-behavior:smooth}
body{margin:0;font-family:Arial,Helvetica,sans-serif;background:#070911;color:#fff}
a{text-decoration:none;color:inherit}
.site-wrap{min-height:100vh;background:radial-gradient(circle at top left,rgba(245,182,66,.24),transparent 34%),radial-gradient(circle at top right,rgba(185,0,0,.26),transparent 36%),linear-gradient(180deg,#070911,#10131f 48%,#070911)}
.container{width:min(1180px,92%);margin:auto}

.topbar{position:sticky;top:0;z-index:999;backdrop-filter:blur(22px);background:linear-gradient(135deg,rgba(7,9,17,.94),rgba(16,19,31,.88));border-bottom:1px solid rgba(255,255,255,.09);box-shadow:0 18px 60px rgba(0,0,0,.25)}
.nav{min-height:88px;display:flex;align-items:center;justify-content:space-between;gap:22px;position:relative}
.logo{display:flex;align-items:center;gap:14px;min-width:240px;font-weight:900;position:relative;z-index:1001}
.logo-img{max-width:250px;max-height:76px;object-fit:contain;display:block;filter:drop-shadow(0 10px 22px rgba(245,182,66,.18))}
.menu{flex:1;display:flex;justify-content:center;align-items:center;gap:8px;padding:8px;border-radius:24px;background:rgba(255,255,255,.045);border:1px solid rgba(255,255,255,.07)}
.menu a{height:46px;display:inline-flex;align-items:center;justify-content:center;gap:7px;padding:0 15px;border-radius:17px;color:#dce3ef;font-size:14px;font-weight:900;transition:.25s;white-space:nowrap}
.menu a:hover{color:#fff;background:rgba(245,182,66,.12);transform:translateY(-2px)}
.menu a.active{color:#111;background:linear-gradient(135deg,#f5b642,#ffcf6a);box-shadow:0 12px 28px rgba(245,182,66,.22)}
.actions{display:flex;gap:10px}
.btn{min-height:48px;padding:0 20px;border-radius:17px;display:inline-flex;align-items:center;justify-content:center;gap:8px;font-weight:1000;border:1px solid rgba(255,255,255,.10);background:rgba(255,255,255,.06);color:#fff;transition:.25s}
.btn:hover{transform:translateY(-2px)}
.btn-gold{color:#111;background:linear-gradient(135deg,#f5b642,#ffcf6a);box-shadow:0 15px 34px rgba(245,182,66,.22)}

.mobile-menu-btn{display:none;width:52px;height:52px;border:none;border-radius:16px;background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.12);cursor:pointer;padding:0;align-items:center;justify-content:center;flex-direction:column;gap:5px;position:relative;z-index:1001;transition:.25s}
.mobile-menu-btn span{width:25px;height:3px;background:#fff;border-radius:20px;transition:.3s}
.mobile-menu-btn.active{background:linear-gradient(135deg,#f5b642,#ffcf6a)}
.mobile-menu-btn.active span{background:#111}
.mobile-menu-btn.active span:nth-child(1){transform:translateY(8px) rotate(45deg)}
.mobile-menu-btn.active span:nth-child(2){opacity:0}
.mobile-menu-btn.active span:nth-child(3){transform:translateY(-8px) rotate(-45deg)}

.page-hero{
    position:relative;
    overflow:hidden;
    padding:74px 0 54px;
    border-bottom:1px solid rgba(245,182,66,.18);
    background:
        linear-gradient(90deg,rgba(0,0,0,.88) 0%,rgba(0,0,0,.70) 44%,rgba(0,0,0,.48) 100%),
        url('<?= site_e($heroBgImage) ?>') center center/cover no-repeat;
}
.page-hero:before{
    content:"";
    position:absolute;
    inset:0;
    background:
        radial-gradient(circle at 18% 28%,rgba(245,182,66,.18),transparent 30%),
        linear-gradient(90deg,rgba(0,0,0,.30),rgba(0,0,0,.08));
}
.page-hero .container{position:relative;z-index:2}
.kicker{display:inline-flex;padding:10px 16px;border-radius:999px;background:rgba(245,182,66,.13);border:1px solid rgba(245,182,66,.35);color:#f5b642;font-weight:1000;margin-bottom:18px}
.page-hero h1{margin:0;font-size:clamp(42px,6vw,74px);line-height:1.02;text-transform:uppercase;letter-spacing:-1.7px}
.page-hero h1 span{color:#f5b642}
.page-hero p{margin:18px 0 0;max-width:720px;color:#dce3ef;font-size:18px;font-weight:800;line-height:1.7}
.hero-buttons{display:flex;gap:14px;flex-wrap:wrap;margin-top:26px}

.stats{display:grid;grid-template-columns:repeat(4,1fr);gap:16px;margin-top:-32px;position:relative;z-index:5}
.stat{padding:23px 22px;border-radius:22px;background:rgba(10,10,10,.92);border:1px solid rgba(255,255,255,.13);box-shadow:0 24px 70px rgba(0,0,0,.35)}
.stat strong{display:block;color:#f5b642;font-size:31px;margin-bottom:6px}
.stat span{color:#cfd6e3;font-size:13px;font-weight:900}

.section{padding:58px 0}
.section-head{display:flex;align-items:flex-end;justify-content:space-between;gap:20px;margin-bottom:25px}
.section-head h2{margin:0;font-size:36px}
.section-head p{margin:8px 0 0;color:#9aa3b2;font-weight:800;line-height:1.6}

.step-grid,.req-grid,.download-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:20px}
.card{position:relative;overflow:hidden;padding:25px;border-radius:28px;background:linear-gradient(145deg,rgba(255,255,255,.075),rgba(255,255,255,.035));border:1px solid rgba(245,182,66,.16);box-shadow:0 24px 72px rgba(0,0,0,.24);transition:.25s}
.card:hover{transform:translateY(-6px);border-color:rgba(245,182,66,.45);box-shadow:0 34px 95px rgba(0,0,0,.38)}
.card:before{content:"";position:absolute;width:170px;height:170px;border-radius:999px;right:-80px;top:-80px;background:rgba(245,182,66,.11)}
.icon{position:relative;width:58px;height:58px;display:grid;place-items:center;border-radius:20px;background:linear-gradient(135deg,rgba(245,182,66,.24),rgba(185,0,0,.20));color:#f5b642;font-size:24px;font-weight:1000;margin-bottom:18px}
.card h3{position:relative;margin:0 0 10px;font-size:22px;color:#fff}
.card p{position:relative;margin:0;color:#aeb7c7;line-height:1.7;font-weight:750}

.download-card{padding:0}
.download-card-image{position:relative;height:180px;background:#070911;overflow:hidden}
.download-card-image img{width:100%;height:100%;object-fit:cover;display:block;transition:.35s}
.download-card:hover .download-card-image img{transform:scale(1.06)}
.download-card-image:after{content:"";position:absolute;inset:0;background:linear-gradient(180deg,rgba(0,0,0,.06),rgba(0,0,0,.72))}
.download-card-label{position:absolute;left:16px;top:15px;z-index:3;display:inline-flex;align-items:center;gap:7px;min-height:32px;padding:0 12px;border-radius:999px;background:rgba(0,0,0,.58);border:1px solid rgba(245,182,66,.32);color:#f5b642;font-size:11px;font-weight:1000;text-transform:uppercase}
.download-card-body{padding:22px}
.file-meta{display:flex;gap:8px;flex-wrap:wrap;margin:16px 0 18px}
.file-meta span{padding:7px 10px;border-radius:999px;color:#dce3ef;background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.10);font-size:12px;font-weight:900}
.download-btn{width:100%;min-height:52px}

.safe-box{padding:38px;border-radius:40px;background:radial-gradient(circle at top right,rgba(245,182,66,.23),transparent 36%),linear-gradient(135deg,rgba(255,255,255,.08),rgba(255,255,255,.035));border:1px solid rgba(255,255,255,.10);display:grid;grid-template-columns:1fr .42fr;gap:24px;align-items:center;box-shadow:0 28px 90px rgba(0,0,0,.32)}
.safe-box h2{margin:0;color:#fff;font-size:34px}
.safe-box p{margin:12px 0 0;color:#aeb7c7;font-weight:800;line-height:1.7}
.safe-list{display:grid;gap:12px}
.safe-item{padding:15px;border-radius:18px;background:rgba(0,0,0,.22);border:1px solid rgba(255,255,255,.08);font-weight:900;color:#dce3ef}

.empty{grid-column:1 / -1;padding:34px;border-radius:28px;text-align:center;color:#9aa3b2;font-weight:900;background:rgba(255,255,255,.045);border:1px solid rgba(255,255,255,.09)}

.footer{margin-top:40px;padding:22px 30px;border-top:1px solid rgba(255,255,255,.08);display:grid;grid-template-columns:1fr auto 1fr;align-items:center;gap:18px;color:#9aa3b2;font-size:13px;font-weight:900}
.footer a{color:#f5b642}
.footer-center{color:#f5b642}
.footer-right{display:flex;justify-content:flex-end;align-items:center;color:#ff5db1}
.footer-right img{width:52px;height:52px;object-fit:contain;margin-left:-2px;filter:drop-shadow(0 0 10px rgba(37,211,102,.8))}

@media(max-width:1100px){
    .nav{padding:14px 0}
    .actions{gap:8px}
    .btn{padding:0 15px;font-size:13px}
    .menu a{padding:0 12px;font-size:13px}
}

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

@media(max-width:768px){
    .topbar{overflow:visible}
    .nav{min-height:82px;padding:12px 0;gap:12px;flex-wrap:nowrap}
    .logo{min-width:auto;flex:1}
    .logo-img{max-width:210px;max-height:68px}
    .mobile-menu-btn{display:flex}
    .menu{position:absolute;top:100%;left:0;right:0;width:100%;flex-direction:column;justify-content:flex-start;align-items:stretch;gap:8px;padding:16px;border-radius:0 0 24px 24px;background:linear-gradient(180deg,rgba(7,9,17,.99),rgba(16,19,31,.98));border:1px solid rgba(245,182,66,.20);border-top:none;box-shadow:0 28px 70px rgba(0,0,0,.55);display:none;z-index:1000}
    .menu.active{display:flex}
    .menu a{width:100%;height:54px;justify-content:flex-start;padding:0 18px;font-size:15px;border-radius:16px}
    .actions{display:none}
    .section-head{display:block}
    .footer{grid-template-columns:1fr;text-align:center}
    .footer-right{justify-content:center}
    .page-hero{padding:54px 0 46px}
    .safe-box{padding:26px;grid-template-columns:1fr}
    .safe-box h2{font-size:28px}
}

@media(max-width:560px){
    .stats,.step-grid,.req-grid,.download-grid{grid-template-columns:1fr}
}

@media(max-width:480px){
    .container{width:min(100% - 24px,1180px)}
    .logo-img{max-width:185px;max-height:62px}
    .mobile-menu-btn{width:48px;height:48px;border-radius:14px}
    .page-hero h1{font-size:40px}
    .hero-buttons .btn{width:100%}
    .section-head h2{font-size:30px}
}
</style>
</head>

<body>
<div class="site-wrap">

<header class="topbar">
    <div class="container nav">
        <a href="index.php" class="logo">
            <img src="<?= site_e($logoImage) ?>" alt="101 Batman Okey" class="logo-img">
        </a>

        <button class="mobile-menu-btn" id="mobileMenuBtn" type="button" aria-label="Menüyü Aç / Kapat" aria-expanded="false">
            <span></span><span></span><span></span>
        </button>

        <nav class="menu" id="mainMenu">
            <a href="index.php">🏠 Ana Sayfa</a>
            <a href="announcements.php">📢 Duyurular</a>
            <a href="downloads.php" class="active">⬇️ İndir</a>
            <a href="packages.php">👑 Paketler</a>
            <a href="contact.php">☎️ İletişim</a>
        </nav>

        <div class="actions">
            <a href="login.php" class="btn">Giriş Yap</a>
            <a href="register.php" class="btn btn-gold">Kayıt Ol</a>
        </div>
    </div>
</header>

<main>

<section class="page-hero">
    <div class="container">
        <div class="kicker">⬇️ Güvenli ve Hızlı Oyun İndirme Merkezi</div>

        <h1>Oyunu <span>İndir</span></h1>

        <p>101 Batman Okey kurulum dosyasını indir, hesabınla giriş yap ve Gold / Premium / VIP avantajlarıyla oyun keyfini yaşa.</p>

        <div class="hero-buttons">
            <a href="<?= site_e($latest ? download_file_url($latest) : '#') ?>" class="btn btn-gold">
                Son Sürümü İndir
            </a>

            <a href="#guide" class="btn">
                Kurulum Rehberi
            </a>
        </div>
    </div>
</section>

<section>
    <div class="container">
        <div class="stats">
            <div class="stat">
                <strong><?= site_e((string)$totalDownloads) ?></strong>
                <span>Aktif Dosya</span>
            </div>

            <div class="stat">
                <strong>Windows</strong>
                <span>Uyumlu</span>
            </div>

            <div class="stat">
                <strong>3 Adım</strong>
                <span>Kolay Kurulum</span>
            </div>

            <div class="stat">
                <strong>7/24</strong>
                <span>Erişim</span>
            </div>
        </div>
    </div>
</section>

<section class="section" id="guide">
    <div class="container">
        <div class="section-head">
            <div>
                <h2>🧩 Kurulum Rehberi</h2>
                <p>Oyuna başlamak için sadece 3 basit adım.</p>
            </div>
        </div>

        <div class="step-grid">
            <div class="card">
                <div class="icon">1</div>
                <h3>Dosyayı İndir</h3>
                <p>Son sürüm oyun kurulum dosyasını güvenli indirme alanından bilgisayarına indir.</p>
            </div>

            <div class="card">
                <div class="icon">2</div>
                <h3>Kurulumu Çalıştır</h3>
                <p>İndirdiğin kurulum dosyasını aç ve yönergeleri takip ederek kurulumu tamamla.</p>
            </div>

            <div class="card">
                <div class="icon">3</div>
                <h3>Giriş Yap</h3>
                <p>Üye hesabınla giriş yap, paketin varsa avantajlarınla oyun masasına katıl.</p>
            </div>
        </div>
    </div>
</section>

<section class="section">
    <div class="container">
        <div class="section-head">
            <div>
                <h2>📦 İndirme Dosyaları</h2>
                <p>Admin panelden eklenen tüm aktif indirme dosyaları burada görünür.</p>
            </div>
        </div>

        <div class="download-grid">
            <?php if ($downloads): ?>
                <?php foreach ($downloads as $item): ?>
                    <article class="card download-card">
                        <div class="download-card-image">
                            <img src="<?= site_e(download_image($item)) ?>" alt="<?= site_e(site_val($item, ['title','name'], 'Okey Dosyası')) ?>">
                            <span class="download-card-label">⬇️ İndirme</span>
                        </div>

                        <div class="download-card-body">
                            <h3><?= site_e(site_val($item, ['title','name'], 'Okey Dosyası')) ?></h3>

                            <p><?= site_e(site_short(site_val($item, ['description','summary'], 'Okey oyun dosyası.'), 120)) ?></p>

                            <div class="file-meta">
                                <span>🔖 <?= site_e(site_val($item, ['version'], 'Güncel')) ?></span>
                                <span>📦 <?= site_e(site_val($item, ['file_size','size'], 'Dosya')) ?></span>
                                <span>📅 <?= site_e(site_date_value(site_val($item, ['updated_at','created_at'], ''))) ?></span>
                            </div>

                            <a href="<?= site_e(download_file_url($item)) ?>" class="btn btn-gold download-btn">
                                İndir
                            </a>
                        </div>
                    </article>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="empty">
                    Henüz aktif indirme dosyası bulunamadı.
                </div>
            <?php endif; ?>
        </div>
    </div>
</section>

<section class="section">
    <div class="container">
        <div class="section-head">
            <div>
                <h2>💻 Sistem Gereksinimleri</h2>
                <p>Oyunu sorunsuz çalıştırmak için önerilen temel gereksinimler.</p>
            </div>
        </div>

        <div class="req-grid">
            <div class="card">
                <div class="icon">🪟</div>
                <h3>Windows</h3>
                <p>Windows 7, 8, 10 veya 11 işletim sistemi ile uyumludur.</p>
            </div>

            <div class="card">
                <div class="icon">🌐</div>
                <h3>İnternet</h3>
                <p>Kesintisiz oyun deneyimi için stabil internet bağlantısı önerilir.</p>
            </div>

            <div class="card">
                <div class="icon">🛡️</div>
                <h3>Güvenli Kurulum</h3>
                <p>Dosyayı sadece resmi indirme sayfasından indirmeniz önerilir.</p>
            </div>
        </div>
    </div>
</section>

<section class="section">
    <div class="container">
        <div class="safe-box">
            <div>
                <h2>Güvenli İndirme Alanı</h2>
                <p>101 Batman Okey dosyaları admin panelden yönetilir. Aktif edilen dosyalar bu sayfada otomatik görünür ve kullanıcılar en güncel sürüme kolayca ulaşır.</p>
            </div>

            <div class="safe-list">
                <div class="safe-item">✅ Admin kontrollü dosyalar</div>
                <div class="safe-item">✅ Güncel sürüm bilgisi</div>
                <div class="safe-item">✅ Mobil uyumlu indirme sayfası</div>
            </div>
        </div>
    </div>
</section>

<footer class="footer">
    <div>Powered By <?= date('Y') ?> <a href="https://www.radyohost.biz" target="_blank">🌐RadyoHost.BiZ</a><span> Web Teknolojileri</span></div>
    <div class="footer-center">Premium Okey ve Sohbet Platformu</div>
    <div class="footer-right">
        Developed By VoLKaN75
        <a href="https://wa.me/905415917536" target="_blank">
            <img src="uploads/footer/whatsapp.png" alt="WhatsApp">
        </a>
    </div>
</footer>

</main>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const mobileMenuBtn = document.getElementById('mobileMenuBtn');
    const mainMenu = document.getElementById('mainMenu');

    if (mobileMenuBtn && mainMenu) {
        mobileMenuBtn.addEventListener('click', function () {
            const isOpen = mainMenu.classList.toggle('active');
            mobileMenuBtn.classList.toggle('active', isOpen);
            mobileMenuBtn.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
        });

        mainMenu.querySelectorAll('a').forEach(function (link) {
            link.addEventListener('click', function () {
                mainMenu.classList.remove('active');
                mobileMenuBtn.classList.remove('active');
                mobileMenuBtn.setAttribute('aria-expanded', 'false');
            });
        });

        window.addEventListener('resize', function () {
            if (window.innerWidth > 768) {
                mainMenu.classList.remove('active');
                mobileMenuBtn.classList.remove('active');
                mobileMenuBtn.setAttribute('aria-expanded', 'false');
            }
        });
    }
});
</script>

</body>
</html>