<?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 it_post_widget 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 'it-post-widget';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'IT Post', '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(
            'it_post_options',
            [
                'label' => __( 'IT Solution Post Options', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'post_shape',
				'label' => esc_html__( 'Post Overlay Shape', 'ximsa' ),
				'types' => [ 'classic' ],
				'exclude' => [ 'color' ],
				'selector' => '{{WRAPPER}} .xis-it-blog-item:before',
			]
		);
        $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(),
            ]
        );
        
        $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(
            'post_option_settngs',
            [
                'label' => __( 'Post Style Settings', 'ximsa' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'post_title_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Post Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );        
        $this->add_control(
            'post_title_cl',
            [
                'label'     => esc_html__( 'Post Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-it-blog-item .inner-text h3' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'post_title_hv_cl',
            [
                'label'     => esc_html__( 'Post Title Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-it-blog-item .inner-text h3:hover' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'post_title_typography',
                'label'          => esc_html__( 'Post Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-it-blog-item .inner-text h3',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'post_meta',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Post Meta Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'meta_clbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-it-blog-item .inner-text .inner-meta a, .xis-it-blog-item .inner-text .inner-meta a i',
			]
		);
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'se_th_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-it-blog-item .inner-text .inner-meta a',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        
		$this->end_controls_section();
	}
		


	protected function render() {
		
		
		$settings = $this->get_settings_for_display(); ?>
		    <div class="xis-it-blog-content">
            <div class="row">
                <?php foreach($settings['postsitems'] as $index => $box): ?>
                <?php  if( $box['post_id'] ) : ?>
                <div class="col-lg-6  wow fadeInUp" data-wow-delay="400ms" data-wow-duration="1500ms">
                    <div class="xis-it-blog-item position-relative">
                        <div class="inner-img">
                            <img src="<?php echo esc_url(get_the_post_thumbnail_url($box['post_id']));?>" alt="">
                        </div>
                        <div class="inner-text position-absolute xis-it-headline">
                            <h3><a href="<?php echo get_the_permalink($box['post_id']);?>"><?php echo get_the_title($box['post_id']);?></a></h3>
                            <div class="inner-meta">
                                <a href="#"><i class="fas fa-calendar-alt"></i> <?php echo get_the_date()?> </a>
                                <a href="#"><i class="fas fa-comment"></i> <?php echo get_comments_number($box['post_id']);?> <?php esc_html_e( 'Commnets', 'ximsa');?> </a>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endif; endforeach;?>
            </div>
        </div>
		<?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;
    }

}

