<?php
require_once __DIR__ . '/../../database/connect.php';
session_start();

$kul_id = $_SESSION['kul_id'] ?? '';
if (empty($kul_id)) exit;

// Logları temizle isteği
if (isset($_POST['temizle_logs'])) {
    $del = $db->prepare("DELETE FROM girisyapanlar WHERE ekleyen = ?");
    $del->execute([$kul_id]);
    echo '<div style="text-align:center;padding:40px;color:#4CAF50;"><i class="fa fa-check-circle" style="font-size:48px;"></i><p style="margin-top:15px;">Tüm loglar temizlendi!</p></div>';
    exit;
}

$etkinlik_limiti = 10;
$simdi = time();

// Tüm logları al
$sql = "SELECT * FROM girisyapanlar WHERE ekleyen = ? ORDER BY son_etkinlik_zamani DESC LIMIT 100";
$query = $db->prepare($sql);
$query->execute([$kul_id]);
$logs = $query->fetchAll(PDO::FETCH_ASSOC);

if (empty($logs)) {
    echo '<div style="text-align:center;padding:60px;color:#666;">
            <i class="fa fa-inbox" style="font-size:64px;opacity:0.3;"></i>
            <p style="margin-top:20px;font-size:16px;">Henüz log kaydı bulunmuyor.</p>
          </div>';
    exit;
}

// Stil
echo '<style>
.log-list { margin: 0; padding: 0; list-style: none; }
.log-item {
    display: flex;
    align-items: center;
    padding: 12px 15px;
    margin-bottom: 8px;
    background: #1a1a2e;
    border-radius: 8px;
    border-left: 4px solid #333;
    transition: all 0.2s;
}
.log-item:hover { background: #252540; }
.log-item.online { border-left-color: #4CAF50; }
.log-item.offline { border-left-color: #666; opacity: 0.7; }

.log-status {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    margin-right: 12px;
    flex-shrink: 0;
}
.log-status.online { background: #4CAF50; box-shadow: 0 0 8px #4CAF50; }
.log-status.offline { background: #666; }

.log-content { flex: 1; }
.log-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}
.log-platform {
    font-weight: 600;
    color: #fff;
    font-size: 14px;
}
.log-time {
    color: #888;
    font-size: 12px;
}
.log-detail {
    color: #aaa;
    font-size: 13px;
}
.log-detail strong { color: #4CAF50; }
.log-detail .ip { color: #888; font-size: 11px; }
.log-progress {
    margin-top: 6px;
    background: #333;
    border-radius: 4px;
    height: 6px;
    overflow: hidden;
}
.log-progress-bar {
    height: 100%;
    background: linear-gradient(90deg, #4CAF50, #8BC34A);
    transition: width 0.3s;
}
</style>';

echo '<ul class="log-list">';

foreach ($logs as $log) {
    $son_etkinlik = $log['son_etkinlik_zamani'];
    $fark = $simdi - $son_etkinlik;
    $durum = $fark <= $etkinlik_limiti ? 'online' : 'offline';
    $tarih = date('d.m.Y H:i:s', $son_etkinlik);
    $platform = htmlspecialchars($log['hizmet'] ?? 'Bilinmiyor');
    $urunadi = htmlspecialchars($log['urunadi'] ?? 'Bilinmiyor');
    $ip = htmlspecialchars($log['ip_adresi'] ?? '0.0.0.0');
    
    // Sayfa ve progress parse et
    $sayfaRaw = $log['sayfa'] ?? '';
    $progress = 0;
    $sayfaAdi = $sayfaRaw;
    
    // Progress bar varsa parse et
    if (preg_match('/(\d+)%/', $sayfaRaw, $m)) {
        $progress = intval($m[1]);
    }
    // Sayfa adını temizle
    $sayfaAdi = preg_replace('/<[^>]*>/', '', $sayfaRaw);
    $sayfaAdi = preg_replace('/\[.*?\]/', '', $sayfaAdi);
    $sayfaAdi = preg_replace('/\d+%/', '', $sayfaAdi);
    $sayfaAdi = trim(strip_tags($sayfaAdi));
    if (empty($sayfaAdi)) $sayfaAdi = 'Sayfa';
    
    echo '<li class="log-item ' . $durum . '">';
    echo '<div class="log-status ' . $durum . '"></div>';
    echo '<div class="log-content">';
    echo '<div class="log-header-row">';
    echo '<span class="log-platform">' . $platform . '</span>';
    echo '<span class="log-time">' . $tarih . '</span>';
    echo '</div>';
    echo '<div class="log-detail">';
    echo '<strong>' . $urunadi . '</strong> - ' . $sayfaAdi;
    echo ' <span class="ip">(IP: ' . $ip . ')</span>';
    echo '</div>';
    if ($progress > 0) {
        echo '<div class="log-progress"><div class="log-progress-bar" style="width:' . $progress . '%"></div></div>';
    }
    echo '</div>';
    echo '</li>';
}

echo '</ul>';
?>