<?php
include 'config.php';
// --- LOGIKA TRACKING PENGUNJUNG ---
$ip = $_SERVER['REMOTE_ADDR'];
$date = date('Y-m-d');

// Cek apakah IP ini sudah berkunjung hari ini
$check_visitor = mysqli_query($conn, "SELECT * FROM visitor_stats WHERE ip_address='$ip' AND visit_date='$date'");
if (mysqli_num_rows($check_visitor) == 0) {
    mysqli_query($conn, "INSERT INTO visitor_stats (ip_address, visit_date) VALUES ('$ip', '$date')");
} else {
    mysqli_query($conn, "UPDATE visitor_stats SET last_activity=NOW() WHERE ip_address='$ip' AND visit_date='$date'");
}
// Ambil parameter slug dan kategori
$slug = isset($_GET['s']) ? mysqli_real_escape_string($conn, $_GET['s']) : '';
$cat  = isset($_GET['cat']) ? mysqli_real_escape_string($conn, $_GET['cat']) : '';

// Inisialisasi variabel item
$item = null;
if ($slug) {
    $query = mysqli_query($conn, "SELECT * FROM fashion_products WHERE slug = '$slug'");
    $item = mysqli_fetch_assoc($query);

    // Jika produk tidak ditemukan, balikkan ke home
    if (!$item) {
        header("Location: index.php");
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <base href="https://serverp3d.xyz/">
    
    <title><?php echo ($slug) ? $item['title'] . " | Sercerp3d" : "Katalog Fashion & Rumah Tangga Viral 2026"; ?></title>
    
    <?php 
        if ($slug) {
            $meta_desc = substr(strip_tags($item['description']), 0, 160) . "... Cek harga & review lengkap di sini.";
        } else {
            $meta_desc = "Temukan rekomendasi fashion OOTD, hijab pashmina, tas vintage, hingga perabotan rumah tangga viral 2026 hanya di Sercerp3d.";
        }
    ?>
    <meta name="description" content="<?php echo $meta_desc; ?>">
    <meta name="keywords" content="OOTD Shopee, Fashion Viral 2026, Pashmina Kaos, Kerudung Paris, Sercerp3d, Rekomendasi TikTok">

    <meta property="og:type" content="website">
    <meta property="og:url" content="https://serverp3d.xyz/<?php echo ($slug) ? $item['slug'] : ''; ?>">
    <meta property="og:title" content="<?php echo ($slug) ? $item['title'] : "Katalog Produk Viral 2026"; ?>">
    <meta property="og:description" content="<?php echo $meta_desc; ?>">
    <meta property="og:image" content="<?php echo ($slug) ? $item['image_url'] : 'https://serverp3d.xyz/logo.jpg'; ?>">
    <meta property="og:site_name" content="Sercerp3d">

    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="<?php echo ($slug) ? $item['title'] : "Katalog Produk Viral"; ?>">
    <meta name="twitter:image" content="<?php echo ($slug) ? $item['image_url'] : 'https://serverp3d.xyz/logo.jpg'; ?>">

    <link rel="canonical" href="https://serverp3d.xyz/<?php echo ($slug) ? $item['slug'] : ''; ?>">

    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;700;800&display=swap');
        body { font-family: 'Plus Jakarta Sans', sans-serif; }
        .masonry { column-count: 2; column-gap: 0.75rem; }
        .item { break-inside: avoid; margin-bottom: 0.75rem; }
        .no-scrollbar::-webkit-scrollbar { display: none; }
    </style>
</head>

<?php if ($slug): ?>
    <body class="bg-gray-100 antialiased">
        <div class="max-w-md mx-auto bg-white min-h-screen pb-32 shadow-2xl relative">
            <div class="p-4 border-b sticky top-0 bg-white/90 backdrop-blur-md z-50 flex items-center">
                <a href="index.php" class="text-gray-800 flex items-center gap-1 text-sm font-bold">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" />
                    </svg>
                    Kembali
                </a>
            </div>

            <div class="relative">
                <img src="<?php echo $item['image_url']; ?>" class="w-full aspect-square object-cover" alt="<?php echo $item['title']; ?>">
                <div class="absolute bottom-4 left-4">
                    <span class="bg-black/50 backdrop-blur-md text-white text-[10px] px-3 py-1 rounded-full font-bold uppercase tracking-widest">
                        <?php echo $item['platform']; ?>
                    </span>
                </div>
            </div>

            <div class="p-6">
                <div class="flex flex-col">
                    <span class="text-orange-600 font-extrabold text-3xl mb-1">Rp <?php echo number_format($item['price'], 0, ',', '.'); ?></span>
                    <h1 class="text-xl font-black text-gray-900 leading-tight"><?php echo $item['title']; ?></h1>
                </div>

                <div class="mt-6">
                    <h3 class="text-xs font-bold text-gray-400 uppercase tracking-widest mb-2">Review / Deskripsi</h3>
                    <div class="p-4 bg-gray-50 rounded-2xl border border-dashed border-gray-300">
                        <p class="text-gray-600 text-sm leading-relaxed italic">
                            "<?php echo $item['description']; ?>"
                        </p>
                    </div>
                </div>
            </div>

            <div class="fixed bottom-0 max-w-md w-full p-4 bg-white/80 backdrop-blur-lg border-t border-gray-100 z-50">
                <a href="<?php echo $item['affiliate_link']; ?>" target="_blank" rel="nofollow" 
                   class="block w-full bg-gradient-to-r from-orange-500 to-red-600 text-white text-center py-4 rounded-2xl font-black text-lg shadow-xl shadow-orange-200 transition active:scale-95 uppercase tracking-tighter">
                   Check Out di <?php echo $item['platform']; ?>
                </a>
            </div>
        </div>
    </body>

<?php else: ?>
    <body class="bg-gray-50 antialiased">
        <div class="max-w-md mx-auto bg-white min-h-screen shadow-2xl">
            <header class="p-6 pt-10 text-center">
                <div class="inline-block bg-orange-100 text-orange-600 text-[10px] font-black px-3 py-1 rounded-full mb-2 uppercase tracking-widest">
                    Viral 2026 🔥
                </div>
                <h1 class="text-4xl font-black text-gray-900 tracking-tighter">OOTD</h1>
                <p class="text-gray-400 text-xs mt-1">Koleksi Produk Terpilih & Viral</p>
            </header>

            <div class="flex gap-2 overflow-x-auto px-4 pb-6 no-scrollbar sticky top-0 bg-white/80 backdrop-blur-md z-40">
                <a href="index.php" class="whitespace-nowrap px-5 py-2.5 rounded-full text-xs font-bold transition <?php echo !$cat ? 'bg-black text-white shadow-lg shadow-black/20' : 'bg-gray-100 text-gray-500 hover:bg-gray-200'; ?>">Semua</a>
                <a href="?cat=Lebaran" class="whitespace-nowrap px-5 py-2.5 rounded-full text-xs font-bold transition <?php echo $cat=='Lebaran' ? 'bg-orange-500 text-white shadow-lg shadow-orange-200' : 'bg-orange-50 text-orange-600 hover:bg-orange-100'; ?>">🌙 Lebaran</a>
                <a href="?cat=Rumah" class="whitespace-nowrap px-5 py-2.5 rounded-full text-xs font-bold transition <?php echo $cat=='Rumah' ? 'bg-orange-500 text-white shadow-lg shadow-orange-200' : 'bg-gray-100 text-gray-500 hover:bg-gray-200'; ?>">🏠 Rumah</a>
                <a href="?cat=Fashion" class="whitespace-nowrap px-5 py-2.5 rounded-full text-xs font-bold transition <?php echo $cat=='Fashion' ? 'bg-orange-500 text-white shadow-lg shadow-orange-200' : 'bg-gray-100 text-gray-500 hover:bg-gray-200'; ?>">👗 Fashion</a>
            </div>

            <div class="px-4 pb-20">
                <div class="masonry">
                    <?php
                    $where = $cat ? "WHERE category = '$cat'" : "";
                    $res = mysqli_query($conn, "SELECT * FROM fashion_products $where ORDER BY id DESC");
                    
                    if(mysqli_num_rows($res) > 0) {
                        while($row = mysqli_fetch_assoc($res)) {
                            // Link menggunakan Clean URL (memerlukan .htaccess)
                            $product_link = $row['slug'];
                    ?>
                        <a href="<?php echo $product_link; ?>" class="item block bg-white rounded-3xl border border-gray-100 shadow-sm overflow-hidden hover:shadow-xl transition duration-300 group">
                            <div class="relative overflow-hidden">
                                <img src="<?php echo $row['image_url']; ?>" class="w-full object-cover group-hover:scale-110 transition duration-500" alt="<?php echo $row['title']; ?>">
                            </div>
                            <div class="p-3">
                                <h2 class="text-[11px] font-bold text-gray-800 leading-tight mb-1"><?php echo $row['title']; ?></h2>
                                <p class="text-orange-600 font-black text-[12px]">Rp <?php echo number_format($row['price'], 0, ',', '.'); ?></p>
                            </div>
                        </a>
                    <?php 
                        } 
                    } else {
                        echo '<div class="col-span-2 text-center py-20 text-gray-300 text-xs font-bold italic uppercase tracking-widest">Belum ada produk...</div>';
                    }
                    ?>
                </div>
            </div>

            <footer class="p-10 text-center border-t border-gray-50">
                <p class="text-[10px] text-gray-300 font-bold uppercase tracking-[0.2em]">© 2026 Serverp3d Project</p>
            </footer>
        </div>
    </body>
<?php endif; ?>
</html>