<?php
namespace UM_Elementor;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Css_Filter;
use Elementor\Repeater;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Widget_Base;
use UM_Elementor\Helpers\WP_User_Meta_Renderer;
use UM_Elementor\Helpers\UM_Elementor_Utils;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * WordPress Native User Directory Grid Widget
 * 
 * Displays WordPress users in a responsive grid layout with customizable styling
 * No Ultimate Member dependencies required
 */
class WP_ELEMENTOR_USER_LIST_MODULE extends Widget_Base {

	/**
	 * @var WP_User_Meta_Renderer
	 */
	private $meta_renderer;

	/**
	 * @var UM_Elementor_Utils
	 */

	/**
	 * Get widget name
	 */
	public function get_name() {
		return 'wp-user-list';
	}

	/**
	 * Get widget title
	 */
	public function get_title() {
		return __( 'WP: User Directory', 'ultimate-member-widgets-for-elementor' );
	}

	/**
	 * Get widget icon
	 */
	public function get_icon() {
		return 'eicon-person';
	}

	/**
	 * Get widget categories
	 */
	public function get_categories() {
		return ['um-addons-elementor'];
	}

	/**
	 * Get meta renderer instance
	 */
	private function get_meta_renderer() {
		if ( !$this->meta_renderer ) {
			$this->meta_renderer = new WP_User_Meta_Renderer();
		}
		return $this->meta_renderer;
	}

	/**
	 * Register widget controls
	 */
	protected function register_controls() {
		$this->register_user_query_controls();
		$this->register_layout_controls();
		$this->register_pagination_controls();
		$this->register_filter_controls();
		$this->register_search_controls();
		$this->register_user_meta_controls();
		$this->register_user_title_controls();
		$this->register_user_image_controls();
		$this->register_style_controls();
	}

	/**
	 * Register user query controls
	 */
	protected function register_user_query_controls() {
		$this->start_controls_section(
			'section_user_query',
			[
				'label' => __('User Query', 'ultimate-member-widgets-for-elementor'),
			]
		);

		// Show members by role
		$this->add_control(
			'query_show_selected_roles',
			[
				'label' => __('Filter by User Role', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::SWITCHER,
				'default' => false,
				'label_on' => __('Yes', 'ultimate-member-widgets-for-elementor'),
				'label_off' => __('No', 'ultimate-member-widgets-for-elementor'),
				'return_value' => 'yes',
			]
		);

		// User Roles - Select2
		$this->add_control(
			'query_user_roles',
			[
				'label' => __('Select Roles', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::SELECT2,
				'default' => '',
				'multiple' => true,
				'label_block' => true,
				'options' => $this->get_user_roles(),
				'condition' => [
					'query_show_selected_roles' => 'yes',
				],
			]
		);

		// Number of Members to display
		$this->add_control(
			'user_numbers',
			[
				'label' => __('Number of Users', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::NUMBER,
				'default' => 4,
				'min' => 1,
				'max' => 1000,
				'step' => 1,
			]
		);

		// Order Members by
		$this->add_control(
			'query_user_order_by',
			[
				'label' => esc_html__('Order by', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'default' => 'display_name',
				'options' => [
					'ID' => esc_html__('ID', 'ultimate-member-widgets-for-elementor'),
					'display_name' => esc_html__('Display Name', 'ultimate-member-widgets-for-elementor'),
					'user_login' => esc_html__('Username', 'ultimate-member-widgets-for-elementor'),
					'user_email' => esc_html__('User Email', 'ultimate-member-widgets-for-elementor'),
					'user_registered' => esc_html__('Registered', 'ultimate-member-widgets-for-elementor'),
					'first_name' => esc_html__('First Name', 'ultimate-member-widgets-for-elementor'),
					'last_name' => esc_html__('Last Name', 'ultimate-member-widgets-for-elementor'),
					'custom_field' => esc_html__('Custom Field', 'ultimate-member-widgets-for-elementor'),
				],
			]
		);

		// Meta Key
		$this->add_control(
			'user_query_by_meta_key',
			[
				'label' => esc_html__('Meta Key', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'label_block' => true,
				'condition' => ['query_user_order_by' => 'custom_field']
			]
		);

		// Members Order
		$this->add_control(
			'query_user_order',
			[
				'label' => esc_html__('Order', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'default' => 'ASC',
				'options' => [
					'ASC' => esc_html__('Ascending', 'ultimate-member-widgets-for-elementor'),
					'DESC' => esc_html__('Descending', 'ultimate-member-widgets-for-elementor'),
				],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Register layout controls
	 */
	protected function register_layout_controls() {
		$this->start_controls_section(
			'um_elementor_section_member_grid_layout_' . $this->get_name(),
			[
				'label' => __( 'Layout', 'ultimate-member-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE
			]
		);

		$this->add_control(
			'grid_column_no',
			[
				'label'   => esc_html__( 'Columns', 'ultimate-member-widgets-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '3',
				'options' => [
					'1' => esc_html__( '1', 'ultimate-member-widgets-for-elementor' ),
					'2' => esc_html__( '2', 'ultimate-member-widgets-for-elementor' ),
					'3' => esc_html__( '3', 'ultimate-member-widgets-for-elementor' ),
					'4' => esc_html__( '4', 'ultimate-member-widgets-for-elementor' ),
					'5' => esc_html__( '5', 'ultimate-member-widgets-for-elementor' ),
					'6' => esc_html__( '6', 'ultimate-member-widgets-for-elementor' ),
				],
			]
		);

		$this->add_control(
			'user_layout',
			[
				'label'   => esc_html__( 'Layout', 'ultimate-member-widgets-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 1,
				'options' => [
					1 => esc_html__( 'Card', 'ultimate-member-widgets-for-elementor' ),
					2 => esc_html__( 'List', 'ultimate-member-widgets-for-elementor' ),
				],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Register pagination controls
	 */
	protected function register_pagination_controls() {
		$this->start_controls_section(
			'um_elementor_section_pagination_' . $this->get_name(),
			[
				'label' => __( 'Pagination', 'ultimate-member-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT
			]
		);

		$this->add_control(
			'enable_pagination',
			[
				'label' => __( 'Enable Pagination', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'no',
				'label_on' => __( 'Yes', 'ultimate-member-widgets-for-elementor' ),
				'label_off' => __( 'No', 'ultimate-member-widgets-for-elementor' ),
				'return_value' => 'yes',
			]
		);

		$this->add_control(
			'users_per_page',
			[
				'label' => __( 'Users Per Page', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::NUMBER,
				'default' => 12,
				'min' => 1,
				'max' => 100,
				'condition' => ['enable_pagination' => 'yes'],
			]
		);

		$this->add_control(
			'pagination_style',
			[
				'label' => __( 'Pagination Style', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'numbers',
				'options' => [
					'numbers' => __( 'Numbers', 'ultimate-member-widgets-for-elementor' ),
					'load_more' => __( 'Load More Button', 'ultimate-member-widgets-for-elementor' ),
					'infinite_scroll' => __( 'Infinite Scroll', 'ultimate-member-widgets-for-elementor' ),
				],
				'condition' => ['enable_pagination' => 'yes'],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Register filter controls
	 */
	protected function register_filter_controls() {
		$this->start_controls_section(
			'um_elementor_section_filters_' . $this->get_name(),
			[
				'label' => __( 'Advanced Filters', 'ultimate-member-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT
			]
		);

		$this->add_control(
			'enable_filters',
			[
				'label' => __( 'Enable Advanced Filters', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'no',
				'label_on' => __( 'Yes', 'ultimate-member-widgets-for-elementor' ),
				'label_off' => __( 'No', 'ultimate-member-widgets-for-elementor' ),
				'return_value' => 'yes',
				'description' => __( 'Enable advanced filtering, sorting, and search functionality', 'ultimate-member-widgets-for-elementor' ),
			]
		);

		// Filter Persistence
		$this->add_control(
			'enable_filter_persistence',
			[
				'label' => __( 'Enable Filter Persistence', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'yes',
				'label_on' => __( 'Yes', 'ultimate-member-widgets-for-elementor' ),
				'label_off' => __( 'No', 'ultimate-member-widgets-for-elementor' ),
				'return_value' => 'yes',
				'description' => __( 'Remember user filter selections across page visits', 'ultimate-member-widgets-for-elementor' ),
				'condition' => ['enable_filters' => 'yes'],
			]
		);

		$this->add_control(
			'enable_url_params',
			[
				'label' => __( 'Enable URL Parameters', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'yes',
				'label_on' => __( 'Yes', 'ultimate-member-widgets-for-elementor' ),
				'label_off' => __( 'No', 'ultimate-member-widgets-for-elementor' ),
				'return_value' => 'yes',
				'description' => __( 'Update URL with filter state for bookmarking and sharing', 'ultimate-member-widgets-for-elementor' ),
				'condition' => ['enable_filters' => 'yes'],
			]
		);

		// Accessibility Settings
		$this->add_control(
			'enable_keyboard_navigation',
			[
				'label' => __( 'Enable Keyboard Navigation', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'yes',
				'label_on' => __( 'Yes', 'ultimate-member-widgets-for-elementor' ),
				'label_off' => __( 'No', 'ultimate-member-widgets-for-elementor' ),
				'return_value' => 'yes',
				'description' => __( 'Allow users to navigate filters using keyboard', 'ultimate-member-widgets-for-elementor' ),
				'condition' => ['enable_filters' => 'yes'],
			]
		);

		$this->add_control(
			'enable_aria_labels',
			[
				'label' => __( 'Enable ARIA Labels', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'yes',
				'label_on' => __( 'Yes', 'ultimate-member-widgets-for-elementor' ),
				'label_off' => __( 'No', 'ultimate-member-widgets-for-elementor' ),
				'return_value' => 'yes',
				'description' => __( 'Add accessibility labels for screen readers', 'ultimate-member-widgets-for-elementor' ),
				'condition' => ['enable_filters' => 'yes'],
			]
		);

		// Advanced Filter Fields
		$filter_repeater = new Repeater();

		$filter_repeater->add_control(
			'wp_user_filter_field_value',
			[
				'label' => esc_html__( 'Meta Key', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SELECT,
				'default' => esc_html__( 'description', 'ultimate-member-widgets-for-elementor' ),
				'label_block' => true,
				'options' => \user_elementor_wp_get_user_meta_keys_combine(),
			]
		);

		$filter_repeater->add_control(
			'wp_user_filter_field_icon',
			[
				'label' => esc_html__( 'Icon', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon',
				'skin' => 'inline',
				'label_block' => false,
			]
		);

		$filter_repeater->add_control(
			'filter_type',
			[
				'label' => __( 'Filter Type', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'dropdown',
				'options' => [
					'dropdown' => __( 'Dropdown', 'ultimate-member-widgets-for-elementor' ),
					'checkbox' => __( 'Checkbox', 'ultimate-member-widgets-for-elementor' ),
					'radio' => __( 'Radio Buttons', 'ultimate-member-widgets-for-elementor' ),
				],
			]
		);

		$filter_upgrade_url = esc_url( UM_Elementor_Pro_Links::upgrade_url( 'widget-filter-fields' ) );

		$this->add_control(
			'wp_user_filter_control',
			[
				'label' => esc_html__( 'Filter Fields', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::REPEATER,
				'seperator' => 'before',
				'fields' => $filter_repeater->get_controls(),
				'title_field' => '{{{wp_user_filter_field_value}}}',
				'default' => [
					['wp_user_filter_field_value' => 'description'],
					['wp_user_filter_field_value' => 'user_url'],
				],
				'condition' => ['enable_filters' => 'yes'],
				'max_items' => 2,
				'description' => sprintf(
					/* translators: %s: Pro upgrade URL */
					__( 'Free version allows up to 2 filter fields. <a href="%s" target="_blank" rel="noopener noreferrer" class="um-upgrade-link">Upgrade to Pro</a> for unlimited filter fields.', 'ultimate-member-widgets-for-elementor' ),
					$filter_upgrade_url
				),
			]
		);

		// Add upgrade notice for filter fields
		$this->add_control(
			'filter_upgrade_notice',
			[
				'type' => Controls_Manager::RAW_HTML,
				'raw' => '<div class="elementor-control-raw-html um-upgrade-notice">
					<div class="um-upgrade-notice-content">
						<i class="fas fa-star" style="color: #ffd700; margin-right: 5px;"></i>
						<strong>' . __( 'Pro Feature', 'ultimate-member-widgets-for-elementor' ) . '</strong><br>
						' . __( 'Add unlimited filter fields with advanced filtering options. Upgrade to Pro for enhanced functionality!', 'ultimate-member-widgets-for-elementor' ) . '
						<br><br>
						<a href="' . $filter_upgrade_url . '" target="_blank" rel="noopener noreferrer" class="um-upgrade-button" style="background: #0073aa; color: white; padding: 8px 16px; text-decoration: none; border-radius: 4px; display: inline-block;">
							' . __( 'Upgrade to Pro', 'ultimate-member-widgets-for-elementor' ) . '
						</a>
					</div>
				</div>',
				'condition' => ['enable_filters' => 'yes'],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Register search controls
	 */
	protected function register_search_controls() {
		$this->start_controls_section(
			'um_elementor_section_search_' . $this->get_name(),
			[
				'label' => __( 'Search', 'ultimate-member-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT
			]
		);

		$this->add_control(
			'enable_search',
			[
				'label' => __( 'Enable Search', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'no',
				'label_on' => __( 'Yes', 'ultimate-member-widgets-for-elementor' ),
				'label_off' => __( 'No', 'ultimate-member-widgets-for-elementor' ),
				'return_value' => 'yes',
			]
		);

		$this->add_control(
			'search_placeholder',
			[
				'label' => __( 'Search Placeholder', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::TEXT,
				'default' => __( 'Search users...', 'ultimate-member-widgets-for-elementor' ),
				'condition' => ['enable_search' => 'yes'],
			]
		);

		$this->add_control(
			'search_fields',
			[
				'label' => __( 'Search Fields', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SELECT2,
				'multiple' => true,
				'default' => ['display_name', 'user_email'],
				'options' => [
					'display_name' => __( 'Display Name', 'ultimate-member-widgets-for-elementor' ),
					'user_email' => __( 'Email', 'ultimate-member-widgets-for-elementor' ),
					'first_name' => __( 'First Name', 'ultimate-member-widgets-for-elementor' ),
					'last_name' => __( 'Last Name', 'ultimate-member-widgets-for-elementor' ),
					'user_login' => __( 'Username', 'ultimate-member-widgets-for-elementor' ),
				],
				'condition' => ['enable_search' => 'yes'],
			]
		);

		$this->add_control(
			'search_position',
			[
				'label' => __( 'Search Position', 'ultimate-member-widgets-for-elementor' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'top',
				'options' => [
					'top' => __( 'Top', 'ultimate-member-widgets-for-elementor' ),
					'bottom' => __( 'Bottom', 'ultimate-member-widgets-for-elementor' ),
				],
				'condition' => ['enable_search' => 'yes'],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Register user meta controls
	 */
	protected function register_user_meta_controls() {
		$this->start_controls_section(
			'um_user_meta_section',
			[
				'label' => esc_html__('User Fields', 'ultimate-member-widgets-for-elementor'),
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'um_user_meta_field_value',
			[
				'label' => esc_html__('Meta Key', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'default' => 'description',
				'label_block' => true,
				'options' => \user_elementor_wp_get_user_meta_keys_combine(),
			]
		);

		$repeater->add_control(
			'um_user_meta_field_label',
			[
				'label' => __('Show Field Label', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					1 => [
						'title' => __('Yes', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-check',
					],
					0 => [
						'title' => __('No', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-ban',
					]
				],
                'default' => '1',
            ]
        );

        $repeater->add_control(
            'field_visibility',
            [
                'label'   => __( 'Visible to', 'ultimate-member-widgets-for-elementor' ),
                'type'    => Controls_Manager::SELECT,
                'default' => \UM_Elementor_Field_Visibility::EVERYONE,
                'options' => [
                    \UM_Elementor_Field_Visibility::EVERYONE       => __( 'Everyone', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::LOGGED_IN      => __( 'Logged-in users only', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::SPECIFIC_ROLES => __( 'Specific roles', 'ultimate-member-widgets-for-elementor' ),
                ],
            ]
        );

        $repeater->add_control(
            'field_visibility_roles',
            [
                'label'       => __( 'Allowed roles', 'ultimate-member-widgets-for-elementor' ),
                'type'        => Controls_Manager::SELECT2,
                'multiple'    => true,
                'default'     => [],
                'options'     => self::get_all_roles_for_visibility(),
                'condition'   => [
                    'field_visibility' => \UM_Elementor_Field_Visibility::SPECIFIC_ROLES,
                ],
                'description' => __( 'Administrators always see all fields.', 'ultimate-member-widgets-for-elementor' ),
            ]
        );

        $repeater->add_control(
            'field_value_condition_enable',
            [
                'label'        => __( 'Conditional display', 'ultimate-member-widgets-for-elementor' ),
                'type'         => Controls_Manager::SWITCHER,
                'label_on'     => __( 'On', 'ultimate-member-widgets-for-elementor' ),
                'label_off'    => __( 'Off', 'ultimate-member-widgets-for-elementor' ),
                'return_value' => 'yes',
                'default'      => '',
                'separator'    => 'before',
                'description'  => __( 'Show this field only when a value on the displayed user matches a rule (e.g. Gold badge only if membership = gold).', 'ultimate-member-widgets-for-elementor' ),
            ]
        );

        $repeater->add_control(
            'field_value_condition_key',
            [
                'label'       => __( 'When field', 'ultimate-member-widgets-for-elementor' ),
                'type'        => Controls_Manager::SELECT,
                'label_block' => true,
                'default'     => '',
                'options'     => \user_elementor_wp_get_user_meta_keys_combine(),
                'condition'   => [ 'field_value_condition_enable' => 'yes' ],
            ]
        );

        $repeater->add_control(
            'field_value_condition_operator',
            [
                'label'     => __( 'Operator', 'ultimate-member-widgets-for-elementor' ),
                'type'      => Controls_Manager::SELECT,
                'default'   => \UM_Elementor_Field_Visibility::OP_IS,
                'options'   => [
                    \UM_Elementor_Field_Visibility::OP_IS           => __( 'is equal to', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_IS_NOT       => __( 'is not equal to', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_CONTAINS     => __( 'contains', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_NOT_CONTAINS => __( 'does not contain', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_IN_LIST      => __( 'is one of (comma list)', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_GREATER_THAN => __( 'is greater than', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_LESS_THAN    => __( 'is less than', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_NOT_EMPTY    => __( 'is not empty', 'ultimate-member-widgets-for-elementor' ),
                    \UM_Elementor_Field_Visibility::OP_EMPTY        => __( 'is empty', 'ultimate-member-widgets-for-elementor' ),
                ],
                'condition' => [ 'field_value_condition_enable' => 'yes' ],
            ]
        );

        $repeater->add_control(
            'field_value_condition_value',
            [
                'label'       => __( 'Value', 'ultimate-member-widgets-for-elementor' ),
                'type'        => Controls_Manager::TEXT,
                'default'     => '',
                'label_block' => true,
                'placeholder' => __( 'e.g. gold', 'ultimate-member-widgets-for-elementor' ),
                'condition'   => [
                    'field_value_condition_enable'    => 'yes',
                    'field_value_condition_operator!' => [
                        \UM_Elementor_Field_Visibility::OP_EMPTY,
                        \UM_Elementor_Field_Visibility::OP_NOT_EMPTY,
                    ],
                ],
            ]
        );

		$this->add_control(
			'um_user_meta_control',
			[
				'label' => esc_html__('User Fields', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'title_field' => '{{{um_user_meta_field_value}}}',
				'default' => [
					['um_user_meta_field_value' => 'description'],
					['um_user_meta_field_value' => 'user_url'],
				],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * All WP + UM roles as an options array for the visibility repeater control.
	 *
	 * @return array<string, string>
	 */
	private static function get_all_roles_for_visibility(): array {
		$options = [];

		foreach ( wp_roles()->get_names() as $key => $name ) {
			if ( $key === 'administrator' ) {
				continue;
			}
			$options[ $key ] = translate_user_role( $name );
		}

		if ( function_exists( 'UM' ) ) {
			$um_role_slugs = (array) get_option( 'um_roles', [] );
			foreach ( $um_role_slugs as $slug ) {
				$role_key = 'um_' . $slug;
				if ( isset( $options[ $role_key ] ) ) {
					continue;
				}
				$role_meta = get_option( 'um_role_' . $slug . '_meta', [] );
				$label     = ! empty( $role_meta['name'] ) ? $role_meta['name'] : $role_key;
				$options[ $role_key ] = esc_html( $label );
			}
		}

		return $options;
	}

	/**
	 * Register user title controls
	 */
	protected function register_user_title_controls() {
		$this->start_controls_section(
			'user_title_section',
			[
				'label' => esc_html__('User Title', 'ultimate-member-widgets-for-elementor'),
			]
		);

		$this->add_control(
			'um_elementor_show_member_name',
			[
				'label' => esc_html__('Show User Title', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					1 => [
						'title' => esc_html__('Yes', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-check',
					],
					0 => [
						'title' => esc_html__('No', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-ban',
					]
				],
				'default' => '1',
			]
		);

		$this->add_control(
			'user_display_title',
			[
				'label' => esc_html__('Title Source', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'default' => 'display_name',
				'options' => [
					'first_name' => esc_html__('First Name', 'ultimate-member-widgets-for-elementor'),
					'last_name' => esc_html__('Last Name', 'ultimate-member-widgets-for-elementor'),
					'full_name' => esc_html__('Full Name (First Last)', 'ultimate-member-widgets-for-elementor'),
					'full_name_reverse' => esc_html__('Full Name (Last, First)', 'ultimate-member-widgets-for-elementor'),
					'display_name' => esc_html__('Display Name', 'ultimate-member-widgets-for-elementor'),
					'username' => esc_html__('Username', 'ultimate-member-widgets-for-elementor'),
					'nickname' => esc_html__('Nickname', 'ultimate-member-widgets-for-elementor'),
				],
				'condition' => ['um_elementor_show_member_name' => '1']
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Register user image controls
	 */
	protected function register_user_image_controls() {
		$this->start_controls_section(
			'user_image_section',
			[
				'label' => esc_html__('User Image', 'ultimate-member-widgets-for-elementor'),
			]
		);

		$this->add_control(
			'um_elementor_show_user_image',
			[
				'label' => esc_html__('Show User Image', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					1 => [
						'title' => esc_html__('Yes', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-check',
					],
					0 => [
						'title' => esc_html__('No', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-ban',
					]
				],
				'default' => '1',
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Register style controls
	 */
	protected function register_style_controls() {
		$this->start_controls_section(
			'um_elementor_section_member_style_' . $this->get_name(),
			[
				'label' => __('User Card', 'ultimate-member-widgets-for-elementor'),
				'tab' => Controls_Manager::TAB_STYLE
			]
		);

		$this->add_control(
			'title_alignment',
			[
				'label' => __('Text Alignment', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'text-left' => [
						'title' => __('Left', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-text-align-left',
					],
					'text-center' => [
						'title' => __('Center', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-text-align-center',
					],
					'text-right' => [
						'title' => __('Right', 'ultimate-member-widgets-for-elementor'),
						'icon' => 'eicon-text-align-right',
					]
				],
				'default' => 'text-center'
			]
		);

		$this->add_control(
			'um_member_block_color',
			[
				'label' => __('Background Color', 'ultimate-member-widgets-for-elementor'),
				'type' => Controls_Manager::COLOR,
				'default' => '#fff9f0',
				'selectors' => [
					'{{WRAPPER}} .um-widget-member-holder' => 'background-color: {{VALUE}}',
				]
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Get user roles
	 */
	private function get_user_roles() {
		global $wp_roles;
		
		if ( ! isset( $wp_roles ) ) {
			$wp_roles = new WP_Roles();
		}

		$roles = [];
		foreach ( $wp_roles->get_names() as $role_key => $role_name ) {
			$roles[$role_key] = $role_name;
		}

		return $roles;
	}



	/**
	 * Render widget output
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();
		$widget_id = $this->get_id();
		
		// Get current page from URL or default to 1
		$current_page = max(1, get_query_var('paged') ? get_query_var('paged') : 1);
		
		// Get users data
		$users_data = $this->get_filtered_users_with_pagination($settings, $current_page);
		
		// Start main container
		$users_per_page = ($settings['enable_pagination'] ?? 'no') === 'yes' ? (int) ($settings['users_per_page'] ?? 12) : 12;
		
		echo '<div class="wp-user-list-container" data-widget-id="' . esc_attr($widget_id) . '" data-post-id="' . esc_attr( (string) get_the_ID() ) . '" data-per-page="' . esc_attr($users_per_page) . '">';
		
		// Render search interface if enabled and position is top
		if (($settings['enable_search'] ?? 'no') === 'yes' && 
			($settings['search_position'] ?? 'top') === 'top') {
			$this->render_search_interface($settings, $widget_id);
		}
		
		// Render filter interface if enabled
		if (($settings['enable_filters'] ?? 'no') === 'yes') {
			$this->render_filter_interface($settings, $widget_id);
		}
		
		// Render user grid container
		echo '<div class="wp-filter-results">';
		
		if ( empty( $users_data['users'] ) ) {
			echo '<p>' . esc_html__( 'No users found', 'ultimate-member-widgets-for-elementor' ) . '</p>';
		} else {
			$this->render_user_grid($users_data['users'], $settings);
		}
		
		echo '</div>'; // End wp-filter-results
		
		// Render pagination if enabled
		if (($settings['enable_pagination'] ?? 'no') === 'yes') {
			$this->render_pagination($settings, $widget_id, $current_page, $users_data['total_pages']);
		}
		
		echo '</div>'; // End wp-user-list-container
		
		// Localize script data
		$this->localize_script_data($settings);

        // Options updates removed
	}

	/**
	 * Get filtered users with pagination
	 */
	private function get_filtered_users_with_pagination($settings, $current_page = 1) {
        $settings['paged'] = $current_page;
        
        if (um_use_new_query_engine()) {
            $result = \UM_User_Query_Engine::get_users($settings);
            $users = $result['users'];
            $total_users = $result['total'];
        } else {
            $args = \UM_Elementor\Helpers\WP_User_Query_Legacy::build($settings);
            $args['paged'] = $current_page;
            $members_query = new \WP_User_Query($args);
            $users_raw = $members_query->get_results();
            $total_users = \UM_Elementor\Helpers\WP_User_Query_Legacy::getTotalUsers($args);
            $users = \UM_Elementor\Helpers\WP_User_Query_Legacy::filterDisplayableUsers($users_raw);
        }

        $total_pages = ($settings['enable_pagination'] ?? 'no') === 'yes' ? 
            ceil($total_users / max(1, (int)($settings['users_per_page'] ?? 12))) : 1;

        return [
            'users' => $users,
            'total_users' => $total_users,
            'total_pages' => $total_pages
        ];
	}

	/**
	 * Render user grid
	 */
	private function render_user_grid($users, $settings) {
		$column = $settings['grid_column_no'] ?? '3';
		$layout = absint($settings['user_layout'] ?? 1);

		?>
		<div class="wp-team-carousel-wrapper grid grid-cols-<?php echo esc_attr($column); ?>">
			<?php 
			foreach ( $users as $user_item ) :
                $user_id = is_object($user_item) ? $user_item->ID : $user_item;
				$this->render_user_item($user_id, $settings, $layout);
			endforeach; 
			?>
		</div>
		<?php
	}

	/**
	 * Render single user item
	 */
	private function render_user_item($user_id, $settings, $layout) {
		$user = get_user_by('ID', $user_id);
		
		if (!$user) {
			return;
		}

		$member_name_type = $settings['user_display_title'] ?? 'display_name';

		if ( $layout == 1 ) {
			$this->render_card_layout($user_id, $settings, $member_name_type);
		} elseif ( $layout == 2 ) {
			$this->render_list_layout($user_id, $settings, $member_name_type);
		}
	}

	/**
	 * Render card layout
	 */
	private function render_card_layout($user_id, $settings, $member_name_type) {
		$align = $settings['title_alignment'] ?? 'text-center';
		$meta_renderer = $this->get_meta_renderer();
		$meta_renderer->setCurrentUser($user_id);
		
		$user = get_user_by('ID', $user_id);
		$display_name = UM_Elementor_Utils::getUserDisplayName($user_id, $member_name_type);
		$profile_url = UM_Elementor_Utils::getUserProfileUrl($user_id);
		$avatar_url = UM_Elementor_Utils::getUserAvatarUrl($user_id, 100);
		
		?>
		<div class="wp-item-user">
			<div class="um-widget-member-holder">
				<?php if (($settings['um_elementor_show_user_image'] ?? 1) == 1): ?>
				<div class="um-elementor-member-image <?php echo esc_attr($align); ?>">
					<a title="<?php echo esc_attr($display_name); ?>" href="<?php echo esc_url($profile_url); ?>">
						<img src="<?php echo esc_url($avatar_url); ?>" alt="<?php echo esc_attr($display_name); ?>" width="100" height="100">
					</a>
				</div>
				<?php endif; ?>
				
				<div class="um-elementor-member-content">
					<?php if (($settings['um_elementor_show_member_name'] ?? 1) == 1): ?>
					<div class="um-elementor-member-name <?php echo esc_attr($align); ?>">
						<p>
							<a title="<?php echo esc_attr($display_name); ?>" href="<?php echo esc_url($profile_url); ?>">
								<?php echo esc_html($display_name); ?>
							</a>
						</p>
					</div>
					<?php endif; ?>
					
					<div class="um-custom-field-container <?php echo esc_attr($align); ?>">
						<?php echo wp_kses_post($meta_renderer->render_meta_fields($settings)); ?>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Render list layout
	 */
	private function render_list_layout($user_id, $settings, $member_name_type) {
		$align = $settings['title_alignment'] ?? 'text-left';
		$meta_renderer = $this->get_meta_renderer();
		$meta_renderer->setCurrentUser($user_id);
		
		$user = get_user_by('ID', $user_id);
		$display_name = UM_Elementor_Utils::getUserDisplayName($user_id, $member_name_type);
		$profile_url = UM_Elementor_Utils::getUserProfileUrl($user_id);
		$avatar_url = UM_Elementor_Utils::getUserAvatarUrl($user_id, 100);
		
		?>
		<div class="wp-item-user">
			<div class="um-widget-member-holder">
				<div class="user-layout-spaced grid">
					<?php if (($settings['um_elementor_show_user_image'] ?? 1) == 1): ?>
					<div class="user-layout-spaced-one um-elementor-member-image <?php echo esc_attr($align); ?>">
						<a title="<?php echo esc_attr($display_name); ?>" href="<?php echo esc_url($profile_url); ?>">
							<img src="<?php echo esc_url($avatar_url); ?>" alt="<?php echo esc_attr($display_name); ?>" width="100" height="100">
						</a>
					</div>
					<?php endif; ?>

					<div class="user-layout-spaced-two um-elementor-member-info <?php echo esc_attr($align); ?>">
						<?php if (($settings['um_elementor_show_member_name'] ?? 1) == 1): ?>
						<div class="um-elementor-member-name <?php echo esc_attr($align); ?>">
							<p>
								<a title="<?php echo esc_attr($display_name); ?>" href="<?php echo esc_url($profile_url); ?>">
									<?php echo esc_html($display_name); ?>
								</a>
							</p>
						</div>
						<?php endif; ?>
						<?php echo wp_kses_post($meta_renderer->render_meta_fields($settings)); ?>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Render filter interface
	 */
	private function render_filter_interface($settings, $widget_id) {
		$enable_aria = $settings['enable_aria_labels'] ?? 'yes';
		?>
		<div class="wp-filter-interface" role="region" aria-label="<?php esc_attr_e('User Filter Controls', 'ultimate-member-widgets-for-elementor'); ?>">
			<div class="wp-filter-header">
				<?php if (($settings['enable_search'] ?? 'no') === 'yes' && ($settings['search_position'] ?? 'top') === 'bottom'): ?>
					<div class="wp-global-search-container">
						<div class="wp-search-input-wrapper">
							<input type="text" 
								   class="wp-global-search" 
								   placeholder="<?php echo esc_attr($settings['search_placeholder'] ?? __('Search users...', 'ultimate-member-widgets-for-elementor')); ?>"
								   data-search-delay="500"
								   <?php if ($enable_aria === 'yes'): ?>
								   aria-label="<?php esc_attr_e('Search users by name, email, or other information', 'ultimate-member-widgets-for-elementor'); ?>"
								   role="searchbox"
								   <?php endif; ?>>
							<i class="fas fa-search wp-search-icon" aria-hidden="true"></i>
						</div>
					</div>
				<?php endif; ?>

				<button type="button" class="wp-filter-toggle-btn"
						<?php if ($enable_aria === 'yes'): ?>
						aria-label="<?php esc_attr_e('Toggle filter options', 'ultimate-member-widgets-for-elementor'); ?>"
						aria-expanded="false"
						<?php endif; ?>>
					<i class="fas fa-filter" aria-hidden="true"></i>
					<?php esc_html_e('Filters', 'ultimate-member-widgets-for-elementor'); ?>
					<i class="fas fa-chevron-down wp-filter-arrow" aria-hidden="true"></i>
				</button>
			</div>

			<div class="wp-filter-panel wp-filter-panel-hidden">
				<div class="wp-filter-controls" role="group" aria-label="<?php esc_attr_e('Filter Options', 'ultimate-member-widgets-for-elementor'); ?>">
					<?php 
					$filter_fields = $this->render_advanced_filters($settings);
					
					if (empty($filter_fields)): ?>
						<div class="wp-no-filters-message">
							<p><?php esc_html_e('No filter fields configured. Please add filter fields in the widget settings.', 'ultimate-member-widgets-for-elementor'); ?></p>
						</div>
					<?php endif; ?>
				</div>

				<?php if (!empty($filter_fields)): ?>
				<div class="wp-filter-actions">
					<button type="button" class="wp-apply-filters"
							<?php if ($enable_aria === 'yes'): ?>
							aria-label="<?php esc_attr_e('Apply selected filters to user list', 'ultimate-member-widgets-for-elementor'); ?>"
							<?php endif; ?>>
						<?php esc_html_e('Apply Filters', 'ultimate-member-widgets-for-elementor'); ?>
					</button>
					<button type="button" class="wp-clear-filters"
							<?php if ($enable_aria === 'yes'): ?>
							aria-label="<?php esc_attr_e('Clear all filters and show all users', 'ultimate-member-widgets-for-elementor'); ?>"
							<?php endif; ?>>
						<?php esc_html_e('Clear All', 'ultimate-member-widgets-for-elementor'); ?>
					</button>
				</div>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}

	/**
	 * Render advanced filters
	 */
	private function render_advanced_filters($settings) {
		$wp_user_filter_control = $settings['wp_user_filter_control'] ?? [];
		$wp_user_meta_control = $settings['um_user_meta_control'] ?? [];
		$rendered_count = 0;
		
		// Use filter control if available, otherwise fall back to user meta control
		$filter_fields = !empty($wp_user_filter_control) ? $wp_user_filter_control : $wp_user_meta_control;
		
		// Limit to 2 fields for free version
		$max_fields = 2;
		$filter_fields = array_slice($filter_fields, 0, $max_fields);
		
		foreach ($filter_fields as $index => $filter) {
			// Handle different field key names
			$meta_key = $filter['wp_user_filter_field_value'] ?? $filter['um_user_meta_field_value'] ?? '';
			$filter_type = $filter['filter_type'] ?? 'dropdown';
			
			if (empty($meta_key)) {
				continue;
			}
			
			$this->render_single_filter($filter, $index, $meta_key, $filter_type);
			$rendered_count++;
		}
		
		return $rendered_count;
	}

	/**
	 * Render single filter
	 */
	private function render_single_filter($filter, $index, $meta_key, $filter_type) {
		$field_label = $this->get_wp_field_label($meta_key);
		$meta_values = $this->get_wp_filter_meta_values($meta_key);
		
		if (empty($meta_values)) {
			return;
		}
		?>
		<div class="wp-filter-item" data-meta-key="<?php echo esc_attr($meta_key); ?>">
			<label class="wp-filter-label">
				<?php if (!empty($filter['wp_user_filter_field_icon']['value'])): ?>
					<i class="<?php echo esc_attr($filter['wp_user_filter_field_icon']['value']); ?>"></i>
				<?php endif; ?>
				<?php echo esc_html($field_label); ?>
			</label>
			
			<?php if ($filter_type === 'dropdown'): ?>
				<select class="wp-filter-control wp-filter-dropdown">
					<option value=""><?php esc_html_e('All', 'ultimate-member-widgets-for-elementor'); ?></option>
					<?php foreach ($meta_values as $value): ?>
						<option value="<?php echo esc_attr($value); ?>"><?php echo esc_html($value); ?></option>
					<?php endforeach; ?>
				</select>
			<?php elseif ($filter_type === 'checkbox'): ?>
				<div class="wp-filter-checkboxes">
					<?php foreach ($meta_values as $value): ?>
						<label class="wp-checkbox-item">
							<input type="checkbox" class="wp-filter-control wp-filter-checkbox" value="<?php echo esc_attr($value); ?>">
							<span><?php echo esc_html($value); ?></span>
						</label>
					<?php endforeach; ?>
				</div>
			<?php elseif ($filter_type === 'radio'): ?>
				<div class="wp-filter-radios">
					<label class="wp-radio-item">
						<input type="radio" class="wp-filter-control wp-filter-radio" name="filter_<?php echo esc_attr($meta_key); ?>" value="">
						<span><?php esc_html_e('All', 'ultimate-member-widgets-for-elementor'); ?></span>
					</label>
					<?php foreach ($meta_values as $value): ?>
						<label class="wp-radio-item">
							<input type="radio" class="wp-filter-control wp-filter-radio" name="filter_<?php echo esc_attr($meta_key); ?>" value="<?php echo esc_attr($value); ?>">
							<span><?php echo esc_html($value); ?></span>
						</label>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Get WordPress field label
	 */
	private function get_wp_field_label($meta_key) {
		$labels = [
			'description' => __('Description', 'ultimate-member-widgets-for-elementor'),
			'user_url' => __('Website', 'ultimate-member-widgets-for-elementor'),
			'first_name' => __('First Name', 'ultimate-member-widgets-for-elementor'),
			'last_name' => __('Last Name', 'ultimate-member-widgets-for-elementor'),
			'nickname' => __('Nickname', 'ultimate-member-widgets-for-elementor'),
			'display_name' => __('Display Name', 'ultimate-member-widgets-for-elementor'),
			'user_email' => __('Email', 'ultimate-member-widgets-for-elementor'),
			'user_login' => __('Username', 'ultimate-member-widgets-for-elementor'),
		];
		
		return $labels[$meta_key] ?? ucfirst(str_replace('_', ' ', $meta_key));
	}

	/**
	 * Get filter meta values
	 */
	private function get_wp_filter_meta_values($meta_key) {
		global $wpdb;
		
		$query = $wpdb->prepare(
			"SELECT meta_value FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value != ''",
			$meta_key
		);
		
		$all_values = $wpdb->get_col($query);
		$unique_options = [];
		
		foreach ($all_values as $value) {
			$unserialized = maybe_unserialize($value);
			
			if (is_array($unserialized)) {
				foreach ($unserialized as $sub_value) {
					$trimmed = trim($sub_value);
					if (!empty($trimmed)) {
						$unique_options[$trimmed] = 1;
					}
				}
			} else {
				$trimmed = trim($unserialized);
				if (!empty($trimmed)) {
					$unique_options[$trimmed] = 1;
				}
			}
		}
		
		$meta_values = array_keys($unique_options);
		sort($meta_values);
		
		return $meta_values;
	}

	/**
	 * Localize script data for advanced filters
	 */
	private function localize_script_data($settings) {
		$widget_id = $this->get_id();
		
		// Extract the meta field keys from the repeater to save them
		$meta_fields_to_save = [];
		if (!empty($settings['um_user_meta_control']) && is_array($settings['um_user_meta_control'])) {
		    foreach ($settings['um_user_meta_control'] as $item) {
		        if (!empty($item['um_user_meta_field_value'])) {
		            $meta_fields_to_save[] = $item['um_user_meta_field_value'];
		        }
		    }
		}

		$localized_data = [
			'ajax_url' => admin_url('admin-ajax.php'),
			'nonce' => wp_create_nonce('wp_filter_nonce'),
			'post_id' => (int) get_the_ID(),
			'enable_persistence' => $settings['enable_filter_persistence'] ?? 'yes',
			'enable_url_params' => $settings['enable_url_params'] ?? 'yes',
			'enable_keyboard_nav' => $settings['enable_keyboard_navigation'] ?? 'yes',
			'enable_aria_labels' => $settings['enable_aria_labels'] ?? 'yes',
			'widget_settings' => [
				'users_per_page' => (int) ($settings['users_per_page'] ?? 12),
				'enable_pagination' => $settings['enable_pagination'] ?? 'no',
				'pagination_style' => $settings['pagination_style'] ?? 'numbers',
                'user_meta_fields' => $meta_fields_to_save,
			]
		];
		
		echo '<script type="text/javascript">';
		echo 'window.wp_elementor_ajax_' . esc_js($widget_id) . ' = ' . wp_json_encode($localized_data) . ';';
		echo '</script>';
	}

	/**
	 * Render search interface
	 */
	private function render_search_interface($settings, $widget_id) {
		$placeholder = $settings['search_placeholder'] ?? __('Search users...', 'ultimate-member-widgets-for-elementor');
		?>
		<div class="wp-search-container" data-widget-id="<?php echo esc_attr($widget_id); ?>">
			<div class="wp-search-form">
				<input type="text" 
					   class="wp-search-input" 
					   placeholder="<?php echo esc_attr($placeholder); ?>">
				<button type="button" class="wp-search-button">
					<?php esc_html_e('Search', 'ultimate-member-widgets-for-elementor'); ?>
				</button>
			</div>
		</div>
		<?php
	}

	/**
	 * Render pagination
	 */
	private function render_pagination($settings, $widget_id, $current_page, $total_pages) {
		$pagination_style = $settings['pagination_style'] ?? 'numbers';
		
		echo '<div class="wp-pagination-container">';
		
		if ($pagination_style === 'load_more') {
			$this->render_load_more_button($settings, $widget_id, $current_page);
		} elseif ($pagination_style === 'infinite_scroll') {
			$this->render_infinite_scroll_trigger($settings, $widget_id, $current_page);
		} else {
			$this->render_numbered_pagination($settings, $widget_id, $current_page, $total_pages);
		}
		
		echo '</div>';
	}

	/**
	 * Render numbered pagination
	 */
	private function render_numbered_pagination($settings, $widget_id, $current_page, $total_pages) {
		?>
		<div class="wp-pagination" data-widget-id="<?php echo esc_attr($widget_id); ?>">
			<?php if ($total_pages > 1): ?>
				<?php if ($current_page > 1): ?>
					<a href="#" class="wp-pagination-link wp-pagination-prev" data-page="<?php echo esc_attr($current_page - 1); ?>">
						<?php echo esc_html__('Previous', 'ultimate-member-widgets-for-elementor'); ?>
					</a>
				<?php endif; ?>
				
				<?php
				$start_page = max(1, $current_page - 2);
				$end_page = min($total_pages, $current_page + 2);
				
				for ($i = $start_page; $i <= $end_page; $i++):
					$current_class = ($i === $current_page) ? 'current' : '';
					?>
					<a href="#" class="wp-pagination-link <?php echo esc_attr($current_class); ?>" data-page="<?php echo esc_attr($i); ?>">
						<?php echo esc_html($i); ?>
					</a>
				<?php endfor; ?>
				
				<?php if ($current_page < $total_pages): ?>
					<a href="#" class="wp-pagination-link wp-pagination-next" data-page="<?php echo esc_attr($current_page + 1); ?>">
						<?php echo esc_html__('Next', 'ultimate-member-widgets-for-elementor'); ?>
					</a>
				<?php endif; ?>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Render load more button
	 */
	private function render_load_more_button($settings, $widget_id, $current_page) {
		?>
		<div class="wp-load-more-container" data-widget-id="<?php echo esc_attr($widget_id); ?>">
			<button type="button" class="wp-load-more-button" data-page="<?php echo esc_attr($current_page); ?>">
				<?php echo esc_html__('Load More Users', 'ultimate-member-widgets-for-elementor'); ?>
			</button>
		</div>
		<?php
	}

	/**
	 * Render infinite scroll trigger
	 */
	private function render_infinite_scroll_trigger($settings, $widget_id, $current_page) {
		?>
		<div class="wp-infinite-scroll-trigger" data-widget-id="<?php echo esc_attr($widget_id); ?>" data-page="<?php echo esc_attr($current_page); ?>">
			<div class="wp-infinite-scroll-spinner" style="display: none;">
				<?php echo esc_html__('Loading...', 'ultimate-member-widgets-for-elementor'); ?>
			</div>
		</div>
		<?php
	}

	/**
	 * Get widget style dependencies.
	 */
	public function get_style_depends() {
		return ['um-elementor-styles', 'wp-user-list-styles'];
	}

	/**
	 * Get script dependencies
	 */
	public function get_script_depends() {
		return ['wp-user-list-js'];
	}

	/**
	 * Content template for Elementor editor
	 */
	protected function content_template() {}
} 
