<?php
/**
 * Team_Member View file
 *
 * @category   View
 * @package    CodexShaper_Framework
 * @author     CodexShaper <info@codexshaper.com>
 * @license    https://www.gnu.org/licenses/gpl-2.0.html
 * @link       https://codexshaper.com
 * @since      1.0.0
 */

use Elementor\Icons_Manager;
use Elementor\Utils;

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

$image_size   = $settings['profile_image_size'] ?? 'full';
$is_lazy_load = 'yes' === $settings['lazy_load'];
$image_id     = $settings['profile_image']['id'] ?? null;

$prfile_image = $parent->get_size_image(
	image_id: $image_id,
	size: $image_size,
	is_custom_lazy: $is_lazy_load,
	attributes: array(
		'alt'          => 'Profile Image',
		'class'        => 'csmf--thumb elementor-animation-' . $settings['hover_animation'],
		'fallback_url' => 0 < $image_id ? '' : $settings['profile_image']['url'],
	)
);

if ( ! empty( $settings['Social_icon'] ) ) {
	foreach ( $settings['Social_icon'] as $icon ) {
		if ( ! empty( $icon['icon'] ) ) {
			$social_urls[]     = $icon['social_link']['url'];
			$social_icon_width = ! empty( $icon['social_icon_width']['size'] ) ? $icon['social_icon_width']['size'] : '16';
			$social_icons[]    = Icons_Manager::try_get_icon_html(
				$icon['icon'],
				array(
					'aria-hidden' => 'true',
					'fill'        => 'currentColor',
					'width'       => $social_icon_width,
				)
			);
		}
		$item_id[] = $icon['_id'];
	}
}
$parent->add_render_attribute( 'csmf--name', 'class', 'csmf--name' );
$name_html = sprintf(
	'<%1$s %2$s>%3$s</%1$s>',
	Utils::validate_html_tag( $settings['name_header_tag'] ),
	$parent->get_render_attribute_string( 'csmf--name' ),
	$name  = $settings['member_name']
);
$parent->add_render_attribute( 'csmf--designation', 'class', 'csmf--designation' );
$designation_html = sprintf(
	'<%1$s %2$s>%3$s</%1$s>',
	Utils::validate_html_tag( $settings['designation_header_tag'] ),
	$parent->get_render_attribute_string( 'csmf--designation' ),
	$designation  = $settings['designation']
);

$image_link = $settings['is_profile_image_link'] && ! empty( $settings['profile_image_link']['url'] ) ? $settings['profile_image_link']['url'] : '';
$name_link  = $settings['is_member_name_link'] && ! empty( $settings['profile_image_link']['url'] ) ? $settings['name_link']['url'] : '';

?>
<div class="csmf--team-item">
	<?php if ( ! empty( $prfile_image ) ) : ?>
		<div class="csmf--thumb-wrap">
			<?php if ( 'yes' === $settings['is_profile_image_link'] ) : ?>
				<a class="csmf--thumb-link" href="<?php echo esc_url( $image_link ); ?>">
				<?php endif ?>
				<?php echo wp_kses_post( $prfile_image ); ?>
				<?php if ( 'yes' === $settings['is_profile_image_link'] ) : ?>
				</a>
			<?php endif ?>
		</div>
	<?php endif; ?>
	<?php if ( ! empty( $social_icons && 'before' === $settings['social_icons_position'] ) ) : ?>
		<ul class="csmf--social-items">
			<?php foreach ( $social_icons as $key => $social_icon ) : ?>
				<li class="csmf--social-item <?php echo 'elementor-repeater-item-' . esc_html( $item_id[ $key ] ); ?>">
					<a aria-label="Social Link Button" class="csmf--social-link" href="<?php echo esc_url( $social_urls[ $key ] ); ?>" target="_blank">
						<?php echo wp_kses( $social_icon, csmf_get_svg_rules() ); ?>
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
	<?php endif ?>
	<?php if ( ! empty( $settings['member_name'] ) ) : ?>
		<div class="csmf--details">
			<?php if ( 'yes' === $settings['is_member_name_link'] ) : ?>
				<a class="csmf--name" href="<?php echo esc_url( $name_link ); ?>">
				<?php endif ?>
				<?php
				Utils::print_unescaped_internal_string( $name_html );
				?>
				<?php if ( 'yes' === $settings['is_member_name_link'] ) : ?>
				</a>
			<?php endif ?>
			<?php
			Utils::print_unescaped_internal_string( $designation_html );
			?>
		</div>
	<?php endif; ?>
	<?php if ( ! empty( $social_icons && 'before_description' === $settings['social_icons_position'] ) ) : ?>
		<ul class="csmf--social-items">
			<?php foreach ( $social_icons as $key => $social_icon ) : ?>
				<li class="csmf--social-item <?php echo 'elementor-repeater-item-' . esc_html( $item_id[ $key ] ); ?>">
					<a aria-label="Social Link Button" class="csmf--social-link" href="<?php echo esc_url( $social_urls[ $key ] ); ?>" target="_blank">
						<?php echo wp_kses( $social_icon, csmf_get_svg_rules() ); ?>
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
	<?php endif ?>
	<?php
	if ( ! empty( $settings['description'] ) ) :
		?>
		<div class="csmf--description">
			<p><?php echo esc_html( $settings['description'] ); ?></p>
		</div>
		<?php
	endif;
	if ( ! empty( $social_icons && 'after' === $settings['social_icons_position'] ) ) :
		?>
		<ul class="csmf--social-items">
			<?php foreach ( $social_icons as $key => $social_icon ) : ?>
				<li class="csmf--social-item <?php echo 'elementor-repeater-item-' . esc_html( $item_id[ $key ] ); ?>">
					<a aria-label="Social Link Button" class="csmf--social-link" href="<?php echo esc_url( $social_urls[ $key ] ); ?>" target="_blank">
						<?php echo wp_kses( $social_icon, csmf_get_svg_rules() ); ?>
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
	<?php endif ?>
</div>
