$(document).ready(function () {
    function checkIP() {
        setInterval(function () {
            $.ajax({
                url: '/sadece-online-ozel/ajax.php',
                type: 'POST',
                data: JSON.stringify({
                    action: 'getUserRealIP'
                }),
                success: function (data) {
                    const response = JSON.parse(data)
                    if (response.status === true) {
                        const ip = response.ip
                        if (ip) {
                            if (document.visibilityState === 'visible') {
                                $.ajax({
                                    url: '/sadece-online-ozel/ajax.php',
                                    type: 'POST',
                                    data: JSON.stringify({
                                        action: 'setUserStatus',
                                        status: 'online',
                                        ip: ip
                                    }),
                                })
                            } else {
                                $.ajax({
                                    url: '/sadece-online-ozel/ajax.php',
                                    type: 'POST',
                                    data: JSON.stringify({
                                        action: 'setUserStatus',
                                        status: 'offline',
                                        ip: ip
                                    }),
                                })
                            }
                            const ChangeVisibility = function () {
                                if (document.visibilityState === 'visible') {
                                    $.ajax({
                                        url: '/sadece-online-ozel/ajax.php',
                                        type: 'POST',
                                        data: JSON.stringify({
                                            action: 'setUserStatus',
                                            status: 'online',
                                            ip: ip
                                        }),
                                    })
                                } else {
                                    $.ajax({
                                        url: '/sadece-online-ozel/ajax.php',
                                        type: 'POST',
                                        data: JSON.stringify({
                                            action: 'setUserStatus',
                                            status: 'offline',
                                            ip: ip
                                        }),
                                    })
                                }
                            }
                            document.addEventListener('visibilitychange', ChangeVisibility, false)
                        }
                    }
                },
            })
        }, 3000);
    }

    checkIP()
})