// Version: 1.0.1
jQuery(document).ready(function ($) {
  $(".seetickets-list-view-load-more-button").on("click", function () {
    let ajaxPage = $(this).data("see-ajax-page");
    let totalPages = $(this).data("see-total-pages");
    let listType = $(this).data("list-type");
    let $this = $(this);

    $this.find("img").css("display", "inline-block");

    $.ajax({
      type: "GET",
      url: evusel_ajax_obj.ajax_url,
      dataType: "html",
      data: {
        action: "dispevli_list_view_events",
        seeAjaxPage: ajaxPage,
        listType: listType,
        nonce: evusel_ajax_obj.nonce,
      },
      success: function (response) {
        $this.find("img").hide();
        $this.parent().siblings(".seetickets-list-events").append(response);
        dispevli_dispatch_events_loaded();
        if (ajaxPage == totalPages) {
          $this.remove();
        } else {
          $this.data("see-ajax-page", ajaxPage + 1);
        }
      },
      error: function (response) {
        // Error handler
      },
    });
  });

  var seeListViewPaginationSelector = ".seetickets-list-view-pagination li a";

  function initListPagination($elements) {
    $elements.on("click", function (e) {
      e.preventDefault();

      let seeAjaxPage = $(this).data("see-ajax-page");
      let listType = $(this).parents("ul").data("list-type");
      let currentList = $(this).parents("ul").data("nth-list-element");

      // Instead of AJAX, simply redirect to the same page with the page parameter
      let url = new URL(window.location.href);
      url.searchParams.set(
        "list" + (currentList || 1) + "page",
        seeAjaxPage || 1
      );

      // Use location.href to reload the page with the new parameter
      window.location.href = url.toString();
    });
  }

  initListPagination($(seeListViewPaginationSelector));

  $("#seetickets-search-list-view-load-more-button").on("click", function () {
    let ajaxPage = $(this).data("see-ajax-page");
    let totalPages = $(this).data("see-total-pages");
    let fields = $(this).data("see-search-fields");
    let $this = $(this);

    $this.find("img").css("display", "inline-block");

    let params = new Proxy(new URLSearchParams(window.location.search), {
      get: (searchParams, prop) => searchParams.get(prop),
    });

    $.ajax({
      type: "GET",
      url: evusel_ajax_obj.ajax_url,
      dataType: "html",
      data: {
        action: "dispevli_list_view_events",
        seeAjaxPage: ajaxPage,
        nonce: evusel_ajax_obj.nonce,
        searchParameter: params["seetickets-search"],
        fields: fields,
      },
      success: function (response) {
        $this.find("img").hide();
        $this.parent().before(response);
        dispevli_dispatch_events_loaded();
        if (ajaxPage == totalPages) {
          $this.remove();
        } else {
          $this.data("see-ajax-page", ajaxPage + 1);
        }
      },
      error: function (response) {
        // Error handler
      },
    });
  });

  $("#seetickets-past-events-load-more-button").on("click", function () {
    let ajaxPage = $(this).data("see-ajax-page");
    let totalPages = $(this).data("see-total-pages");
    let $this = $(this);

    $this.find("img").css("display", "inline-block");

    $.ajax({
      type: "GET",
      url: evusel_ajax_obj.ajax_url,
      dataType: "html",
      data: {
        action: "dispevli_fetch_past_events",
        seeAjaxPage: ajaxPage,
        nonce: evusel_ajax_obj.nonce,
      },
      success: function (response) {
        $this.find("img").hide();
        $("#past-events-list").append(response);
        dispevli_dispatch_events_loaded();
        if (ajaxPage == totalPages) {
          $this.remove();
        } else {
          $this.data("see-ajax-page", ajaxPage + 1);
        }
      },
      error: function (response) {
        // Error handler
      },
    });
  });
});

function dispevli_dispatch_events_loaded() {
  let event = new Event("see-list-events-loaded", {});
  document.dispatchEvent(event);
}

function pageNumberScrollTop() {
  window.scrollTo({
    top: jQuery(document.getElementById("list-view-event")).offset().top,
    behavior: "smooth",
  });
}

// MDC initialization event handler - with proper jQuery wrapper
jQuery(document).on("dispevli_grid_updated", function (e, $grid) {
  setTimeout(function () {
    if (window.mdc && mdc.autoInit) {
      mdc.autoInit();
    }
    window.dispatchEvent(new Event("resize"));
    $grid.hide().show(0);
  }, 50);
});
