<?php
/**
 * Featured News hook for our theme.
 *
 * This is the template that displays slider of the theme
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package OMag
 */

if ( ! function_exists( 'omag_home_featured_news_primary' ) ) :

  function omag_home_featured_news_primary(){

    $theme_options  = omag_theme_options();

    if(!empty( $theme_options['featured_news_cat'] )){

      $featured_news_args = array( 
        'cat'             => absint( $theme_options['featured_news_cat'] ), 
        'post_status'     => 'publish', 
        'posts_per_page'  => 3,
        'ignore_sticky_posts' => 1,
      );

    } else {

      $featured_news_args = array( 'post_status' => 'publish', 'posts_per_page' => 3, 'ignore_sticky_posts' => 1, );

    }

    $featured_news_query = new WP_Query( $featured_news_args ); 

    if ( $featured_news_query->have_posts() ) {

      ?>

      <div class="col-lg-6">
        <div class="side-banner">

          <?php

          $index = 1;

          while ( $featured_news_query->have_posts() ) : $featured_news_query->the_post(); 

            $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );

            if( 1 === $index ) {
              ?>

              <div class="top-banner">
                <a href="<?php the_permalink(); ?>">
                  <?php if(!empty($image_url)) { ?>
                    <img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php the_title(); ?>">
                  <?php } else { ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/images/default.jpg" alt="<?php the_title(); ?>">
                  <?php } ?>
                </a>
                <div class="banner-text">
                  <div class="banner-text-cover">
                    <div class="tags-button">
                      <?php if( 1 === $theme_options['enable_post_tags']) {
                        omag_posts_tag();
                      } ?>
                    </div>
                    <div class="banner-header">
                      <h3>
                        <a href="<?php the_permalink(); ?>">
                          <?php the_title(); ?>
                        </a> 
                      </h3>
                    </div>
                  </div>
                </div>
              </div>

              <?php }
              if( 2 === $index || 3 == $index ) {
                if( 2 === $index ) {
                  ?>

                  <div class="bottom-banner">
                    <div class="row">

                      <?php } ?>

                      <div class="col-lg-6 col-md-6">
                        <div class="bottom-small-banner">

                          <a href="<?php the_permalink(); ?>">
                            <?php if(!empty($image_url)) { ?>
                              <img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php the_title(); ?>">
                            <?php } else { ?>
                              <img src="<?php echo get_template_directory_uri(); ?>/assets/images/default.jpg" alt="<?php the_title(); ?>">
                            <?php } ?>
                          </a>
                          <div class="banner-text">
                            <div class="banner-text-cover">
                              <div class="tags-button">
                                <?php if( 1 === $theme_options['enable_post_tags']) {
                                  omag_posts_tag();
                                } ?>
                              </div>
                              <div class="banner-header">
                                <h5>
                                  <a href="<?php the_permalink(); ?>">
                                    <?php the_title(); ?>
                                  </a>  
                                </h5>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>

                      <?php if( 3 === $index ) { ?>

                    </div>
                  </div>

                  <?php } } ?>


                  <?php
                  $index++;
                endwhile;
                wp_reset_postdata(); 
                ?>

                <?php 
                if( 3 === $index ) {
                  echo '</div></div>';
                }
                ?>


              </div>
            </div>

            <?php
          }

        }
      endif;

      add_action( 'omag_featured_news_primary', 'omag_home_featured_news_primary', 10 );

      if ( ! function_exists( 'omag_home_featured_news_secondary' ) ) :

        function omag_home_featured_news_secondary(){

          $theme_options  = omag_theme_options();

          if(!empty( $theme_options['featured_news_cat'] )){

            $featured_news_args = array( 
              'cat'             => absint( $theme_options['featured_news_cat'] ), 
              'post_status'     => 'publish', 
              'posts_per_page'  => 4,
              'ignore_sticky_posts' => 1,
            );

          } else{

            $featured_news_args = array( 'post_status' => 'publish', 'posts_per_page' => 4, 'ignore_sticky_posts' => 1, );

          } 

          $featured_news_query = new WP_Query( $featured_news_args ); 

          if ( $featured_news_query->have_posts() ) {

            ?>
            <div class="col-lg-6">
              <div class="side-banner">

                <?php

                $index = 1;

                while ( $featured_news_query->have_posts() ) : $featured_news_query->the_post(); 

                  $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );

                  if( 1 === $index || 2 == $index ) {

                    if( 1 === $index ) {
                      ?>

                      <div class="bottom-banner second-banner">
                        <div class="row">

                          <?php } ?>

                          <div class="col-lg-6">
                            <div class="bottom-small-banner banner-type2">
                              <a href="<?php the_permalink(); ?>">
                                <?php if(!empty($image_url)) { ?>
                                  <img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php the_title(); ?>">
                                <?php } else { ?>
                                  <img src="<?php echo get_template_directory_uri(); ?>/assets/images/default.jpg" alt="<?php the_title(); ?>">
                                <?php } ?>
                              </a>
                              <div class="banner-text">
                                <div class="banner-text-cover">
                                  <div class="category-button">
                                    <?php if( 1 === $theme_options['enable_post_tags']) {
                                      omag_posts_tag();
                                    } ?>
                                  </div>
                                  <div class="banner-header">
                                    <h5>
                                      <a href="<?php the_permalink(); ?>">
                                        <?php the_title(); ?>
                                      </a> 
                                    </h5>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>

                          <?php  if( 2 === $index ) { ?>

                        </div>
                      </div>

                      <?php } } ?>


                      <?php  if( 3 === $index || 4 === $index ) {

                        if( 3 === $index ) {
                          ?>

                          <div class="bottom-banner">
                            <div class="row">

                              <?php } ?>

                              <div class="col-lg-6">
                                <div class="bottom-small-banner banner-type2">
                                  <a href="<?php the_permalink(); ?>">
                                    <?php if(!empty($image_url)) { ?>
                                      <img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php the_title(); ?>">
                                    <?php } else { ?>
                                      <img src="<?php echo get_template_directory_uri(); ?>/assets/images/default.jpg" alt="<?php the_title(); ?>">
                                    <?php } ?>
                                  </a>
                                  <div class="banner-text">
                                    <div class="banner-text-cover">
                                      <div class="category-button">
                                        <?php if( 1 === $theme_options['enable_post_tags']) {
                                          omag_posts_tag();
                                        } ?>
                                      </div>
                                      <div class="banner-header">
                                        <h5>
                                          <a href="<?php the_permalink(); ?>">
                                            <?php the_title(); ?>
                                          </a> 
                                        </h5>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>

                              <?php  if( 4 === $index ) { ?>
                            </div>
                          </div>

                          <?php 
                        }
                      }
                      $index++;
                    endwhile;
                    wp_reset_postdata(); 
                    ?>

                    <?php 
                    if( 2 === $index || 4 === $index ) {
                      echo '</div></div>';
                    }
                    ?>

                  </div>
                </div>

              <?php
          }

      }
  endif;

add_action( 'omag_featured_news_secondary', 'omag_home_featured_news_secondary', 10 );
