<?php
session_start();
include "db.class.php";

class core
{

    function login_session_script()
    {
        // check if user is logged in via sessions
?>
        <script>
            function check_login_session() {
                console.log("session_check ...");
                // Create a new XMLHttpRequest object
                const xhttp = new XMLHttpRequest();
                // Define a callback function
                xhttp.onload = function() {
                    // Here you can use the Data
                    if (this.responseText == 0) {
                        window.location.href = 'index.php';
                        console.log("FAILED");
                    } else {
                        console.log("PASSED");
                    }
                }
                xhttp.open("GET", 'ajax/check_login_session.php');
                xhttp.send();

            }
            // system inveral
            setInterval(check_login_session, 5000);
        </script>

    <?php
    }

    function notifications()
    {
    ?>
        <script>
            function check_notifications() {
                console.log("notification_check ...");
                // Create a new XMLHttpRequest object
                const xhttp = new XMLHttpRequest();
                // Define a callback function
                xhttp.onload = function() {
                    console.log("NOTIFICATION DATA: " + this.responseText);
                    // Here you can use the Data
                    if (this.responseText != 0) {

                        if (!('Notification' in window)) {
                            console.log('This browser does not support desktop notification');
                        } else if (Notification.permission === 'granted') {
                            var notification = new Notification(this.responseText);
                        } else if (Notification.permission !== 'denied') {
                            Notification.requestPermission(function(permission) {
                                if (permission === 'granted') {
                                    var notification = new Notification('Welcome to our website');
                                }
                            });
                        }
                    }
                }
                xhttp.open("GET", 'ajax/user_notifications.php');
                xhttp.send();

            }
            // system inveral
            // setInterval(check_notifications, 1000);
        </script>
    <?php
    }


    function webapp_notifications()
    {
    ?>
        <script>
            function check_webapp_notifications() {
                console.log("notification_check ...");
                // Create a new XMLHttpRequest object
                const xhttp = new XMLHttpRequest();
                // Define a callback function
                xhttp.onload = function() {
                    console.log("NOTIFICATION DATA: " + this.responseText);
                    // Here you can use the Data
                    if (this.responseText != 0) {
                        var div = document.createElement('div');
                        div.style.cssText = "position: fixed; top: 2%; right: 2%; background-color: #145593; color: white; padding: 2vw; border-radius: 5px; opacity: 0;";
                        div.innerHTML = this.responseText;
                        document.getElementById('alert').play();
                        document.body.appendChild(div);
                        setTimeout(function() {
                            div.style.transition = 'opacity 1s';
                            div.style.opacity = '1';
                            setTimeout(function() {
                                div.style.transition = 'opacity 1s';
                                div.style.opacity = '0';
                                setTimeout(function() {
                                    document.body.removeChild(div);
                                }, 1000);
                            }, 3000);
                        }, 100);
                    }
                }
                xhttp.open("GET", 'ajax/webapp_notifications.php');
                xhttp.send();

            }
            // system inveral
            setInterval(check_webapp_notifications, 1000);
        </script>
    <?php
    }

    function navbar()
    {
        $db = new DBMain();
    ?>

        <div class='navbar' style='width:100%;  height:100%; background-color:#323131; overflow-y:auto;'>
            <img src="../uploads/images/logo.svg" width="100%" />
            <input type='text' readonly value='<?php echo strtoupper($_SESSION['username']); ?>' style='width:90%; margin-top: 1vh; margin-left:5%; '>
            <hr>
            <button class='banner_inputs' onclick='change_to("home.php")'>HOME</button>
            <button class='banner_inputs' onclick='change_to("quotes/quotes.php")'>QUOTES</button>

            <button class='banner_inputs' onclick='change_to("jobcards/jobcards.php")'>JOBCARDS</button>
            <button class='banner_inputs' onclick='change_to("clients/clients.php")'>CLIENTS</button>
            <button class='banner_inputs' onclick='change_to("users/users.php")'>USERS</button>
            <button class='banner_inputs' onclick='change_to("stock/stock.php")'>STOCK</button>
            <button class='banner_inputs' onclick='change_to("tools/tools.php")'>TOOLS</button>
            <button class='banner_inputs' onclick='window.location.href = "logout.php"'>LOG OUT</button>


        </div>

        <script>
            function change_to(url) {
                document.getElementById('content_pannel').src = "/app/" + url;
                console.log("Clicked");
            }
        </script>

    <?php
    }


    function content()
    {
    ?>
        <iframe id="content_pannel" style='width:100%; height:100%; border: 0px;' name="content_pannel" src=""></iframe>
    <?php

    }


    function __construct()
    {
        self::login_session_script();
        self::notifications();
        self::webapp_notifications();

    ?>

        <head>
            <link rel="stylesheet" href="../styles/styles.css">
        </head>

        <audio id='alert' src="../assets/alert_sound.mp3"></audio>
        <audio id='loadin' src="../assets/loadin.mp3"></audio>
        <div class="container">
            <div style='float:left; width:15%; height:100%;'>

                <?php self::navbar(); ?>

            </div>
            <div style='float:right; width:85%;  height:96%;'>

                <?php self::content(); ?>

            </div>
        </div>

        <script>
            document.getElementById('loadin').play();
        </script>
<?php
    }
}
