<?php

namespace GenixCore\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Repeater;
use \Elementor\Utils;

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

/**
 * Bitrader Core
 *
 * Elementor widget for hello world.
 *
 * @since 1.0.0
 */
class TG_Team extends Widget_Base
{

    /**
     * Retrieve the widget name.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name()
    {
        return 'tg-team';
    }

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('Team', 'genixcore');
    }

    /**
     * Retrieve the widget icon.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon()
    {
        return 'genix-icon';
    }

    /**
     * Retrieve the list of categories the widget belongs to.
     *
     * Used to determine where to display the widget in the editor.
     *
     * Note that currently Elementor supports only one category.
     * When multiple categories passed, Elementor uses the first one.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget categories.
     */
    public function get_categories()
    {
        return ['genixcore'];
    }

    /**
     * Retrieve the list of scripts the widget depended on.
     *
     * Used to set scripts dependencies required to run the widget.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget scripts dependencies.
     */
    public function get_script_depends()
    {
        return ['genixcore'];
    }

    /**
     * Register the widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function register_controls()
    {

        // layout Panel
        $this->start_controls_section(
            'genix_layout',
            [
                'label' => esc_html__('Design Layout', 'genixcore'),
            ]
        );

        $this->add_control(
            'genix_design_style',
            [
                'label' => esc_html__('Select Layout', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'layout-1' => esc_html__('Layout 1', 'genixcore'),
                    'layout-2' => esc_html__('Layout 2', 'genixcore'),
                ],
                'default' => 'layout-1',
            ]
        );

        $this->end_controls_section();


        // member list
        $this->start_controls_section(
            '_section_teams',
            [
                'label' => esc_html__('Members', 'genixcore'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'team_col_style',
            [
                'label' => esc_html__('Select Column', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'col-sm-6' => esc_html__('2 Column', 'genixcore'),
                    'col-sm-6 col-lg-4' => esc_html__('3 Column', 'genixcore'),
                    'col-sm-6 col-lg-3' => esc_html__('4 Column', 'genixcore'),
                ],
                'default' => 'col-sm-6 col-lg-3',
                'condition' => [
                    'genix_design_style' => 'layout-1',
                ]
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_section();


        $this->start_controls_section(
            'tg_teamsBox_group',
            [
                'label' => esc_html__('Post Limit', 'genixcore'),
            ]
        );
        $this->add_control(
            'select_post',
            [
                'label' => __('Post Count', 'genixcore'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'label_block' => true,
                'default' => 8,

            ]
        );
        $this->end_controls_section();



        // STYLE TAB
        $this->start_controls_section(
            'section_style',
            [
                'label' => esc_html__('Style', 'genixcore'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'text_transform',
            [
                'label' => esc_html__('Text Transform', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'default' => '',
                'options' => [
                    '' => esc_html__('None', 'genixcore'),
                    'uppercase' => esc_html__('UPPERCASE', 'genixcore'),
                    'lowercase' => esc_html__('lowercase', 'genixcore'),
                    'capitalize' => esc_html__('Capitalize', 'genixcore'),
                ],
                'selectors' => [
                    '{{WRAPPER}} .title' => 'text-transform: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();



    }




    /**
     * Render the widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display(); ?>

        <script>
            jQuery(document).ready(function($) {

                AOS.init();

            });
        </script>

        <!-- style 2 -->
        <?php if ($settings['genix_design_style'] === 'layout-2') : ?>

        <div class="team__wrapper">
            <div class="row g-4 align-items-center">
                <?php
                $i= 8;
                $teams1 = new \WP_Query(array(
                    'post_type'  => 'teams',
                    'posts_per_page'  => $settings['select_post'],
                    'post_status'     => 'publish',
                    'order'     => 'ASC',

                ));

                if ($teams1->have_posts()):
                    while($teams1->have_posts()): 
                    $teams1->the_post(); 
                ?>
                <div class="col-sm-6 col-lg-4 col-xl-3">
                    <div class="team__item team__item--shape" data-aos="fade-up" data-aos-duration="<?php echo $i++; ?>00">
                        <div class="team__item-inner team__item-inner--shape">
                            <?php if ( has_post_thumbnail() ) { ?>
                            <div class="team__item-thumb team__item-thumb--style2">
                                <?php the_post_thumbnail(); ?>
                            </div>
                            <?php } ?>
                            <div class="team__item-content ">
                                <div class="team__item-author team__item-author--style2">
                                    <div class="team__item-authorinfo">
                                        <h6 class="mb-0">                      
                                            <a class="stretched-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                        </h6>
                                        <?php if(get_field('team_member_designation')): ?>
                                            <p class="mb-0">
                                                <?php echo get_field('team_member_designation'); ?>    
                                            </p>
                                        <?php endif; ?>
                                    </div>
                                    <div class="team__item-authorsocial">
                                        <ul class="social justify-content-center">
                                            <?php if(get_field('facebook_url')): ?>
                                                <li class="social__item">
                                                    <a href="<?php echo esc_url(get_field('facebook_url')); ?>" class="social__link social__link--style3"><i class="fab fa-facebook-f"></i></a>
                                                </li>
                                            <?php endif; ?>

                                            <?php if(get_field('twitter_url')): ?>
                                                <li class="social__item">
                                                    <a href="<?php echo esc_url(get_field('twitter_url')); ?>" class="social__link social__link--style3"><i class="fab fa-twitter"></i></a>
                                                </li>
                                            <?php endif; ?>

                                            <?php if(get_field('instagram_url')): ?>
                                                <li class="social__item">
                                                    <a href="<?php echo esc_url(get_field('instagram_url')); ?>" class="social__link social__link--style3"><i class="fab fa-instagram"></i></a>
                                                </li>
                                            <?php endif; ?>

                                            <?php if(get_field('linkedin_url')): ?>
                                                <li class="social__item">
                                                    <a href="<?php echo esc_url(get_field('linkedin_url')); ?>" class="social__link social__link--style3"><i class="fab fa-linkedin-in"></i></a>
                                                </li>
                                            <?php endif; ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php
                endwhile;
                wp_reset_query(); 
                endif;
                ?>
            </div>
        </div>
        <!-- style default -->
        <?php else : ?>

            <div class="team__wrapper">
                <div class="row g-4 align-items-center">
                    <?php
                    $i= 8;
                    $teamss = new \WP_Query(array(
                        'post_type'  => 'teams',
                        'posts_per_page'  => $settings['select_post'],
                        'post_status'     => 'publish',
                        'order'     => 'ASC',

                    ));

                    if ($teamss->have_posts()):
                        while($teamss->have_posts()): 
                        $teamss->the_post(); 
                    ?>
                    <div class="<?php echo esc_attr( $settings['team_col_style'] ); ?>">
                        <div class="team__item team__item--shape" data-aos="fade-up" data-aos-duration="<?php echo $i++; ?>00">
                            <div class="team__item-inner team__item-inner--shape">
                                <?php if ( has_post_thumbnail() ) { ?>
                                    <div class="team__item-thumb team__item-thumb--style1">
                                        <?php the_post_thumbnail(); ?>
                                    </div>
                                <?php } ?>
                                <div class="team__item-content team__item-content--style1">
                                    <div class="team__item-author team__item-author--style1">
                                        <div class="team__item-authorinfo">
                                            <h6 class="mb-1">
                                                <a class="stretched-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                            </h6>
                                            <?php if(get_field('team_member_designation')): ?>
                                            <p class="mb-0">
                                                <?php echo get_field('team_member_designation'); ?>    
                                            </p>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php
                    endwhile;
                    wp_reset_query(); 
                    endif;
                    ?>
                </div>
            </div>
        <?php endif; ?>

<?php
    }
}

$widgets_manager->register(new TG_Team());
