(function ($) {
    "use strict";

    $(window).on("load", function () {
        $(".post-pagination > li:nth-child(1) > a").addClass("current");
        $(".newest_posts").addClass("active-short");
        forumax_forum.forumax_search(
            "#search_field",
            "#UserList .userlist",
            ".current-user"
        );
        forumax_forum.forumax_search(
            "#search_fields",
            "#tagList .tagList",
            ".frmx-dropdown-item"
        );
        forumax_forum.forumax_loading_forum();
        forumax_forum.forumax_open_forum();
        forumax_forum.forumax_sort_forum();
    });

    var forumax_forum = {
        forumax_search: function (
            search_field,
            searchable_elements,
            searchable_text_class
        ) {
            $(search_field).keyup(function (e) {
                e.preventDefault();
                var query = $(this).val().toLowerCase();
                if (query) {
                    $.each($(searchable_elements), function () {
                        var title = $(this)
                            .find(searchable_text_class)
                            .text()
                            .toLowerCase();
                        if (title.indexOf(query) == -1) {
                            $(this).hide();
                        } else {
                            $(this).show();
                        }
                    });
                } else {
                    $(searchable_elements).show();
                }
            });
        },
        forumax_auth_select: function () {
            $(document).on("click", ".data-auth", function () {
                var _this = $(this),
                    text = _this.text();
                $(".UserList").html(text);
            });
        },
        forumax_tag_select: function () {
            $(document).on("click", ".data-tag", function () {
                var _this = $(this),
                    text = _this.text();
                $(".tagLista").html(text);
            });
        },
        forumax_loading_forum: function () {
            $(document).on("click", ".forumax-data", function () {
                $(".forumax-data").removeClass("loading");
                $(".close-ticket.reset-btn").addClass("reset-btn-active");
                var _this = $(this),
                    _ajaxUrl = forumax_localize_script.ajaxurl,
                    _class = _this.addClass("loading selected"),
                    _a = "forumax_loading_post",
                    _n = forumax_localize_script.nonce,
                    _t = _this.data("type"),
                    _id = _this.data("id"),
                    _parent = _this.data("parent"),
                    _count = _this.data("count"),
                    data = {
                        type: _t,
                        action: _a,
                        nonce: _n,
                        a_t_id: _id,
                        count: _count,
                        parent: _parent,
                    };

                if ($(this).hasClass("loading")) {
                    $.ajax({
                        url: _ajaxUrl,
                        method: "post",
                        data: data,
                        beforeSend: function () {
                            $(".load-forum").html(
                                "<div class='forum-loading'><div class='configure-border-1'><div class='configure-core'></div></div><div class='configure-border-2'><div class='configure-core'></div></div></div>"
                            );
                        },
                        success: function (response) {
                            $(".load-forum").html(response);
                        },
                        error: function () {
                            console.log("Oops! Something wrong, try again!");
                        },
                    });
                }

                return false;
            });
        },
        forumax_open_forum: function () {
            $(document).on("click", ".open-data", function () {
                $(".open-data").removeClass("loading");
                $(this).parent().removeClass("reset-btn-active");
                $('.sort-by').removeClass('active-short');
                $('.newest_posts').addClass('active-short');
                var _this = $(this),
                    _ajaxUrl = forumax_localize_script.ajaxurl,
                    _class = _this.addClass("loading selected"),
                    _a = "forumax_open_post",
                    _n = forumax_localize_script.nonce,
                    _t = _this.data("type"),
                    _id = _this.data("id"),
                    _count = _this.data("count"),
                    _parent = _this.data("parent"),
                    data = {
                        type: _t,
                        action: _a,
                        nonce: _n,
                        a_t_id: _id,
                        count: _count,
                        parent: _parent,
                    };

                if ($(this).hasClass("loading")) {
                    $.ajax({
                        url: _ajaxUrl,
                        method: "post",
                        data: data,
                        beforeSend: function () {
                            $(".load-forum").html(
                                "<div class='forum-loading'><div class='configure-border-1'><div class='configure-core'></div></div><div class='configure-border-2'><div class='configure-core'></div></div></div>"
                            );
                        },
                        success: function (response) {
                            $(".load-forum").html(response);
                        },
                        error: function () {
                            console.log("Oops! Something wrong, try again!");
                        },
                    });
                }

                return false;
            });
        },
        forumax_sort_forum: function () {
            $(document).on("click", ".sort-by", function () {
                $(".sort-by").removeClass("loading active-short");
                $(".close-ticket.reset-btn").addClass("reset-btn-active");
                var _this = $(this),
                    _ajaxUrl = forumax_localize_script.ajaxurl,
                    _class = _this.addClass("loading active-short"),
                    _parent = _this.data("parent"),
                    _a = "forumax_loading_sort_post",
                    _n = forumax_localize_script.nonce,
                    _sort = _this.data("sort"),
                    data = {
                        action: _a,
                        nonce: _n,
                        sort: _sort,
                        parent: _parent,
                    };
                if ($(this).hasClass("loading")) {
                    $.ajax({
                        url: _ajaxUrl,
                        method: "post",
                        data: data,
                        beforeSend: function () {
                            $(".load-forum").html(
                                "<div class='forum-loading'><div class='configure-border-1'><div class='configure-core'></div></div><div class='configure-border-2'><div class='configure-core'></div></div></div>"
                            );
                        },
                        success: function (response) {
                            $(".load-forum").html(response);
                        },
                        error: function () {
                            console.log("Oops! Something wrong, try again!");
                        },
                    });
                }

                return false;
            });
        },
    };

    $('[id]').each(function () {
        $('[id="' + this.id + '"]:gt(0)').remove();
    });

    $('a.prev.page-numbers').html('<i class="arrow_carrot-left"></i>');
    $('a.next.page-numbers').html('<i class="arrow_carrot-right"></i>');

    // Login Modal for subscription
    $(document).on('click', '.subscription-login-btn', function (e) {
        e.preventDefault();

        var $btn = $(this);
        var modalSelector = $btn.data('modal');
        var $modal = $();

        if (modalSelector) {
            $modal = $(modalSelector);
        }

        // Fallback: open the closest sibling modal near the clicked button.
        if (!$modal.length) {
            $modal = $btn.closest('#subscription-toggle, .frmx-forum-subscription-action, .action-button-container, .bbp-no-reply, .bbp-no-topic')
                .find('.subscription-login-modal')
                .first();
        }

        // Last fallback: keep old behavior.
        if (!$modal.length) {
            $modal = $('.subscription-login-modal').first();
        }

        $modal.addClass('active');
    });

    $(document).on('click', '.subscription-login-modal-close', function () {

        $(this).closest('.subscription-login-modal').removeClass('active');
    });

    // Outside click
    $(document).on('mouseup', function (e) {

        $('.subscription-login-modal.active').each(function () {
            var $modal = $(this);
            var $container = $modal.find('.subscription-login-modal-inner');
            if (!$container.is(e.target) && $container.has(e.target).length === 0) {
                $modal.removeClass('active');
            }
        });
    });

    // Handle tab click
    $('.support-total-info li a').on('click', function (e) {
        e.preventDefault(); // Prevent default link behavior

        const $this = $(this);
        const activeTabUrl = $this.attr('href'); // Get tab URL (e.g., ?tab=open)
        const contentContainer = $('.load-forum'); // The content container for AJAX response

        // Remove 'active' class from all tabs and add to the clicked tab
        $('.support-total-info li').removeClass('active');
        $this.parent().addClass('active');

        // Update the browser URL to reflect the selected tab
        window.history.pushState(null, '', activeTabUrl);

        // Perform AJAX request to fetch new content
        $.ajax({
            url: activeTabUrl,
            method: 'GET',
            beforeSend: function () {
                contentContainer.html('<p>Loading...</p>'); // Show a loading message
            },
            success: function (response) {
                // Parse the returned HTML and replace the forum content
                const newContent = $(response).find('.load-forum').html();
                contentContainer.html(newContent);
            },
            error: function () {
                contentContainer.html('<p>Error loading content. Please try again.</p>');
            }
        });
    });
})(jQuery);