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

/*
|--------------------------------------------------------------------------
| ÜYELİK / PAKET TAKİP EKRANI
|--------------------------------------------------------------------------
| membership-tracking.php
| Gold / Premium / VIP paketli üyeleri takip ekranı
| Developed By VoLKaN75
|--------------------------------------------------------------------------
*/

declare(strict_types=1);

$pageTitle = 'Üyelik Takibi';
$pageDescription = 'Gold, Premium ve VIP paket atanmış üyeleri takip edin.';

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

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

function mt_e($value): string
{
    return function_exists('admin_e')
        ? admin_e($value)
        : htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

try {
    $stmt = $pdo->query("
        SELECT
            up.id,
            up.user_id,
            up.package_id,
            up.package_type,
            up.package_name,
            up.start_date,
            up.end_date,
            up.status AS package_status,
            up.created_at AS assigned_at,
            u.name,
            u.username,
            u.email,
            u.phone,
            u.status AS user_status,
            p.title AS package_title,
            p.duration_days,
            p.price
        FROM user_packages up
        LEFT JOIN users u ON u.id = up.user_id
        LEFT JOIN packages p ON p.id = up.package_id
        ORDER BY up.id DESC
    ");

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (Throwable $e) {
    $message = 'Üyelik takip kayıtları alınırken sistem hatası oluştu.';
    $messageType = 'error';
}

$total = count($rows);
$active = 0;
$expired = 0;
$cancelled = 0;

foreach ($rows as $r) {
    $status = (string)($r['package_status'] ?? '');

    if ($status === 'active') {
        $active++;
    } elseif ($status === 'expired') {
        $expired++;
    } elseif ($status === 'cancelled') {
        $cancelled++;
    }
}
?>

<style>
.tracking-page{display:grid;gap:22px}
.tracking-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)}
.tracking-hero h2{margin:0 0 8px;color:#f5b642;font-size:28px}
.tracking-hero p{margin:0;color:#aeb7c7;font-weight:700}
.stat-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:16px}
.stat-card{padding:22px;border-radius:26px;background:rgba(255,255,255,.055);border:1px solid rgba(255,255,255,.09)}
.stat-card strong{display:block;color:#f5b642;font-size:28px;margin-bottom:6px}
.stat-card span{color:#9aa3b2;font-weight:800;font-size:13px}
.table-card{overflow:hidden;border-radius:30px;background:rgba(255,255,255,.055);border:1px solid rgba(255,255,255,.09)}
.table-wrap{width:100%;overflow-x:auto}
.tracking-table{width:100%;min-width:1150px;border-collapse:collapse}
.tracking-table th,.tracking-table td{padding:16px;border-bottom:1px solid rgba(255,255,255,.07);text-align:left;color:#dce3ef;font-size:14px}
.tracking-table th{color:#f5b642;background:rgba(0,0,0,.18);font-weight:900}
.tracking-table tr:hover td{background:rgba(255,255,255,.035)}
.user-box strong{display:block;color:#fff;margin-bottom:4px}
.user-box span{display:block;color:#9aa3b2;font-size:12px;font-weight:700}
.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,.14);color:#f5b642;border:1px solid rgba(245,182,66,.25)}
.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-expired{background:rgba(255,180,0,.14);color:#f5b642;border:1px solid rgba(245,182,66,.24)}
.badge-cancelled{background:rgba(185,0,0,.18);color:#ffdede;border:1px solid rgba(255,80,80,.25)}
.badge-none{background:rgba(255,255,255,.08);color:#9aa3b2;border:1px solid rgba(255,255,255,.12)}
.action-group{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}
.alert{padding:15px 18px;border-radius:16px;font-weight:800;background:rgba(185,0,0,.20);border:1px solid rgba(255,80,80,.30);color:#ffdede}
.empty{padding:30px;text-align:center;color:#aeb7c7;font-weight:800}
@media(max-width:900px){.stat-grid{grid-template-columns:repeat(2,1fr)}}
@media(max-width:560px){.stat-grid{grid-template-columns:1fr}}
</style>

<section class="tracking-page">

    <div class="tracking-hero">
        <h2>Üyelik / Paket Takibi</h2>
        <p>Üyelere tanımlanmış Gold, Premium ve VIP paketleri tek ekrandan takip edin.</p>
    </div>

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

    <div class="stat-grid">
        <div class="stat-card">
            <strong><?= mt_e($total) ?></strong>
            <span>Toplam Kayıt</span>
        </div>
        <div class="stat-card">
            <strong><?= mt_e($active) ?></strong>
            <span>Aktif Paket</span>
        </div>
        <div class="stat-card">
            <strong><?= mt_e($expired) ?></strong>
            <span>Süresi Dolan</span>
        </div>
        <div class="stat-card">
            <strong><?= mt_e($cancelled) ?></strong>
            <span>İptal Edilen</span>
        </div>
    </div>

    <div class="table-card">
        <div class="table-wrap">
            <table class="tracking-table">
                <thead>
                    <tr>
                        <th>Üye</th>
                        <th>E-Posta</th>
                        <th>Telefon</th>
                        <th>Paket Türü</th>
                        <th>Paket Adı</th>
                        <th>Fiyat</th>
                        <th>Başlangıç</th>
                        <th>Bitiş</th>
                        <th>Kalan</th>
                        <th>Durum</th>
                        <th>İşlem</th>
                    </tr>
                </thead>
                <tbody>
                <?php if ($rows): ?>
                    <?php foreach ($rows as $row): ?>
                        <?php
                        $type = strtolower((string)($row['package_type'] ?? ''));
                        $status = strtolower((string)($row['package_status'] ?? ''));
                        $typeClass = $type === 'vip' ? 'badge-vip' : ($type === 'premium' ? 'badge-premium' : 'badge-gold');
                        $statusClass = $status === 'cancelled' ? 'badge-cancelled' : ($status === 'expired' ? 'badge-expired' : 'badge-active');

                        $remaining = 'Süresiz';
                        $remainingClass = 'badge-none';

                        if (!empty($row['end_date'])) {
                            $endTime = strtotime((string)$row['end_date']);
                            if ($endTime !== false) {
                                $days = (int)ceil(($endTime - time()) / 86400);
                                if ($days <= 0) {
                                    $remaining = 'Bitti';
                                    $remainingClass = 'badge-expired';
                                } else {
                                    $remaining = $days . ' Gün';
                                    $remainingClass = $days <= 7 ? 'badge-expired' : 'badge-active';
                                }
                            }
                        }
                        ?>
                        <tr>
                            <td>
                                <div class="user-box">
                                    <strong><?= mt_e($row['name'] ?? '-') ?></strong>
                                    <span>@<?= mt_e($row['username'] ?? '-') ?></span>
                                </div>
                            </td>

                            <td><?= mt_e($row['email'] ?? '-') ?></td>
                            <td><?= mt_e($row['phone'] ?? '-') ?></td>

                            <td>
                                <span class="badge <?= mt_e($typeClass) ?>">
                                    <?= mt_e(strtoupper($type ?: '-')) ?>
                                </span>
                            </td>

                            <td><?= mt_e($row['package_name'] ?: ($row['package_title'] ?? '-')) ?></td>
                            <td><?= mt_e(number_format((float)($row['price'] ?? 0), 2, ',', '.')) ?> ₺</td>
                            <td><?= mt_e($row['start_date'] ?? '-') ?></td>
                            <td><?= mt_e($row['end_date'] ?? 'Süresiz') ?></td>

                            <td>
                                <span class="badge <?= mt_e($remainingClass) ?>">
                                    <?= mt_e($remaining) ?>
                                </span>
                            </td>

                            <td>
                                <span class="badge <?= mt_e($statusClass) ?>">
                                    <?= mt_e($status ?: '-') ?>
                                </span>
                            </td>

                            <td>
                                <div class="action-group">
                                    <a href="user-detail.php?id=<?= mt_e($row['user_id'] ?? '') ?>" class="small-btn">Detay</a>
                                    <a href="user-edit.php?id=<?= mt_e($row['user_id'] ?? '') ?>" class="small-btn">Düzenle</a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="11">
                            <div class="empty">Henüz paket atanmış üye bulunamadı.</div>
                        </td>
                    </tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</section>

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