<?php
namespace Elementor;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
class Newswidget extends Widget_Base {

	public function get_name() {
		return 'gc-daynaweb-newswidget';
	}

	public function get_title() {
		return 'ابزارک آخرین اخبار';
	}

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

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

	protected function _register_controls() {

    $post_cat = array();
    $categories = get_terms("category");
    if ( !empty( $categories ) && !is_wp_error( $categories ) ){
    foreach ( $categories as $category ) {
    $post_cat[ $category->term_id ] = $category->name;
    }
    }

    $this->start_controls_section(
      'section_title',
      [
        'label' => __( 'Content', 'elementor' ),
      ]
    );

    $this->add_control(
			'title',
			[
				'label' => __( 'عنوان', 'gajcommerce' ),
				'label_block' => true,
				'type' => Controls_Manager::TEXT,
        'default' => 'اخبار',
			]
		);

    $this->add_control(
			'post_cat',
			[
				'label' => __( 'دسته‌بندی مقالات', 'gajcommerce' ),
				'type' => Controls_Manager::SELECT2,
				'options' => $post_cat,
        'description' => __( 'برای نمایش خودکار آخرین مقالات، دسته‌بندی را خالی بگذارید.' , 'gajcommerce' ),
			]
		);

    $this->add_control(
			'post_count',
			[
				'label' => __( 'تعداد مقالات', 'gajcommerce' ),
				'type' => Controls_Manager::NUMBER,
				'min' => 1,
				'max' => 10,
				'step' => 1,
				'default' => 5,
			]
		);

    $this->end_controls_section();

    $this->start_controls_section(
      'gc-widget-styles',
      [
        'label' => __('تنظیمات استایل', 'gajcommerce'),
        'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
  
    $this->add_control(
      'gc-widget-background',
      [
        'label'     => __('رنگ پس زمینه', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidebox' => 'background: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-title-color',
      [
        'label'     => __('رنگ عنوان ویجت', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidebox header p' => 'color: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-article-title-color',
      [
        'label'     => __('رنگ عنوان مقالات', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .titlesideeach' => 'color: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-article-date-color',
      [
        'label'     => __('رنگ تاریخ مقالات', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidedategm' => 'color: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-button-background',
      [
        'label'     => __('رنگ پس زمینه دکمه', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidecta' => 'background: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-button-background-hover',
      [
        'label'     => __('رنگ پس زمینه دکمه در حالت هاور', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidecta:hover' => 'background: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-button-color',
      [
        'label'     => __('رنگ متن دکمه', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidecta span' => 'color: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-separator',
      [
        'label'     => __('رنگ جداکننده', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidebox header' => 'border-bottom-color: {{VALUE}};',
        ],
      ]
    );

    $this->add_control(
      'gc-widget-separator-two',
      [
        'label'     => __('رنگ جداکننده 2 و فلش', 'gajcommerce'),
        'type'      => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .sidebox header p:after' => 'background: {{VALUE}};',
          '{{WRAPPER}} .sidecta i' => 'color: {{VALUE}};',
        ],
      ]
    );

    $this->end_controls_section();

	}

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

    <div class="specialsidebox">
      <div class="sidebox">
        <header>
          <p><?php echo $settings['title'] ?></p>
        </header>
        <section>
          <?php
          if (empty($settings['post_cat'])) {
            $specialwidgetgccat = '';
          } else {
            $specialwidgetgccat = get_category($settings['post_cat']) -> slug;
            $specialwidgetgclink = get_category_link(get_category($settings['post_cat']));
          }
          $specialwidgetgccount = $settings['post_count'];
          $the_query = new \WP_Query( array( 'post_type' => 'post' , 'posts_per_page'=> $specialwidgetgccount , 'category_name'=> $specialwidgetgccat, 'post_status' => 'publish' ) ); ?>
          <?php if ( $the_query->have_posts() ) : ?>
          <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <ul>
              <li><a href="<?php echo the_permalink(); ?>"><?php echo the_post_thumbnail( 'side-thumb' ); ?>
                  <div class="titlesideeach">
                    <?php echo the_title(); ?>
                  </div>
                  <div class="sidedategm">
                    <?php echo the_time('j F Y'); ?>
                  </div>
                </a>
              </li>
            </ul>
          <?php endwhile; ?>
          <?php wp_reset_postdata(); ?>
          <?php else : ?>
          <p><?php _e( 'متاسفیم، محتوایی پیدا نشد.' ); ?></p>
          <?php endif; ?>
          <?php if (!empty($settings['post_cat'])) { ?>
          <a class="sidectalink" target="_blank" href="<?php echo $specialwidgetgclink ?>">
            <div class="sidecta">
              <span>مطالب بیشتر</span><i class="fas fa-angle-left"></i></div>
          </a>
        <?php } elseif (get_option( 'page_for_posts' ) && empty($settings['post_cat'])) { ?>
            <a class="sidectalink" target="_blank" href="<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>">
              <div class="sidecta">
                <span>مطالب بیشتر</span><i class="fas fa-angle-left"></i></div>
            </a>
          <?php } else {}?>
        </section>
    </div>
  </div>
<?php

	}

	protected function _content_template() {

    }


}
