<?php
/**
 * The template for Element Profiles List results.
 * This is the template that elementor element profiles results, list
 *
 */

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

?>

<div class="wdk-membership-element" id="wdk_el_<?php echo esc_html($id_element);?>">
    <div class="wdk-membership-profiles-list" id='results_profile'>
        <?php if(count($profiles_list)>0):?>
            <div class="wdk-row">
                <?php foreach($profiles_list as $profile_id => $value):?>
                <?php 
                    $profile_url = NULL;
                    $profile_avatar = NULL;
                    $count_listings = wmvc_show_data('listings_counter', $value, 0);

                    if(wmvc_show_data('agency_listings_counter', $value, 0)) {
                        $count_listings += intval(wmvc_show_data('agency_listings_counter', $value, 0));
                    }

                    $userdata = array();

                    /* try get from cache */
                    if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED  
                        && isset($value->cacheduser_display_name) && !empty($value->cacheduser_display_name)) {
                            $userdata = wdk_generated_cached_row_userdata($value);
                            $profile_url = wmvc_show_data('profile_url', $userdata);
                            $profile_avatar = wmvc_show_data('avatar_url', $userdata);
                    } else {
                        $wdk_userdata = wdk_get_user_data(wmvc_show_data('ID', $value));

                        $userdata = $wdk_userdata['userdata'];
                        $profile_url = wmvc_show_data('profile_url', $wdk_userdata);
                        $profile_avatar = wmvc_show_data('avatar', $wdk_userdata);
                    }
                ?>
                <div class="wdk-col-12">
                    <div class="profiles-item-list profile_id_<?php echo esc_attr(wmvc_show_data('ID', $value));?>">
                        <div class="wdk-thumbnail">
                            <img onerror="this.src = '<?php echo esc_url(wdk_placeholder_image_src());?>';" src="<?php echo esc_url($profile_avatar);?>" alt="<?php echo esc_attr(wmvc_show_data('display_name', $userdata));?>" class='wdk-image jsplaceholder'>
                            <a href="<?php echo esc_url( $profile_url);?>" class="thumbnail_link"></a>
                            <div class="wdk-hover">
                                <ul  class="wdk-list-social">
                                    <?php if( wmvc_show_data('wdk_facebook', $userdata, false)):?>
                                        <li><a href="<?php echo esc_url(wmvc_show_data('wdk_facebook', $userdata,'#'));?>"><i class="fab fa-facebook-f"></i></a></li>
                                    <?php endif;?>

                                    <?php if( wmvc_show_data('wdk_linkedin', $userdata, false)):?>
                                        <li><a href="<?php echo esc_url(wmvc_show_data('wdk_linkedin', $userdata,'#'));?>"><i class="fa fa-linkedin"></i></a></li>
                                    <?php endif;?>

                                    <?php if( wmvc_show_data('wdk_twitter', $userdata, false)):?>
                                        <li><a href="<?php echo esc_url(wmvc_show_data('wdk_twitter', $userdata,'#'));?>"><i class="fa fa-twitter"></i></a></li>
                                    <?php endif;?>

                                    <?php if( wmvc_show_data('wdk_telegram', $userdata, false)):?>
                                        <li><a href="<?php echo esc_url(wmvc_show_data('wdk_telegram', $userdata,'#'));?>"><i class="fa fa-telegram"></i></a></li>
                                    <?php endif;?>

                                    <?php if( wmvc_show_data('wdk_whatsapp', $userdata, false)):?>
                                        <li><a href="//wa.me/<?php echo esc_attr(wdk_filter_phone(wmvc_show_data('wdk_whatsapp', $userdata,'#')));?>"><i class="fa fa-whatsapp"></i></a></li>
                                    <?php endif;?>

                                    <?php if( wmvc_show_data('wdk_viber', $userdata, false)):?>
                                        <li><a href="viber://chat?number=<?php echo esc_attr(wdk_filter_phone(wmvc_show_data('wdk_viber', $userdata,'#')));?>"><i class="fab fa-viber"></i></a></li>
                                    <?php endif;?>
                                </ul>
                                <a href="<?php echo esc_url( $profile_url);?>" class="wdk-profile-btn" title="<?php echo esc_attr(wmvc_show_data('display_name', $userdata));?>"><?php echo esc_html__('View My Profile','classified-ads-directory');?></a>
                            </div>
                        </div>
                        <div class="wdk-content">
                            <div class="wdk-header">
                                <div class="wdk-side">
                                <h3 class="wdk-title"><a href="<?php echo esc_url( $profile_url);?>" title="<?php echo esc_attr(wmvc_show_data('display_name', $userdata));?>"><?php echo esc_html(wmvc_show_data('display_name', $userdata));?></a></h3>
                                <span class="wdk-subtitle">
                                    <?php if(wmvc_show_data('wdk_position_title', $userdata, false, TRUE, TRUE)):?>
                                            <?php echo esc_html(wmvc_show_data('wdk_position_title', $userdata, false, TRUE, TRUE));?>
                                    <?php else:?>
                                        <?php 
                                            if( !empty( wmvc_show_data('roles', $userdata, false)) ){
                                                foreach(wmvc_show_data('roles', $userdata) as $role ){
                                                    if(isset($wdk_wp_roles[$role])) {
                                                        echo esc_html($wdk_wp_roles[$role]);
                                                    } else {
                                                        echo esc_html($role);
                                                    }
                                                }
                                            }
                                        ?>
                                    <?php endif;?>
                                </div>
                                <div class="wdk-side text-right">
                                    <a href="<?php echo esc_url( $profile_url);?>#listings" class="wdk-profile-listings">
                                        <?php
                                            echo esc_html(sprintf(_nx(
                                                    '%1$s Listing',
                                                    '%1$s Listings',
                                                    $count_listings,
                                                    'profile listings count',
                                                    'classified-ads-directory'
                                            ), $count_listings));
                                        ?>
                                    </a>
                                </div>
                            </span>
                            </div>
                            
                            <div class="wdk-text <?php if(!empty( $settings['text_limit'])):?> unlimited <?php endif;?>">
                                <?php if(!empty( $settings['text_limit'])):?>
                                    <?php
                                        echo (wp_strip_all_tags(html_entity_decode(wp_trim_words(htmlentities(wmvc_show_data('description', $userdata)), $settings['text_limit'], '...'))));
                                    ?>
                                <?php else:?>
                                    <?php echo esc_html(wmvc_show_data('description', $userdata));?>
                                <?php endif;?>
                            </div>
                            <div class="wdk-footer">
                                <div class="wdk-side">
                                    <ul class="wdk-list">
                                        <?php
                                        $used_fields = array();
                                        if(!empty(wmvc_show_data('meta_fields_list', $settings)))
                                            foreach (wmvc_show_data('meta_fields_list', $settings) as $meta):?>
                                                <?php
                                                    if(isset($used_fields [$meta['meta_field']])) continue;
                                                    $used_fields [$meta['meta_field']] = true;
                                                ?>
                                                <?php
                                                    $value = wmvc_show_data($meta['meta_field'], $userdata);
                                                    if(empty($value)) continue;
                                                ?>
                                                <?php if(filter_var($value, FILTER_VALIDATE_EMAIL) !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Email Address','classified-ads-directory');?>:</span><a href="mailto:<?php echo esc_attr($value);?>"><i class="far fa-envelope"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'youtube') !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Youtube','classified-ads-directory');?>:</span><a href="<?php echo esc_url($value);?>"><i class="fa fa-youtube"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'facebook') !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Facebook','classified-ads-directory');?>:</span><a href="<?php echo esc_url($value);?>"><i class="fab fa-facebook-f"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'linkedin') !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Linkedin','classified-ads-directory');?>:</span><a href="<?php echo esc_url($value);?>"><i class="fa fa-linkedin"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'twitter') !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Twitter','classified-ads-directory');?>:</span><a href="<?php echo esc_url($value);?>"><i class="fa fa-twitter"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'telegram') !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Telegram','classified-ads-directory');?>:</span><a href="<?php echo esc_url($value);?>"><i class="fa fa-telegram"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'whatsapp') !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Whats App','classified-ads-directory');?>:</span><a href="//wa.me/<?php echo esc_attr(wdk_filter_phone($value));?>"><i class="fa fa-whatsapp"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'viber') !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Viber','classified-ads-directory');?>:</span><a href="viber://chat?number=<?php echo esc_attr(wdk_filter_phone($value));?>"><i class="fab fa-viber"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(filter_var($value, FILTER_VALIDATE_URL) !== FALSE):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Web Address','classified-ads-directory');?>:</span><a href="<?php echo esc_url($value);?>"><i class="fa fa-anchor"></i><?php echo esc_html($value);?></a></li>
                                                <?php elseif(strpos($meta['meta_field'],'phone') !== FALSE || wdk_is_phone($value)):?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html__('Phone Number','classified-ads-directory');?>:</span><a href="tel:<?php echo esc_attr(wdk_filter_phone($value));?>"><i class="far fa-phone"></i><?php echo esc_html($value);?></a></li>
                                                <?php else:?>
                                                    <li class="meta-item <?php echo esc_html($meta['meta_field']);?>"><span class="field-value"><?php echo esc_html($meta['meta_field']);?>:</span><?php echo esc_html($value);?></li>
                                                <?php endif;?>
                                        <?php endforeach;?>
                                    </ul>

                                    <ul  class="wdk-list-social">
                                        <?php if( wmvc_show_data('wdk_facebook', $userdata, false)):?>
                                            <li><a href="<?php echo esc_url(wmvc_show_data('wdk_facebook', $userdata,'#'));?>"><i class="fab fa-facebook-f"></i></a></li>
                                        <?php endif;?>

                                        <?php if( wmvc_show_data('wdk_linkedin', $userdata, false)):?>
                                            <li><a href="<?php echo esc_url(wmvc_show_data('wdk_linkedin', $userdata,'#'));?>"><i class="fa fa-linkedin"></i></a></li>
                                        <?php endif;?>

                                        <?php if( wmvc_show_data('wdk_twitter', $userdata, false)):?>
                                            <li><a href="<?php echo esc_url(wmvc_show_data('wdk_twitter', $userdata,'#'));?>"><i class="fa fa-twitter"></i></a></li>
                                        <?php endif;?>

                                        <?php if( wmvc_show_data('wdk_facebook', $userdata, false)):?>
                                            <li><a href="<?php echo esc_url(wmvc_show_data('wdk_facebook', $userdata,'#'));?>"><i class="fa fa-telegram"></i></a></li>
                                        <?php endif;?>

                                        <?php if( wmvc_show_data('wdk_whatsapp', $userdata, false)):?>
                                            <li><a href="//wa.me/<?php echo esc_attr(wdk_filter_phone(wmvc_show_data('wdk_viber', $userdata,'#')));?>"><i class="fa fa-whatsapp"></i></a></li>
                                        <?php endif;?>

                                        <?php if( wmvc_show_data('wdk_viber', $userdata, false)):?>
                                            <li><a href="viber://chat?number=<?php echo esc_attr(wdk_filter_phone(wmvc_show_data('wdk_viber', $userdata,'#')));?>"><i class="fab fa-viber"></i></a></li>
                                        <?php endif;?>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endforeach;?>
            </div>
        <?php else:?>
            <p class="wdk_alert wdk_alert-danger"><?php echo esc_html__('Profiles not found', 'classified-ads-directory');?></p>
        <?php endif;?>
        <?php echo wmvc_xss_clean($pagination_output); ?>
    </div>
</div>

