<?php
namespace XIMSAPLUGIN\Element;

use Elementor\Controls_Manager;
use Elementor\Controls_Stack;
use Elementor\Group_Control_Typography;
use Elementor\Scheme_Typography;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Border;
use Elementor\Repeater;
use Elementor\Widget_Base;
use Elementor\Utils;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Plugin;

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

class digm_service extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * Retrieve Elementor widget name.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'digm-service-';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'Digital marketing Service', 'ximsa' );
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve Elementor widget icon.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'gl-custom-icon';
	}

	/**
	 * Get widget categories.
	 *
	 * Retrieve the list of categories the Elementor widget belongs to.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'ximsa' ];
	}

	/**
	 * Register Elementor 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(
            'section_title_control',
            [
                'label' => __( 'Section Title Control', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
			'shape',
			[
				'label' => esc_html__( 'Shape', 'ximsa' ),
				'type' => Controls_Manager::MEDIA,
                'label_block' => true,
			]
		);
        $this->add_control(
			'tm_subtitle',
			[
				'label' => esc_html__( 'Sub Title', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
			]
		);
        $this->add_control(
			'tm_title',
			[
				'label' => esc_html__( 'Title', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $this->add_control(
			'description',
			[
				'label' => esc_html__( 'Description', 'ximsa' ),
				'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,
			]
		);
        
        $this->end_controls_section();

		$this->start_controls_section(
            'service_options',
            [
                'label' => __( 'service Options', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
			'item_shape',
			[
				'label' => esc_html__( 'Item Shape', 'ximsa' ),
				'type' => Controls_Manager::MEDIA,
                'label_block' => true,
			]
		);
        $repeater = new \Elementor\Repeater();
        $repeater->add_control(
            'service_id', [
                'label'       => esc_html__( 'Select service', 'ximsa' ),
                'type'        => Controls_Manager::SELECT,
                'label_block' => true,
                'options'     => $this->select_param_posts(),
            ]
        );
        $repeater->add_control(
			'service_icon',
			[
				'label' => esc_html__( 'Service Icon Image Upload', 'ximsa' ),
				'type' => Controls_Manager::MEDIA,
                'label_block' => true,
			]
		);
        $repeater->add_control(
			'service_excerpt',
			[
				'label' => esc_html__( 'Service Escerpt', 'ximsa' ),
				'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,
			]
		);
        $repeater->add_control(
			'list_item',
			[
				'label' => esc_html__( 'List Item', 'ximsa' ),
				'type' => \Elementor\Controls_Manager::WYSIWYG,
				'placeholder' => esc_html__( 'Type your Service List Item here', 'ximsa' ),
			]
		);
        $repeater->add_control(
			'btn_label',
			[
				'label' => esc_html__( 'Button Label', 'ximsa' ),
				'type' => \Elementor\Controls_Manager::TEXT,
			]
		);
        
        $repeater->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'item_bg_color',
				'label' => esc_html__( 'Background', 'plugin-name' ),
				'types' => [ 'gradient' ],
				'selector' => '
                    {{WRAPPER}} {{CURRENT_ITEM}} .xis-dm-service-inner-item:before
                ',
			]
		);
        $this->add_control(
			'servicesitems',
			[
				'label' => esc_html__( 'Service List', 'ximsa' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
			]
		);
        $this->add_control(
			'btm_info',
			[
				'label' => esc_html__( 'Service Bottom Info', 'ximsa' ),
				'type' => \Elementor\Controls_Manager::WYSIWYG,
				'placeholder' => esc_html__( 'Type your Service Bottom Info here', 'ximsa' ),
			]
		);
		$this->end_controls_section();
        
        $this->start_controls_section(
            'section_title_style',
            [
                'label' => __( 'Section Title Settings', 'ximsa' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
			'container_width',
			[
				'label' => esc_html__( 'Container Width', 'ximsa' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 2000,
					],
					'%' => [
						'min' => 0,
						'max' => 100,
					],
				],
				
				'selectors' => [
					'{{WRAPPER}} .container, .container-lg, .container-md, .container-sm, .container-xl, .container-xxl' => 'max-width: {{SIZE}}{{UNIT}};',
				],
			]
		);
        $this->add_control(
            'se__sub_title_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Sub Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );        
        $this->add_control(
            'sub_h_bg_cl',
            [
                'label'     => esc_html__( 'Sub Heading BG Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-section-title span' => 'background-color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'se_sub_h_color',
            [
                'label'     => esc_html__( 'Sub Heading Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-section-title span' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'se_sth_typography',
                'label'          => esc_html__( 'Sub Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-dm-section-title span',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'se_title_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'main_title_color',
            [
                'label'     => esc_html__( 'Heading Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-section-title h2' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'main_title_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-dm-section-title h2',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'se_desc_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Desc Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'desc_title_color',
            [
                'label'     => esc_html__( 'Description Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-section-title p' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'desc_clo_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-dm-section-title p',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        
        $this->end_controls_section();
        $this->start_controls_section(
            'service_settings_style',
            [
                'label' => __( 'service Settings', 'ximsa' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'service_title_stl',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'service Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'tm_ttl_color',
            [
                'label'     => esc_html__( 'service Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text h3' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'tm_ttl_hv_color',
            [
                'label'     => esc_html__( 'Service Title Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item:hover .xis-dm-service-inner-text h3' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'tm_ttl_typography',
                'label'          => esc_html__( 'service Title Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text h3
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'serv_excerpt_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Service Excerpt Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'excerpt_color',
            [
                'label'     => esc_html__( 'Excerpt Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text p' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'excerpt_hov_color',
            [
                'label'     => esc_html__( 'Excerpt Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item:hover .xis-dm-service-inner-text p' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'excerpt_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text p
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'list_item_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'List Item Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'lst_item_color',
            [
                'label'     => esc_html__( 'List Item Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text ul li' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'list_itm_cl_hover',
            [
                'label'     => esc_html__( 'List Item Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item:hover .xis-dm-service-inner-text li' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'list_item_hover_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text ul li
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'readmore_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Readmore Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'readmore_cl',
            [
                'label'     => esc_html__( 'Read More Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'readmore_hv_cl',
            [
                'label'     => esc_html__( 'Read More Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-inner-item:hover .xis-dm-service-inner-text a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'readmore_hover_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-dm-service-inner-item .xis-dm-service-inner-text a
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'lineSt',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Line Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'line_1_cl',
            [
                'label'     => esc_html__( 'Line One Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-content .line-shape-wrap .line-shape1' => 'background-color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'line_2_cl',
            [
                'label'     => esc_html__( 'Line Two Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-content .line-shape-wrap .line-shape2' => 'background-color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'line_3_cl',
            [
                'label'     => esc_html__( 'Line Three Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dm-service-content .line-shape-wrap .line-shape3' => 'background-color: {{VALUE}} ',
                ],
            ]
        );        
             
        $this->end_controls_section();
	}
		


	protected function render() {
		
		
		$settings = $this->get_settings_for_display();?>
		<!-- Start of Service section
        ============================================= -->
        <section id="xis-dm-service" class="xis-dm-service-section  position-relative">
            <span class="xis-dm-shape1 position-absolute"><img src="<?php echo esc_url($settings['shape']['url']);?>"></span>
            <div class="xis-dm-section-title xis-dm-headline pera-content text-center wow fadeInUp" data-wow-delay="0ms" data-wow-duration="1500ms">
                <span><?php echo esc_html($settings['tm_subtitle']);?></span>
                <h2><?php echo esc_html($settings['tm_title']);?></h2>
                <p><?php echo wp_kses( $settings['description'], true );?></p>
            </div>
            <div class="xis-dm-service-content  position-relative">
                <div class="container">
                    <div class="xis-dm-service-area position-relative">
                        <div class="line-shape-wrap position-absolute d-flex justify-content-between">
                            <span class="line-shape1"></span>
                            <span class="line-shape2"></span>
                            <span class="line-shape3"></span>
                        </div>
                        <div class="row justify-content-center">
                            <?php $shape = 0; foreach($settings['servicesitems'] as $index => $box): $shape++;?>
                            <?php  if( $box['service_id'] ) : ?>
                            <div class="col-lg-4 col-md-6 wow fadeInUp elementor-repeater-item-<?php echo esc_attr($box['_id']);?>" data-wow-delay="600ms" data-wow-duration="1500ms">
                                <div class="xis-dm-service-inner-item position-relative">
                                    <span class="hover-shape position-absolute"><img src="<?php echo esc_url($settings['item_shape']['url']);?>"></span>
                                    <div class="xis-dm-inner-icon">
                                        <img src="<?php echo esc_url($box['service_icon']['url']);?>">
                                    </div>
                                    <div class="xis-dm-service-inner-text xis-dm-headline pera-content ul-li-block">
                                        <h3><?php echo get_the_title($box['service_id']);?></h3>
                                        <p><?php echo wp_kses( $box['service_excerpt'], true );?></p>
                                            <?php echo wp_kses( $box['list_item'], true );?>
                                        <a href="<?php echo get_the_permalink($box['service_id']);?>"><?php echo esc_html($box['btn_label']);?> <i class="far fa-angle-right"></i></a>
                                    </div>
                                </div>
                            </div>
                            <?php endif; endforeach;?>
                        </div>
                    </div>
                </div>
                <div class="xis-dm-service-more pera-content text-center wow fadeInUp" data-wow-delay="800ms" data-wow-duration="1500ms">
                    <?php echo wp_kses( $settings['btm_info'], true );?>
                </div>
            </div>
        </section>	
    <!-- End of Service section
        ============================================= -->	

    <?php     
      }
	
      protected function select_param_posts() {
        $args = wp_parse_args( [
            'post_type'   => 'service',
            'numberposts' => -1,
            'orderby'     => 'title',
            'order'       => 'ASC',
        ] );
    
        $query_query = get_posts( $args );
    
        $posts = [];
        if ( $query_query ) {
            foreach ( $query_query as $query ) {
                $posts[$query->ID] = $query->post_title;
            }
        }
    
        return $posts;
    }

}

