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

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

// Retrieve settings with default values
    $image_size = $settings['dwl_team_select_image_size'][0] ?? 'thumbnail';
    $show_other_info = !empty($settings['dwl_team_team_show_other_info'][0]);
    $show_social = !empty($settings['dwl_team_team_show_social'][0]);
    $show_read_more = empty($settings['dwl_team_team_show_read_more'][0]); 
    $show_progress_bar = !empty($settings['dwl_team_show_progress_bar'][0]);
    $hide_short_bio_control = !empty($settings['dwl_team_hide_short_bio'][0]);
    $hide_team_show_position = !empty($settings['dwl_team_team_show_position'][0]);

// Pro feature: Disable single team member view (global setting)
$disable_single_template = Helper::is_pro_option_enabled( 'single_team_member_view' );

// Get team member fields with a default value
$tm_single_fields = get_option('tm_single_fields', ['tm_jtitle']);
$tm_single_fields = is_array($tm_single_fields) ? $tm_single_fields : ['tm_jtitle'];
$context = isset($settings['context']) ? $settings['context'] : '';

// Ensure data['posts'] exists
    ?>

        <div class="dwl-team-elementor-layout-table">
            <div class="dwl-team-table-responsive team-table-style-1">
                <table class="table">
                  <?php if($context !== 'ajax'): ?> 
                <thead>
                        <tr>
                            <th scope="col"><?php esc_html_e( "Image", "wp-team-manager" )?></th>

                            <th scope="col"><?php esc_html_e( "Name", "wp-team-manager" )?></th>

                            <th scope="col"><?php esc_html_e( "Designation", "wp-team-manager" )?></th>

                            <th scope="col"><?php esc_html_e( "Short Bio", "wp-team-manager" )?></th>

                            <th scope="col"><?php esc_html_e( "EMAIL", "wp-team-manager" )?></th>
                        </tr>
                    </thead>
                <?php endif;?>

                    <tbody>
                        <?php
                        
                            foreach ($data['posts'] as $key => $teamInfo):

                                $meta = get_post_custom($teamInfo->ID);
                                
                                $job_title = !empty($meta['tm_jtitle'][0]) ? sanitize_text_field($meta['tm_jtitle'][0]) : '';
                                $short_bio = $meta['tm_short_bio'][0] ?? '';
                                $tm_email = !empty($meta['tm_email'][0]) ? sanitize_email($meta['tm_email'][0]) : '';
                                
                                ?>
                                
                                    <tr class="dwl-table-row" scope="row">
                                        
                                            <td class="dwl-table-data">
                                                <div class="dwl-table-img-icon-wraper">
                                                        <div class="dwl-table-img-wraper">
                                                        <?php if (!$disable_single_template): ?>
                                                            <a href="<?php echo esc_url( get_the_permalink($teamInfo->ID) ); ?>">
                                                        <?php endif;?>
                                                                <?php echo wp_kses_post( Helper::get_team_picture( $teamInfo->ID, $image_size, 'dwl-box-shadow' ) ); ?>
                                                        <?php if (!$disable_single_template): ?>
                                                            </a>
                                                        <?php endif;?>
                                                            </a>
                                                        </div>
                                                        <?php if (!$show_social): ?>
                                                            <?php echo wp_kses_post( Helper::display_social_profile_output($teamInfo->ID) ); ?>
                                                        <?php endif; ?>
                                                </div>
                                                
                                            </td>

                                            <td class="dwl-table-data">
                                                <div class="team-member-head">
                                                    <?php if (!$disable_single_template): ?>
                                                        <a href="<?php echo esc_url( get_the_permalink($teamInfo->ID) ); ?>">
                                                    <?php endif;?>
                                                        <h2 class="team-member-title"><?php echo esc_html( get_the_title($teamInfo->ID) ); ?></h2>
                                                    <?php if (!$disable_single_template): ?>
                                                        </a>
                                                    <?php endif;?>
                                                </div>
                                            </td>

                                            <td class="dwl-table-data">
                                                <div class="team-position-wraper">
                                                    <?php if (!$hide_team_show_position): ?>
                                                        <p class="team-position"><?php echo esc_html( $job_title ); ?></p>
                                                    <?php endif; ?>
                                                </div>
                                            </td>

                                            <td class="dwl-table-data-short-bio">
                                                <?php if (!$hide_short_bio_control): ?>
                                                    <div class="team-short-bio">

                                                          <?php 
                                                 // Trim to 20 words, default is escaped output
                                                    $trimmed_bio = wp_trim_words( $short_bio, 20, '...' );
                                                    echo apply_filters('wtm_team_short_bio_output', esc_html($trimmed_bio), $short_bio, $teamInfo->ID); ?>

                                                            <?php 
                                                            $post_content = !empty($teamInfo->post_excerpt) 
                                                                ? $teamInfo->post_excerpt 
                                                                : wp_trim_words(strip_tags($teamInfo->post_content), 20, '...');

                                                            echo esc_html($post_content);
                                                            ?>

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

                                            <td class="dwl-table-data">
                                                <div class="team-member-info">
                                                    <?php if (!$show_other_info): ?>
                                                        <a href="mailto:<?php echo esc_attr($tm_email) ?>" target="_blank">
                                                            <i class="fas fa-envelope"></i>
                                                            <?php echo esc_html($tm_email) ?>
                                                        </a>
                                                    <?php endif; ?>
                                                </div>
                                            </td>
                                    </tr>
                                <?php
                        ?>
                            <?php endforeach; ?>
                    </tbody>    
                </table>
            </div>
        </div>


    <?php

?>