<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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


class Bloglistkaveh extends Widget_Base{

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

  public function get_title(){
    return esc_html__( 'المان بلاگ لیست', 'kaveh-core' );
  }

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

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

  protected function register_controls(){
    
    $this->start_controls_section(
        'contentbloglistkaveh',
        [
          'label' => esc_html__( 'محتوای المان', 'kaveh-core' ),
                  'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
        ]
      );

  $optionsblog = array();
  $categories = get_categories(array(
    'hide_empty' => false,
  )
  ); 
  foreach ( $categories as $key => $category ) {
      $optionsblog[$category->term_id] = $category->name;
  }
  $this->add_control(
      'catblogenfhess',
      [
        'label' => esc_html__( 'انتخاب دسته بندی', 'kaveh-core' ),
        'type' => \Elementor\Controls_Manager::SELECT2, 
        'multiple' => true,
        'options' => $optionsblog,
      ]
  );
  
$this->add_control(
  'productnumberbloglistkaveh',
  [
    'label' => esc_html__( 'تعداد محصولات نمایش داده شده', 'kaveh-core' ),
    'type' => \Elementor\Controls_Manager::NUMBER,
    'min' => 0,
    'max' => 20,
    'step' => 1,
    'default' => 5,
  ]
);
       $this->end_controls_section();

          
        }


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

<ul class="footer-three-articles">
              <?php
              $kproducts = KavehRestRules::kaveh_get_rest_posts(array(
                  'per_page' => $settings['productnumberbloglistkaveh'],
                  'orderby' => 'date',
                  'order' => 'desc',
                  'categories' => $settings['catblogenfhess'],
              ));
              ?>

            <?php if(is_array($kproducts)): ?>
                <?php foreach($kproducts as $kproduct): ?>
                  <li class="d-flex align-items-center">
                    <img src="<?php echo $kproduct['kaveh_custom']['thumbnail_url'] ?>" alt="article" class="d-block w-100" />
                    <div class="detail w-100">
                      <h2 class="mb-0 text-nowrap overflow-hidden">
                        <a href="<?php echo $kproduct['link'] ?>"><?php echo wp_trim_words($kproduct['title']['rendered'], 6) ?></a>
                      </h2>
                      <span class="d-block text-nowrap overflow-hidden"><?php echo wp_trim_words( $kproduct['kaveh_custom']['excerpt'], 7)  ?></span>
                    </div>
                  </li>
                <?php endforeach; ?>
            <?php endif; ?>
              
            </ul>
    <?php
  }


}
