<?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;
use \Elementor\Control_Media;

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

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

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

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('Services Box Home-5', '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()
    {

        $this->start_controls_section(
            'tg_servicesBox_Home5_group',
            [
                'label' => esc_html__('Service Section Heading', 'genixcore'),
            ]
        );

        $this->add_control(
            'serviceh5_heading',
            [
                'label' => esc_html__('Service Heading', 'genixcore'),
                'type' => Controls_Manager::TEXT,
                'label_block' => true,

            ]
        );
        $this->add_control(
            'serviceh5_stitle',
            [
                'label' => esc_html__('Service Short Description', 'genixcore'),
                'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,

            ]
        );
        $this->end_controls_section();




        $this->start_controls_section(
            'tg_servicesBox_grouph5',
            [
                'label' => esc_html__('Services Group', 'genixcore'),
            ]
        );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'select_posth5',
            [
                'label' => __('Select a Post', 'genixcore'),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'label_block' => true,
                'default' => 'none',
                'options' => $this->get_all_services(),
            ]
        );

        $repeater->add_control(
            'tg_imageh5',
            [
                'label' => esc_html__('Upload Icon', 'genixcore'),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ],
            ]
        );

        $repeater->add_control(
            'descriptionh5',
            [
                'label' => esc_html__('Short Description', 'genixcore'),
                'description' => esc_html__('This Field for short Description', 'genixcore'),
                'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,
            ]
        );

        $this->add_control(
            'tg_post_listh5',
            [
                'label' => esc_html__('All Post List', 'genixcore'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
            ]
        );

        $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();




    }


    // Get All Services
    public function get_all_services()
    {

        $wp_query = get_posts([
            'post_type' => 'services',
            'orderby' => 'date',
            'posts_per_page' => -1,
        ]);

        $options = ['none' => 'None'];
        foreach ($wp_query as $services) {
            $options[$services->ID] = $services->post_name;
        }

        return $options;
    }





  






    /**
     * 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>

        <section class="service padding-top padding-bottom bg-color-7">
            <div class="section-header section-header--max50">
                <h2 class="mb-10 mt-minus-5"><?php if(!empty($settings['serviceh5_heading'])): echo esc_html($settings['serviceh5_heading']); endif; ?></h2>
                <p>
                    <?php if(!empty($settings['serviceh5_stitle'])): echo wp_kses_post($settings['serviceh5_stitle']); endif; ?>
                </p>
            </div>
            <div class="container">
                <div class="service__wrapper">
                    <div class="row g-4 align-items-center">
                    <?php
                    $i= 8; foreach ($settings['tg_post_listh5'] as $items) : 

                    $args = new \WP_Query(array(
                        'post_type' => 'services',
                        'post_status' => 'publish',
                        'post__in' => [
                            $items['select_posth5']
                        ]
                    ));

                    /* Start the Loop */
                    while ($args->have_posts()) : $args->the_post();
                    ?>
                    <div class="col-sm-6 col-md-6 col-lg-4">
                        <div class="service__item service__item--style3" data-aos="fade-up" data-aos-duration="<?php echo $i++; ?>00">
                            <div class="service__item-inner text-center">
                                <?php
                                 if (!empty($items['tg_imageh5']['url'])) : 
                                ?>
                                <div class="service__item-thumb  mb-30">
                                  <img src="<?php echo esc_url($items['tg_imageh5']['url']) ?>" width="48" alt="<?php echo esc_attr__('Icon', 'genixcore') ?>">
                                </div>
                                <?php
                                 endif;
                                ?>
                                <div class="service__item-content">
                                    <h5 > 
                                        <a class="stretched-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h5>
                                    <?php if (!empty($items['descriptionh5'])) : ?>
                                        <p class="mb-0"><?php echo genix_kses($items['descriptionh5']); ?></p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php
                    endwhile;
                    wp_reset_postdata(); 
                    endforeach; 
                    ?>
                    </div>
                </div>
            </div>
        </section>

<?php
    }
}

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