/**
 * 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 element exists
        if ($('#pagination-dacdb').length) {
            console.log('Found pagination element');
            
            // Ensure pagination data exists
            if (typeof dacdbMemberPagination === 'undefined') {
                console.error('Pagination data not found');
                return;
            }

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

            console.log('Pagination data:', {
                totalPages: totalPages,
                currentPage: currentPage,
                debug: debug
            });
            
            try {
                // Destroy existing pagination if it exists
                try {
                    $('#pagination-dacdb').twbsPagination('destroy');
                } catch (e) {
                    // Ignore errors if pagination doesn't exist yet
                }

                // Initialize pagination
                $('#pagination-dacdb').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);
                    }
                });

                console.log('Pagination initialized successfully');

                // Show first page by default and hide others
                $('.page-content').hide();
                $('#pagsec1').show();

                // Log the page sections for debugging
                if (debug) {
                    console.log('Available page sections:', 
                        $('.page-content').map(function() {
                            return this.id;
                        }).get()
                    );
                }
            } catch (error) {
                console.error('Error initializing pagination:', error);
            }
        } else {
            console.warn('Pagination element not found');
        }
    }

})(jQuery); 