/home/bdqbpbxa/rc-subdomains/optoman-rc.goodface.com.ua/wp-content/themes/optoman/assets/js/ajax.js
jQuery(document).ready(function ($) {
    const filterButtons = $('.representative-filter-item');
    const representativesList = $('#representatives-list');

    function loadRepresentatives(categoryId = null) {
        representativesList.addClass('-loading');
        $.ajax({
            url: ajaxUrl.ajax_url,
            type: 'POST',
            data: {
                action: 'load_representatives',
                category_id: categoryId,
            },
            success: function (response) {
                // Витягуємо отримані елементи
                const newContent = $(response).find('.representative-item');

                representativesList.html(newContent);

                if (newContent.length > 1) {
                    representativesList.addClass('-many');
                } else {
                    representativesList.removeClass('-many');
                }
                representativesList.removeClass('-loading');
            },
            error: function (xhr, status, error) {
                console.error('Failed to load representatives:', error);
                representativesList.removeClass('-loading');
            },
        });
    }

    filterButtons.on('click', function () {
        filterButtons.removeClass('-active');
        $(this).addClass('-active');

        const categoryId = $(this).data('category-id');
        loadRepresentatives(categoryId);
    });

    // if (filterButtons.length > 0) {
    //     const firstButton = filterButtons.first();
    //     firstButton.addClass('-active');
    //     const firstCategoryId = firstButton.data('category-id');
    //     loadRepresentatives(firstCategoryId);
    // }
});