<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);

// Tangkap ID jika ada (misal: /result/45), jika tidak ada berarti halaman utama /result
$id_pasaran = isset($_GET['id']) ? preg_replace('/[^0-9]/', '', $_GET['id']) : '';

// Ambil parameter lain (seperti ?page=2)
$query_params = $_GET;
unset($query_params['id']);

// Bangun URL Target ke arah Data Result
$target_url = "https://blog9.inatips.com/client/blog/result";
if ($id_pasaran !== '') {
    $target_url .= "/" . $id_pasaran;
}
if (!empty($query_params)) {
    $target_url .= '?' . http_build_query($query_params);
}

// Proses Menyedot HTML secara LIVE
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)');

$html = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($http_code == 200 && $html) {
    
    // 1. Bersihkan script Cloudflare
    $html = preg_replace('/<script defer src="https:\/\/static\.cloudflareinsights\.com.*?<\/script>/is', '', $html);

    // 2. CSS SAKTI: Mindahin Sidebar ke Kanan & Basmi Tombol Kontak Tanpa Ngerusak Struktur
    $custom_style = "
    <style>
        /* --- ATURAN MOBILE (DI BAWAH 1024px) --- */
        @media (max-width: 1023px) {
            nav.sidebar {
                top: 65px !important; 
                right: 0 !important;
                left: auto !important;
                height: calc(100vh - 65px) !important;
                z-index: 9999 !important;
                display: block !important;
            }

            /* Sembunyikan sidebar saat tombol silang/burger ditekan */
            body:not(.toggle-sidebar) nav.sidebar {
                right: -260px !important; 
            }
            body.toggle-sidebar nav.sidebar {
                right: 0 !important;
            }
        }

        /* --- ATURAN DESKTOP (1024px KE ATAS) --- */
        @media (min-width: 1024px) {
            nav.sidebar {
                display: none !important; /* Hilangkan sidebar di versi PC */
            }
        }

        /* BASMI TOMBOL KONTAK (WA, TELE, DLL) */
        header .flex-1.py-2.5.flex.items-center, 
        header a[href*='whatsapp'],
        header a[href*='t.me'], 
        header a[href*='livechat'],
        header a[href*='download'],
        header a[href*='daftar'] {
            display: none !important;
        }

        /* Matikan Sticky Header Result */
        .sticky.top-\\[167px\\], .sticky.sm\\:top-\\[118px\\] {
            position: relative !important;
            top: 0 !important;
        }
    </style>";
    
    // Injeksi CSS ke dalam Head
    $html = str_replace('</head>', $custom_style . '</head>', $html);
    
    // 2. Banner buatan kita sendiri
    $banner_kita = '
    <div class="swiper-wrapper">
        <div class="overflow-hidden bg-center bg-cover rounded-md sm:max-w-sm swiper-slide">
            <img src="https://cloudassetskita.com/uploads/new-wbo-7680ad23.webp" />
        </div>
        <div class="overflow-hidden bg-center bg-cover rounded-md sm:max-w-sm swiper-slide">
            <img src="https://cloudassetskita.com/uploads/to-wbo-f033171a.webp" />
        </div>
        <div class="overflow-hidden bg-center bg-cover rounded-md sm:max-w-sm swiper-slide">
            <img src="https://cloudassetskita.com/uploads/harian-wbo-ea4597c9.webp" />
        </div>
    </div>';

    // 3. Potong dan sambung Banner dengan presisi
    $potong_atas = explode('<div class="swiper-wrapper"', $html);
    if (isset($potong_atas[1])) {
        $potong_bawah = explode('<div class="flex flex-col items-center w-full gap-2 px-2 pb-2 sm:px-4 sm:gap-3 sm:flex-row">', $potong_atas[1]);
        if (isset($potong_bawah[1])) {
            $html = $potong_atas[0] . $banner_kita . "\n</div>\n</div>\n\n<div class=\"flex flex-col items-center w-full gap-2 px-2 pb-2 sm:px-4 sm:gap-3 sm:flex-row\">" . $potong_bawah[1];
        }
    }
    
    // 4. INJEKSI LOGO DAN LINK TOMBOL KITA
    $logo_kita = '<a href="/" class="flex items-center main-logo shrink-0"><img src="https://cloudassetskita.com/uploads/logo-a11fcbc3.webp" class="inline max-h-9"></a>';
    $html = preg_replace('/<a href="\/" class="flex items-center main-logo shrink-0">.*?<\/a>/is', $logo_kita, $html);

    $html = str_replace('https://sorty.bio/ina1', 'https://rajarestoran.com/wbototo', $html); 
    $html = str_replace('https://sorty.bio/apkina', 'https://rajarestoran.com/wbototo', $html); 
    $html = str_replace('https://sorty.bio/ina', 'https://rajarestoran.com/wbototo', $html); 
    $html = str_replace('https://sorty.bio/Inapromo', 'https://rajarestoran.com/promowbo', $html); 
    $html = str_replace('https://sorty.bio/rtpina/', 'https://rajarestoran.com/rtpwbo', $html); 
    $html = str_replace('https://sorty.bio/buktiina/', 'https://rajarestoran.com/buktiwbo', $html); 
    $html = str_replace('https://t.me/inatogel88_official', 'https://t.me/wbototo', $html);
    $html = str_replace('https://secure.livechatinc.com/customer/action/open_chat?license_id=13079895', 'https://rajarestoran.com/livechatwbo', $html);
    $html = str_replace('6281389114046', '6282312855613', $html); 
    
    // 5. Perbaiki link gambar (bendera / ikon pasaran)
    $html = str_replace('src="/storage/', 'src="https://blog9.inatips.com/storage/', $html);
    
    // 6. Ganti Teks Nama Web
    $html = str_replace('blog9.inatips.com', 'pusat-prediksi.com', $html);
    $html = str_replace('Inatogel', 'WBOTOTO', $html);
    $html = str_replace('inatogel', 'wbototo', $html);

    // ==============================================================
    // 7. TAMBAHAN DARI SAYA: HAPUS SIFAT "STICKY" BIAR BISA DI-SCROLL
    // ==============================================================
    $html = str_replace('sticky top-[167px] sm:top-[118px]', '', $html);
    $html = str_replace('sticky sm:top-[118px] top-[167px]', '', $html);

    // ==============================================================
    // 8. TAMBAHAN DARI SAYA: INJEKSI SCRIPT ANTI-TOMBOL BAWAAN
    // ==============================================================
    $js_satpam = "
    <script>
        setInterval(() => {
            const headerLinks = document.querySelectorAll('header a');
            headerLinks.forEach(link => {
                const text = link.innerText.toLowerCase();
                const href = link.href.toLowerCase();
                if (href.includes('whatsapp') || href.includes('t.me') || href.includes('livechat') || text.includes('download aplikasi') || text.includes('daftar jadi member') || text.includes('telegram')) {
                    link.style.display = 'none';
                    if (link.parentElement && link.parentElement.classList.contains('flex')) {
                        link.parentElement.style.display = 'none';
                    }
                }
            });
        }, 300); // Cek tiap 0.3 detik
    </script>
    </body>";
    
    // Pasang scriptnya tepat sebelum tag </body>
    $html = str_replace('</body>', $js_satpam, $html);
    
    // Tampilkan hasilnya!
    echo $html;
    exit;
} else {
    echo "<h2 style='text-align:center; margin-top:50px; color:white;'>Halaman Data Result tidak ditemukan.</h2>";
    exit;
}
?>