<?php



use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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


class Blogcarouselkaveh extends Widget_Base{

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

  public function get_title(){
    return esc_html__( 'Blog Carousel Element', 'kavehTheme' );
  }

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

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

  protected function register_controls(){
    
    $this->start_controls_section(
        'contentblogcarouselkaveh',
        [
          'label' => esc_html__( 'Element Content', 'kavehTheme' ),
                  'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
        ]
      );
  
      $this->add_control(
          'title1blogcarouselkaveh',
          [
              'label' => esc_html__( 'Before Title', 'kavehTheme' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__( 'Latest', 'kavehTheme' ),
      
          ]
      );
      $this->add_control(
        'title2blogcarouselkaveh',
        [
            'label' => esc_html__( 'Title', 'kavehTheme' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__( 'Articles', 'kavehTheme' ),
    
        ]
    );
    $this->add_control(
      'title3blogcarouselkaveh',
      [
          'label' => esc_html__( 'After Title', 'kavehTheme' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__( 'Educational', 'kavehTheme' ),
  
      ]
  );
  $this->add_control(
    'tagheadingblogcarouselkaveh',
    [
      'label' => esc_html__( 'Title Tag', 'kavehTheme' ),
      'type' => \Elementor\Controls_Manager::SELECT,
      'default' => 'H2',
      'options' => [
        'H1'  => esc_html__( 'H1', 'kavehTheme' ),
        'H2' => esc_html__( 'H2', 'kavehTheme' ),
        'H3' => esc_html__( 'H3', 'kavehTheme' ),
        'H4' => esc_html__( 'H4', 'kavehTheme' ),
        'H5' => esc_html__( 'H5', 'kavehTheme' ),
        'H6' => esc_html__( 'H6', 'kavehTheme' ),
        'p' => esc_html__( 'p', 'kavehTheme' ),
        'div' => esc_html__( 'div', 'kavehTheme' ),
        'span' => esc_html__( 'span', 'kavehTheme' ),
      ],
    ]
  );
      $this->add_control(
        'moreblogcarouselkaveh',
        [
          'label' => esc_html__( 'Show More Button?', 'kavehTheme' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'yes' => esc_html__( 'Yes', 'kavehTheme' ),
                'no' => esc_html__( 'No', 'kavehTheme' ),
          'return_value' => 'yes',
          'default' => 'yes',
        ]
      );
      $this->add_control(
        'buttonblogcarouselkaveh',
        [
            'label' => esc_html__( 'Button', 'kavehTheme' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__( 'Read More', 'kavehTheme' ),
            'condition' => [
              'moreblogcarouselkaveh' => 'yes',
            ],
    
        ]
    );
    $this->add_control(
      'buttonlinkblogcarouselkaveh',
      [
          'label' => esc_html__( 'Button Link', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::TEXT,
          'default' => esc_html__( '#', 'kavehTheme' ),
          'condition' => [
            'moreblogcarouselkaveh' => 'yes',
          ],
  
      ]
  );
  $this->end_controls_section();
  $this->start_controls_section(
    'content2blogcarouselkaveh',
    [
      'label' => esc_html__( 'Blog Content', 'kavehTheme' ),
              'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
  );

  $this->add_control(
    'styleblogcarouselkaveh',
    [
      'label' => esc_html__( 'Blog Layout', 'kavehTheme' ),
      'type' => \Elementor\Controls_Manager::SELECT,
      'default' => 'two',
      'options' => [
        'one'  => esc_html__( 'Layout One', 'kavehTheme' ),
        'two'  => esc_html__( 'Layout Two', 'kavehTheme' ),
        'three'  => esc_html__( 'Layout Three', 'kavehTheme' ),
        'four'  => esc_html__( 'Layout Four', 'kavehTheme' ),
        'five'  => esc_html__( 'Layout Five', 'kavehTheme' ),
        'six'  => esc_html__( 'Layout Six', 'kavehTheme' ),
        'seven'  => esc_html__( 'Layout Seven', 'kavehTheme' ),
        'eight'  => esc_html__( 'Layout Eight', 'kavehTheme' ),
        'nine'  => esc_html__( 'Layout Nine', 'kavehTheme' ),
        'ten'  => esc_html__( 'Layout Ten', 'kavehTheme' ),
        'eleven'  => esc_html__( 'Layout Eleven', 'kavehTheme' ),
        'twelve'  => esc_html__( 'Layout Twelve', 'kavehTheme' ),
      ],
    ]
    );


  $optionsblog = array();
  $categories = get_categories(array(
    'hide_empty' => false,
  )
  ); 
  foreach ( $categories as $key => $category ) {
      $optionsblog[$category->term_id] = $category->name;
  }
  $this->add_control(
      'catblogcarouselkaveh',
      [
        'label' => esc_html__( 'Select Category', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::SELECT2, 
        'multiple' => true,
        'options' => $optionsblog,
      ]
  );
  $this->end_controls_section();
  $this->start_controls_section(
    'content3blogcarouselkaveh',
    [
      'label' => esc_html__( 'Display Settings', 'kavehTheme' ),
              'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
  );
$this->add_control(
  'productnumberblogcarouselkaveh',
  [
    'label' => esc_html__( 'Number of displayed products', 'kavehTheme' ),
    'type' => \Elementor\Controls_Manager::NUMBER,
    'min' => 0,
    'max' => 20,
    'step' => 1,
    'default' => 5,
  ]
);
$this->add_control(
  'productcolumnblogcarouselkaveh',
  [
    'label' => esc_html__( 'Number of product columns on desktop', 'kavehTheme' ),
    'type' => \Elementor\Controls_Manager::NUMBER,
    'min' => 0,
    'max' => 5,
    'step' => 1,
    'default' => 3,

  ]
);
$this->add_control(
  'productcolumn2blogcarouselkaveh',
  [
    'label' => esc_html__( 'Number of product columns on tablet', 'kavehTheme' ),
    'type' => \Elementor\Controls_Manager::NUMBER,
    'min' => 0,
    'max' => 5,
    'step' => 1,
    'default' => 3,

  ]
);
$this->add_control(
  'productcolumn3blogcarouselkaveh',
  [
    'label' => esc_html__( 'Number of product columns on mobile', 'kavehTheme' ),
    'type' => \Elementor\Controls_Manager::NUMBER,
    'min' => 0,
    'max' => 5,
    'step' => 1,
    'default' => 1,

  ]
);
       $this->end_controls_section();
       $this->start_controls_section(
        'style1_blogcarouselkaveh',
        [
          'label' => esc_html__( 'Title Style', 'kavehTheme' ),
          'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        ]
      );
      $this->add_control(
        'title3stblogcarouselkaveh',
        [
          'label' => esc_html__( 'Title Color', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::COLOR,
          'selectors' => [
            '{{WRAPPER}} .new-article .heading .title' => 'color: {{VALUE}}',
            '{{WRAPPER}} .new-article .heading .title mark' => 'color: {{VALUE}}',

          ],
        ]
      );
      $this->add_group_control(
        \Elementor\Group_Control_Typography::get_type(),
        [
          'name' => 'title3_blogcarouselkaveh',
          'label' => esc_html__( 'Title Typography', 'kavehTheme' ),
          'selector' => '{{WRAPPER}} .new-article .heading .title',
        ]
      );

      $this->add_control(
        'title32stblogcarouselkaveh',
        [
          'label' => esc_html__( 'Marked Section Color', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::COLOR,
          'selectors' => [
            '{{WRAPPER}} .new-article .heading .title mark' => 'background-color: {{VALUE}}',
          ],
        ]
      );
      $this->end_controls_section();
      $this->start_controls_section(
        'style2_blogcarouselkaveh',
        [
          'label' => esc_html__( 'Button Style', 'kavehTheme' ),
          'tab' => \Elementor\Controls_Manager::TAB_STYLE,
          'condition' => [
            'moreblogcarouselkaveh' => 'yes',
          ],
        ]
      );

      $this->add_group_control(
        \Elementor\Group_Control_Typography::get_type(),
        [
          'name' => 'button_blogcarouselkaveh',
          'label' => esc_html__( 'Button Text Typography', 'kavehTheme' ),
          'selector' => '{{WRAPPER}} .new-article .heading a',
        ]
      );
      $this->add_group_control(
        \Elementor\Group_Control_Text_Shadow::get_type(),
        [
          'name' => 'text_shadowblogcarouselkaveh',
          'label' => esc_html__( 'Text Shadow', 'kavehTheme' ),
          'selector' => '{{WRAPPER}} .new-article .heading a',
        ]
      );
      $this->start_controls_tabs(
        'style_tabsblogcarouselkaveh'
      );
      
      $this->start_controls_tab(
        'style_normal_tabblogcarouselkaveh',
        [
          'label' => esc_html__( 'Normal', 'kavehTheme' ),
        ]
      );
      $this->add_control(
        'textbuttoncolorblogcarouselkaveh',
        [
          'label' => esc_html__( 'Button Text Color', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::COLOR,
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a' => 'color: {{VALUE}}',
          ],
        ]
      ); 
      $this->add_control(
        'buttoncolorblogcarouselkaveh',
        [
          'label' => esc_html__( 'Button Background Color', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::COLOR,
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a' => 'background-color: {{VALUE}}',
          ],
        ]
      ); 
      $this->add_control(
        'buttonradiusblogcarouselkaveh',
        [
          'label' => esc_html__( 'More Button Rounded Corner', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::DIMENSIONS,
          'size_units' => [ 'px' ],
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
          ],
        ]
      );
      $this->add_group_control(
        \Elementor\Group_Control_Border::get_type(),
        [
          'name' => 'borderblogcarouselkaveh',
          'label' => esc_html__( 'Border', 'kavehTheme' ),
          'selector' => '{{WRAPPER}} .new-article .heading a',
        ]
      );
      $this->add_control(
        'buttonbordersblogcarouselkaveh',
        [
          'label' => esc_html__( 'Border Width', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::DIMENSIONS,
          'size_units' => [ 'px' ],
          'condition' => [
            'borderblogcarouselkaveh!' => '',
          ],
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;',
          ],
        ]
      );
      $this->end_controls_tab();
      $this->start_controls_tab(
        'style_hove_tabblogcarouselkaveh',
        [
          'label' => esc_html__( 'Hover', 'kavehTheme' ),
        ]
      );
      $this->add_control(
        'textbuttoncolorhoverblogcarouselkaveh',
        [
          'label' => esc_html__( 'Button Text Color', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::COLOR,
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a:hover' => 'color: {{VALUE}}',
          ],
        ]
      ); 
      $this->add_control(
        'buttoncolorhoverblogcarouselkaveh',
        [
          'label' => esc_html__( 'Button Background Color', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::COLOR,
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a:hover' => 'background-color: {{VALUE}}',
          ],
        ]
      ); 
      $this->add_control(
        'buttonradiushoverblogcarouselkaveh',
        [
          'label' => esc_html__( 'More Button Rounded Corner', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::DIMENSIONS,
          'size_units' => [ 'px' ],
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
          ],
        ]
      );
      $this->add_group_control(
        \Elementor\Group_Control_Border::get_type(),
        [
          'name' => 'borderhoverblogcarouselkaveh',
          'label' => esc_html__( 'Border', 'kavehTheme' ),
          'selector' => '{{WRAPPER}} .new-article .heading a:hover',
        ]
      );
      $this->add_control(
        'buttonbordershoverblogcarouselkaveh',
        [
          'label' => esc_html__( 'Border Width', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::DIMENSIONS,
          'size_units' => [ 'px' ],
          'condition' => [
            'borderhoverblogcarouselkaveh!' => '',
          ],
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a:hover' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;',
          ],
        ]
      );
      $this->end_controls_tab();
      $this->end_controls_tabs();
      $this->add_group_control(
        \Elementor\Group_Control_Box_Shadow::get_type(),
        [
          'name' => 'box_shadowblogcarouselkavehh',
          'label' => esc_html__( 'Button Shadow', 'kavehTheme' ),
          'selector' => '{{WRAPPER}} .new-article .heading a',
        ]
      );
      $this->add_control(
        'hr1blogcarouselkaveh',
        [
          'type' => \Elementor\Controls_Manager::DIVIDER,
        ]
      );
      $this->add_control(
        'buttonpaddingblogcarouselkaveh',
        [
          'label' => esc_html__( 'Internal Spacing', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::DIMENSIONS,
          'size_units' => [ 'px' ],
          'selectors' => [
            '{{WRAPPER}} .new-article .heading a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
          ],
        ]
      );
      $this->end_controls_section();
          
        }

  protected function render(){
    $settings = $this->get_settings_for_display();
    $clsid = $this->get_id();
    ?>

<div class="new-article">
      <div class="container">
        <!-- Start Heading -->
        <div class="heading text-center">
          <<?php echo  esc_attr($settings['tagheadingblogcarouselkaveh']); ?> class="title position-relative">
          <?php echo  esc_html($settings['title1blogcarouselkaveh']); ?>
            <mark> <?php echo  esc_html($settings['title2blogcarouselkaveh']); ?> </mark>
            <?php echo  esc_html($settings['title3blogcarouselkaveh']); ?>
          </<?php echo  esc_attr($settings['tagheadingblogcarouselkaveh']); ?>>
          <?php if("yes"===$settings['moreblogcarouselkaveh']){?>
          <a href="<?php echo  esc_html($settings['buttonlinkblogcarouselkaveh']); ?>"> <?php echo  esc_html($settings['buttonblogcarouselkaveh']); ?> </a>
          <?php } ?>
        </div>
        <!-- End Heading -->
        <!-- Start Articles -->
        <div class="swiper swiper-articles-two overflow-hidden <?php if('seven'===$settings['styleblogcarouselkaveh']){ ?>swiper-articles-eleven<?php }elseif('eight'===$settings['styleblogcarouselkaveh']){ ?>swiper-new-article-eight<?php } ?>" data-swiper='{"breakpoints":{"320":{"slidesPerView":<?php echo  esc_html($settings['productcolumn3blogcarouselkaveh']); ?>,"spaceBetween":4},"576":{"slidesPerView":<?php echo  esc_html($settings['productcolumn2blogcarouselkaveh']); ?>,"spaceBetween":8},"768":{"slidesPerView":<?php echo  esc_html($settings['productcolumn2blogcarouselkaveh']); ?>,"spaceBetween":20},"992":{"slidesPerView":<?php echo  esc_html($settings['productcolumnblogcarouselkaveh']); ?>,"spaceBetween":30}}}'>
    <div class="swiper-wrapper">
        <?php
        // Define your custom query arguments
        $query_args = array(
          'post_type' => 'post',
          'posts_per_page' => $settings['productnumberblogcarouselkaveh'],
          'order' => 'desc',
          'orderby' => 'date',
          'post_status' => 'publish',
          'tax_query' => array(
          array(
              'taxonomy' => 'category',
              'field'    => 'term_id',
              'terms'    => $settings['catblogcarouselkaveh'],
              ),
          ),
        );
        // Get the cached query results
        $cached_query = KavehCustomQuery::get_cached_query($query_args);
        ?>
        <?php 
        if ($cached_query->have_posts()) :
          while ($cached_query->have_posts()) : $cached_query->the_post();
              ?>
              <div class="swiper-slide">
                <?php $styleblogcarousel=$settings['styleblogcarouselkaveh']; ?>
                <?php get_template_part("template-part/blog-cart-$styleblogcarousel"); ?>
              </div>
              <?php
          endwhile;
          wp_reset_postdata();
      else :
          echo 'No posts found.';
      endif;
        ?>

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


}
