<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : filters-tpl.php
 * @location     : /modules/components/filters-tpl.php
 * @type         : Component
 * @scope        : Cross-Module
 * @allowed      : Filter rendering, search functionality
 * @reusability  : Shared
 * @notes        : Dynamic filter component for all modules
 */
/*
 * <?php igny8_render_filters('planner_clusters'); ?>
 * <?php igny8_render_filters('writer_drafts'); ?>
 * 
 * @package Igny8Compact
 * @since 1.0.0
 */

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

// Render filters function
function igny8_render_filters($table_id) {
    // Load filters configuration
    $filters_config = require plugin_dir_path(__FILE__) . '../config/filters-config.php';
    $filters = $filters_config[$table_id] ?? [];

    // Load table configuration to get humanize_columns
    $tables_config = require plugin_dir_path(__FILE__) . '../config/tables-config.php';
    $GLOBALS['igny8_tables_config'] = $tables_config;
    $table_config = igny8_get_dynamic_table_config($table_id);
    $humanize_columns = $table_config['humanize_columns'] ?? [];

    // Set variables for component
    $module = explode('_', $table_id)[0];
    $tab = explode('_', $table_id)[1] ?? '';

    // Debug: Log filters array for verification
    
    // Start output buffering to capture HTML
    ob_start();
    ?>
<!-- Filters HTML -->
<div class="igny8-filters igny8-mb-20" data-table="<?php echo esc_attr($table_id); ?>">
    <div class="igny8-filter-bar">
        <?php foreach ($filters as $filter_key => $filter_config): ?>
            <div class="igny8-filter-group">
                <?php if ($filter_config['type'] === 'search'): ?>
                    <!-- Search Input -->
                    <input type="text" 
                           class="igny8-search-input igny8-input-md" 
                           id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>" 
                           placeholder="<?php echo esc_attr($filter_config['placeholder'] ?? 'Search...'); ?>" 
                           data-filter="<?php echo esc_attr($filter_key); ?>">
                <?php elseif ($filter_config['type'] === 'select'): ?>
                    <!-- Dropdown Filter -->
                    <div class="select" data-filter="<?php echo esc_attr($filter_key); ?>">
                        <button class="select-btn" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_btn" data-value="">
                            <span class="select-text"><?php echo esc_html($filter_config['label'] ?? (in_array($filter_config['field'] ?? $filter_key, $humanize_columns) ? igny8_humanize_label($filter_config['field'] ?? $filter_key) : ucfirst($filter_key))); ?></span>
                            <span class="dd-arrow">▼</span>
                        </button>
                        <div class="select-list" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_list">
                            <div class="select-item" data-value="">All <?php echo esc_html($filter_config['label'] ?? (in_array($filter_config['field'] ?? $filter_key, $humanize_columns) ? igny8_humanize_label($filter_config['field'] ?? $filter_key) : ucfirst($filter_key))); ?></div>
                            <?php if (isset($filter_config['options'])): ?>
                                <?php if (is_array($filter_config['options'])): ?>
                                    <?php foreach ($filter_config['options'] as $value => $label): ?>
                                        <div class="select-item" data-value="<?php echo esc_attr($value); ?>"><?php echo esc_html($label); ?></div>
                                    <?php endforeach; ?>
                                <?php elseif ($filter_config['options'] === 'dynamic_clusters'): ?>
                                    <?php
                                    // Load cluster options dynamically
                                    $cluster_options = igny8_get_cluster_options();
                                    if ($cluster_options) {
                                        foreach ($cluster_options as $option) {
                                            if (!empty($option['value'])) { // Skip "No Cluster" option
                                                echo '<div class="select-item" data-value="' . esc_attr($option['value']) . '">' . esc_html($option['label']) . '</div>';
                                            }
                                        }
                                    }
                                    ?>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php elseif ($filter_config['type'] === 'range'): ?>
                    <!-- Numeric Range Filter -->
                    <div class="select" data-filter="<?php echo esc_attr($filter_key); ?>">
                        <button class="select-btn" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_btn" data-value="">
                            <span class="select-text"><?php echo esc_html($filter_config['label'] ?? (in_array($filter_config['field'] ?? $filter_key, $humanize_columns) ? igny8_humanize_label($filter_config['field'] ?? $filter_key) : ucfirst($filter_key))); ?></span>
                            <span>▼</span>
                        </button>
                        <div class="select-list igny8-dropdown-panel" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_list">
                            <div style="margin-bottom: 10px;">
                                <label class="igny8-text-sm igny8-mb-5 igny8-text-muted">Min <?php echo esc_html($filter_config['label'] ?? (in_array($filter_config['field'] ?? $filter_key, $humanize_columns) ? igny8_humanize_label($filter_config['field'] ?? $filter_key) : ucfirst($filter_key))); ?>:</label>
                                <input type="number" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_min" placeholder="0" class="igny8-input-sm">
                            </div>
                            <div style="margin-bottom: 10px;">
                                <label class="igny8-text-sm igny8-mb-5 igny8-text-muted">Max <?php echo esc_html($filter_config['label'] ?? (in_array($filter_config['field'] ?? $filter_key, $humanize_columns) ? igny8_humanize_label($filter_config['field'] ?? $filter_key) : ucfirst($filter_key))); ?>:</label>
                                <input type="number" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_max" placeholder="10000" class="igny8-input-sm">
                            </div>
                            <div class="igny8-flex igny8-flex-gap-10">
                                <button type="button" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_ok" class="igny8-btn igny8-btn-primary igny8-flex igny8-p-5 igny8-text-xs">Ok</button>
                                <button type="button" id="<?php echo esc_attr($table_id); ?>_filter_<?php echo esc_attr($filter_key); ?>_clear" class="igny8-btn igny8-btn-secondary igny8-flex igny8-p-5 igny8-text-xs">Clear</button>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
        
        <div class="igny8-filter-actions">
            <button class="igny8-btn igny8-btn-primary" id="<?php echo esc_attr($table_id); ?>_filter_apply_btn">Apply Filters</button>
            <button class="igny8-btn igny8-btn-secondary" id="<?php echo esc_attr($table_id); ?>_filter_reset_btn">Reset</button>
        </div>
    </div>
</div>
<?php
    return ob_get_clean();
}

// Set default values
$table_id = $table_id ?? 'data_table';
$filters = $filters ?? [];
$module = $module ?? '';
$tab = $tab ?? '';

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