<?php
use DWL\Wtm\Classes\Helper;

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

if ( ! empty( $data ) ) :

    $image_size = isset( $settings['image_size'] ) ? sanitize_text_field( $settings['image_size'] ) : 'thumbnail';
    $show_short_bio = isset( $settings['team_show_short_bio'] ) ? sanitize_text_field( $settings['team_show_short_bio'] ) : '';
    $team_read_more = !empty( $settings['read_more_text'] ) ? sanitize_text_field( $settings['read_more_text'] ) : 'Read More';
    $team_arrow_position = isset( $settings['team_arrow_position'] ) && ( $settings['team_arrow_position'] === 'side' ) ? 'team-arrow-postion-side' : '';

    // Pro feature: Popup functionality
    $show_popup = Helper::is_pro_feature_enabled( $settings, 'popup_bar_show' );
    $popup_settings = $show_popup ? "true" : 'false';

    // Pro feature: Disable single member
    $disable_single_member = Helper::is_pro_feature_enabled( $settings, 'disable_single_member' );


    $allowed_tags = array_merge(
        wp_kses_allowed_html( 'post' ), // All default post tags
        array(
            'progress' => array(
                'value' => true,
                'max'   => true,
                'style' => true,
            ),
        )
    );

    ?>
    <div class="team-member-slider-wrap <?php echo esc_attr( $team_arrow_position ); ?>" data-slider_settings="<?php echo esc_attr( json_encode( $settings['slider_settings'] ) ); ?>">
    <?php
        foreach ( $data as $team ) :

        $meta = get_post_meta( $team->ID );
        $job_title = isset($meta['tm_jtitle'][0]) ? sanitize_text_field($meta['tm_jtitle'][0]) : '';
        $short_bio = $meta['tm_short_bio'][0] ?? '';

    ?>
        <div <?php post_class( 'team-member-info-wrap' ); ?>>
            <div class="team-member-info-content">
                <header>
                    <?php if ( 'yes' === $settings['show_image'] ) : ?>
                        <?php if ( !$disable_single_member ) : ?>
                            <a href="<?php echo esc_url( get_the_permalink( $team->ID ) ); ?>">
                        <?php endif; ?>
                        <?php if($show_popup): ?>
                            <div class="team-popup" data-popup="<?php echo esc_attr($popup_settings); ?>" data-id="<?php echo esc_attr($team->ID); ?>">
                        <?php endif; ?>
                            <?php echo wp_kses_post( Helper::get_team_picture( $team->ID, $image_size, 'dwl-box-shadow' ) ); ?>
                        <?php if($show_popup): ?>
                            </div>
                        <?php endif; ?>
                        <?php if ( !$disable_single_member ) : ?>
                            </a>
                        <?php endif; ?>

                    <?php endif; ?>
                </header>
                <div class="team-member-desc">
                    <?php if ( 'yes' === $settings['show_title'] ) : ?>
                        <?php if (!$disable_single_member ) : ?>
                            <a href="<?php echo esc_url( get_the_permalink( $team->ID ) ); ?>">
                        <?php endif; ?>
                        <?php if($show_popup): ?>
                            <div class="team-popup" data-popup="<?php echo esc_attr($popup_settings); ?>" data-id="<?php echo esc_attr($team->ID); ?>">
                        <?php endif; ?>
                                <h2 class="team-member-title"><?php echo esc_html( get_the_title( $team->ID ) ); ?></h2>
                        <?php if($show_popup): ?>
                            </div>
                        <?php endif; ?>
                        <?php if (!$disable_single_member ) : ?>
                            </a>
                        <?php endif; ?>
                    <?php endif; ?>

                    <?php if ( ! empty( $job_title ) && 'yes' === $settings['show_sub_title'] ) : ?>
                        <p class="team-position"><?php echo esc_html( $job_title ); ?></p>
                    <?php endif; ?>

                    <?php if ( 'yes' === $show_short_bio ) : ?>
                        <div class="team-short-bio">
                            <?php if ( ! empty( $short_bio ) && 'yes' === $settings['team_show_short_bio'] ) : ?>
                                <?php echo apply_filters('wtm_team_short_bio_output', wp_strip_all_tags($short_bio), $short_bio, $teamInfo->ID); ?>
                            <?php else : ?>
                                <?php 
                                $post_content = !empty($team->post_excerpt) 
                                    ? $team->post_excerpt 
                                    : wp_trim_words(strip_tags($team->post_content), 40, '...');

                                echo esc_html($post_content);
                                ?>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>

                       <?php if(!empty($settings['show_other_info']) && $settings['show_other_info'] === 'yes'): ?>
                        <?php
                            // Pro feature: Other info links
                            $enable_links = Helper::is_pro_feature_enabled( $settings, 'other_info_link' ) ? 'yes' : 'no';
                            echo wp_kses_post(
                                Helper::get_team_other_infos($team->ID, $settings['other_info_elements'], $enable_links)
                            );
                        ?>
                    <?php endif; ?>


                    <?php if (tmwstm_fs()->is_paying_or_trial()): ?>
                        <?php if (isset($settings['progress_bar_show']) && 'yes' === $settings['progress_bar_show']): ?>
                            <div class="wtm-progress-bar">
                            <?php
                                if (class_exists('DWL_Wtm_Pro_Helper')) {

                                    echo wp_kses(DWL_Wtm_Pro_Helper::display_skills_output($team->ID), $allowed_tags);

                                } ?>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                    <?php if ( isset( $settings['show_social'] ) && 'yes' === $settings['show_social'] ) : ?>
                        <?php echo wp_kses_post( Helper::display_social_profile_output( $team->ID ) ); ?>
                    <?php endif; ?>
                    <?php if (isset($settings['show_read_more']) && 'yes' === $settings['show_read_more'] && 'yes' === $settings['disable_single_member']) : ?>
                        <div class="wtm-read-more-wrap">
                            <a href="<?php echo esc_url( get_the_permalink( $team->ID ) ); ?>" class="wtm-read-more">
                                <?php echo esc_html( $team_read_more ); ?>
                            </a>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <?php endforeach; ?>
        </div>
        <?php endif; ?>
