<?php
/**
 * Footer Page
 * Author: Aarti Ojha
 * Date: 16-01-2024
 * Description: This file handles the Footer bar and includes some common JavaScript code.
 */
if(!isset($_SESSION['VD_login'])){
?>
<!-- End of conditional flow -->
<style type="text/css">
.holder { 
    position: relative;
    overflow: hidden;
    margin: 0;
    margin-left: 36px;
    font-size: 15px;
    height: 22px;
    padding: -4px;
    margin-top: 7px;
    margin-bottom: -7px;
}
.holder > p { 
    position: absolute;
    -webkit-transition: all 5s;
    transition: all 4s;
    padding-left: -65px;
    color: #efffff;
    font-family: fangso;
    font-weight: 500;
}
.left   { left: -100%; }
.right  { left: 100%; }
.middle { left: 0; }
.holder p:before {
    content: "\201C";
    font-size: 3em;
    line-height: 0.1em;
    margin-right: 0.1em;
    vertical-align: -0.4em;
}
.holder p:after {
    content: "\201D";
    font-size: 3em;
    line-height: 0.1em;
    margin-left: 0.1em;
    vertical-align: -0.45em;
}
.footer-wrap {
    padding-top: 8px !Important;
}
</style>

<?php }?>
<div class="holder">
</div>
<div class="footer-wrap">
   <div class="row">
      <div class="col-sm-12">
            <img
                class="vision2"
                src="<?=$SiteURL?>public/images/<?= defined('BIS_LOGO_FILE') ? BIS_LOGO_FILE : 'bis-research-logo.png' ?>"
                alt="BIS Research"
                style="width: 106px;height: auto;max-height: 37px;background:#fff;margin-top: -11px;padding: 4px;border-radius: 3px;"
                onerror="this.onerror=null;this.src='https://bisresearch.com/img/logo.png';"
            >
         <strong style="color:#adb1b9">Copyright &copy; <?=date('Y')?>-<?=date('Y',strtotime('+1 year'))?> <a href="#" style="text-decoration:none;<?=($dbtheme=='yoda')?'color:#222':'color:#fff'?>"><?=strtoupper($_SESSION['companyName'])?></a>.</strong>
         All rights reserved.
      </div>
   </div>
</div>

<!-- jquery files  -->
<script type="text/javascript" src="<?=$SiteURL?>public/js/chart.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/js/jquery.canvasjs.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/js/jquery-3.7.1.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/js/jquery.validate.min.js"></script>
<!-- <script src="<?=$SiteURL?>public/js/common.js"></script> -->
<script src="<?=$SiteURL?>public/js/form-validation.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/js/jquery.smartmenus.js"></script>
<script type="text/javascript">
    $(function() {
        $('#main-menu').smartmenus({
            mainMenuSubOffsetX: -1,
            subMenusSubOffsetX: 10,
            subMenusSubOffsetY: 0
        });
    });
</script>
<script src="<?=$SiteURL?>public/js/jquery.colorbox.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        //Examples of how to assign the Colorbox event to elements
        $(".ico-setting").colorbox({
            iframe: true,
            innerWidth: 410,
            innerHeight: 200
        });
        $(".ico-interaction").colorbox({
            iframe: true,
            innerWidth: 800,
            innerHeight: 600
        });
        $(".ico-interaction2").colorbox({
            iframe: true,
            innerWidth: 800,
            innerHeight: 600
        });
        $(".ico-display").colorbox({
            iframe: true,
            innerWidth: 800,
            innerHeight: 65
        });
        $(".supportsection").colorbox({
            iframe: true,
            innerWidth: 550,
            innerHeight: 400
        });
        $(".newdocument").colorbox({
            iframe: true,
            innerWidth: 550,
            innerHeight: 390
        });
        $(".group3").colorbox({
            rel: 'group3',
            transition: "none",
            width: "75%",
            height: "75%"
        });
        $(".group4").colorbox({
            rel: 'group4',
            slideshow: true
        });
        $(".ajax").colorbox();
        $(".form-ele").colorbox({
            iframe: true,
            innerWidth: 250,
            innerHeight: 390
        });
        $(".vimeo").colorbox({
            iframe: true,
            innerWidth: 500,
            innerHeight: 409
        });
        $(".iframe").colorbox({
            iframe: true,
            width: "80%",
            height: "80%"
        });
        $(".inline").colorbox({
            inline: true,
            width: "450"
        });
        $(".pop-all").colorbox({
            iframe: true,
            width: "50%",
            height: "70%"
        });
        $(".callbacks").colorbox({
            onOpen: function() {
                alert('onOpen: colorbox is about to open');
            },
            onLoad: function() {
                alert('onLoad: colorbox has started to load the targeted content');
            },
            onComplete: function() {
                alert('onComplete: colorbox has displayed the loaded content');
            },
            onCleanup: function() {
                alert('onCleanup: colorbox has begun the close process');
            },
            onClosed: function() {
                alert('onClosed: colorbox has completely closed');
            }
        });

        $('.non-retina').colorbox({
            rel: 'group5',
            transition: 'none'
        })
        $('.retina').colorbox({
            rel: 'group5',
            transition: 'none',
            retinaImage: true,
            retinaUrl: true
        });
        //Example of preserving a JavaScript event for inline calls.
        $("#click").click(function() {
            $('#click').css({
                "background-color": "#f00",
                "color": "#fff",
                "cursor": "inherit"
            }).text("Open this window again and this message will still be here.");
            return false;
        });
    });
</script>
<script type="text/javascript" src="<?=$SiteURL?>public/js/jquery-ui.min2.js"></script>
<!-- <script type="text/javascript" src="templates/js/jquery-ui-timepicker-addon2.js"></script> -->


<!-- <link href="templates/fullcalendar/fullcalendar.print.css" rel="stylesheet" media="print" /> -->
<script src="<?=$SiteURL?>public/js/moment.min.js"></script>
<!-- <script src="templates/fullcalendar/fullcalendar.min.js"></script> -->
<!-- <script type="text/javascript" src="templates/js/jquery.datepick.js"></script> -->
<script src="<?=$SiteURL?>public/datatables/js/dataTables.js"></script>
<script src="<?=$SiteURL?>public/js/jquery.datetimepicker.js"></script>
<!-- social notification and bulletin related code in this file -->
<script src="<?=$SiteURL?>/public/js/app.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/nprogress.js" ></script>
<script src="<?=$SiteURL?>/public/js/dropify.min.js"></script>


<script type="text/javascript" src="<?=$SiteURL?>public/bootstrap/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/jszip.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/pdfmake.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/vfs_fonts.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/buttons.print.min.js"></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/buttons.colVis.min.js" ></script>
<script type="text/javascript" src="<?=$SiteURL?>public/datatables/js/dataTables.responsive.min.js" ></script>
<script type="text/javascript" src="<?=$SiteURL?>public/js/ss.custom.js" ></script>
<script type="text/javascript">
    $('.date_class').datetimepicker({
        format : 'd-m-Y H:i:s',
        formatTime:'H:i:s',
        formatDate:'d-m-Y'

    });
</script>

<!-- Only for CRM login code for logout after 10 min inactivity [Aarti][11-04-2025] -->
<?php 
if(!isset($_SESSION['VD_login'])){?>
    <script type="text/javascript">
       $(document).ready(function () {

            var groupid = "<?php echo $groupid; ?>";

            let lastUserAction = Date.now();
            let popupVisible = false;
            let isAway = false;

            const INACTIVITY_TIME = 10 * 60 * 1000;   // 10 minutes

            // Track real user actions only
            $(document).on('mousemove keydown mousedown touchstart', function () {
                lastUserAction = Date.now();
            });

            // Check inactivity every 5 seconds
            setInterval(function () {

                if (groupid !== "060000" && groupid !== "080000") {
                    return;
                }

                var idleTime = Date.now() - lastUserAction;

                if (idleTime > INACTIVITY_TIME && !popupVisible) {
                    showInactivityPopup();
                    updateStatus('Appears Away', 'away-icon', 'offline');
                    isAway = true;
                }

            }, 5000);


            function showInactivityPopup() {

                popupVisible = true;

                let popup = `
                <div id="inactivityPopup" style="
                    position: fixed;
                    top:0; left:0; width:100%; height:100%;
                    background: rgba(0,0,0,0.5);
                    display:flex;
                    align-items:center;
                    justify-content:center;
                    z-index:9999;">

                    <div style="background:#fff;padding:20px;border-radius:5px;width:320px;text-align:center">
                        <p><strong>You are inactive for 10 minutes</strong></p>
                        <p>Do you want to stay logged in?</p>
                        <button id="stayBtn" class="btn btn-success btn-sm">Stay</button>
                        <button id="leaveBtn" class="btn btn-danger btn-sm">Logout</button>
                    </div>
                </div>`;

                $('body').append(popup);

                $('#stayBtn').one('click', function () {
                    $('#inactivityPopup').remove();
                    popupVisible = false;
                    lastUserAction = Date.now();

                    if (isAway) {
                        updateStatus('Available', 'available-icon', 'online');
                        isAway = false;
                    }
                });

                $('#leaveBtn').one('click', function () {
                    window.location.href = "../web_login.php?flage1=1";
                });
            }

        });


    </script>
<?php } ?>


<!-- This code handles updating the user’s status (e.g., Available, Away) in the database. -->
<!-- Aarti ojha [07-11-2024] -->
<script type="text/javascript">
    $(document).ready(function(){
        // Show or hide dropdown on clicking the current status
        $('#current-status').click(function(e) {
            e.stopPropagation();
            $('#status-dropdown').toggle();
        });

        // Function to update the status display and send it to the backend
        function updateStatus(newStatus, newStatusClass,newStatusId) {
            console.log('updateStatus');
            console.log(newStatusClass);
            console.log(newStatus);
            // Update displayed status text and icon color
            $('#current-status').html(`<span id="current-status-icon" class="status-icon ${newStatus}"></span> ${newStatus}`);
            
            if(newStatus === 'Available'){
                $('#status-icon').addClass('status-available');
                $('#status-icon_IM').addClass('status-available');
                $('#status-icon').removeClass('status-away');
                $('#status-icon_IM').removeClass('status-away');
                $('#IM_statustext').text('online');
            }else{
                $('#status-icon').addClass('status-away');
                $('#status-icon').removeClass('status-available');
                 $('#status-icon_IM').addClass('status-away');
                $('#status-icon_IM').removeClass('status-available');

                
                $('#IM_statustext').text('offline');
            }
            // Send status update to backend
            const userId = $('#userId').val();
            $.post('update_status.php', { user_id: userId, status: newStatusId, action: 'change_status' }, function(response) {
                if (!response.success) {
                    // alert('Failed to update status');
                }
            }, 'json');
        }

        // Update status on selecting an option from the dropdown
        $('.status-option').click(function() {
            const newStatus = $(this).data('status');
            const newStatusId = $(this).data('statusid');
            const newStatusClass = $(this).find('.status-icon').attr('class').split(' ')[1]; // Get color class
            // Update the status and close dropdown
            updateStatus(newStatus,newStatusClass,newStatusId);
            $('#status-dropdown').hide();
        });

        // Close dropdown if clicked outside
        $(document).click(function(e) {
            if (!$(e.target).closest('#status-dropdown, #current-status').length) {
                $('#status-dropdown').hide();
            }
        });
    });

</script>

<script>
/*
    Script Name: Heartbeat Timestamp Updater
    Purpose:
        - Reads the logged-in user's ID from a hidden input.
        - Sends heartbeat to update timestamp every 10 seconds.
        - Shows alerts for manual testing (success & failure).

    Modified Date: 03-Dec-2025
    Modified By: Farhan Akhtar
*/

// Get user ID from hidden field
function getUserId() {
    const el = document.getElementById("loggedUserId");
    return el ? el.value : null;
}

// Send heartbeat to update timestamp
function sendHeartbeat() {
    const userId = getUserId();

    if (!userId) {
        console.error("User ID not found in hidden field");
        // alert("User ID not found in hidden field");   // For testing
        return;
    }

    fetch(`https://bis-research.co.uk/ensembler/script/heartbeat_update.php?user_id=${userId}=${userId}`, {
        method: "GET",
        cache: "no-store"
    })
    .then(response => {
        if (!response.ok) {
            throw new Error("Server error: " + response.status);
        }
        return response.json();
    })
    .then(data => {
        if (!data.status) {
            console.error("Heartbeat failed:", data.message);
            // alert("Heartbeat failed: " + data.message);  // Testing alert
        } else {
            console.log("Heartbeat OK at", data.timestamp);
            // alert("Heartbeat OK at: " + data.timestamp); // Testing alert
        }
    })
    .catch(error => {
        console.error("Heartbeat error:", error.message);
        // alert("Heartbeat error: " + error.message); // Testing alert
    });
}

// First call immediately
sendHeartbeat();

// Repeat every 10 seconds
setInterval(sendHeartbeat, 10000);


</script>
