<?php
/**
 * Elementor Widget
 * @package Geroz
 * @since 1.0.0
 */

namespace Elementor;

class Home6_Blog_Post extends Widget_Base
{

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

    /**
     * Get widget title.
     *
     * Retrieve Elementor widget title.
     *
     * @return string Widget title.
     * @since 1.0.0
     * @access public
     *
     */
    public function get_title()
    {
        return esc_html__('Home 6 Blog Post', 'geroz-core');
    }

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

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

    /**
     * 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(
            'blog_settings_section',
            [
                'label' => esc_html__('General Settings', 'geroz-core'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        ); 

        $this->add_control(
            'subtitle',
            [
                'label'       => __( 'Sub Title', 'geroz-core' ),
                'type'        => Controls_Manager::TEXT,
                'dynamic'     => [
                    'active' => true,
                ],
                'placeholder' => esc_html__( 'Enter your sub title', 'geroz-core' ),
                'default' => esc_html__('Latest News', 'geroz-core'),
            ]
        );      
        
        $this->add_control(
            'title',
            [
                'label'       => __( 'Title', 'geroz-core' ),
                'type'        => Controls_Manager::TEXT,
                'dynamic'     => [
                    'active' => true,
                ],
                'placeholder' => esc_html__( 'Enter your title', 'geroz-core' ),
                'default' => esc_html__('Latest News', 'geroz-core'),
            ]
        );
        
        $this->add_control(
            'text',
            [
                'label'       => __( 'Text', 'geroz-core' ),
                'type'        => Controls_Manager::TEXTAREA,
                'dynamic'     => [
                    'active' => true,
                ],
                'placeholder' => esc_html__( 'Enter your text', 'geroz-core' ),
                'default' => esc_html__('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'geroz-core'),
            ]
        );

         $this->add_control('blog_grid', [
            'label' => esc_html__('Blog Grid', 'geroz-core'),
            'type' => Controls_Manager::SELECT,
            'options' => array(
                'col-lg-2' => esc_html__('col-lg-2', 'geroz-core'),
                'col-lg-3' => esc_html__('col-lg-3', 'geroz-core'),
                'col-lg-4' => esc_html__('col-lg-4', 'geroz-core'),
                'col-lg-6' => esc_html__('col-lg-6', 'geroz-core'),
                'col-lg-12' => esc_html__('col-lg-12', 'geroz-core'),
            ),
            'default' => 'col-lg-4',
            'description' => esc_html__('Select Blog Grid', 'geroz-core')
        ]);   
        

        $this->add_control(
            'word_limit',
            [
                'label' => esc_html__('Word Limit', 'geroz-core'),
                'type' => Controls_Manager::NUMBER,
                'default' => 20,
                'description' => esc_html__('Set the number of words to display from the post content.', 'geroz-core'),
            ]
        );

        $this->add_control(
            'more_text',
            [
                'label' => esc_html__('More Text', 'geroz-core'),
                'type' => Controls_Manager::TEXT,
                'default' => '...',
                'description' => esc_html__('Set the text to append after the trimmed words, e.g., "Read More". Leave blank for no text.', 'geroz-core'),
            ]
        );
   
        $this->add_control('total', [
            'label' => esc_html__('Total Posts', 'geroz-core'),
            'type' => Controls_Manager::TEXT,
            'default' => '-1',
            'description' => esc_html__('enter how many post you want in masonry , enter -1 for unlimited post.')
        ]);
        $this->add_control('offset', [
            'label' => esc_html__('Offset Posts', 'geroz-core'),
            'type' => Controls_Manager::TEXT,
            'default' => '0',
            'description' => esc_html__('enter how many post you want in masonry , enter -1 for unlimited post.')
        ]);
        $this->add_control('category', [
            'label' => esc_html__('Category', 'geroz-core'),
            'type' => Controls_Manager::SELECT2,
            'multiple' => true,
            'options' => geroz_core()->get_terms_names('category', 'id'),
            'description' => esc_html__('Select category as you want, leave it blank for all categories', 'geroz-core'),
        ]);        

        $this->add_control('order', [
            'label' => esc_html__('Order', 'geroz-core'),
            'type' => Controls_Manager::SELECT,
            'options' => array(
                'ASC' => esc_html__('Ascending', 'geroz-core'),
                'DESC' => esc_html__('Descending', 'geroz-core'),
            ),
            'default' => 'DESC',
            'description' => esc_html__('select order', 'geroz-core')
        ]);
        $this->add_control('orderby', [
            'label' => esc_html__('OrderBy', 'geroz-core'),
            'type' => Controls_Manager::SELECT,
            'options' => array(
                'ID' => esc_html__('ID', 'geroz-core'),
                'title' => esc_html__('Title', 'geroz-core'),
                'date' => esc_html__('Date', 'geroz-core'),
                'rand' => esc_html__('Random', 'geroz-core'),
                'comment_count' => esc_html__('Most Comments', 'geroz-core'),
            ),
            'default' => 'ID',
            'description' => esc_html__('select order', 'geroz-core')
        ]);      
        
           $this->add_control(
            'image_thumb_display',
            [
                'label' => esc_html__('Image Display', 'geroz-core'),
                'type' => Controls_Manager::SWITCHER,
                'description' => esc_html__('show/hide description', 'geroz-core'),
            ]
        );
        
        $this->add_control(
            'thumb_date',
            [
                'label' => esc_html__('Thumb Date Show/Hide', 'geroz-core'),
                'type' => Controls_Manager::SWITCHER,
                'description' => esc_html__('show/hide Date', 'geroz-core'),
            ]
        );
      
        $this->add_control(
            'pagination',
            [
                'label' => esc_html__('Pagination', 'geroz-core'),
                'type' => Controls_Manager::SWITCHER,
                'description' => esc_html__('you can set yes to show pagination.', 'geroz-core'),
                'default' => 'no',
            ]
        );
        
          $this->add_control(
            'category_display',
            [
                'label' => esc_html__('Category Display', 'geroz-core'),
                'type' => Controls_Manager::SWITCHER,
                'description' => esc_html__('Show  Or Hide Category.', 'geroz-core'),
            ]
        );

          $this->add_control(
            'tag_display',
            [
                'label' => esc_html__('Tags Display', 'geroz-core'),
                'type' => Controls_Manager::SWITCHER,
                'description' => esc_html__('Show  Or Hide Tags.', 'geroz-core'),
            ]
        );
        
        $this->add_control(
            'pagination_alignment',
            [
                'label' => esc_html__('Pagination Alignment', 'geroz-core'),
                'type' => Controls_Manager::SELECT,
                'options' => array(
                    'start' => esc_html__('Left Align', 'geroz-core'),
                    'center' => esc_html__('Center Align', 'geroz-core'),
                    'end' => esc_html__('Right Align', 'geroz-core'),
                ),
                'description' => esc_html__('you can set pagination alignment.', 'geroz-core'),
                'default' => 'start',
                'condition' => array('pagination' => 'yes'),
            ]
        );
        $this->end_controls_section();


    }

    /**
     * Render Elementor 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();
        $rand_numb = rand(333, 999999999);
        //query settings
        $total_posts = $settings['total'];
        $category = $settings['category'];
        $order = $settings['order'];
        $orderby = $settings['orderby'];
        $offset = $settings['offset'];
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        //other settings
        $pagination = $settings['pagination'] ? false : true;
        $pagination_alignment = $settings['pagination_alignment'];

        //setup query       

        $args = array(
            'post_type' => 'post',
            'posts_per_page' => $total_posts,
            'order' => $order,
            'orderby' => $orderby,
            'paged' => $paged, // Use paged instead of offset
            'post_status' => 'publish',
            'ignore_sticky_posts' => 1,
        );
        
        if (!empty($category)) {
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'term_id',
                    'terms' => $category
                )
            );
        }
        
        $post_data = new \WP_Query($args);
        

        ?>


           <section class="news-wrapper news-5 section-padding">
                <div class="container">
                    <?php if($settings['title']): ?>
                    <div class="section-title text-center section-title-font">
                        <div class="sub-title">
                            <div class="dot"></div> <span><?php echo $settings['subtitle'];?></span>
                        </div>
                        <h2 class="tp-char-animation"><?php echo $settings['title'];?></h2>
                    </div>
                    <?php endif; ?>
                    <div class="row mt-4">
                    <?php while ($post_data->have_posts()):$post_data->the_post(); 
                        $img_id = get_post_thumbnail_id(get_the_ID()) ? get_post_thumbnail_id(get_the_ID()) : false;
                        $img_url_val = $img_id ? wp_get_attachment_image_src($img_id, 'geroz_grid_blog_12', false) : '';
                        $img_url = is_array($img_url_val) && !empty($img_url_val) ? $img_url_val[0] : '';
                        $img_alt = get_post_meta($img_id, '_wp_attachment_image_alt', true);

                        $comments_count = get_comments_number(get_the_ID());
                        $comment_text = ($comments_count > 1) ? 'Comments (' . $comments_count . ')' : 'Comment (' . $comments_count . ')';
                    ?> 
                        <div class="<?php echo esc_attr($settings['blog_grid']);?> wow fadeInUp" data-wow-delay="200ms">
                            <div class="single-news-item">
                                <?php if(!empty($settings['image_thumb_display'])) : ?>
                                <div class="image">
                                    <img src="<?php echo esc_url($img_url); ?>" alt="">
                                </div>
                                 <?php endif; ?>
                                <div class="content">
                                    <ul class="d-flex align-items-center">
                                        <?php if(!empty($settings['thumb_date'])) : ?>
                                        <li>
                                            <i class="fas fa-calendar-alt"></i>
                                            <?php echo get_the_date('d M, Y'); ?>
                                        </li>
                                        <?php endif; ?>                                
                                        <li>
                                            <i class="far fa-user"></i>
                                            <?php echo get_the_author(); ?>
                                        </li>
                                    </ul>
                                    <h3 class="title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>

                                    <a href="<?php the_permalink(); ?>" class="read">Read More<i
                                            class="far fa-long-arrow-right"></i></a>
                                </div>
                            </div>
                        </div>
                           <?php
                            endwhile;
                            wp_reset_query();
                        ?>
                        <?php if (!$pagination) { ?>
                            <div class="col-lg-12">
                                <div class="blog-pagination text-<?php echo esc_attr($pagination_alignment) ?>">
                                    
                                        <?php geroz()->post_pagination($post_data); ?>

                                </div>
                            </div>
                        <?php } ?>
                    </div>
                </div>
            </section>



        <?php
    }
}

Plugin::instance()->widgets_manager->register_widget_type(new Home6_Blog_Post());