<?php
/**
 * Konum: /magaza/admin/sayac/ziyaretci-detay.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';

adminCheck(); // Sisteminizin yetki kontrol fonksiyonu

$ip = $_GET['ip'] ?? null;
if (!$ip) die("IP adresi belirtilmedi.");

// Zaman sütununuz created_at olarak var sayıldı. Eğer visit_time ise ona göre düzeltebilirsiniz.
$sql = "SELECT * FROM visitor_logs WHERE ip_address = ? ORDER BY id DESC LIMIT 500";
$stmt = $db->prepare($sql);
$stmt->execute([$ip]);
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (empty($logs)) die("<div class='alert alert-warning m-4'>Kayıt bulunamadı.</div>");

$first_visit = end($logs);
$last_visit = reset($logs);
$total_views = count($logs);

require_once '../header.php';
?>

<div class="row mb-4 align-items-center text-dark">
    <div class="col-md-12 d-flex align-items-center">
        <a href="stats.php" class="btn btn-light border shadow-sm me-3"><i class="bi bi-arrow-left"></i></a>
        <div>
            <h2 class="fw-bold mb-0">Ziyaretçi: <span class="badge bg-light text-primary border font-monospace"><?= htmlspecialchars($ip) ?></span></h2>
            <p class="text-muted small mb-0 mt-1">Bu IP adresinin sitedeki ayak izleri.</p>
        </div>
    </div>
</div>

<div class="card shadow-sm border-0 mb-4 bg-light">
    <div class="card-body p-4">
        <div class="row text-center text-md-start">
            <div class="col-md-3 mb-3 mb-md-0 border-md-end border-light-subtle">
                <span class="d-block small fw-bold text-muted text-uppercase mb-1">İlk Görülme</span>
                <span class="fw-bold fs-5 text-dark"><?= isset($first_visit['created_at']) ? date('d.m.Y H:i', strtotime($first_visit['created_at'])) : '-' ?></span>
            </div>
            <div class="col-md-3 mb-3 mb-md-0 border-md-end border-light-subtle">
                <span class="d-block small fw-bold text-muted text-uppercase mb-1">Son Görülme</span>
                <span class="fw-bold fs-5 text-dark"><?= isset($last_visit['created_at']) ? date('d.m.Y H:i', strtotime($last_visit['created_at'])) : '-' ?></span>
            </div>
            <div class="col-md-3 mb-3 mb-md-0 border-md-end border-light-subtle">
                <span class="d-block small fw-bold text-muted text-uppercase mb-1">Sayfa Gezdi</span>
                <span class="fw-bold fs-4 text-primary"><?= $total_views ?></span>
            </div>
            <div class="col-md-3">
                <span class="d-block small fw-bold text-muted text-uppercase mb-1">Durum</span>
                <span class="fw-bold fs-5 text-secondary">Misafir</span>
            </div>
        </div>
    </div>
</div>

<div class="card shadow-sm border-0 mb-5 text-dark">
    <div class="card-body p-4">
        <h5 class="fw-bold mb-4 border-bottom pb-3"><i class="bi bi-geo-alt-fill text-danger me-2"></i> Ziyaretçi Yolculuğu</h5>
        
        <div class="border-start border-2 border-primary ms-3 ps-4 position-relative">
            <?php foreach($logs as $log): 
                $is_conversion = (strpos($log['request_url'], 'kayit.php') !== false || strpos($log['request_url'], 'sepet') !== false);
                $color = $is_conversion ? 'success' : 'secondary';
                $icon = $is_conversion ? 'bi-check-lg' : 'bi-cursor-fill';
                $visitTime = isset($log['created_at']) ? strtotime($log['created_at']) : time();
            ?>
            <div class="mb-4 position-relative">
                <span class="position-absolute top-0 start-0 translate-middle p-2 bg-<?= $color ?> rounded-circle border border-white" style="left: -1.5rem !important; border-width: 4px !important;">
                    <i class="bi <?= $icon ?> text-white" style="font-size: 0.7rem; line-height: 0;"></i>
                </span>
                
                <div class="card border border-light bg-light shadow-sm">
                    <div class="card-body p-3 d-flex flex-column flex-md-row justify-content-between align-items-md-start">
                        <div>
                            <div class="fw-bold text-dark mb-1" style="word-break: break-all;">
                                <?= htmlspecialchars($log['request_url']) ?>
                            </div>
                            <div class="small text-muted d-flex align-items-center">
                                <i class="bi bi-box-arrow-in-down-right me-2 text-secondary"></i>
                                <?= $log['referer_url'] == 'direct' ? 'Doğrudan Giriş' : htmlspecialchars($log['referer_url']) ?>
                            </div>
                        </div>
                        <div class="text-md-end mt-2 mt-md-0 min-vw-10">
                            <div class="fw-bold text-secondary"><?= date('H:i:s', $visitTime) ?></div>
                            <div class="small text-muted" style="font-size: 0.7rem;"><?= date('d.m.Y', $visitTime) ?></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        </div>
</div>

<?php require_once '../footer.php'; ?>