 (function() {
  "use strict";

  /**
   * Easy selector helper function
   */
  const select = (el, all = false) => {
    el = el.trim()
    if (all) {
      return [...document.querySelectorAll(el)]
    } else {
      return document.querySelector(el)
    }
  }

  /**
   * Easy event listener function
   */
  const on = (type, el, listener, all = false) => {
    if (all) {
      select(el, all).forEach(e => e.addEventListener(type, listener))
    } else {
      select(el, all).addEventListener(type, listener)
    }
  }

  /**
   * Easy on scroll event listener
   */
  const onscroll = (el, listener) => {
    el.addEventListener('scroll', listener)
  }

  /**
   * Sidebar toggle
   */
  if (select('.toggle-sidebar-btn')) {
    on('click', '.toggle-sidebar-btn', function(e) {
      select('body').classList.toggle('toggle-sidebar')
    })
  }

  /**
   * Search bar toggle
   */
  if (select('.search-bar-toggle')) {
    on('click', '.search-bar-toggle', function(e) {
      select('.search-bar').classList.toggle('search-bar-show')
    })
  }

  /**
   * Navbar links active state on scroll
   */
  let navbarlinks = select('#navbar .scrollto', true)
  const navbarlinksActive = () => {
    let position = window.scrollY + 200
    navbarlinks.forEach(navbarlink => {
      if (!navbarlink.hash) return
      let section = select(navbarlink.hash)
      if (!section) return
      if (position >= section.offsetTop && position <= (section.offsetTop + section.offsetHeight)) {
        navbarlink.classList.add('active')
      } else {
        navbarlink.classList.remove('active')
      }
    })
  }
  window.addEventListener('load', navbarlinksActive)
  onscroll(document, navbarlinksActive)

  /**
   * Toggle .header-scrolled class to #header when page is scrolled
   */
  let selectHeader = select('#header')
  if (selectHeader) {
    const headerScrolled = () => {
      if (window.scrollY > 100) {
        selectHeader.classList.add('header-scrolled')
      } else {
        selectHeader.classList.remove('header-scrolled')
      }
    }
    window.addEventListener('load', headerScrolled)
    onscroll(document, headerScrolled)
  }

  /**
   * Back to top button
   */
  let backtotop = select('.back-to-top')
  if (backtotop) {
    const toggleBacktotop = () => {
      if (window.scrollY > 100) {
        backtotop.classList.add('active')
      } else {
        backtotop.classList.remove('active')
      }
    }
    window.addEventListener('load', toggleBacktotop)
    onscroll(document, toggleBacktotop)
  }

  /**
   * Initiate tooltips
   */
  var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
  var tooltipList = tooltipTriggerList.map(function(tooltipTriggerEl) {
    return new bootstrap.Tooltip(tooltipTriggerEl)
  })


  /**
   * Autoresize echart charts
   */
  const mainContainer = select('#main');
  if (mainContainer) {
    setTimeout(() => {
      new ResizeObserver(function() {
        select('.echart', true).forEach(getEchart => {
          echarts.getInstanceByDom(getEchart).resize();
        })
      }).observe(mainContainer);
    }, 200);
  }

})();
$(document).ready(function () {
  $(function () {
      new DataTable('.tt_datatable', {
          "order":[],
          "responsive": true,
          "autoWidth": false,
      });
  });
  $('.tt-select-single').select2();
  $('.addMediaButton').click(function () {
    $('.loader_image').show();
    $('#AllMedia').html('');
    $('#files').html('');
    var target = $(this).data('target');
    var mediaType = $(this).data('media_type');

    var modalTitle = mediaType === 'image' ? 'Select Image' : 'Select .' + mediaType + ' File';
    $('#MediaSelectorTitle').text(modalTitle);
    $('#MediaSelectorTitle').attr('data-target_input', target);

    $.ajax({
      url: 'inc/load-media-select.php',
      type: 'POST',
      data: {
        target: target,
        mediaType: mediaType
      },
      success: function (response) {
        $('.loader_image').hide();
        $('#AllMedia').html(response);
      },
    });
  });
});
function confirmDelete(event) {
    event.preventDefault();
    var confirmed = window.confirm("You are about to permanently delete this item from your site. This action cannot be undone. 'Cancel' to stop, 'OK' to delete.");
    if (confirmed) {
        var link = event.target.getAttribute("href");
        window.location.href = link;
    }
}

document.querySelectorAll('.nav-item i').forEach(icon => {
    let colors = [
        '#8B0000', // Dark Red
        '#4B0082', // Indigo
        '#006400', // Dark Green
        '#800000', // Maroon
        '#483D8B', // Dark Slate Blue
        '#2F4F4F', // Dark Slate Gray
        '#8B4513', // Saddle Brown
        '#191970', // Midnight Blue
        '#556B2F', // Dark Olive Green
        '#B22222'  // Firebrick
    ];
    let randomColor = colors[Math.floor(Math.random() * colors.length)];
    icon.style.color = randomColor;
});
