/**
 * Fichier  : assets/js/pagination.js
 * Auteur   : KREMER Régis
 * Version  : 2.2.0
 * Date     : 2026-02-21
 * Description : JS — Module pagination client avec contrôles
 *
 * Changelog :
 *   v2.1.0 (2026-02-21) — Version initiale auditée et sécurisée
 *   v2.1.1 (2026-02-21) — Corrections sécurité, utils.js centralisé
 *   v2.2.0 (2026-02-21) — Permissions magasins, bons de commande, rapport d'écart,
 *                      import CSV, historique prix, bon de livraison multi-lignes
 */
// ================================================================
// pagination.js — MageStock V2.1
// Reusable pagination component
// ================================================================

const Paginator = {
    instances: {},
    
    /**
     * Create a new pagination instance
     * @param {string} id - Unique identifier for this paginator
     * @param {number} itemsPerPage - Items per page
     * @returns {object} Paginator instance
     */
    create(id, itemsPerPage = 20) {
        this.instances[id] = {
            id,
            itemsPerPage,
            currentPage: 1,
            totalPages: 0,
            totalItems: 0,
            onPageChange: null
        };
        return this.instances[id];
    },
    
    /**
     * Get paginated items
     * @param {string} id - Paginator ID
     * @param {array} items - All items
     * @returns {array} Paginated items
     */
    paginate(id, items) {
        const p = this.instances[id];
        if (!p) return items;
        
        p.totalItems = items.length;
        p.totalPages = Math.ceil(items.length / p.itemsPerPage);
        
        // Ensure current page is valid
        if (p.currentPage > p.totalPages && p.totalPages > 0) {
            p.currentPage = p.totalPages;
        }
        if (p.currentPage < 1) p.currentPage = 1;
        
        const start = (p.currentPage - 1) * p.itemsPerPage;
        const end = start + p.itemsPerPage;
        
        return items.slice(start, end);
    },
    
    /**
     * Render pagination controls
     * @param {string} id - Paginator ID
     * @param {string} containerId - DOM container ID
     */
    render(id, containerId) {
        const p = this.instances[id];
        const container = document.getElementById(containerId);
        
        if (!p || !container) return;
        
        if (p.totalPages <= 1) {
            container.innerHTML = '';
            return;
        }
        
        const hasPrev = p.currentPage > 1;
        const hasNext = p.currentPage < p.totalPages;
        
        const start = ((p.currentPage - 1) * p.itemsPerPage) + 1;
        const end = Math.min(p.currentPage * p.itemsPerPage, p.totalItems);
        
        container.innerHTML = `
            <div class="pagination">
                <button class="btn-page" ${!hasPrev ? 'disabled' : ''} onclick="Paginator.prev('${id}')">
                    ← Préc.
                </button>
                <span class="page-info">
                    Page ${p.currentPage} / ${p.totalPages}
                    <small>(${start}-${end} sur ${p.totalItems})</small>
                </span>
                <button class="btn-page" ${!hasNext ? 'disabled' : ''} onclick="Paginator.next('${id}')">
                    Suiv. →
                </button>
            </div>
        `;
    },
    
    /**
     * Go to previous page
     */
    prev(id) {
        const p = this.instances[id];
        if (!p || p.currentPage <= 1) return;
        p.currentPage--;
        if (p.onPageChange) p.onPageChange();
    },
    
    /**
     * Go to next page
     */
    next(id) {
        const p = this.instances[id];
        if (!p || p.currentPage >= p.totalPages) return;
        p.currentPage++;
        if (p.onPageChange) p.onPageChange();
    },
    
    /**
     * Go to specific page
     */
    goTo(id, page) {
        const p = this.instances[id];
        if (!p) return;
        p.currentPage = Math.max(1, Math.min(page, p.totalPages));
        if (p.onPageChange) p.onPageChange();
    },
    
    /**
     * Reset to page 1
     */
    reset(id) {
        const p = this.instances[id];
        if (!p) return;
        p.currentPage = 1;
    }
};
