<?php

namespace Elementor;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class multiblog_elementor_grid_blog extends Widget_Base {
    
    public function get_name() {
        return 'multiblog-grid-blog';
    }
    
    public function get_title() {
        return __( 'Blog Grid Layout', 'multiblog' );
    }
    
    public function get_icon() {
        return 'eicon-call-to-action';
    }
    
    public function get_categories() {
        return [ 'multiblog-elementor-widget' ];
    }
    
    protected function register_controls() {
        $this->start_controls_section(
            'multiblog_blog_section',
            [
                'label' => esc_html__('Blog Options', 'multiblog'),
            ]
        );
        
        $this->add_control(
            'multiblog_blog_get_category',
            [
                'label' => __('Select Category', 'multiblog'),
                'type' => Controls_Manager::SELECT2,
                'default' => array(),
                'options' => multiblog_blog_get_category(),
                'multiple' => true
            ]
        );
        
        $this->add_control(
            'multiblog_blog_post_count',
            [
                'label' => __('No. Of Posts (Max 8 Posts in Free version', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' =>4,
                'max' =>8,
            ]
        );

        $this->add_control(
            'multiblog_blog_post_offset',
            [
                'label' => __('Post Offset', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 0,
                'description' => __('Number of posts to skip', 'multiblog'),
            ]
        );

        $this->add_control(
            'show_image',
            [
                'label' => esc_html__( 'Show Featured Image', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_title',
            [
                'label' => esc_html__( 'Show Post Excerpt', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_author_meta',
            [
                'label' => esc_html__( 'Show Author Meta', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_date_meta',
            [
                'label' => esc_html__( 'Show Date Meta', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'columnno',
            [
                'label' => esc_html__( 'Select Column', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'fourcolumn',
                'options' => [
                    'twocolumn' => esc_html__( '2 Column', 'multiblog' ),
                    'threecolumn' => esc_html__( '3 Column', 'multiblog' ),
                    'fourcolumn' => esc_html__( '4 Column', 'multiblog' ),
                ]
            ]
        );

        $this->end_controls_section();

        // Heading style
        $this->start_controls_section(
            'multiblog_heading_style',
            [
                'label' => esc_html__('Heading Style', 'multiblog'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'heading_typography',
                'label' => __('Typography', 'multiblog'),
                'selector' => '{{WRAPPER}} .custom-blog-widget h3',
            ]
        );

        $this->add_control(
            'heading_color',
            [
                'label' => __('Heading Color', 'multiblog'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-blog-widget h3.entry-title a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Post Excerpt style
        $this->start_controls_section(
            'multiblog_excerpt_style',
            [
                'label' => esc_html__('Excerpt Style', 'multiblog'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'excerpt_typography',
                'label' => __('Typography', 'multiblog'),
                'selector' => '{{WRAPPER}} .post-excerpt',
            ]
        );
        
        $this->add_control(
            'excerpt_color',
            [
                'label' => __('Excerpt Color', 'multiblog'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-blog-widget p.post-excerpt' => 'color: {{VALUE}};',
                ],
            ]
        );       

        $this->end_controls_section();
    }
    
    protected function render() {
        $settings = $this->get_settings_for_display();
        $taxs = $settings['multiblog_blog_get_category'] ?? [];
        $posts_count = $settings['multiblog_blog_post_count'] ?? 4;
        $post_offset = $settings['multiblog_blog_post_offset'] ?? 0;
        $column_layout = $settings['columnno'] ?? 'threecolumn';

        $loop = ($posts_count <= 0) ? 8 : $posts_count;
        
        $args = [
            'post_type' => 'post',
            'posts_per_page' => esc_attr($loop),
            'offset' => esc_attr($post_offset),
            'post_status' => 'publish',
            'orderby' => 'menu_order date',
            'order' => 'desc',
        ];

        if (!empty($taxs)) {
            $args['tax_query'] = [
                [
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $taxs,
                ]
            ];
        }

        $query = new \WP_Query($args);

        if ($query->have_posts()): ?>
            <div class="custom-blog-widget <?php echo esc_attr($column_layout); ?>">
                <?php while ($query->have_posts()) : $query->the_post();
                    global $post;
                    $post_thumbnail_id = get_post_thumbnail_id(get_the_ID());
                    $image = wp_get_attachment_image_src($post_thumbnail_id, 'multiblog-blog-thumbnail-img');
                    
                    if($loop >= 1) : ?>
                        <article class="blog-post">
                            <?php if ('yes' === $settings['show_image'] && !empty($image)) : ?>
                                <div class="blog-img">
                                    <img src="<?php echo esc_url($image[0]); ?>" alt="<?php the_title_attribute(); ?>" />
                                </div>
                            <?php endif; ?>
                            
                            <div class="post-content">
                                <h3 class="entry-title"><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php the_title(); ?></a></h3>
                                
                                <?php if ('yes' === $settings['show_title']) : ?>
                                    <p class="post-excerpt"><?php echo wp_kses_post(multiblog_get_excerpt(get_the_ID(), 125)); ?></p>
                                <?php endif; ?>
                                
                                <?php if ('yes' === $settings['show_author_meta'] || 'yes' === $settings['show_date_meta']) : ?>
                                    <div class="entry-meta">
                                        <?php if ('yes' === $settings['show_author_meta']) : ?>
                                            <div class="author">
                                                <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>" title="<?php echo esc_attr(get_the_author()); ?>">
                                                    <span><?php the_author(); ?></span>
                                                </a>
                                            </div>
                                        <?php endif; ?>
                                        
                                        <?php if ('yes' === $settings['show_date_meta']) : ?>
                                            <div class="date">
                                                <a href="<?php echo esc_url(get_month_link(get_the_time('Y'), get_the_time('m'))); ?>">
                                                    <span><?php echo esc_html(get_the_date()); ?></span>
                                                </a>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </article>
                    <?php 
                    $loop--;
                    endif; 
                endwhile;
                wp_reset_postdata(); ?>
            </div>
        <?php endif;
    }
}

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