<?php

namespace GenixCore\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Image_Size;

if (!defined('ABSPATH')) exit; // Exit if accessed directly

/**
 * Bitrader Core
 *
 * Elementor widget for hello world.
 *
 * @since 1.0.0
 */
class Genix_Blog_Post extends Widget_Base
{

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

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('Blog Post', 'genixcore');
    }

    /**
     * Retrieve the widget icon.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon()
    {
        return 'genix-icon';
    }

    /**
     * Retrieve the list of categories the widget belongs to.
     *
     * Used to determine where to display the widget in the editor.
     *
     * Note that currently Elementor supports only one category.
     * When multiple categories passed, Elementor uses the first one.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget categories.
     */
    public function get_categories()
    {
        return ['genixcore'];
    }

    /**
     * Retrieve the list of scripts the widget depended on.
     *
     * Used to set scripts dependencies required to run the widget.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget scripts dependencies.
     */
    public function get_script_depends()
    {
        return ['genixcore'];
    }

    /**
     * Register the 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()
    {

        // layout Panel
        $this->start_controls_section(
            'genix_layout',
            [
                'label' => esc_html__('Design Layout', 'genixcore'),
            ]
        );
        $this->add_control(
            'tg_design_style',
            [
                'label' => esc_html__('Select Layout', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'layout-1' => esc_html__('Layout 1', 'genixcore'),
                    'layout-2' => esc_html__('Layout 2', 'genixcore'),
                    'layout-3' => esc_html__('Layout 3', 'genixcore'),
                ],
                'default' => 'layout-1',
            ]
        );

        $this->end_controls_section();


        // h5 Blog section area field 
        $this->start_controls_section(
            '__tg_blogs_headerh5',
            [
                'label' => esc_html__('Blog Section Header', 'genixcore'),
                'condition' => [
                    'tg_design_style' => 'layout-3'
                ]
            ]
        );
        $this->add_control(
            'blogs_sectiontitle',
            [
                'label' => esc_html__('Blog Title', 'genixcore'),
                'type' => Controls_Manager::TEXT,
                'default' => wp_kses_post('Articles For Pro Traders', 'genixcore'),
                'label_block' => true,
            ]
        );
        $this->add_control(
            'blogs_shorttitle',
            [
                'label' => esc_html__('Blog Short Description', 'genixcore'), 
                'type' => Controls_Manager::TEXTAREA,
                'default' => wp_kses_post('Hey there pro traders, check out these articles with tips to take your trading game to the next level!', 'genixcore'),
                'label_block' => true,
            ]
        );

        $this->end_controls_section(); 


        // h5 Blog section BG field 
        $this->start_controls_section(
            '__tg_blogs_bgh5',
            [
                'label' => esc_html__('Blog Section Background', 'genixcore'),
                'condition' => [
                    'tg_design_style' => 'layout-3'
                ]
            ]
        );
        $this->add_control(
            'blogs_sectionbg',
            [
                'label' => esc_html__('Blog Background', 'genixcore'),
                'type' => Controls_Manager::MEDIA,
            ]
        );

        $this->end_controls_section(); 










        // Blog Query
        $this->start_controls_section(
            'genix_post_query',
            [
                'label' => esc_html__('Blog Query', 'genixcore'),
            ]
        );

        $post_type = 'post';
        $taxonomy = 'category';

        $this->add_control(
            'posts_per_page',
            [
                'label' => esc_html__('Posts Per Page', 'genixcore'),
                'description' => esc_html__('Leave blank or enter -1 for all.', 'genixcore'),
                'type' => Controls_Manager::NUMBER,
                'default' => '3',
            ]
        );

        $this->add_control(
            'category',
            [
                'label' => esc_html__('Include Categories', 'genixcore'),
                'description' => esc_html__('Select a category to include or leave blank for all.', 'genixcore'),
                'type' => Controls_Manager::SELECT2,
                'multiple' => true,
                'options' => genix_get_categories($taxonomy),
                'label_block' => true,
            ]
        );

        $this->add_control(
            'exclude_category',
            [
                'label' => esc_html__('Exclude Categories', 'genixcore'),
                'description' => esc_html__('Select a category to exclude', 'genixcore'),
                'type' => Controls_Manager::SELECT2,
                'multiple' => true,
                'options' => genix_get_categories($taxonomy),
                'label_block' => true
            ]
        );

        $this->add_control(
            'post__not_in',
            [
                'label' => esc_html__('Exclude Item', 'genixcore'),
                'type' => Controls_Manager::SELECT2,
                'options' => genix_get_all_types_post($post_type),
                'multiple' => true,
                'label_block' => true
            ]
        );

        $this->add_control(
            'offset',
            [
                'label' => esc_html__('Offset', 'genixcore'),
                'type' => Controls_Manager::NUMBER,
                'default' => '0',
            ]
        );

        $this->add_control(
            'orderby',
            [
                'label' => esc_html__('Order By', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'options' => array(
                    'ID' => 'Post ID',
                    'author' => 'Post Author',
                    'title' => 'Title',
                    'date' => 'Date',
                    'modified' => 'Last Modified Date',
                    'parent' => 'Parent Id',
                    'rand' => 'Random',
                    'comment_count' => 'Comment Count',
                    'menu_order' => 'Menu Order',
                ),
                'default' => 'date',
            ]
        );

        $this->add_control(
            'order',
            [
                'label' => esc_html__('Order', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'asc'     => esc_html__('Ascending', 'genixcore'),
                    'desc'     => esc_html__('Descending', 'genixcore')
                ],
                'default' => 'desc',

            ]
        );

        $this->add_control(
            'ignore_sticky_posts',
            [
                'label' => esc_html__('Ignore Sticky Posts', 'genixcore'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Yes', 'genixcore'),
                'label_off' => esc_html__('No', 'genixcore'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'genix_blog_title_word',
            [
                'label' => esc_html__('Title Word Count', 'genixcore'),
                'description' => esc_html__('Set how many word you want to display!', 'genixcore'),
                'type' => Controls_Manager::NUMBER,
                'default' => '6',
            ]
        );

        $this->add_control(
            'genix_post_content',
            [
                'label' => esc_html__('Content', 'genixcore'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Show', 'genixcore'),
                'label_off' => esc_html__('Hide', 'genixcore'),
                'return_value' => 'yes',
                'default' => '',
            ]
        );

        $this->add_control(
            'genix_post_content_limit',
            [
                'label' => esc_html__('Content Limit', 'genixcore'),
                'type' => Controls_Manager::TEXT,
                'label_block' => true,
                'default' => '12',
                'dynamic' => [
                    'active' => true,
                ],
                'condition' => [
                    'genix_post_content' => 'yes'
                ]
            ]
        );

        $this->add_group_control(
            Group_Control_Image_Size::get_type(),
            [
                'name' => 'thumbnail',
                'exclude' => ['custom'],
                'default' => 'full',
            ]
        );

        $this->end_controls_section();



        // style control
        $this->start_controls_section(
            'section_style',
            [
                'label' => esc_html__('Style', 'genixcore'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'text_transform',
            [
                'label' => esc_html__('Text Transform', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'default' => '',
                'options' => [
                    '' => esc_html__('None', 'genixcore'),
                    'uppercase' => esc_html__('UPPERCASE', 'genixcore'),
                    'lowercase' => esc_html__('lowercase', 'genixcore'),
                    'capitalize' => esc_html__('Capitalize', 'genixcore'),
                ],
                'selectors' => [
                    '{{WRAPPER}} .title' => 'text-transform: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();




        // blog button area of bitrader ===================
        $this->start_controls_section(
            'tg_button_group',
            [
                'label' => esc_html__('Button', 'genixcore'),
                'condition' => [
                    'tg_design_style' => 'layout-3'
                ]
            ]
        );

        $this->add_control(
            'tg_button_show',
            [
                'label' => esc_html__('Show Button', 'genixcore'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Show', 'genixcore'),
                'label_off' => esc_html__('Hide', 'genixcore'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'tg_btn_text',
            [
                'label' => esc_html__('Button Text', 'genixcore'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Button Text', 'genixcore'),
                'title' => esc_html__('Enter button text', 'genixcore'),
                'label_block' => true,
                'condition' => [
                    'tg_button_show' => 'yes'
                ],
            ]
        );

        $this->add_control(
            'tg_btn_link_type',
            [
                'label' => esc_html__('Button Link Type', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    '1' => 'Custom Link',
                    '2' => 'Internal Page',
                ],
                'default' => '1',
                'label_block' => true,
                'condition' => [
                    'tg_button_show' => 'yes'
                ],
            ]
        );

        $this->add_control(
            'tg_btn_link',
            [
                'label' => esc_html__('Button link', 'genixcore'),
                'type' => Controls_Manager::URL,
                'dynamic' => [
                    'active' => true,
                ],
                'placeholder' => esc_html__('https://your-link.com', 'genixcore'),
                'show_external' => false,
                'default' => [
                    'url' => '#',
                    'is_external' => true,
                    'nofollow' => true,
                    'custom_attributes' => '',
                ],
                'condition' => [
                    'tg_btn_link_type' => '1',
                    'tg_button_show' => 'yes'
                ],
                'label_block' => true,
            ]
        );
        $this->add_control(
            'tg_btn_page_link',
            [
                'label' => esc_html__('Select Button Page', 'genixcore'),
                'type' => Controls_Manager::SELECT2,
                'label_block' => true,
                'options' => genix_get_all_pages(),
                'condition' => [
                    'tg_btn_link_type' => '2',
                    'tg_button_show' => 'yes'
                ]
            ]
        );

        $this->add_responsive_control(
            'tg_align',
            [
                'label' => esc_html__('Alignment', 'genixcore'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => esc_html__('Left', 'genixcore'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__('Center', 'genixcore'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => esc_html__('Right', 'genixcore'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'left',
                'toggle' => false,
                'selectors' => [
                    '{{WRAPPER}}' => 'text-align: {{VALUE}};'
                ]
            ]
        );

        $this->end_controls_section();


        // button style tabe of bitrader ================
        $this->start_controls_section(
            'section_style_home5',
            [
                'label' => esc_html__('Style', 'genixcore'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'btn_padding',
            [
                'label' => esc_html__('Button Padding', 'genixcore'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .trk-btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'bg_color',
            [
                'label' => esc_html__('Background Color', 'genixcore'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .trk-btn' => 'background: {{VALUE}}',
                    '{{WRAPPER}} .trk-btn--primary' => 'border-color: {{VALUE}}',
                    '{{WRAPPER}} .trk-btn--outline22' => 'border-color: {{VALUE}}',
                ],
            ]
        );

        $this->add_control(
            'btn_color',
            [
                'label' => esc_html__('Text Color', 'genixcore'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .trk-btn' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_control(
            'bg_hover_color',
            [
                'label' => esc_html__('Hover Background Color', 'genixcore'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .trk-btn:hover' => 'background: {{VALUE}}',
                    '{{WRAPPER}} .trk-btn--primary:hover' => 'border-color: {{VALUE}}',
                    '{{WRAPPER}} .trk-btn--outline22:hover' => 'border-color: {{VALUE}}',
                ],
            ]
        );

        $this->add_control(
            'btn_hover_color',
            [
                'label' => esc_html__('Hover Text Color', 'genixcore'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .trk-btn:hover' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_control(
            'text_transform_home5',
            [
                'label' => esc_html__('Text Transform', 'genixcore'),
                'type' => Controls_Manager::SELECT,
                'default' => '',
                'options' => [
                    '' => esc_html__('None', 'genixcore'),
                    'uppercase' => esc_html__('UPPERCASE', 'genixcore'),
                    'lowercase' => esc_html__('lowercase', 'genixcore'),
                    'capitalize' => esc_html__('Capitalize', 'genixcore'),
                ],
                'selectors' => [
                    '{{WRAPPER}} .trk-btn' => 'text-transform: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();



        // Bittrade button bg color
        $this->start_controls_section(
            '_bittrade_btn_bg_colorsss',
            [
                'label' => esc_html__('Bittrade Button BG Color', 'genixcore'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );
        // Banner button bg color
        $this->add_control(
            'bittrade_btn_style11',
            [
                'label' => esc_html__( 'Button BG Color', 'genixcore' ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#3863E0',
                'selectors' => [
                    '{{WRAPPER}} .myclass-forblogbtn a' => 'background-color: {{VALUE}}',
                ],
            ]
        );


        $this->end_controls_section();



    }

    /**
     * Render the 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();

        if (get_query_var('paged')) {
            $paged = get_query_var('paged');
        } else if (get_query_var('page')) {
            $paged = get_query_var('page');
        } else {
            $paged = 1;
        }

        // include_categories
        $category_list = '';
        if (!empty($settings['category'])) {
            $category_list = implode(", ", $settings['category']);
        }
        $category_list_value = explode(" ", $category_list);

        // exclude_categories
        $exclude_categories = '';
        if (!empty($settings['exclude_category'])) {
            $exclude_categories = implode(", ", $settings['exclude_category']);
        }
        $exclude_category_list_value = explode(" ", $exclude_categories);

        $post__not_in = '';
        if (!empty($settings['post__not_in'])) {
            $post__not_in = $settings['post__not_in'];
            $args['post__not_in'] = $post__not_in;
        }
        $posts_per_page = (!empty($settings['posts_per_page'])) ? $settings['posts_per_page'] : '-1';
        $orderby = (!empty($settings['orderby'])) ? $settings['orderby'] : 'post_date';
        $order = (!empty($settings['order'])) ? $settings['order'] : 'desc';
        $offset_value = (!empty($settings['offset'])) ? $settings['offset'] : '0';
        $ignore_sticky_posts = (!empty($settings['ignore_sticky_posts']) && 'yes' == $settings['ignore_sticky_posts']) ? true : false;


        // number
        $off = (!empty($offset_value)) ? $offset_value : 0;
        $offset = $off + (($paged - 1) * $posts_per_page);
        $p_ids = array();

        // build up the array
        if (!empty($settings['post__not_in'])) {
            foreach ($settings['post__not_in'] as $p_idsn) {
                $p_ids[] = $p_idsn;
            }
        }

        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page,
            'orderby' => $orderby,
            'order' => $order,
            'offset' => $offset,
            'paged' => $paged,
            'post__not_in' => $p_ids,
            'ignore_sticky_posts' => $ignore_sticky_posts
        );

        // exclude_categories
        if (!empty($settings['exclude_category'])) {

            // Exclude the correct cats from tax_query
            $args['tax_query'] = array(
                array(
                    'taxonomy'    => 'category',
                    'field'         => 'slug',
                    'terms'        => $exclude_category_list_value,
                    'operator'    => 'NOT IN'
                )
            );

            // Include the correct cats in tax_query
            if (!empty($settings['category'])) {
                $args['tax_query']['relation'] = 'AND';
                $args['tax_query'][] = array(
                    'taxonomy'    => 'category',
                    'field'        => 'slug',
                    'terms'        => $category_list_value,
                    'operator'    => 'IN'
                );
            }
        } else {
            // Include the cats from $cat_slugs in tax_query
            if (!empty($settings['category'])) {
                $args['tax_query'][] = [
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => $category_list_value,
                ];
            }
        }

        $filter_list = $settings['category'];

        // The Query
        $query = new \WP_Query($args); ?>



        <?php if ($settings['tg_design_style'] == 'layout-2') : ?>

            <script>
                jQuery(document).ready(function($) {

                    AOS.init();

                });
            </script>

            <?php if ($query->have_posts()) : ?>
                <div class="blog__wrapper">
                    <div class="row g-4">
                        <?php while ($query->have_posts()) :
                            $query->the_post();
                            global $post;
                            $categories = get_the_category($post->ID);
                        ?>
                            <div class="col-md-6">
                                <div class="blog__item blog__item--style2" data-aos="fade-right" data-aos-duration="1000">
                                    <div class="blog__item-inner blog__item-inner--style2 pt-30 px-30 pb-40">
                                        <?php if (has_post_thumbnail()) : ?>
                                            <div class="blog__thumb">
                                                <?php the_post_thumbnail($post->ID, $settings['thumbnail_size']); ?>
                                            </div>
                                        <?php endif; ?>
                                        <div class="blog__content mt-30">
                                            <h5 class="style2">
                                                <a href="<?php the_permalink(); ?>"><?php echo wp_trim_words(get_the_title(), $settings['genix_blog_title_word'], ''); ?></a>
                                            </h5>

                                            <?php if (!empty($settings['genix_post_content'])) :
                                                $genix_post_content_limit = (!empty($settings['genix_post_content_limit'])) ? $settings['genix_post_content_limit'] : '';
                                            ?>
                                                <p class="mb-15"><?php print wp_trim_words(get_the_excerpt(get_the_ID()), $genix_post_content_limit, ''); ?></p>
                                            <?php endif; ?>

                                            <a href="<?php the_permalink(); ?>" class="text-btn text-btn--style2"><?php echo esc_html__('Read more', 'genixcore') ?> <i class="fas fa-angle-right"></i></a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endwhile;
                        wp_reset_query(); ?>
                    </div>
                </div>
            <?php endif; ?>



        <?php elseif ($settings['tg_design_style'] == 'layout-3') : ?>

        <script>
            jQuery(document).ready(function($) {

                AOS.init();

                // blog  slider here
                const blog = new Swiper('.blog__slider3', {
                    spaceBetween: 24,
                    grabCursor: true,
                    loop: true,
                    slidesPerView: 1,
                    breakpoints: {
                        576: {
                            slidesPerView: 1,
                        },
                        768: {
                            slidesPerView: 2,
                        },
                        992: {
                            slidesPerView: 3,
                        },
                        1200: {
                            slidesPerView: 3,
                        }
                    },

                    autoplay: true,
                    speed: 500,
                    navigation: {
                        nextEl: ".blog__slider-next",
                        prevEl: ".blog__slider-prev",
                    },
                });

            });
        </script>

        <?php if ($query->have_posts()) : ?>
        <section class="blog padding-top padding-bottom bg-color">
            <div class="blog__bg"> 
              <div class="blog__bg-element">
                <?php
                $blogs_sectionbg = wp_get_attachment_url( $settings['blogs_sectionbg']['id'], 'full' );
                if (!empty($blogs_sectionbg)) : 
                ?>
                <img src="<?php echo esc_url($blogs_sectionbg); ?>" alt="<?php echo esc_attr__('Thumbnail', 'genixcore') ?>"  class="dark">
                <?php endif; ?>
              </div>
            </div>
            <div class="container">
              <div class="section-header d-md-flex align-items-center justify-content-between">
                <div class="section-header__content"> 
                    <?php  if(!empty($settings['blogs_sectiontitle'])): ?>
                        <h2 class="mb-10"><?php echo esc_html($settings['blogs_sectiontitle']); ?></h2>
                    <?php endif;  ?>
                    <?php  if(!empty($settings['blogs_shorttitle'])): ?>
                        <p class="mb-0"><?php echo esc_html($settings['blogs_shorttitle']); ?></p>
                    <?php endif;  ?>
                </div>
                <div class="section-header__action">
                  <div class="swiper-nav swiper-nav--style1">
                    <button class="swiper-nav__btn swiper-nav__btn--style3 blog__slider-prev"><i
                        class="fa-solid fa-angle-left"></i></button>
                    <button class="swiper-nav__btn swiper-nav__btn--style3 blog__slider-next active"><i
                        class="fa-solid fa-angle-right"></i></button>
                  </div>
                </div>
              </div>
              <div class="blog__wrapper" data-aos="fade-up" data-aos-duration="1000">
                <div class="blog__slider3 swiper">
                  <div class="swiper-wrapper">

                    <?php while ($query->have_posts()) :
                        $query->the_post();
                        global $post;
                        $categories = get_the_category($post->ID);
                    ?>
                    <div class="swiper-slide">
                      <div class="blog__item blog__item--style3">
                        <div class="blog__item-inner">
                            <?php if (has_post_thumbnail()) : ?>
                                <div class="blog__thumb">
                                    <?php the_post_thumbnail($post->ID, $settings['thumbnail_size']); ?>
                                </div>
                            <?php endif; ?>
                          <div class="blog__content">
                            <div class="blog__meta">                       
                                <?php if (!empty($categories[0]->name)) : ?>
                                    <span class="blog__meta-tag blog__meta-tag--style1"><?php echo esc_html($categories[0]->name); ?></span>
                                <?php endif; ?>
                            </div>

                            <h5 class="10">
                                <a href="<?php the_permalink(); ?>"><?php echo wp_trim_words(get_the_title(), $settings['genix_blog_title_word'], ''); ?></a>
                            </h5>
                            <?php if (!empty($settings['genix_post_content'])) :
                                $genix_post_content_limit = (!empty($settings['genix_post_content_limit'])) ? $settings['genix_post_content_limit'] : '';
                            ?>
                                <p class="mb-15"><?php print wp_trim_words(get_the_excerpt(get_the_ID()), $genix_post_content_limit, ''); ?></p>
                            <?php endif; ?>
                            <div class="blog__writer">
                                <div class="blog__writer-thumb">
                                    <img src="<?php echo esc_url(get_avatar_url(get_the_author_meta('ID'), ['size' => '40'])); ?>" alt="<?php the_author(); ?>">
                                </div>
                                <div class="blog__writer-designation">
                                    <h6 class="mb-0"><?php print get_the_author(); ?></h6>
                                    <span><?php the_time(get_option('date_format')); ?></span>
                                </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <?php endwhile;
                    wp_reset_query();
                    ?>

                  </div>
                </div>
              </div>

              <?php  
            // blog button 
            if ('2' == $settings['tg_btn_link_type']) {
                $this->add_render_attribute('tg-button-arg', 'href', get_permalink($settings['tg_btn_page_link']));
                $this->add_render_attribute('tg-button-arg', 'target', '_self');
                $this->add_render_attribute('tg-button-arg', 'rel', 'nofollow');
                $this->add_render_attribute('tg-button-arg', 'class', 'trk-btn trk-btn--tertiary mt-40');
            } else {
                if (!empty($settings['tg_btn_link']['url'])) {
                    $this->add_link_attributes('tg-button-arg', $settings['tg_btn_link']);
                    $this->add_render_attribute('tg-button-arg', 'class', 'trk-btn trk-btn--tertiary mt-40');
                }
            }
            ?>



            <?php if (!empty($settings['tg_button_show'])) : ?>
              <div class="text-center myclass-forblogbtn">
               <a <?php echo $this->get_render_attribute_string('tg-button-arg'); ?>>
                        <?php echo $settings['tg_btn_text']; ?>
                    </a>
              </div>
            <?php endif; ?>

              
  



            </div>
        </section>
        <?php endif; ?>



        <?php else : ?>


            <script>
                jQuery(document).ready(function($) {

                    AOS.init();

                    // blog  slider here
                    const blog = new Swiper('.blog__slider', {
                        spaceBetween: 24,
                        grabCursor: true,
                        loop: true,
                        slidesPerView: 1,
                        breakpoints: {
                            576: {
                                slidesPerView: 1,
                            },
                            768: {
                                slidesPerView: 2,
                            },
                            992: {
                                slidesPerView: 3,
                            },
                            1200: {
                                slidesPerView: 3,
                            }
                        },

                        autoplay: true,
                        speed: 500,
                        navigation: {
                            nextEl: ".blog__slider-next",
                            prevEl: ".blog__slider-prev",
                        },
                    });

                });
            </script>

            <?php if ($query->have_posts()) : ?>
                <div class="blog__wrapper" data-aos="fade-up" data-aos-duration="1000">
                    <div class="blog__slider swiper">
                        <div class="swiper-wrapper">
                            <?php while ($query->have_posts()) :
                                $query->the_post();
                                global $post;
                                $categories = get_the_category($post->ID);
                            ?>
                                <div class="swiper-slide">
                                    <div class="blog__item home-blog-post blog__item--style2">
                                        <div class="blog__item-inner">
                                            <?php if (has_post_thumbnail()) : ?>
                                                <div class="blog__thumb">
                                                    <?php the_post_thumbnail($post->ID, $settings['thumbnail_size']); ?>
                                                </div>
                                            <?php endif; ?>
                                            <div class="blog__content">
                                                <?php if (!empty($categories[0]->name)) : ?>
                                                    <div class="blog__meta">
                                                        <a href="<?php print esc_url(get_category_link($categories[0]->term_id)); ?>" class="blog__meta-tag blog__meta-tag--style1"><?php echo esc_html($categories[0]->name); ?></a>
                                                    </div>
                                                <?php endif; ?>
                                                <h5 class="10">
                                                    <a href="<?php the_permalink(); ?>"><?php echo wp_trim_words(get_the_title(), $settings['genix_blog_title_word'], ''); ?></a>
                                                </h5>

                                                <?php if (!empty($settings['genix_post_content'])) :
                                                    $genix_post_content_limit = (!empty($settings['genix_post_content_limit'])) ? $settings['genix_post_content_limit'] : '';
                                                ?>
                                                    <p class="mb-15"><?php print wp_trim_words(get_the_excerpt(get_the_ID()), $genix_post_content_limit, ''); ?></p>
                                                <?php endif; ?>

                                                <div class="blog__writer">
                                                    <div class="blog__writer-thumb">
                                                        <img src="<?php echo esc_url(get_avatar_url(get_the_author_meta('ID'), ['size' => '40'])); ?>" alt="<?php the_author(); ?>">
                                                    </div>
                                                    <div class="blog__writer-designation">
                                                        <h6 class="mb-0"><?php print get_the_author(); ?></h6>
                                                        <span><?php the_time(get_option('date_format')); ?></span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endwhile;
                            wp_reset_query(); ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>







        <?php endif; ?>

<?php
    }
}

$widgets_manager->register(new Genix_Blog_Post());
