<?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\Plugin;

/**
 * Elementor button widget.
 * Elementor widget that displays a button with the ability to control every
 * aspect of the button design.
 *
 * @since 1.0.0
 */
class Latest_News extends Widget_Base {

	/**
	 * Get widget name.
	 * Retrieve button widget name.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'ximsa_latest_news';
	}

	/**
	 * Get widget title.
	 * Retrieve button widget title.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'Latest News', 'ximsa' );
	}

	/**
	 * Get widget icon.
	 * Retrieve button widget icon.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-library-open';
	}

	/**
	 * Get widget categories.
	 * Retrieve the list of categories the button widget belongs to.
	 * Used to determine where to display the widget in the editor.
	 *
	 * @since  2.0.0
	 * @access public
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'ximsa' ];
	}
	
	/**
	 * Register button 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(
			'latest_news',
			[
				'label' => esc_html__( 'Latest News', 'ximsa' ),
			]
		);
		$this->add_control(
			'subtitle',
			[
				'label'       => __( 'Sub Title', 'ximsa' ),
				'label_block' => true,
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Title', 'ximsa' ),
			]
		);
		$this->add_control(
			'title',
			[
				'label'       => __( 'Title', 'ximsa' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Title', 'ximsa' ),
			]
		);
		$this->add_control(
			'btn_title',
			[
				'label'       => __( 'Button Title', 'ximsa' ),
				'label_block' => true,
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Button Title', 'ximsa' ),
			]
		);
		$this->add_control(
			'btn_link',
				[
				  'label' => __( 'Button Url', 'ximsa' ),
				  'type' => Controls_Manager::URL,
				  'placeholder' => __( 'https://your-link.com', 'plugin-domain' ),
				  'show_external' => true,
				  'default' => [
				    'url' => '',
				    'is_external' => true,
				    'nofollow' => true,
				  ],
			 ]
		);
		$this->add_control(
			'text_limit',
			[
				'label'   => esc_html__( 'Text Limit', 'ximsa' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 3,
				'min'     => 1,
				'max'     => 100,
				'step'    => 1,
			]
		);
		$this->add_control(
			'query_number',
			[
				'label'   => esc_html__( 'Number of post', 'ximsa' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 3,
				'min'     => 1,
				'max'     => 100,
				'step'    => 1,
			]
		);
		$this->add_control(
			'query_orderby',
			[
				'label'   => esc_html__( 'Order By', 'ximsa' ),
				'label_block' => true,
				'type'    => Controls_Manager::SELECT,
				'default' => 'date',
				'options' => array(
					'date'       => esc_html__( 'Date', 'ximsa' ),
					'title'      => esc_html__( 'Title', 'ximsa' ),
					'menu_order' => esc_html__( 'Menu Order', 'ximsa' ),
					'rand'       => esc_html__( 'Random', 'ximsa' ),
				),
			]
		);
		$this->add_control(
			'query_order',
			[
				'label'   => esc_html__( 'Order', 'ximsa' ),
				'label_block' => true,
				'type'    => Controls_Manager::SELECT,
				'default' => 'DESC',
				'options' => array(
					'DESC' => esc_html__( 'DESC', 'ximsa' ),
					'ASC'  => esc_html__( 'ASC', 'ximsa' ),
				),
			]
		);
		$this->add_control(
        	'query_category', 
			[
				'type' => Controls_Manager::SELECT,
				'label' => esc_html__('Category', 'ximsa'),
				'label_block' => true,
				'options' => get_blog_categories()
			]
		);
		$this->end_controls_section();
	}

	/**
	 * Render button 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();
        $allowed_tags = wp_kses_allowed_html('post');
		
        $paged = get_query_var('paged');
		$paged = ximsa_set($_REQUEST, 'paged') ? esc_attr($_REQUEST['paged']) : $paged;

		$this->add_render_attribute( 'wrapper', 'class', 'templatepath-ximsa' );
		$args = array(
			'post_type'      => 'post',
			'posts_per_page' => ximsa_set( $settings, 'query_number' ),
			'orderby'        => ximsa_set( $settings, 'query_orderby' ),
			'order'          => ximsa_set( $settings, 'query_order' ),
			'paged'         => $paged
		);
		if( ximsa_set( $settings, 'query_category' ) ) $args['category_name'] = ximsa_set( $settings, 'query_category' );
		$query = new \WP_Query( $args );

		if ( $query->have_posts() ) 
	{ ?>
		
    <!-- News Section -->
	<section class="news-section">
		<div class="auto-container">
			<!-- Sec Title -->
			<?php if($settings['subtitle'] || $settings['title'] || $settings['btn_link']['url']) { ?>
            <div class="sec-title">
				<div class="clearfix">
					<?php if($settings['subtitle'] || $settings['title']) { ?>
                    <div class="pull-left">
						<?php if($settings['subtitle']) { ?><div class="title"><?php echo wp_kses($settings['subtitle'], true);?></div><?php } ?>
						<?php if($settings['title']) { ?><h2><?php echo wp_kses($settings['title'], true);?></h2><?php } ?>
					</div>
                    <?php } ?>
                    <?php if($settings['btn_link']['url']){ ?>
					<div class="pull-right">
						<!-- Button Box -->
						<div class="button-box">
							<a href="<?php echo esc_url($settings['btn_link']['url']); ?>" class="theme-btn btn-style-four"><span class="txt"><?php echo wp_kses($settings['btn_title'], true); ?><i class="fa fa-angle-right"></i></span></a>
						</div>
					</div>
                    <?php } ?>
				</div>
			</div>
            <?php } ?>
			<!-- End Sec Title -->
			<div class="row clearfix">
				<?php while ( $query->have_posts() ) : $query->the_post(); ?>
				<!-- News Block -->
				<div class="news-block col-lg-4 col-md-6 col-sm-12">
					<div class="inner-box wow fadeInLeft" data-wow-delay="0ms" data-wow-duration="1500ms">
						<div class="image">
							<a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><?php the_post_thumbnail('ximsa_370x250'); ?></a>
						</div>
						<div class="lower-content">
							<ul class="post-info">
								<li><?php the_category(' '); ?></li>
								<li><?php echo get_the_date( ); ?></li>
							</ul>
							<h5><a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><?php the_title(); ?></a></h5>
							<div class="text"><?php echo wp_kses(ximsa_trim(get_the_content(), $settings['text_limit']), true); ?></div>
							<!-- Button Box -->
							<div class="button-box">
								<a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>" class="theme-btn btn-style-one"><span class="txt"><?php esc_html_e('Read More', 'ximsa'); ?><i class="fa fa-angle-right"></i></span></a>
							</div>
						</div>
					</div>
				</div>
				<?php endwhile; ?>
			</div>
		</div>
	</section>
	<!-- End News Section -->
	
	<?php }
    wp_reset_postdata();
	}

}
