
<?php
// =========================
// Callback halaman admin
// =========================
function check_site_page_callback() {
    ?>
    <div class="wrap pro-check-site">
        <h1 class="pro-title"></h1>
        <h1 class="pro-title">Check Site</h1>

        <!-- Info Notifikasi -->
       <div class="pro-info">
			🔹 Use this feature to fast check the status of a website, see if it is blocked by Cloudflare, and generate a new source for the APK Extractor. The result will show HTTP status, title, H1, size, mod features and description.
		</div>


        <!-- Form URL -->
       <div class="pro-form">
			<div class="floating-input">
				<input type="url" id="url" name="url" placeholder=" " required>
				<label for="url">Enter URL</label>
				<span id="status"></span>
			</div>
			<div class="pro-input-info" style="text-align: left;">
				Example: <code>https://an1.com/6089-coin-master.html</code>
			</div>
		</div>

 

        <!-- Result Terminal -->
        <div id="result" class="pro-terminal"></div>

        <!-- Loading Overlay -->
        <div id="loadingOverlay" class="pro-overlay">
            <div class="pro-spinner-container">
                <div class="pro-spinner"></div>
                Loading...
            </div>
        </div>
    </div>

    <!-- ===========================
         STYLES
         =========================== -->
<style>
body{font-family:system-ui,sans-serif;background:#f9f9f9;color:#111;line-height:1.6}
.pro-check-site{margin:30px auto;position:relative}
.pro-title{text-align:center;font-size:28px;margin-bottom:20px;color:#111}
.pro-info{background:#e6f0ff;color:#0052cc;border-left:4px solid #0073aa;padding:12px 15px;border-radius:8px;font-size:14px;margin-bottom:25px;text-align:left}
.pro-form{background:#fff;padding:25px;border-radius:12px;box-shadow:0 6px 18px rgba(0,0,0,0.08);margin-bottom:25px;text-align:center;border:1px solid #ddd;position:relative;z-index:1}
.pro-form label{display:block;font-weight:600;color:#333;margin-bottom:10px;font-size:14px}
.pro-form input[type="url"]{width:100%;padding:12px 15px;font-size:16px;border-radius:6px;border:1px solid #ccc;margin-bottom:10px;transition:0.3s}
.pro-form input[type="url"]:focus{border-color:#0073aa;outline:none;box-shadow:0 0 8px rgba(0,115,170,0.2)}
.pro-terminal{display:none;background:#fff;color:#111;padding:20px;border-radius:10px;border:1px solid #ddd;font-family:'Courier New',monospace;min-height:300px;white-space:pre-wrap;overflow:auto;transition:0.3s;box-shadow:0 4px 12px rgba(0,0,0,0.05);position:relative;z-index:1}
.pro-terminal span{font-weight:600}
.pro-overlay{display:none;position:absolute;top:0;left:0;width:100%;height:100%;background:rgba(255,255,255,0.85);z-index:10;display:flex;align-items:center;justify-content:center;opacity:0;transition:opacity 0.3s;pointer-events:none;border-radius:12px}
.pro-overlay.active{display:flex;pointer-events:auto}
.pro-spinner-container{color:#0073aa;font-size:1.5rem;text-align:center}
.pro-spinner{margin:0 auto 10px;width:50px;height:50px;border:5px solid #ccc;border-top:5px solid #0073aa;border-radius:50%;animation:spin 1s linear infinite}
@keyframes spin{0%{transform:rotate(0deg)}
100%{transform:rotate(360deg)}
}
@media(max-width:640px){.pro-title{font-size:24px}
.pro-form input[type="url"]{font-size:14px}
.pro-info{font-size:13px}
}
.floating-input{position:relative;margin-bottom:8px;width:100%}
.floating-input input{width:100%;padding:16px 12px;font-size:16px;border-radius:6px;border:1px solid #ccc;background:#fff;transition:0.3s}
.floating-input label{position:absolute;top:42%;left:12px;transform:translateY(-50%);color:#aaa;font-size:16px;pointer-events:none;transition:0.3s;background:#fff;padding:0 4px}
.floating-input input:focus + label,.floating-input input:not(:placeholder-shown) + label{top:-8px;left:8px;font-size:12px;color:#0073aa}
.floating-input input:focus{border-color:#0073aa;box-shadow:0 0 8px rgba(0,115,170,0.2)}
.pro-input-info{font-size:13px;color:#666;margin-top:4px}
.pro-input-info code{background:#f0f0f0;padding:2px 4px;border-radius:4px}.aps-tab-content.active{display:block}

</style>

    <!-- ===========================
         JS
         =========================== -->
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        const urlInput = document.getElementById('url');
        const resultEl = document.getElementById('result');
        const overlay = document.getElementById('loadingOverlay');
        const statusEl = document.getElementById('status');

        async function fetchSiteData(url) {
            if (!url) return;

            overlay.classList.add('active');
            setTimeout(()=>{ overlay.style.opacity=1; }, 10);

            statusEl.innerHTML='<span style="color:#ffaa00;font-style:italic;">Fetching data...</span>';
            resultEl.style.display='none';
            resultEl.innerHTML='';

            try {
                const formData = new FormData();
                formData.append('action','apkdone_scrape');
                formData.append('target_url', url);

                const response = await fetch('<?php echo admin_url("admin-ajax.php"); ?>',{
                    method:'POST',
                    body:formData
                });

                const text = await response.text();

                if(text.trim().length>0){
                    resultEl.style.display='block';
                    resultEl.innerHTML = text
                        .replace(/SUCCESS ✅/g,'<span style="color:#00aa00;">SUCCESS ✅</span>')
                        .replace(/FAILED ❌/g,'<span style="color:#cc0000;">FAILED ❌</span>')
                        .replace(/Status Code : (\d+)/g,'<span style="color:#0073aa;">Status Code : $1</span>')
                        .replace(/• Method\s+:/g,'<span style="color:#ffaa00;">• Method :</span>')
                        .replace(/• Title\s+:/g,'<span style="color:#ff6600;">• Title :</span>')
                        .replace(/• H1\s+:/g,'<span style="color:#ff8800;">• H1 :</span>')
                        .replace(/• Mod\s+:/g,'<span style="color:#aa00ff;">• Mod :</span>')
                        .replace(/• Size\s+:/g,'<span style="color:#00aa88;">• Size :</span>')
                        .replace(/• Download\s+:/g,'<span style="color:#0088ff;">• Download :</span>')
                        .replace(/• Desc\s+:/g,'<span style="color:#111;">• Desc :</span>')
                        .replace(/===============================/g,'<span style="color:#aaa;">===============================</span>');
                    resultEl.scrollIntoView({behavior:'smooth', block:'end'});
                }

            } catch(err){
                resultEl.style.display='block';
                resultEl.innerHTML=`<span style="color:#cc0000;">ERROR: ${err.message}</span>`;
            } finally {
                overlay.style.opacity=0;
                setTimeout(()=>{ overlay.classList.remove('active'); },300);
                statusEl.innerHTML='';
            }
        }

        let typingTimer;
        const typingDelay=500;
        function handleInput() {
            clearTimeout(typingTimer);
            typingTimer = setTimeout(()=>{
                const url=urlInput.value.trim();
                fetchSiteData(url);
            },typingDelay);
        }

        urlInput.addEventListener('input', handleInput);
        urlInput.addEventListener('paste', handleInput);
    });
    </script>
    <?php
}


// =========================
// AJAX handler
// =========================
add_action('wp_ajax_apkdone_scrape', 'apkdone_scrape_callback');

function apkdone_scrape_callback() {
    if (empty($_POST['target_url'])) {
        echo '<span style="color:#ff4d4d;">ERROR: URL not found</span>';
        wp_die();
    }

    $target_url = filter_var(trim($_POST['target_url']), FILTER_SANITIZE_URL);

    if (!filter_var($target_url, FILTER_VALIDATE_URL)) {
        echo '<span style="color:#ff4d4d;">ERROR: Invalid URL</span>';
        wp_die();
    }

    $sc = new StructurCode();
    $data = $sc->fetchWithCloudflareBypass($target_url);

    echo "<pre>";
    echo "===============================\n";
    echo "• Try Checking The Site : $target_url\n";
    echo "===============================\n";

    if (empty($data['success'])) {
        echo "• FAILED ❌\n";
        echo "• Error: {$data['error']}\n";
    } else {
        echo "• SUCCESS ✅\n";
        echo "• HTML Length : " . number_format($data['html_length']) . " character\n";
    }

    if (!empty($data['status_code'])) {
        echo "• Status Code : {$data['status_code']}\n";
    }

    echo "• Method      : " . ($data['method'] ?? 'unknown') . "\n";
    echo "===============================\n";

    if (!empty($data['success'])) {
        echo "• Title    : {$data['title']}\n";
        echo "• H1       : {$data['h1']}\n";
        echo "• Mod      : {$data['mod_features']}\n";

        if (!empty($data['size'])) {
            echo "• Size     : {$data['size']}\n";
        }
        if (!empty($data['download_link'])) {
            echo "• Download : {$data['download_link']}\n";
        }

        echo "• Desc     : {$data['description']}\n";
        echo "===============================\n";
    }

    echo "</pre>";
    //wp_die();
}
