$('.date_class').datetimepicker({
	format : 'd-m-Y H:i:s',
	formatTime:'H:i:s',
	formatDate:'d-m-Y'
});

$(function () {
 $(".c_h").click(function(e) {
 if ($(".chat_container").is(":visible")) {
 $(".c_h .right_c .mini").text("+")
 } else {
 $(".c_h .right_c .mini").text("-")
 }
 $(".chat_container").slideToggle("slow");
 return false
 });
});
   $(document).ready(function () {
    // Get notification
    function notification(){
        var url = 'notification/get_notif_data.php';
        $.ajax({
            type: "POST",
            dataType: 'JSON',
            url: url,
            success: function(data) {
                // console.log('data')
                // console.log(data)

                if (data.status === 'success') 
                {
                    var obj = data.info;          
                $("#fbcount").text(obj.fbcount);
                $("#mailcount").text(obj.mailcount);
                $("#tweetcount").text(obj.tweetcount);
                $("#chatcount").text(obj.chatcount);
                $("#smscount").text(obj.smscount);
                $("#fbMcount").text(obj.fbMcount);
                $("#whatsapp_count").text(obj.whatsapp_count);
                $("#instagram_count").text(obj.instagram_count);
                console.log("Misscall Count:", obj.misscall_count); // Log the misscall_count value[vastvikta][03-02-2025]
        
                $("#misscall_count").text(obj.misscall_count);

                 // 2) Process red ribbon alerts
                processCrmIncomingAlerts(data);
             
                } 
            },
            error: function(data) {
                console.log('error')
                // console.log(data)
            }
        });
    }
    // miscall and voicemail today count
    function ajaxCall() {
        $.ajax({    
          type: "POST",
          url: "notification/voicemail_notif.php",             
          dataType: "html",                
          success: function(data){                    
              //console.log('voicemail')
              //console.log(data)
              $("#voicemail").html('('+data+')');

            }
        });
        $.ajax({    
            type: "POST",
            url: "notification/misscall_notif.php",             
            dataType: "html",                
            success: function(data){                    
                // console.log('misscall')
                // console.log(data)
                $("#misscall").html('('+data+')');
            }
        });
    }
    function workingStatus(){
        $.ajax({    
          type: "POST",
          url: "working_omnichannel.php",                            
          success: function(data){                    
              // console.log(data)
          }
        });
    }

    // ajaxCall(); // To output when the page loads
    setInterval(ajaxCall, (10 * 1000)); // x * 1000 to get it in seconds

    // notification();
    setInterval(notification, (10 * 1000)); // x * 1000 to get it in seconds

    setInterval(function() { 
        // workingStatus();
    },300000);

       //Examples of how to assign the Colorbox event to elements
      $(".ico-interaction2").colorbox({
      iframe: true,
      innerWidth: 800,
      innerHeight: 600
      });
   
       $(".ico-setting").colorbox({ iframe: true, innerWidth: 520, innerHeight: 380 });
       $(".ico-display").colorbox({ iframe: true, innerWidth: 800, innerHeight: 85 });
       $(".kno-display").colorbox({ iframe: true, width: "50%", height: "80%" });
       $(".supportsection").colorbox({ iframe: true, innerWidth: 550, innerHeight: 400 });
       $(".newdocument").colorbox({ iframe: true, innerWidth: 550, innerHeight: 280 });
       $(".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: 520 });
       $(".inline").colorbox({ inline: true, width: "450" });
       $("#inline_service_click").colorbox({ inline: true, width: 450, innerHeight: 420 });
       $(".inline_service_click2").colorbox({ inline: true, width: 450, innerHeight: 420 });
       $(".inline2").colorbox({ inline: true, width: "450", height: "80%" });
       $(".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;
       });
   });
   document.addEventListener("DOMContentLoaded", function() {
       // make it as accordion for smaller screens
       if (window.innerWidth < 992) {
   
           document.querySelectorAll('.sidebar .nav-link').forEach(function(element) {
   
               element.addEventListener('click', function(e) {
   
                   let nextEl = element.nextElementSibling;
                   let parentEl = element.parentElement;
                   let allSubmenus_array = parentEl.querySelectorAll('.submenu');
   
                   if (nextEl && nextEl.classList.contains('submenu')) {
                       e.preventDefault();
                       if (nextEl.style.display == 'block') {
                           nextEl.style.display = 'none';
   
                       } else {
                           nextEl.style.display = 'block';
                       }
   
                   }
               });
           })
       }
       // end if innerWidth
   
   });
   // DOMContentLoaded  end

   /****Bulletin feching code start****/
//    setInterval(function () {
//         $(".holder > p:first")
//             .removeClass("middle, right")
//             .next()
//             .addClass("middle").removeClass("right")
//             .end()
                      
//             .addClass("right").removeClass("middle").appendTo(".holder");
//     }, 6000); 

//   fetching_bulletin();
//    inverval_timer = setInterval(function() { 
//         fetching_bulletin();
//     },30000);

// /*fetching bulletin details*/
// function fetching_bulletin(){
//     var Data = {}
//     Data.action = 'fetching_bulletin';
//     var url = "common_function.php";
//     $.ajax({
//         url: url,
//         type: "POST",
//         data:  Data,
//         dataType:"JSON",
//         success: function(data){
//         //  console.log('fetching_message_list');
//          $('.holder').html(data);
//         }
//     });
// }
$(document).ready(function () {
    $('#admin_table').DataTable({
    "ordering": false,
    "pageLength": 25
    });

    $('#admin_table2').DataTable({
    "ordering": false,
    "pageLength": 25
    });
});
// [Aarti][16-04-2024] for this code channel configuration and channel license acces provide
  $('.disable_menu').click(function(){
      $("#popupContainer").fadeIn();
      setTimeout(function(){ 
          $("#popupContainer").fadeOut();
      }, 2000);
  });
  $('.disable_channel').click(function(){
      $("#popupContainer_channel").fadeIn();
      setTimeout(function(){ 
          $("#popupContainer_channel").fadeOut();
      }, 2000);
  });

  /* =========================================================
   CHECK NEW MESSAGES FROM AJAX RESPONSE
========================================================= */

/*
Expected backend structure:

data = {
  status: "success",
  info: {
    mailcount: 2,
    fbcount: 1,
    tweetcount: 3,
    whatsapp_count: 1
  },
  latest_messages: {
    livechat: {
      message_id: "LC_1001",
      sender: "Visitor #1023",
      message: "Hi, I need help",
      url: "live_chat.php?session=1023"
    },
    whatsapp: {
      message_id: "WA_5001",
      sender: "John Doe",
      message: "Hello team",
      url: "omni_channel.php?token=abc&channel=whatsapp"
    }
  }
}
*/

var crmInitialLoadDone = false;

function processCrmIncomingAlerts(data) {
    if (!data || data.status !== 'success') return;
    if (!data.latest_messages) return;

    // FIRST LOAD: only store IDs, don't show alerts for old unread messages
    if (!crmInitialLoadDone) {
        $.each(data.latest_messages, function(channelKey, msgObj) {
            if (msgObj && msgObj.message_id) {
                lastSeenMessageIds[channelKey] = msgObj.message_id;
            }
        });

        crmInitialLoadDone = true;
        console.log("Initial load complete. Existing messages stored, no alerts shown.");
        return;
    }

    // FUTURE LOADS: show only truly new messages
    $.each(data.latest_messages, function(channelKey, msgObj) {
        if (!msgObj || !msgObj.message_id) return;

        console.log("channel:", channelKey);
        console.log("lastSeen:", lastSeenMessageIds[channelKey]);
        console.log("current:", msgObj.message_id);

        // Same message already seen
        if (lastSeenMessageIds[channelKey] === msgObj.message_id) {
            return;
        }

        // Update latest ID
        lastSeenMessageIds[channelKey] = msgObj.message_id;

        var cfg = crmChannelConfig[channelKey] || {
            label: channelKey,
            icon: '<i class="fa fa-bell"></i>'
        };

        var sender = msgObj.sender ? msgObj.sender : '';
        var preview = msgObj.message ? msgObj.message : 'You have received a new message';

        enqueueCrmAlert({
            channel: channelKey,
            title: '🔴 New ' + cfg.label + ' Message',
            text: (sender ? sender + ': ' : '') + preview,
            url: msgObj.url || '#',
            icon: cfg.icon
        });
    });
}

/* =========================================================
   CRM RED ALERT RIBBON - COMMON JS
========================================================= */

var crmAlertQueue = [];
var crmAlertShowing = false;
var crmAlertCurrentUrl = '#';
var crmAlertAutoHideTimer = null;
var crmAlertDuration = 8000; // 8 seconds

// Store last seen unique message IDs to avoid duplicate alert
var lastSeenMessageIds = {
    chat: null,
    email: null,
    whatsapp: null,
    facebookpost: null,
    facebookcomments: null,
    messenger: null,
    instagram: null,
    twitterdm: null,
    twitterpost: null,
    twittercomments: null,
    sms: null
};

// Channel config (icons + fallback URLs)
var crmChannelConfig = {
    chat: {
        label: 'Chat',
        icon: '<i class="fa fa-comments"></i>'
    },
    email: {
        label: 'Email',
        icon: '<i class="fa fa-envelope"></i>'
    },
    whatsapp: {
        label: 'WhatsApp',
        icon: '<i class="fa fa-whatsapp"></i>'
    },
    facebookcomments: {
        label: 'facebookComments',
        icon: '<i class="fa fa-facebook"></i>'
    },
    facebookpost: {
        label: 'facebookPost',
        icon: '<i class="fa fa-facebook"></i>'
    },
    messenger: {
        label: 'Messenger',
        icon: '<i class="fa fa-commenting"></i>'
    },
    instagram: {
        label: 'Instagram',
        icon: '<i class="fa fa-instagram"></i>'
    },
    twitterpost: {
        label: 'X',
        icon: '<img src="../public/images/x-twitter.svg" alt="X">'
    },
    twittercomments: {
        label: 'X',
        icon: '<img src="../public/images/x-twitter.svg" alt="X">'
    },
    twitterdm: {
        label: 'X',
        icon: '<img src="../public/images/x-twitter.svg" alt="X">'
    },
    sms: {
        label: 'SMS',
        icon: '<i class="fa fa-comment"></i>'
    }
};

// Queue new alert
function enqueueCrmAlert(alertData) {
    console.log(alertData);
    crmAlertQueue.push(alertData);
    processCrmAlertQueue();
}

// Process queue one-by-one
function processCrmAlertQueue() {
    if (crmAlertShowing || crmAlertQueue.length === 0) return;

    crmAlertShowing = true;
    var alertData = crmAlertQueue.shift();

    showCrmAlert(alertData);
}

// Show ribbon
function showCrmAlert(alertData) {
    crmAlertCurrentUrl = alertData.url || '#';

    $('#crmAlertIcon').html(alertData.icon || '<i class="fa fa-bell"></i>');
    $('#crmAlertTitle').text(alertData.title || 'New Message');
    $('#crmAlertText').text(alertData.text || 'You have received a new message');

    // Reset progress bar
    $('#crmAlertProgressBar').stop(true, true).css({
        width: '100%'
    });

    // Show ribbon
    $('#crmGlobalAlertRibbon').stop(true, true).fadeIn(200);

    // Animate progress bar
    $('#crmAlertProgressBar').animate({
        width: '0%'
    }, crmAlertDuration, 'linear');

    // Play sound
    playCrmAlertSound();

    // Auto hide
    clearTimeout(crmAlertAutoHideTimer);
    crmAlertAutoHideTimer = setTimeout(function () {
        hideCrmAlertAndContinue();
    }, crmAlertDuration);
}

// Hide current + show next
function hideCrmAlertAndContinue() {
    clearTimeout(crmAlertAutoHideTimer);

    $('#crmGlobalAlertRibbon').fadeOut(200, function () {
        crmAlertShowing = false;
        processCrmAlertQueue();
    });
}

// Sound play
function playCrmAlertSound() {
    var sound = document.getElementById('crmAlertSound');
    if (sound) {
        try {
            sound.pause();
            sound.currentTime = 0;
            var playPromise = sound.play();
            if (playPromise !== undefined) {
                playPromise.catch(function (e) {
                    console.log('Sound autoplay blocked until user interacts.');
                });
            }
        } catch (e) {
            console.log('Sound error:', e);
        }
    }
}

// Open button click
$(document).on('click', '#crmAlertOpenBtn', function () {
    if (crmAlertCurrentUrl && crmAlertCurrentUrl !== '#') {
        // window.open(crmAlertCurrentUrl, '_blank');
        // Open a new tab
        let newTab = window.open(crmAlertCurrentUrl, "_blank");
        // Later, when phone call event happens
        window.focus(); // brings current tab (first one) into focus

    }
});

// Close button click
$(document).on('click', '#crmAlertCloseBtn', function () {
    hideCrmAlertAndContinue();
});