<?php
if (!defined('ABSPATH')) {
exit;
}

use Elementor\Controls_Manager;
use Elementor\Plugin;
use Elementor\Utils;
use Elementor\Widget_Base;

class Blogs extends Widget_Base {

public function get_name() {
return 'dexen_blogs';
}

public function get_title() {
return esc_html__('Blog', 'dexen-core');
}

public function get_icon() {
return 'eicon-post';
}

public function get_categories() {
return ['dexencore'];
}

private function get_blog_categories() {
$options = array();
$taxonomy = 'category';
if (!empty($taxonomy)) {
    $terms = get_terms(
        array(
            'parent' => 0,
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
        )
    );
    if (!empty($terms)) {
        foreach ($terms as $term) {
            if (isset($term)) {
                $options[''] = 'Select';
                if (isset($term->slug) && isset($term->name)) {
                    $options[$term->slug] = $term->name;
                }
            }
        }
    }
}
return $options;
}

protected function _register_controls() {

$this->start_controls_section(
    'section_blogs', [
        'label' => esc_html__('Blogs', 'dexen-core'),
    ]
);

$this->add_control(
    'title_1', [
        'label' => esc_html__('Title 1', 'dexen-core'),
        'type' => Controls_Manager::TEXT,
        'default' => esc_html__('Our Blog Posts', 'dexen-core'),
    ]
);

$this->add_control(
    'title_2', [
        'label' => esc_html__('Title 2', 'dexen-core'),
        'type' => Controls_Manager::TEXTAREA,
        'default' => 'News &amp; Articles'
    ]
);

$this->add_control(
    'category_id', [
        'type' => \Elementor\Controls_Manager::SELECT,
        'label' => esc_html__('Category', 'dexen-core'),
        'options' => $this->get_blog_categories()
    ]
);

    $this->add_control(
    'number', [
'label' => esc_html__('Number of Post', 'bebio-core'),
'type' => Controls_Manager::TEXT,
'default' => 2
    ]
);


$this->add_control(
    'column', 
    [
        'label' => esc_html__('Number of Column', 'dexen-core'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => '2',
        'options' => [
            '2' => esc_html__('2', 'dexen-core'),
            '3' => esc_html__('3', 'dexen-core'),
            '4' => esc_html__('4', 'dexen-core'),
        ]
    ]
);

$this->add_control(
    'order_by', [
        'label' => esc_html__('Order By', 'dexen-core'),
        'type' => Controls_Manager::SELECT,
        'default' => 'date',
        'options' => [
            'date' => esc_html__('Date', 'dexen-core'),
            'ID' => esc_html__('ID', 'dexen-core'),
            'author' => esc_html__('Author', 'dexen-core'),
            'title' => esc_html__('Title', 'dexen-core'),
            'modified' => esc_html__('Modified', 'dexen-core'),
            'rand' => esc_html__('Random', 'dexen-core'),
            'comment_count' => esc_html__('Comment count', 'dexen-core'),
            'menu_order' => esc_html__('Menu order', 'dexen-core')
        ]
    ]
);

$this->add_control(
    'order', [
        'label' => esc_html__('Order', 'dexen-core'),
        'type' => Controls_Manager::SELECT,
        'default' => 'desc',
        'options' => [
            'desc' => esc_html__('DESC', 'dexen-core'),
            'asc' => esc_html__('ASC', 'dexen-core')
        ]
    ]
);
$this->end_controls_section();
}

protected function render() {
$settings = $this->get_settings();
$posts_per_page = $settings['number'];
$order_by = $settings['order_by'];
$order = $settings['order'];
$pg_num = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
    'post_type' => array('post'),
    'post_status' => array('publish'),
    'nopaging' => false,
    'paged' => $pg_num,
    'posts_per_page' => $posts_per_page,
    'category_name' => $settings['category_id'],
    'orderby' => $order_by,
    'order' => $order,
);
$query = new WP_Query($args);

$columnClass = '';
$column =    $settings["column"];
switch ($column) {
    case "3":
    $columnClass = 'col-lg-4';
    break;
    case "4":
    $columnClass = 'col-lg-3';
    break;
    default:
    $columnClass = 'col-lg-6';
}

?>
<section class="blog-one sec-pad sec-pad-content-margin-80">
    <div class="container">
        <div class="block-title text-center">
            <p class="block-title__tag-line ">
                <?php
                echo wp_kses_post($settings['title_1']);
                ?></p>
                <h2 class="block-title__title">
                    <?php
                    echo wp_kses_post($settings['title_2']);
                    ?></h2>
                </div><!-- /.block-title -->
                <div class="row">
                    <?php
                    $i = 0;
                    if ($query->have_posts()) {
                        while ($query->have_posts()) {
                            $query->the_post();?>
                            <div class="<?php echo esc_attr($columnClass); ?> ">
                                <div class="blog-one__single content-margin-80 text-center">
                                    <div class="blog-one__image-block">
                                        <?php
                                        if ( has_post_thumbnail() ) {
                                            echo get_the_post_thumbnail(get_the_ID(), '');
                                        } ?>
                                    </div><!-- /.blog-one__image-block -->
                                    <div class="blog-one__text-block thm-gray-bg">
                                        <div class="blog-one__meta">
                                        <a class="blog-one__meta-item" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) )?>"><i class="fa fa-user"></i><?php echo esc_html__('by','dexen-core');?> <?php echo get_the_author();?></a>
                                            <a class="blog-one__meta-item" href="<?php echo esc_url(get_permalink());?>"><i class="fa fa-calendar"></i><?php echo get_the_date('d m, Y');?></a>
                                            <?php dexen_comments_count(); ?>
                                        </div><!-- /.blog-one__meta -->
                                        <h3 class="blog-one__title"><a href="<?php echo the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                        <p class="blog-one__text">
                                            <?php the_excerpt();?>
                                        </p><!-- /.blog-one__text -->
                                        <a href="<?php echo the_permalink(); ?>" class="blog-one__link"><i class="fa fa-angle-right"></i></a>
                                    </div><!-- /.blog-one__text-block -->
                                </div><!-- /.blog-one__single -->
                            </div><!-- /.col-lg-6 -->
                            <?php
                            $i++;
                        }
                        wp_reset_postdata();
                    } ?>
                </div><!-- /.row -->
            </div><!-- /.container -->
        </section>
        <?php
    }
    protected function content_template() {
    }
}
Plugin::instance()->widgets_manager->register_widget_type(new Blogs());