<?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 saas_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 'saas-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__( 'Saas 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(
			'sm_title',
			[
				'label' => esc_html__( 'Service 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->add_control(
			'gallery_thumb',
			[
				'label' => esc_html__( 'Add Gallery Thumb', 'ximsa' ),
				'type' => Controls_Manager::GALLERY,
                'label_block' => true,
			]
		);
        $this->add_control(
			'link_text',
			[
				'label' => esc_html__( 'Link Text', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $this->add_control(
			'link_url',
			[
				'label' => esc_html__( 'Link URL', 'ximsa' ),
				'type' => Controls_Manager::URL,
                'label_block' => true,
			]
		);
        $this->add_control(
			'btn_text',
			[
				'label' => esc_html__( 'Button Text', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $this->add_control(
			'btn_url',
			[
				'label' => esc_html__( 'Button URL', 'ximsa' ),
				'type' => Controls_Manager::URL,
                'label_block' => true,
			]
		);
        $this->end_controls_section();

		$this->start_controls_section(
            'service_options',
            [
                'label' => __( 'service Options', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        
        $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', 'ximsa' ),
				'type' => Controls_Manager::ICONS,
                '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(
			'postitems',
			[
				'label' => esc_html__( 'Add Post Item', 'ximsa' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
			]
		);
        
		$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' => 'max-width: {{SIZE}}{{UNIT}};',
				],
			]
		);
        
        $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__( 'Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-service-top-content .xis-saas-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-saas-service-top-content .xis-saas-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-saas-service-title-text' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'desc_clo_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-saas-service-title-text',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'se_link_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Link Text Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'lnk_txt_color',
            [
                'label'     => esc_html__( 'Link Text Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-service-more .xis-saas-service-more-btn a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'lnk_txt_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-saas-service-more .xis-saas-service-more-btn a',
                '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_icon',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Service Icon Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'background',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-saas-service-item .xis-saas-service-icon i',
			]
		);
        
        $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-saas-service-item .xis-saas-service-text h3 a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'tm_ttl_hv_color',
            [
                'label'     => esc_html__( 'Service Title Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-service-item .xis-saas-service-text h3 a:hover' => '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-saas-service-item .xis-saas-service-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-saas-service-item:hover .xis-saas-service-text p' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'excerpt_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-saas-service-item:hover .xis-saas-service-text p
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'service_link_icon',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Link Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'link_bg_cl',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-saas-service-item .xis-saas-service-text .service_more',
			]
		); 
        $this->end_controls_section();
        $this->start_controls_section(
            'button__content',
            [
                'label' => __( 'Button Style', 'ximsa' ),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name'           => 'btn_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                    {{WRAPPER}} .xis-saas-btn a .txt
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        
        $this->start_controls_tabs( '_banner_button_1' );
        $this->start_controls_tab(
            '_glow-extension_btn__banner_normal',
            [
                'label' => esc_html__( 'Normal', 'glow-extension-extension' ),
            ]
        );
        $this->add_control(
            'btn__color',
            [
                'label'     => esc_html__( 'Color', 'glow-extension-extension' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-btn a' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Background::get_type(),
            [
                'name'     => 'btn_bg_color',
                'label'    => __( 'Background', 'glow-extension-extension' ),
                'types'    => ['classic', 'gradient'],
                'exclude'  => ['image'],
                'selector' => '
                    {{WRAPPER}} .xis-saas-btn a
                ',
            ]
        );
        $this->add_responsive_control(
            'btn_border_radious',
            [
                'label'      => esc_html__( 'Border Radius', 'glow-extension-extension' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} .xis-saas-btn a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        
        $this->end_controls_tab();
        $this->start_controls_tab(
            '_glow-extension_btn_hover',
            [
                'label' => esc_html__( 'Hover', 'glow-extension-extension' ),
            ]
        );
        $this->add_control(
            'btn__hover_color',
            [
                'label'     => esc_html__( 'Color', 'glow-extension-extension' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-btn a:hover' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Background::get_type(),
            [
                'name'     => 'btn_hover_bg_color',
                'label'    => __( 'Background', 'glow-extension-extension' ),
                'types'    => ['classic', 'gradient'],
                'exclude'  => ['image'],
                'selector' => '
                    {{WRAPPER}} .xis-saas-btn a:before
                ',
            ]
        );
        $this->add_responsive_control(
            'btn_hover_border_radious',
            [
                'label'      => esc_html__( 'Border Radius', 'glow-extension-extension' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} .xis-saas-btn a:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',                    
                ],
            ]
        );
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->end_controls_section();
	}
		


	protected function render() {
		
		
		$settings = $this->get_settings_for_display();?>

		<!-- Start of Service section
        ============================================= -->
        <section id="xis-saas-service" class="xis-saas-service-section">
            <div class="container">
                <div class="xis-saas-service-top-content d-flex flex-wrap position-relative">
                    <div class="xis-saas-section-title  xis-saas-headline">
                        <h2><?php echo wp_kses( $settings['sm_title'], true );?></h2>
                    </div>
                    <div class="xis-saas-service-title-text">
                        <?php echo wp_kses( $settings['description'], true );?>
                    </div>
                </div>
                <div class="xis-saas-service-slider-wrapper position-relative">
                    <div class="row">
                        <div class="col-lg-3">
                            <div class="xis-saas-service-more d-flex justify-content-center align-items-center">
                                <div class="xis-saas-service-img ul-li">
                                    <ul>
                                        <?php foreach($settings['gallery_thumb'] as $gall):?>
                                        <li><span class="testi-img"><img src="<?php echo esc_url($gall['url']);?>" alt=""></span></li>
                                        <?php endforeach;?>
                                    </ul>
                                </div>
                                <div class="xis-saas-service-more-btn">
                                    <a href="<?php echo esc_url($settings['link_url']['url']);?>"><?php echo wp_kses( $settings['link_text'], true );?></a>
                                </div>
                            </div>
                            <div class="xis-saas-btn">
                                <a class="d-flex justify-content-center align-items-center" href="<?php echo esc_url($settings['btn_url']['url']);?>"><?php echo wp_kses( $settings['btn_text'], true );?> <i class="fal fa-long-arrow-right"></i></a>
                            </div>
                        </div>
                        <div class="col-lg-9">
                            <div class="xis-saas-service-slider-area">
                                <div class="xis-saas-service-slider">
                                    <?php $shape = 0; foreach($settings['postitems'] as $index => $box): $shape++;?>
                                    <?php  if( $box['service_id'] ) : ?>
                                    <div class="xis-saas-service-item">
                                        <div class="xis-saas-service-icon">
                                            <?php \Elementor\Icons_Manager::render_icon( $box['service_icon'], [ 'aria-hidden' => 'true' ] ); ?>
                                        </div>
                                        <div class="xis-saas-service-text xis-saas-headline pera-content">
                                            <h3><a href="<?php echo get_the_permalink($box['service_id']);?>"><?php echo get_the_title($box['service_id']);?></a></h3>
                                            <p><?php echo wp_kses( $box['service_excerpt'], true );?></p>
                                            <div class="service_more_btn d-flex justify-content-end">
                                                <a class="service_more d-flex align-items-center justify-content-center" href="<?php echo get_the_permalink($box['service_id']);?>"><i class="fal fa-long-arrow-right"></i></a>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endif; endforeach;?>
                                </div>
                            </div>
                        </div>
                    </div>
                </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;
    }

}

