(function($) {
    "use strict";

    // Use ONE entry point for everything
    $(function() {

        // 1. Handling Tab Logic
        $('.afdp-dashboard-wrapper [data-bs-toggle="pill"]').on('shown.bs.tab', function (event) {
            const targetId = $(event.target).attr('data-bs-target');
            console.log('Switched to tab:', targetId);
        });

        // 2. Login Form Submission UI
        const $loginForm = $('.afdp-auth-form-side form');
        if ($loginForm.length) {
            $loginForm.on('submit', function() {
                const $btn = $(this).find('.afdp-auth-btn');
                if ($btn.length) {
                    $btn.html('<i class="fas fa-spinner fa-spin"></i> Signing In...')
                        .css({ 'opacity': '0.7', 'pointer-events': 'none' });
                }
            });
        }

        // 3. Registration Form Submission UI
        const $regForm = $('#afdp-registrationForm');
        if ($regForm.length) {
            $regForm.on('submit', function() {
                const $submitBtn = $(this).find('.afdp-reg-btn');
                $submitBtn.html('<i class="fas fa-circle-notch fa-spin"></i> Processing...')
                          .css({ 'pointer-events': 'none', 'opacity': '0.8' });
            });
        }

        // 4. Swiper Initialization
        // Check if Swiper class exists and the element is on the page
        if (typeof Swiper !== 'undefined' && $('.afdp-cat-carousel').length > 0) {
            new Swiper('.afdp-cat-carousel', {
                loop: true,
                slidesPerView: 1,
                spaceBetween: 25,
                autoplay: { 
                    delay: 4000,
                    disableOnInteraction: false 
                },
                pagination: { 
                    el: '.afdp-swiper-pagination', 
                    clickable: true 
                },
                breakpoints: {
                    480: { slidesPerView: 1.5 },
                    768: { slidesPerView: 2 },
                    991: { slidesPerView: 3 },
                    1200: { slidesPerView: 4 }
                }
            });
        }

    }); // end ready

})(jQuery);