<?php
/*
|--------------------------------------------------------------------------
| RadyoHost.BiZ / Okey Platformu
|--------------------------------------------------------------------------
| Sayfa Adı : Müşteri Üye Paneli
| Dosya     : customer/index.php
|--------------------------------------------------------------------------
| Açıklama:
| Üyenin aktif Gold / Premium / VIP paketine göre dinamik tasarlanan
| müşteri panel ana sayfasıdır.
|--------------------------------------------------------------------------
| Modül     : Müşteri Paneli
| Yetki     : Üye
| Sürüm     : V2.0
|--------------------------------------------------------------------------
| Developed By VoLKaN75
|--------------------------------------------------------------------------
*/

declare(strict_types=1);

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

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$pdo = $pdo ?? $db ?? null;

if (!$pdo instanceof PDO) {
    die('Veritabanı bağlantısı bulunamadı.');
}

if (empty($_SESSION['user_id']) && empty($_SESSION['customer_id'])) {
    header('Location: ../login.php');
    exit;
}

$userId = (int)($_SESSION['user_id'] ?? $_SESSION['customer_id']);

function e(string $v): string
{
    return htmlspecialchars($v, ENT_QUOTES, 'UTF-8');
}

function tr_date(?string $date): string
{
    if (!$date || $date === '0000-00-00 00:00:00') {
        return 'Belirtilmemiş';
    }

    try {
        return (new DateTime($date))->format('d.m.Y H:i');
    } catch (Throwable $e) {
        return $date;
    }
}

$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id LIMIT 1");
$stmt->execute(['id' => $userId]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    session_destroy();
    header('Location: ../login.php');
    exit;
}

$name  = $user['name'] ?? $user['username'] ?? 'Üye';
$email = $user['email'] ?? '-';
$phone = $user['phone'] ?? '-';
$image = !empty($user['image']) ? '../uploads/user/' . $user['image'] : '../assets/img/default-user.png';
$status = $user['status'] ?? 'active';

$stmtPackage = $pdo->prepare("
    SELECT up.*, p.title, p.amount, p.price, p.duration, p.description
    FROM user_packages up
    LEFT JOIN packages p ON p.id = up.package_id
    WHERE up.user_id = :user_id
    ORDER BY FIELD(up.status,'active','expired','cancelled'), up.id DESC
    LIMIT 1
");
$stmtPackage->execute(['user_id' => $userId]);
$package = $stmtPackage->fetch(PDO::FETCH_ASSOC);

$packageType = strtolower((string)($package['package_type'] ?? 'gold'));
if (!in_array($packageType, ['gold', 'premium', 'vip'], true)) {
    $packageType = 'gold';
}

$theme = [
    'gold' => ['class' => 'gold', 'icon' => '🪙', 'name' => 'Gold Üye Paneli'],
    'premium' => ['class' => 'premium', 'icon' => '👑', 'name' => 'Premium Üye Paneli'],
    'vip' => ['class' => 'vip', 'icon' => '💎', 'name' => 'VIP Üye Paneli'],
][$packageType];

$packageName = $package['title'] ?? $package['package_name'] ?? 'Henüz Paket Atanmamış';
$packageEnd  = $package['end_date'] ?? null;
$packageStatus = $package['status'] ?? 'offline';
$activeCount = 0;

$countStmt = $pdo->prepare("SELECT COUNT(*) FROM user_packages WHERE user_id = :id AND status = 'active'");
$countStmt->execute(['id' => $userId]);
$activeCount = (int)$countStmt->fetchColumn();
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Üye Paneli | RadyoHost Okey</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
*{box-sizing:border-box}
body{
    margin:0;
    font-family:Arial,Helvetica,sans-serif;
    color:#fff;
    background:#070914;
}
body.gold{--main:#f5b642;--main2:#ff7a18;--soft:#f5b64222}
body.premium{--main:#38bdf8;--main2:#8b5cf6;--soft:#38bdf822}
body.vip{--main:#ff5db1;--main2:#a855f7;--soft:#ff5db122}
body:before{
    content:"";
    position:fixed;
    inset:0;
    background:
        radial-gradient(circle at 15% 10%,var(--soft),transparent 30%),
        radial-gradient(circle at 85% 20%,var(--soft),transparent 30%),
        linear-gradient(135deg,#070914,#111827,#080b14);
    z-index:-1;
}
a{text-decoration:none;color:inherit}
.panel{min-height:100vh;padding:28px}
.topbar{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:26px;
    gap:20px;
}
.topbar h1{margin:0;font-size:31px}
.topbar p{margin:8px 0 0;color:#cbd5e1}
.logout{
    padding:15px 22px;
    border-radius:18px;
    background:linear-gradient(135deg,#ef4444,#fb7185);
    font-weight:900;
    box-shadow:0 15px 35px rgba(239,68,68,.35);
}
.hero{
    display:grid;
    grid-template-columns:330px 1fr;
    gap:24px;
    margin-bottom:24px;
}
.card{
    background:linear-gradient(145deg,#151b31dd,#0d1224ee);
    border:1px solid #ffffff18;
    border-radius:32px;
    box-shadow:0 25px 80px #0009;
}
.profile{
    padding:30px;
    text-align:center;
}
.profile img{
    width:145px;
    height:145px;
    border-radius:50%;
    object-fit:cover;
    border:5px solid var(--main);
    box-shadow:0 0 50px var(--soft);
}
.profile h2{margin:18px 0 7px}
.profile p{margin:5px 0;color:#aeb7d4}
.badge{
    display:inline-flex;
    margin-top:15px;
    padding:10px 15px;
    border-radius:999px;
    background:var(--soft);
    border:1px solid var(--main);
    color:var(--main);
    font-weight:900;
}
.welcome{
    padding:34px;
    position:relative;
    overflow:hidden;
}
.welcome:after{
    content:"";
    position:absolute;
    right:-90px;
    top:-90px;
    width:260px;
    height:260px;
    border-radius:50%;
    background:var(--soft);
}
.welcome-inner{position:relative}
.welcome h2{margin:0 0 12px;font-size:34px}
.welcome p{color:#cbd5e1;line-height:1.7;max-width:720px}
.package-line{
    display:grid;
    grid-template-columns:repeat(4,1fr);
    gap:15px;
    margin-top:25px;
}
.info{
    padding:18px;
    border-radius:20px;
    background:#ffffff0c;
    border:1px solid #ffffff14;
}
.info span{display:block;color:#94a3b8;font-size:13px;margin-bottom:8px}
.info strong{font-size:17px}
.info.main strong{color:var(--main)}
.quick-grid{
    display:grid;
    grid-template-columns:repeat(4,1fr);
    gap:18px;
}
.quick{
    padding:24px;
    min-height:190px;
    border-radius:28px;
    background:linear-gradient(145deg,#151b31dd,#0d1224ee);
    border:1px solid #ffffff16;
    box-shadow:0 20px 65px #0008;
    transition:.25s;
}
.quick:hover{
    transform:translateY(-6px);
    border-color:var(--main);
}
.icon{
    width:58px;
    height:58px;
    display:grid;
    place-items:center;
    border-radius:20px;
    font-size:27px;
    background:linear-gradient(135deg,var(--main),var(--main2));
    margin-bottom:18px;
}
.quick h3{margin:0 0 10px}
.quick p{margin:0 0 18px;color:#aeb7d4;line-height:1.6;font-size:14px}
.btn{
    display:inline-flex;
    padding:11px 16px;
    border-radius:14px;
    background:#ffffff10;
    border:1px solid #ffffff18;
    font-weight:900;
}
.admin-footer{
    margin-top:30px;
    padding:18px 24px;
    display:grid;
    grid-template-columns:1fr 1fr 1fr;
    gap:18px;
    align-items:center;
    border-radius:24px;
    background:linear-gradient(180deg,rgba(255,255,255,.04),rgba(255,255,255,.015));
    color:#9aa3b2;
    font-size:12px;
    font-weight:800;
}
.footer-link{color:var(--main);font-weight:900}
.footer-center{text-align:center;color:var(--main);font-weight:900;font-size:13px}
.footer-right{text-align:right}
.footer-signature{color:#ff5db1;font-size:14px;font-weight:900}
.footer-whatsapp img{
    width:52px;
    height:52px;
    vertical-align:middle;
    filter:drop-shadow(0 0 10px rgba(37,211,102,.8));
}
@media(max-width:1050px){
    .hero{grid-template-columns:1fr}
    .package-line{grid-template-columns:repeat(2,1fr)}
    .quick-grid{grid-template-columns:repeat(2,1fr)}
}
@media(max-width:700px){
    .panel{padding:18px}
    .topbar{flex-direction:column;align-items:flex-start}
    .package-line,.quick-grid{grid-template-columns:1fr}
    .admin-footer{grid-template-columns:1fr;text-align:center}
    .footer-right{text-align:center}
}
</style>
</head>

<body class="<?= e($theme['class']) ?>">

<div class="panel">

    <div class="topbar">
        <div>
            <h1><?= e($theme['icon'] . ' ' . $theme['name']) ?></h1>
            <p>Profil, paket, yenileme ve admin iletişim merkezi.</p>
        </div>
        <a class="logout" href="logout.php">🚪 Güvenli Çıkış</a>
    </div>

    <div class="hero">

        <div class="card profile">
            <img src="<?= e((string)$image) ?>" alt="Profil Resmi">
            <h2><?= e((string)$name) ?></h2>
            <p><?= e((string)$email) ?></p>
            <p><?= e((string)$phone) ?></p>
            <span class="badge"><?= $status === 'active' ? 'Aktif Üye' : 'Pasif Üye' ?></span>
        </div>

        <div class="card welcome">
            <div class="welcome-inner">
                <h2>Hoş geldin, <?= e((string)$name) ?> 👋</h2>
                <p>
                    Panelin aktif paketine göre özel renklendirildi.
                    Gold, Premium veya VIP üyelik durumuna göre sana özel görünüm hazırlanır.
                </p>

                <div class="package-line">
                    <div class="info main">
                        <span>Aktif Paket</span>
                        <strong><?= e((string)$packageName) ?></strong>
                    </div>

                    <div class="info">
                        <span>Paket Türü</span>
                        <strong><?= strtoupper(e($packageType)) ?></strong>
                    </div>

                    <div class="info">
                        <span>Paket Bitiş</span>
                        <strong><?= e(tr_date($packageEnd)) ?></strong>
                    </div>

                    <div class="info">
                        <span>Aktif Paket Sayısı</span>
                        <strong><?= $activeCount ?></strong>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <div class="quick-grid">

        <a class="quick" href="profile.php">
            <div class="icon">👤</div>
            <h3>Profil Yönetimi</h3>
            <p>Profil bilgilerini, resmini ve şifreni güncelle.</p>
            <span class="btn">Profili Aç</span>
        </a>

        <a class="quick" href="my-package.php">
            <div class="icon">🎁</div>
            <h3>Paket Bilgilerim</h3>
            <p>Aktif ve kapalı paket geçmişini görüntüle.</p>
            <span class="btn">Paketleri Gör</span>
        </a>

        <a class="quick" href="renew-package.php">
            <div class="icon">🔄</div>
            <h3>Paket Talep Et</h3>
            <p>Gold, Premium veya VIP paket talebi oluştur.</p>
            <span class="btn">Talep Gönder</span>
        </a>

        <a class="quick" href="support.php">
            <div class="icon">💬</div>
            <h3>Admin İletişim</h3>
            <p>Paket, ödeme ve destek için yönetime mesaj yaz.</p>
            <span class="btn">Mesaj Yaz</span>
        </a>

    </div>

    <footer class="admin-footer">
        <div>
            <span>Powered By <?= date('Y') ?></span>
            <a href="https://www.radyohost.biz" target="_blank" rel="noopener" class="footer-link">🌐 RadyoHost.BiZ</a>
            <span>Web Teknolojileri</span>
        </div>

        <div class="footer-center">Premium Okey Üye Paneli</div>

        <div class="footer-right">
            <span class="footer-signature">Developed By VoLKaN75</span>
            <a href="https://wa.me/905415917536" target="_blank" rel="noopener" class="footer-whatsapp">
                <img src="../uploads/footer/whatsapp.png" alt="WhatsApp">
            </a>
        </div>
    </footer>

</div>

</body>
</html>