/**
 * Member pagination functionality for DACdb Plus
 * 
 * This file handles the pagination of member listings using the twbs-pagination plugin.
 */

(function($) {
    'use strict';

    // Initialize pagination when document is ready
    $(document).ready(function() {
        // console.log('Member pagination script loaded');
        initializePagination();
    });

    // Function to initialize pagination
    function initializePagination() {
        // Check if pagination elements exist
        const paginationSelectors = ['#pagination-dacdb-top', '#pagination-dacdb-bottom'];
        let paginationFound = false;
        
        paginationSelectors.forEach(selector => {
            if ($(selector).length) {
                paginationFound = true;
                // console.log('Found pagination element:', selector);
                
                // Ensure pagination data exists
                if (typeof window.dacdbMemberPagination === 'undefined') {
                    console.error('Pagination data not found');
                    return;
                }

                // Ensure numeric values
                const totalPages = parseInt(window.dacdbMemberPagination.totalPages) || 1;
                const currentPage = parseInt(window.dacdbMemberPagination.currentPage) || 1;
                const debug = Boolean(window.dacdbMemberPagination.debug);

                // console.log('Initializing pagination for', selector, 'with', totalPages, 'pages');
                
                try {
                    // Destroy existing pagination if it exists
                    try {
                        $(selector).twbsPagination('destroy');
                    } catch (e) {
                        // Ignore errors if pagination doesn't exist yet
                    }

                    // Initialize pagination
                    $(selector).twbsPagination({
                        totalPages: totalPages,
                        visiblePages: Math.min(7, totalPages),
                        startPage: currentPage,
                        hideOnlyOnePage: false,
                        first: '«',
                        last: '»',
                        prev: '‹',
                        next: '›',
                        onPageClick: function(event, page) {
                            // console.log('Page clicked:', page);
                            // Hide all pages first
                            $('.page-content').hide();
                            // Show the selected page with a fade effect
                            $('#pagsec' + page).fadeIn(200);
                        }
                    });
                } catch (error) {
                    console.error('Error initializing pagination:', error, selector);
                }
            }
        });
        
        if (!paginationFound) {
            console.warn('No pagination elements found with selectors:', paginationSelectors);
        }
        
        // Always show first page by default and hide others
        $('.page-content').hide();
        $('#pagsec1').show();

        // Log the page sections for debugging
        // if (window.dacdbMemberPagination && window.dacdbMemberPagination.debug) {
        //     console.log('Available page sections:', 
        //         $('.page-content').map(function() {
        //             return this.id;
        //         }).get()
        //     );
        // }
    }

})(jQuery); 