<?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_blog 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-blog-';
	}

	/**
	 * 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 Blog', '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_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(
            'post_options',
            [
                'label' => __( 'post Options', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'item_shape', [
                'label'       => esc_html__( 'Item Inside Sahpe', 'ximsa' ),
                'type'        => Controls_Manager::MEDIA,
                'label_block' => true,
            ]
        );
        $repeater = new \Elementor\Repeater();
        $repeater->add_control(
            'post_id', [
                'label'       => esc_html__( 'Select post', 'ximsa' ),
                'type'        => Controls_Manager::SELECT,
                'label_block' => true,
                'options'     => $this->select_param_posts(),
            ]
        );
        $repeater->add_control(
			'post_icon',
			[
				'label' => esc_html__( 'post Icon Image Upload', 'ximsa' ),
				'type' => Controls_Manager::MEDIA,
                'label_block' => true,
			]
		);
        $repeater->add_control(
			'post_excerpt',
			[
				'label' => esc_html__( 'post 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 post 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', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '
                    {{WRAPPER}} {{CURRENT_ITEM}} .xis-dm-post-inner-item:before
                ',
			]
		);
        $this->add_control(
			'postsitems',
			[
				'label' => esc_html__( 'post List', '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, .container-lg, .container-md, .container-sm, .container-xl, .container-xxl' => '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__( 'Heading Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-blog-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-blog-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-blog-top-content .xis-saas-blog-top-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-blog-top-content .xis-saas-blog-top-text',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        
        $this->end_controls_section();
        $this->start_controls_section(
            'post_settings_style',
            [
                'label' => __( 'post Settings', 'ximsa' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'post_title_stl',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'post Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'tm_ttl_color',
            [
                'label'     => esc_html__( 'post Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-blog-item .xis-saas-blog-text h3 a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'tm_ttl_hv_color',
            [
                'label'     => esc_html__( 'Post Active Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-blog-item-area.slick-current.slick-active .xis-saas-blog-text h3 a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'tm_ttl_typography',
                'label'          => esc_html__( 'post Title Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-saas-blog-item .xis-saas-blog-text h3
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        
        $this->add_control(
            'post_meta_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Post Meta Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'post_meta-cl',
            [
                'label'     => esc_html__( 'Post Meta Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-blog-item .xis-saas-blog-text .blog-meta a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'list_item_hover_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-saas-blog-item .xis-saas-blog-text .blog-meta a
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'readmore_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Category Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'cate-bgbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-saas-blog-item .xis-saas-blog-text .blog-cat',
			]
		);
        
        $this->add_control(
            'cate_color',
            [
                'label'     => esc_html__( 'Cate Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-blog-item .xis-saas-blog-text .blog-cat' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'cat_cl_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-saas-blog-item .xis-saas-blog-text .blog-cat
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'nav_txt',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Nav Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        
        $this->add_control(
            'nav_cl',
            [
                'label'     => esc_html__( 'Nav Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-saas-blog-slider .slick-dots li.slick-active span' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'nav-bar-bgbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-saas-blog-section .progress',
			]
		);
        $this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'nav-arrow-bgbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-saas-blog-carousel-pagination .blg-left_arrow:before, .xis-saas-blog-carousel-pagination .blg-right_arrow:before',
			]
		);
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'cat_cl_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-saas-blog-slider .slick-dots li.slick-active span
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
             
        $this->end_controls_section();
	}
		


	protected function render() {
		
		
		$settings = $this->get_settings_for_display();?>
		<!-- Start of blog section
        ============================================= -->
        <section id="xis-saas-blog" class="xis-saas-blog-section" data-background="<?php echo esc_url($settings['shape']['url']);?>">
            <div class="container">
                <div class="xis-saas-blog-top-content d-flex align-items-center">
                    <div class="xis-saas-section-title  xis-saas-headline">
                        <h2><?php echo wp_kses( $settings['tm_title'], true );?></h2> 
                    </div>
                    <div class="xis-saas-blog-top-text">
                        <?php echo wp_kses( $settings['description'], true );?>
                    </div>
                </div>
                <div class="xis-saas-blog-content">
                    <div class="xis-saas-blog-slider">
                       <?php $shape = 0; foreach($settings['postsitems'] as $index => $box): $shape++;?>
                       <?php  if( $box['post_id'] ) : 
                        $categories = get_the_category($box['post_id']); 
                       ?>
                        <div class="xis-saas-blog-item-area position-relative">
                            <span class="xis-saas-blog-shape position-absolute"><img src="<?php echo esc_url($settings['item_shape']['url']);?>"></span>
                            <div class="xis-saas-blog-item position-relative">
                                <div class="xis-saas-blog-img position-absolute"><img src="<?php echo esc_url(get_the_post_thumbnail_url($box['post_id']));?>"></div>
                                <div class="xis-saas-blog-text xis-saas-headline position-relative">
                                    <span class="blog-cat"><?php echo esc_html($categories[0]->name);?></span>
                                    <h3><a href="<?php echo get_the_permalink($box['post_id']);?>"><?php echo get_the_title($box['post_id']);?></a></h3>
                                    <div class="blog-meta">
                                        <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) );?>"><?php esc_html_e( 'by', 'ximsa' ); ?> <?php echo esc_html((get_the_author_meta( 'display_name' )));?></a>
                                        <a href="#"><?php echo get_the_date()?></a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endif; endforeach;?>
                    </div>
                    <div class="progress" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                        <span class="slider__label sr-only">
                        </span>
                    </div>
                    <div class="xis-saas-blog-carousel-pagination">
                        <div class="carousel_nav text-right ">
                            <button type="button" class="blg-left_arrow"><i class="far fa-long-arrow-left"></i></button>
                            <button type="button" class="blg-right_arrow"><i class="far fa-long-arrow-right"></i></button>
                        </div>
                    </div>
                </div>
            </div>
        </section>		
    <!-- End of blog section
        ============================================= -->

    <?php     
      }
	
      protected function select_param_posts() {
        $args = wp_parse_args( [
            'post_type'   => 'post',
            '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;
    }

}

