$(function () {

  /* scrollar */

  new PerfectScrollbar(".notify-list");

  /* toggle button */

  $(".btn-toggle").click(function () {
    $("body").hasClass("toggled")
      ? ($("body").removeClass("toggled"),
        $(".sidebar-wrapper").unbind("hover"))
      : ($("body").addClass("toggled"),
        $(".sidebar-wrapper").hover(
          function () {
            $("body").addClass("sidebar-hovered");
          },
          function () {
            $("body").removeClass("sidebar-hovered");
          }
        ));
  });

  /* menu */

  $(function () {
    $("#sidenav").metisMenu();
  });

  $(".sidebar-close").on("click", function () {
    $("body").removeClass("toggled");
  });

  /* sticky header */

  $(document).ready(function () {
    $(window).on("scroll", function () {
      if ($(this).scrollTop() > 60) {
        $(".top-header .navbar").addClass("sticky-header");
      } else {
        $(".top-header .navbar").removeClass("sticky-header");
      }
    });
  });

  // Email Panel Toggle
  $(".email-toggle-btn").on("click", function () {
    $(".email-wrapper").toggleClass("email-toggled");
  });

  $(".email-toggle-btn-mobile").on("click", function () {
    $(".email-wrapper").removeClass("email-toggled");
  });

  // Compose Mail Popup
  $(".compose-mail-btn").on("click", function () {
    $(".compose-mail-popup").fadeIn(); // Better UX than .show()
  });

  $(".compose-mail-close").on("click", function () {
    $(".compose-mail-popup").fadeOut(); // Better UX than .hide()
  });

  /* chat */

  $(".chat-toggle-btn").on("click", function () {
    $(".chat-wrapper").toggleClass("chat-toggled");
  });
  $(".chat-toggle-btn-mobile").on("click", function () {
    $(".chat-wrapper").removeClass("chat-toggled");
  });

  // Button Hover JS
  $(function () {
    $(".default-btn, .default-btn-green, .default-btn-two")
      .on("mouseenter", function (e) {
        var parentOffset = $(this).offset(),
          relX = e.pageX - parentOffset.left,
          relY = e.pageY - parentOffset.top;
        $(this).find("span").css({ top: relY, left: relX });
      })
      .on("mouseout", function (e) {
        var parentOffset = $(this).offset(),
          relX = e.pageX - parentOffset.left,
          relY = e.pageY - parentOffset.top;
        $(this).find("span").css({ top: relY, left: relX });
      });
  });

  /* search control */

  $(".search-control").click(function () {
    $(".search-popup").addClass("d-block");
    $(".search-close").addClass("d-block");
  });

  $(".search-close").click(function () {
    $(".search-popup").removeClass("d-block");
    $(".search-close").removeClass("d-block");
  });

  $(".mobile-search-btn").click(function () {
    $(".search-popup").addClass("d-block");
  });

  $(".mobile-search-close").click(function () {
    $(".search-popup").removeClass("d-block");
  });

  /* menu active */

  $(function () {
    for (
      var e = window.location,
      o = $(".metismenu li a")
        .filter(function () {
          return this.href == e;
        })
        .addClass("")
        .parent()
        .addClass("mm-active");
      o.is("li");

    )
      o = o.parent("").addClass("mm-show").parent("").addClass("mm-active");
  });

  $("#show_hide_password a").on("click", function (event) {
    event.preventDefault();
    if ($("#show_hide_password input").attr("type") == "text") {
      $("#show_hide_password input").attr("type", "password");
      $("#show_hide_password i").addClass("bi-eye-slash-fill");
      $("#show_hide_password i").removeClass("bi-eye-fill");
    } else if ($("#show_hide_password input").attr("type") == "password") {
      $("#show_hide_password input").attr("type", "text");
      $("#show_hide_password i").removeClass("bi-eye-slash-fill");
      $("#show_hide_password i").addClass("bi-eye-fill");
    }
  });

  $("#show_hide_password2 a").on("click", function (event) {
    event.preventDefault();
    if ($("#show_hide_password2 input").attr("type") == "text") {
      $("#show_hide_password2 input").attr("type", "password");
      $("#show_hide_password2 i").addClass("bi-eye-slash-fill");
      $("#show_hide_password2 i").removeClass("bi-eye-fill");
    } else if ($("#show_hide_password2 input").attr("type") == "password") {
      $("#show_hide_password2 input").attr("type", "text");
      $("#show_hide_password2 i").removeClass("bi-eye-slash-fill");
      $("#show_hide_password2 i").addClass("bi-eye-fill");
    }
  });

  $("#spinner-overlay").hide();

  $(".dataTableInit").each(function () {
    const table = $(this).DataTable({
      lengthChange: false,
      ordering: false,
      buttons: [
        {
          extend: "excel",
          exportOptions: {
            columns: ":not(.noExport)", // Skip columns with this class
          },
        },
      ],
    });

    table
      .buttons()
      .container()
      .appendTo($(this).closest(".dataTables_wrapper").find(".col-md-6:eq(0)"));

      const statusColumn = table.column(12);
      const uniqueStatuses = statusColumn.data().unique().sort();
      uniqueStatuses.each(function (d) {
        if (d.trim() !== "") {
          $("#statusFilter").append(
            '<option value="' + d + '">' + d + "</option>"
          );
        }
      });
      $("#statusFilter").on("change", function () {
        const val = $.fn.dataTable.util.escapeRegex($(this).val());
        statusColumn.search(val ? "^" + val + "$" : "", true, false).draw();
      });
  });

  $(".dataTableSInit").each(function () {
    $(this).DataTable({
      lengthChange: false,
      ordering: false
    });
  });

});

$('.single-select-field').select2({
  theme: "bootstrap-5",
  width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style',
  placeholder: $(this).data('placeholder'),
});

$('.multiple-select-field').select2({
  theme: "bootstrap-5",
  width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style',
  placeholder: $(this).data('placeholder'),
  closeOnSelect: false,
});

function initAllPhoneInputs() {
  document.querySelectorAll('input[type="tel"]:not([data-intl-initialized])')
    .forEach(initIntlTelInput);
}

// Observe for dynamically added inputs
var observer = new MutationObserver(initAllPhoneInputs);
observer.observe(document.body, {
  childList: true,
  subtree: true
});

$(".date-time").flatpickr({
    enableTime: true,
    dateFormat: "Y-m-d H:i",
});