<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : pagination-tpl.php
 * @location     : /modules/components/pagination-tpl.php
 * @type         : Component
 * @scope        : Cross-Module
 * @allowed      : Pagination rendering, navigation controls
 * @reusability  : Shared
 * @notes        : Dynamic pagination component for all modules
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Render pagination function
function igny8_render_pagination($table_id, $pagination = []) {
    // Set variables for component
    $module = explode('_', $table_id)[0];
    $tab = explode('_', $table_id)[1] ?? '';
    
    // Set default pagination values
    $pagination = array_merge([
        'current_page' => 1,
        'total_pages' => 1,
        'per_page' => 10,
        'total_items' => 0
    ], $pagination);
    
    // Start output buffering to capture HTML
    ob_start();
    ?>
<!-- Pagination -->
<div class="igny8-pagination igny8-mt-20" 
     data-table="<?php echo esc_attr($table_id); ?>" 
     data-current-page="<?php echo esc_attr($pagination['current_page']); ?>" 
     data-per-page="<?php echo esc_attr($pagination['per_page']); ?>" 
     data-total-items="<?php echo esc_attr($pagination['total_items']); ?>" 
     data-module="<?php echo esc_attr($module); ?>" 
     data-tab="<?php echo esc_attr($tab); ?>">
    
    <button class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-page-btn" 
            data-page="<?php echo max(1, $pagination['current_page'] - 1); ?>"
            <?php echo $pagination['current_page'] <= 1 ? 'disabled' : ''; ?>>
        ‹ Previous
    </button>
    
    <?php if ($pagination['total_items'] > 20): ?>
        <?php
        $current = $pagination['current_page'];
        $total = $pagination['total_pages'];
        ?>
        
        <?php // Always show first 2 pages ?>
        <?php for ($i = 1; $i <= min(2, $total); $i++): ?>
            <button class="igny8-btn igny8-btn-sm igny8-page-btn <?php echo $i === $current ? 'igny8-btn-primary' : 'igny8-btn-outline'; ?>" 
                    data-page="<?php echo $i; ?>">
                <?php echo $i; ?>
            </button>
        <?php endfor; ?>
        
        <?php // Add ellipsis if there's a gap ?>
        <?php if ($total > 4 && $current > 3): ?>
            <span style="margin: 0 8px; color: #666;">...</span>
        <?php endif; ?>
        
        <?php // Show current page if it's not in first 2 or last 2 ?>
        <?php if ($current > 2 && $current < $total - 1): ?>
            <button class="igny8-btn igny8-btn-sm igny8-page-btn igny8-btn-primary" 
                    data-page="<?php echo $current; ?>">
                <?php echo $current; ?>
            </button>
        <?php endif; ?>
        
        <?php // Add ellipsis before last 2 if there's a gap ?>
        <?php if ($total > 4 && $current < $total - 2): ?>
            <span style="margin: 0 8px; color: #666;">...</span>
        <?php endif; ?>
        
        <?php // Always show last 2 pages (if different from first 2) ?>
        <?php for ($i = max(3, $total - 1); $i <= $total; $i++): ?>
            <?php if ($i > 2): // Don't duplicate first 2 pages ?>
                <button class="igny8-btn igny8-btn-sm igny8-page-btn <?php echo $i === $current ? 'igny8-btn-primary' : 'igny8-btn-outline'; ?>" 
                        data-page="<?php echo $i; ?>">
                    <?php echo $i; ?>
                </button>
            <?php endif; ?>
        <?php endfor; ?>
    <?php endif; ?>
    
    <button class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-page-btn" 
            data-page="<?php echo min($pagination['total_pages'], $pagination['current_page'] + 1); ?>"
            <?php echo $pagination['current_page'] >= $pagination['total_pages'] ? 'disabled' : ''; ?>>
        Next ›
    </button>
    
    <!-- Records per page selector -->
    <div class="igny8-per-page-selector" style="margin-left: 20px; display: inline-flex; align-items: center; gap: 8px;">
        <label for="<?php echo esc_attr($table_id); ?>_per_page" style="font-size: 12px; color: #666;">Show:</label>
        <select id="<?php echo esc_attr($table_id); ?>_per_page" class="igny8-per-page-select" data-table="<?php echo esc_attr($table_id); ?>" style="padding: 4px 8px; font-size: 12px; border: 1px solid #ddd; border-radius: 4px;">
            <option value="10" <?php selected($pagination['per_page'], 10); ?>>10</option>
            <option value="20" <?php selected($pagination['per_page'], 20); ?>>20</option>
            <option value="50" <?php selected($pagination['per_page'], 50); ?>>50</option>
            <option value="100" <?php selected($pagination['per_page'], 100); ?>>100</option>
        </select>
        <span style="font-size: 12px; color: #666;">per page</span>
    </div>
    
    <span style="margin-left: 12px; font-size: 12px; color: #666;">
        Showing <?php echo (($pagination['current_page'] - 1) * $pagination['per_page']) + 1; ?>-<?php echo min($pagination['current_page'] * $pagination['per_page'], $pagination['total_items']); ?> of <?php echo $pagination['total_items']; ?> items
    </span>
</div>
<?php
    return ob_get_clean();
}

// Set default values
$table_id = $table_id ?? 'data_table';
$pagination = $pagination ?? [
    'current_page' => 1,
    'total_pages' => 1,
    'per_page' => 10,
    'total_items' => 0
];
$module = $module ?? '';
$tab = $tab ?? '';

// Debug state: Pagination HTML rendered
if (function_exists('igny8_debug_state')) {
    igny8_debug_state('PAGINATION_HTML_RENDERED', true, 'Pagination HTML rendered for ' . $table_id);
}
?>
