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

/*
|--------------------------------------------------------------------------
| PAKET YÖNETİMİ
|--------------------------------------------------------------------------
| packages.php
| Gold / Premium / VIP paket listeleme ekranı
| Developed By VoLKaN75
|--------------------------------------------------------------------------
*/

declare(strict_types=1);

$pageTitle = 'Paket Yönetimi';
$pageDescription = 'Gold, Premium ve VIP paketleri oluşturun, listeleyin ve yönetin.';

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

$packages = [];
$message = '';
$messageType = '';

function packages_e($value): string
{
    if (function_exists('admin_e')) {
        return admin_e($value);
    }

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

try {
    if (!isset($pdo) || !$pdo instanceof PDO) {
        throw new RuntimeException('Veritabanı bağlantısı bulunamadı.');
    }

    $stmt = $pdo->query("
        SELECT
            id,
            package_type,
            title,
            amount,
            price,
            duration,
            description,
            status,
            created_at
        FROM packages
        ORDER BY FIELD(package_type, 'gold', 'premium', 'vip'), id DESC
    ");

    $packages = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (Throwable $e) {
    $message = 'Packages tablosu okunamadı. Veritabanı bağlantısını ve tablo adını kontrol edin.';
    $messageType = 'error';
}

$grouped = [
    'gold' => [],
    'premium' => [],
    'vip' => [],
];

foreach ($packages as $package) {
    $type = strtolower((string)($package['package_type'] ?? ''));

    if (isset($grouped[$type])) {
        $grouped[$type][] = $package;
    }
}

$groupTitles = [
    'gold' => 'Gold Paketler',
    'premium' => 'Premium Paketler',
    'vip' => 'VIP Paketler',
];

$groupDescriptions = [
    'gold' => 'Gold miktarı tanımlanan üyelik ve oyun paketleri.',
    'premium' => 'Premium kullanıcı ayrıcalıkları için açılan paketler.',
    'vip' => 'VIP kullanıcılar için en üst seviye özel paketler.',
];

$groupBadges = [
    'gold' => 'badge-gold',
    'premium' => 'badge-premium',
    'vip' => 'badge-vip',
];
?>

<style>
.packages-page{
    display:grid;
    gap:22px;
}

.packages-hero{
    padding:28px;
    border-radius:32px;
    background:
        radial-gradient(circle at top left,rgba(245,182,66,.20),transparent 34%),
        linear-gradient(135deg,rgba(255,255,255,.075),rgba(255,255,255,.035));
    border:1px solid rgba(255,255,255,.09);
    box-shadow:0 24px 70px rgba(0,0,0,.18);
}

.packages-hero h2{
    margin:0 0 8px;
    color:#f5b642;
    font-size:28px;
}

.packages-hero p{
    margin:0;
    color:#aeb7c7;
    font-weight:700;
    line-height:1.6;
}

.packages-actions{
    display:flex;
    justify-content:space-between;
    align-items:center;
    gap:14px;
    flex-wrap:wrap;
}

.packages-count{
    color:#fff;
    font-weight:900;
}

.package-section{
    padding:24px;
    border-radius:30px;
    background:rgba(255,255,255,.055);
    border:1px solid rgba(255,255,255,.09);
    box-shadow:0 24px 70px rgba(0,0,0,.16);
}

.package-section-head{
    display:flex;
    align-items:flex-end;
    justify-content:space-between;
    gap:18px;
    margin-bottom:18px;
}

.package-section-head h3{
    margin:0 0 6px;
    color:#f5b642;
    font-size:24px;
}

.package-section-head p{
    margin:0;
    color:#9aa3b2;
    font-weight:700;
    line-height:1.6;
}

.package-grid{
    display:grid;
    grid-template-columns:repeat(3,1fr);
    gap:18px;
}

.package-card{
    position:relative;
    overflow:hidden;
    padding:24px;
    border-radius:26px;
    background:rgba(0,0,0,.24);
    border:1px solid rgba(255,255,255,.08);
    transition:.25s;
}

.package-card:before{
    content:"";
    position:absolute;
    width:160px;
    height:160px;
    border-radius:999px;
    left:-70px;
    top:-80px;
    background:rgba(245,182,66,.10);
}

.package-card:hover{
    transform:translateY(-4px);
    border-color:rgba(245,182,66,.35);
    box-shadow:0 22px 60px rgba(0,0,0,.22);
}

.package-card h4{
    position:relative;
    margin:12px 0 10px;
    color:#fff;
    font-size:21px;
}

.package-card p{
    position:relative;
    margin:0 0 16px;
    color:#aeb7c7;
    line-height:1.6;
    font-weight:700;
    min-height:48px;
}

.price{
    position:relative;
    display:block;
    color:#f5b642;
    font-size:30px;
    font-weight:1000;
    margin-bottom:10px;
}

.meta{
    position:relative;
    display:flex;
    gap:8px;
    flex-wrap:wrap;
    margin-bottom:18px;
}

.badge{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    min-height:30px;
    padding:0 12px;
    border-radius:999px;
    font-size:12px;
    font-weight:900;
    white-space:nowrap;
}

.badge-gold{
    background:rgba(245,182,66,.13);
    color:#f5b642;
    border:1px solid rgba(245,182,66,.22);
}

.badge-premium{
    background:rgba(100,180,255,.14);
    color:#9fd0ff;
    border:1px solid rgba(100,180,255,.25);
}

.badge-vip{
    background:rgba(255,93,177,.14);
    color:#ff8fcb;
    border:1px solid rgba(255,93,177,.25);
}

.badge-active{
    background:rgba(0,150,70,.18);
    color:#d8ffe8;
    border:1px solid rgba(0,255,120,.25);
}

.badge-passive{
    background:rgba(185,0,0,.18);
    color:#ffdede;
    border:1px solid rgba(255,80,80,.25);
}

.badge-info{
    background:rgba(255,255,255,.08);
    color:#dce3ef;
    border:1px solid rgba(255,255,255,.12);
}

.card-actions{
    position:relative;
    display:flex;
    gap:8px;
    flex-wrap:wrap;
}

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

.small-btn:hover{
    border-color:rgba(245,182,66,.35);
    color:#f5b642;
}

.empty-box{
    padding:24px;
    border-radius:22px;
    background:rgba(0,0,0,.20);
    border:1px solid rgba(255,255,255,.08);
    color:#aeb7c7;
    font-weight:800;
    line-height:1.6;
}

.form-alert{
    padding:15px 18px;
    border-radius:16px;
    font-weight:800;
}

.form-alert.error{
    background:rgba(185,0,0,.20);
    border:1px solid rgba(255,80,80,.30);
    color:#ffdede;
}

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

@media(max-width:680px){
    .package-grid{
        grid-template-columns:1fr;
    }

    .package-section-head{
        display:block;
    }
}
</style>

<section class="packages-page">

    <div class="packages-hero">
        <h2>Paket Yönetimi</h2>
        <p>Gold, Premium ve VIP paketleri oluşturun. Paketler üyelik yönetimi ekranından üyelerle birleştirilir.</p>
    </div>

    <div class="packages-actions">
        <div class="packages-count">
            Toplam Paket: <?= packages_e((string)count($packages)) ?>
        </div>

        <a href="package-view.php" class="admin-btn">Yeni Paket Oluştur</a>
    </div>

    <?php if ($message !== ''): ?>
        <div class="form-alert <?= packages_e($messageType) ?>">
            <?= packages_e($message) ?>
        </div>
    <?php endif; ?>

    <?php foreach ($grouped as $type => $items): ?>
        <div class="package-section">
            <div class="package-section-head">
                <div>
                    <h3><?= packages_e($groupTitles[$type]) ?></h3>
                    <p><?= packages_e($groupDescriptions[$type]) ?></p>
                </div>

                <span class="badge <?= packages_e($groupBadges[$type]) ?>">
                    <?= packages_e((string)count($items)) ?> Paket
                </span>
            </div>

            <?php if ($items): ?>
                <div class="package-grid">
                    <?php foreach ($items as $item): ?>
                        <?php
                            $packageType = strtolower((string)($item['package_type'] ?? 'gold'));
                            $typeBadge = $groupBadges[$packageType] ?? 'badge-info';
                            $status = (string)($item['status'] ?? 'active');
                            $duration = trim((string)($item['duration'] ?? ''));
                            $amount = (int)($item['amount'] ?? 0);
                            $createdAt = trim((string)($item['created_at'] ?? ''));
                        ?>

                        <div class="package-card">
                            <span class="badge <?= packages_e($typeBadge) ?>">
                                <?= packages_e(strtoupper($packageType)) ?>
                            </span>

                            <h4><?= packages_e($item['title'] ?? '-') ?></h4>

                            <span class="price">
                                <?= packages_e(number_format((float)($item['price'] ?? 0), 2, ',', '.')) ?> ₺
                            </span>

                            <p><?= packages_e($item['description'] ?? 'Paket açıklaması bulunmuyor.') ?></p>

                            <div class="meta">
                                <span class="badge badge-info">
                                    Miktar: <?= packages_e((string)$amount) ?>
                                </span>

                                <span class="badge badge-info">
                                    Süre: <?= packages_e($duration !== '' ? $duration : '-') ?>
                                </span>

                                <?php if ($status === 'active'): ?>
                                    <span class="badge badge-active">Aktif</span>
                                <?php else: ?>
                                    <span class="badge badge-passive">Pasif</span>
                                <?php endif; ?>

                                <?php if ($createdAt !== ''): ?>
                                    <span class="badge badge-info">
                                        <?= packages_e($createdAt) ?>
                                    </span>
                                <?php endif; ?>
                            </div>

                            <div class="card-actions">
                                <a href="package-edit.php?id=<?= packages_e($item['id'] ?? '') ?>" class="small-btn">Düzenle</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="empty-box">
                    <?= packages_e($groupTitles[$type]) ?> için henüz paket oluşturulmadı.
                </div>
            <?php endif; ?>
        </div>
    <?php endforeach; ?>

</section>

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