<?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_EL extends Widget_Base
{

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

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('Services Box EL', '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' => [
                    'service__item--' => esc_html__('Layout 1', 'genixcore'),
                ],
                'default' => 'service__item--',
            ]
        );

        $this->end_controls_section();
    // _tg_list
        $this->start_controls_section(
            '_tg_icon_list',
            [
                'label' => esc_html__('Section Title', 'genixcore'),
            ]
        );

        $this->add_control(
            'main_section_title',
            [
                'label' => esc_html__('Title', 'genixcore'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Easy to lend', 'genixcore'),
                'placeholder' => esc_html__('Type list text', 'genixcore'),
                'label_block' => true,
            ]
        );

        $this->add_control(
            'main_section_desc',
            [
                'label' => esc_html__('Description', 'genixcore'),
                'type' => Controls_Manager::TEXTAREA,
                'default' => esc_html__('It\'s easy to lend a helping hand to those in need, especially when we all stick together.', 'genixcore'),
                'placeholder' => esc_html__('Type list desc', 'genixcore'),
                'label_block' => true,
            ]
        );
        $this->end_controls_section();

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

        $repeater = new \Elementor\Repeater();
 
      $repeater->add_control(
            'services_title',
            [
                'label' => esc_html__('Service Title', 'genixcore'),
                'type' => Controls_Manager::TEXT,
                'default' => 'services title',
                   'label_block' => true,
                'dynamic' => [
                    'active' => true
                ]
            ]
        );
        $repeater->add_control(
            'services_title_url',
            [
                'label' => esc_html__( 'Title Url', 'text-domain' ),
                'type' => Controls_Manager::URL,
                'options' => [ 'url', 'is_external', 'nofollow' ],
                'default' => [
                    'url' => '',
                    'is_external' => true,
                    'nofollow' => true,
                ],
                'label_block' => true,
            ]
        );
        $repeater->add_control(
            'tg_image',
            [
                'label' => esc_html__('Upload Icon', 'genixcore'),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ],
            ]
        );

        $repeater->add_control(
            'description',
            [
                '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_list',
            [
                'label' => esc_html__('Item Lists', 'genixcore'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [
                        'services_title' => esc_html__('Easy to lend', 'genixcore'),
                    ],
                    [
                        'services_title' => esc_html__('Exchange money', 'genixcore'),
                    ],
                    [
                        'services_title' => esc_html__('Online wallet', 'genixcore'),
                    ],
                ],
            ]
        );
        $this->end_controls_section();
                    // Section Heading Controls
            $this->start_controls_section(
                'section_heading',
                [
                    'label' => esc_html__('Section Heading', 'genixcore'),
                ]
            );

            $this->add_control(
                'section_title',
                [
                    'label' => esc_html__('Section Title', 'genixcore'),
                    'type' => Controls_Manager::TEXT,
                    'default' => 'Services We Offer',
                ]
            );

            $this->add_control(
                'section_desc',
                [
                    'label' => esc_html__('Section Description', 'genixcore'),
                    'type' => Controls_Manager::TEXTAREA,
                    'default' => 'We offer the best services around - from installations to repairs, maintenance, and more!',
                ]
            );

            // Typography and Color for Section Title
            $this->add_group_control(
                \Elementor\Group_Control_Typography::get_type(),
                [
                    'name' => 'section_title_typo',
                    'selector' => '{{WRAPPER}} .section-header h2',
                ]
            );

            $this->add_control(
                'section_title_color',
                [
                    'label' => esc_html__('Section Title Color', 'genixcore'),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .section-header h2' => 'color: {{VALUE}}',
                    ],
                ]
            );

            // Typography and Color for Section Description
            $this->add_group_control(
                \Elementor\Group_Control_Typography::get_type(),
                [
                    'name' => 'section_desc_typo',
                    'selector' => '{{WRAPPER}} .section-header p',
                ]
            );

            $this->add_control(
                'section_desc_color',
                [
                    'label' => esc_html__('Section Description Color', 'genixcore'),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .section-header p' => 'color: {{VALUE}}',
                    ],
                ]
            );

            $this->end_controls_section();

            // Style for Section Heading Style 1
            $this->start_controls_section(
                'section_heading_style1',
                [
                    'label' => esc_html__('Style 1 Typography & Color', 'genixcore'),
                    'tab' => \Elementor\Controls_Manager::TAB_STYLE,
           
                ]
            );

            $this->add_control(
                'style1_title_color',
                [
                    'label' => esc_html__('Title Color', 'genixcore'),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .section-header h2 span' => 'color: {{VALUE}}',
                    ],
                ]
            );

            $this->add_group_control(
                \Elementor\Group_Control_Typography::get_type(),
                [
                    'name' => 'style1_title_typo',
                    'selector' => '{{WRAPPER}} .section-header h2 span',
                ]
            );

            $this->add_control(
                'style1_desc_color',
                [
                    'label' => esc_html__('Description Color', 'genixcore'),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .section-header p' => 'color: {{VALUE}}',
                    ],
                ]
            );

            $this->add_group_control(
                \Elementor\Group_Control_Typography::get_type(),
                [
                    'name' => 'style1_desc_typo',
                    'selector' => '{{WRAPPER}} .section-header p',
                ]
            );

            $this->end_controls_section();


            // Services Box Style Controls
            $this->start_controls_section(
                'box_style_section',
                [
                    'label' => esc_html__('Service Box Style', 'genixcore'),
                    'tab' => Controls_Manager::TAB_STYLE,
                ]
            );

            $this->add_group_control(
                \Elementor\Group_Control_Background::get_type(),
                [
                    'name' => 'box_background',
                    'label' => esc_html__('Background', 'genixcore'),
                    'types' => ['classic', 'gradient'],
                    'selector' => '{{WRAPPER}} .service__item-inner',
                ]
            );

            $this->add_responsive_control(
                'box_padding',
                [
                    'label' => esc_html__('Padding', 'genixcore'),
                    'type' => Controls_Manager::DIMENSIONS,
                    'size_units' => ['px', '%', 'em'],
                    'selectors' => [
                        '{{WRAPPER}} .service__item-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );

            $this->add_responsive_control(
                'box_margin',
                [
                    'label' => esc_html__('Margin', 'genixcore'),
                    'type' => Controls_Manager::DIMENSIONS,
                    'size_units' => ['px', '%', 'em'],
                    'selectors' => [
                        '{{WRAPPER}} .service__item' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );

            $this->add_group_control(
                \Elementor\Group_Control_Border::get_type(),
                [
                    'name' => 'box_border',
                    'selector' => '{{WRAPPER}} .service__item-inner',
                ]
            );

            $this->add_control(
                'box_border_radius',
                [
                    'label' => esc_html__('Border Radius', 'genixcore'),
                    'type' => Controls_Manager::DIMENSIONS,
                    'selectors' => [
                        '{{WRAPPER}} .service__item-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );

            // Box Title Style
            $this->add_control(
                'box_title_color',
                [
                    'label' => esc_html__('Box Title Color', 'genixcore'),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .service__item-content h5 a' => 'color: {{VALUE}}',
                    ],
                ]
            );

            $this->add_group_control(
                \Elementor\Group_Control_Typography::get_type(),
                [
                    'name' => 'box_title_typo',
                    'selector' => '{{WRAPPER}} .service__item-content h5 a',
                ]
            );

            // Box Description Style
            $this->add_control(
                'box_desc_color',
                [
                    'label' => esc_html__('Box Description Color', 'genixcore'),
                    'type' => Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .service__item-content p' => 'color: {{VALUE}}',
                    ],
                ]
            );

            $this->add_group_control(
                \Elementor\Group_Control_Typography::get_type(),
                [
                    'name' => 'box_desc_typo',
                    'selector' => '{{WRAPPER}} .service__item-content p',
                ]
            );

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

?>

<section class="service padding-top padding-bottom">
	<div class="section-header section-header--max50">
		<h2 class="mb-10 mt-minus-5"><span><?php echo esc_html( $settings['main_section_title']); ?></h2>
		<p><?php echo esc_html( $settings['main_section_desc']); ?></p>
	</div>
	<div class="container">
		<div class="service__wrapper">
			<div class="row g-4 align-items-center">
				<?php foreach ($settings['tg_post_list'] as $item) : ?>
				<div class="col-sm-6 col-md-6 col-lg-4">
					<div class="service__item service__item--style1 aos-init">
						<div class="service__item-inner text-center">
							<div class="service__item-thumb mb-30">
								<img class="dark" src="<?php echo esc_url($item['tg_image']['url']); ?>"
									alt="<?php echo esc_attr($item['services_title']); ?>">
							</div>
							<div class="service__item-content">
								<h5> <a class="stretched-link"
										href="<?php echo esc_url($item['services_title_url']['url']); ?>"><?php echo esc_html($item['services_title']); ?></a>
								</h5>
								<p class="mb-0"><?php echo esc_html($item['description']); ?></p>
							</div>
						</div>
					</div>
				</div>
				<?php endforeach; ?>
			</div>
		</div>
	</div>
</section>

<?php
    }
}

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