<?php
/**
 * Template Name: Home Page
 */

get_header();
?>

<main id="primary">

    <?php 
        $news_current_affair_main_slider_wrap = absint(get_theme_mod('news_current_affair_enable_slider', 0));

        if ($news_current_affair_main_slider_wrap == 1): 
    ?>
        <section id="main-slider-wrap">
            <div class="slider-main">
                <div class="flex-row">
                    <div class="left-post">
                        <div class="main-post-content featured-news-box">
                            <?php
                            $news_current_affair_postdata = get_theme_mod('news_current_affair_static_blog_1','Global Leaders Unite to Address Climate Crisis at Historic Summit in Geneva');
                            if ($news_current_affair_postdata) {
                                $news_current_affair_args = array(
                                    'name'        => esc_html($news_current_affair_postdata),
                                    'post_type'   => 'post',
                                    'post_status' => 'publish',
                                );

                                $news_current_affair_query = new WP_Query($news_current_affair_args);
                                if ($news_current_affair_query->have_posts()) :
                                    while ($news_current_affair_query->have_posts()) : $news_current_affair_query->the_post(); 
                                        $news_current_affair_categories = get_the_category();
                                        $news_current_affair_category_name = !empty($news_current_affair_categories) ? esc_html($news_current_affair_categories[0]->name) : '';
                                        ?>
                                        <?php 
                                        $news_current_affair_thumbnail_url = get_the_post_thumbnail_url(get_the_ID(), 'full');
                                        $news_current_affair_has_thumbnail = !empty($news_current_affair_thumbnail_url);
                                        ?>
                                        <div class="featured-news-wrapper <?php echo $news_current_affair_has_thumbnail ? '' : 'slider-color'; ?>" 
                                             <?php if ($news_current_affair_has_thumbnail): ?>
                                                style="background-image: url('<?php echo esc_url($news_current_affair_thumbnail_url); ?>');"
                                             <?php endif; ?>>
                                            <div class="overlay-box wow zoomIn" data-wow-duration="2s">
                                                <div class="banner-content">
                                                    <?php if ($news_current_affair_category_name): ?>
                                                        <span class="category"><?php echo esc_html($news_current_affair_category_name); ?></span>
                                                    <?php endif; ?>
                                                    <h1 class="featured-title">
                                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                    </h1>
                                                    <div class="banner-meta">
                                                        <span class="author">
                                                            <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
                                                                <i class="fa fa-user"></i> <?php echo esc_html( get_the_author() ); ?>
                                                            </a>
                                                        </span>
                                                        <span class="date">
                                                            <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                                            <a href="<?php echo esc_url( get_permalink() ); ?>">
                                                               <i class="far fa-calendar-alt"></i> <?php echo esc_html( get_the_date( 'd M Y' ) ); ?>
                                                            </a>
                                                        </span>
                                                        <span class="comments">
                                                            <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                                            <a href="<?php echo esc_url( get_comments_link() ); ?>">
                                                                <i class="far fa-comment-alt"></i> <?php echo esc_html( get_comments_number() ); ?>
                                                            </a>
                                                        </span>
                                                    </div>
                                                </div>
                                                <?php $news_current_affair_play_url = get_theme_mod('news_current_affair_play_button_url1'); ?>
                                                    <?php if ($news_current_affair_play_url): ?>
                                                    <div class="play-button">
                                                        <a href="<?php echo esc_url($news_current_affair_play_url); ?>" target="_blank" rel="noopener noreferrer">
                                                            <i class="fa fa-play"></i>
                                                        </a>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    <?php
                                    endwhile;
                                    wp_reset_postdata();
                                else : ?>
                                    <div class="no-postfound">No post found</div>
                                <?php endif;
                            }
                            ?>
                        </div>
                    </div>
                    <div class="right-post">
                        <div class="main-post-content featured-news-box">
                            <?php
                            $news_current_affair_postdata = get_theme_mod('news_current_affair_static_blog_2','Economy Rebounds with g Strong Growth');
                            if ($news_current_affair_postdata) {
                                $news_current_affair_args = array(
                                    'name'        => esc_html($news_current_affair_postdata),
                                    'post_type'   => 'post',
                                    'post_status' => 'publish',
                                );
                                $news_current_affair_query = new WP_Query($news_current_affair_args);
                                if ($news_current_affair_query->have_posts()) :
                                    while ($news_current_affair_query->have_posts()) : $news_current_affair_query->the_post(); 
                                        $news_current_affair_categories = get_the_category();
                                        $news_current_affair_category_name = !empty($news_current_affair_categories) ? esc_html($news_current_affair_categories[0]->name) : '';
                                        ?>
                                        <?php 
                                        $news_current_affair_thumbnail_url = get_the_post_thumbnail_url(get_the_ID(), 'full');
                                        $news_current_affair_has_thumbnail = !empty($news_current_affair_thumbnail_url);
                                        ?>
                                        <div class="featured-news-wrapper <?php echo $news_current_affair_has_thumbnail ? '' : 'slider-color'; ?>" 
                                             <?php if ($news_current_affair_has_thumbnail): ?>
                                                style="background-image: url('<?php echo esc_url($news_current_affair_thumbnail_url); ?>');"
                                             <?php endif; ?>>
                                             
                                            <div class="overlay-box wow zoomIn" data-wow-duration="2s">
                                                <div class="banner-content">
                                                    <?php if ($news_current_affair_category_name): ?>
                                                        <span class="category"><?php echo esc_html($news_current_affair_category_name); ?></span>
                                                    <?php endif; ?>
                                                    <h2 class="featured-title">
                                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                    </h2>
                                                    <div class="banner-meta">
                                                        <span class="author">
                                                            <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
                                                                <i class="fa fa-user"></i> <?php echo esc_html( get_the_author() ); ?>
                                                            </a>
                                                        </span>
                                                        <span class="date">
                                                            <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                                            <a href="<?php echo esc_url( get_permalink() ); ?>">
                                                               <i class="far fa-calendar-alt"></i> <?php echo esc_html( get_the_date( 'd M Y' ) ); ?>
                                                            </a>
                                                        </span>
                                                        <span class="comments">
                                                            <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                                            <a href="<?php echo esc_url( get_comments_link() ); ?>">
                                                                <i class="far fa-comment-alt"></i> <?php echo esc_html( get_comments_number() ); ?>
                                                            </a>
                                                        </span>
                                                    </div>
                                                </div>
                                                <?php $news_current_affair_play_url = get_theme_mod('news_current_affair_play_button_url2'); ?>
                                                    <?php if ($news_current_affair_play_url): ?>
                                                    <div class="play-button">
                                                        <a href="<?php echo esc_url($news_current_affair_play_url); ?>" target="_blank" rel="noopener noreferrer">
                                                            <i class="fa fa-play"></i>
                                                        </a>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    <?php
                                    endwhile;
                                    wp_reset_postdata();
                                else : ?>
                                    <div class="no-postfound">No post found</div>
                                <?php endif;
                            }
                            ?>
                        </div>
                        <div class="main-post-content featured-news-box">
                            <?php
                            $news_current_affair_postdata = get_theme_mod('news_current_affair_static_blog_3','Global Warming Threatens Ecosystem Stabilit');
                            if ($news_current_affair_postdata) {
                                $news_current_affair_args = array(
                                    'name'        => esc_html($news_current_affair_postdata),
                                    'post_type'   => 'post',
                                    'post_status' => 'publish',
                                );

                                $news_current_affair_query = new WP_Query($news_current_affair_args);

                                if ($news_current_affair_query->have_posts()) :
                                    while ($news_current_affair_query->have_posts()) : $news_current_affair_query->the_post(); 
                                        $news_current_affair_categories = get_the_category();
                                        $news_current_affair_category_name = !empty($news_current_affair_categories) ? esc_html($news_current_affair_categories[0]->name) : '';
                                        ?>
                                        <?php 
                                        $news_current_affair_thumbnail_url = get_the_post_thumbnail_url(get_the_ID(), 'full');
                                        $news_current_affair_has_thumbnail = !empty($news_current_affair_thumbnail_url);
                                        ?>
                                        <div class="featured-news-wrapper <?php echo $news_current_affair_has_thumbnail ? '' : 'slider-color'; ?>" 
                                             <?php if ($news_current_affair_has_thumbnail): ?>
                                                style="background-image: url('<?php echo esc_url($news_current_affair_thumbnail_url); ?>');"
                                             <?php endif; ?>>
                                            <div class="overlay-box wow zoomIn" data-wow-duration="2s">
                                                <div class="banner-content">
                                                    <?php if ($news_current_affair_category_name): ?>
                                                        <span class="category"><?php echo esc_html($news_current_affair_category_name); ?></span>
                                                    <?php endif; ?>
                                                    <h2 class="featured-title">
                                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                    </h2>
                                                    <div class="banner-meta">
                                                        <span class="author">
                                                            <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
                                                                <i class="fa fa-user"></i> <?php echo esc_html( get_the_author() ); ?>
                                                            </a>
                                                        </span>
                                                        <span class="date">
                                                            <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                                            <a href="<?php echo esc_url( get_permalink() ); ?>">
                                                               <i class="far fa-calendar-alt"></i> <?php echo esc_html( get_the_date( 'd M Y' ) ); ?>
                                                            </a>
                                                        </span>
                                                        <span class="comments">
                                                            <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                                            <a href="<?php echo esc_url( get_comments_link() ); ?>">
                                                                <i class="far fa-comment-alt"></i> <?php echo esc_html( get_comments_number() ); ?>
                                                            </a>
                                                        </span>
                                                    </div>
                                                </div>
                                                <?php $news_current_affair_play_url = get_theme_mod('news_current_affair_play_button_url3'); ?>
                                                    <?php if ($news_current_affair_play_url): ?>
                                                    <div class="play-button">
                                                        <a href="<?php echo esc_url($news_current_affair_play_url); ?>" target="_blank" rel="noopener noreferrer">
                                                            <i class="fa fa-play"></i>
                                                        </a>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    <?php
                                    endwhile;
                                    wp_reset_postdata();
                                else : ?>
                                    <div class="no-postfound">No post found</div>
                                <?php endif;
                            }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    <?php endif; ?>

    <?php 
    $news_current_affair_servicesec = get_theme_mod('news_current_affair_service_show_hide', 0);
    if ($news_current_affair_servicesec == 1) : 
    ?>
    <section id="projetcs-sec">
      <div class="container"> 
        <div class="flex-row">
          <div class="post-box-left">
            <div class="title-head">
              <?php if( get_theme_mod('news_current_affair_projetcs_main_heading') != 'LATEST NEWS' ){ ?>
                <h2><?php echo esc_html(get_theme_mod('news_current_affair_projetcs_main_heading','LATEST NEWS'));?></h2>
              <?php }?>
              <?php 
              $news_current_affair_featured_post = absint(get_theme_mod('news_current_affair_projetcs_number', 4)); 
              ?>
              <div class="tab">
                <?php
                for ( $news_current_affair_j = 1; $news_current_affair_j <= $news_current_affair_featured_post; $news_current_affair_j++ ){ 
                  $news_current_affair_tab_text = get_theme_mod('news_current_affair_projetcs_text'.$news_current_affair_j);
                  if ( !empty($news_current_affair_tab_text) ) {
                    $news_current_affair_tab_id = esc_attr(str_replace(' ', '-', strtolower($news_current_affair_tab_text)));
                    ?>
                    <button class="tablinks <?php echo ($news_current_affair_j === 1) ? 'active' : ''; ?>" 
                            onclick="news_current_affair_projetcs_tab(event, '<?php echo $news_current_affair_tab_id; ?>')">
                      <?php echo esc_html($news_current_affair_tab_text); ?>
                    </button>
                  <?php } 
                } ?>
              </div>
            </div>

            <?php
            for ( $news_current_affair_j = 1; $news_current_affair_j <= $news_current_affair_featured_post; $news_current_affair_j++ ){ 
              $news_current_affair_tab_text = get_theme_mod('news_current_affair_projetcs_text'.$news_current_affair_j);
              if ( !empty($news_current_affair_tab_text) ) {
                $news_current_affair_tab_id = esc_attr(str_replace(' ', '-', strtolower($news_current_affair_tab_text)));
                $category_slug = get_theme_mod('news_current_affair_projetcs_category'.$news_current_affair_j);
                $category_slug = !empty($category_slug) ? $category_slug : 'uncategorized';
            ?>
              <div id="<?php echo $news_current_affair_tab_id; ?>" class="tabcontent mt-3 <?php echo ($news_current_affair_j === 1) ? 'active' : ''; ?>">
                <div class="posts-wrapper owl-theme">
                  <?php
                  $page_query = new WP_Query(array(
                    'category_name' => sanitize_text_field($category_slug),
                    'posts_per_page' => 50,
                  ));

                  if ($page_query->have_posts()) :
                    while ($page_query->have_posts()) : $page_query->the_post(); ?>
                      <div class="post-box item">
                        <div class="post-thumbnail wow zoomIn" data-wow-duration="2s">
                            <?php if (has_post_thumbnail()) {
                                the_post_thumbnail();
                            } else { ?>
                                <div class="post-color"></div>
                            <?php } ?>
                            <?php
                            $news_current_affair_categories = get_the_category();
                            if ( ! empty( $news_current_affair_categories ) ) : ?>
                                <span class="serv-categorys"><?php echo esc_html( $news_current_affair_categories[0]->name ); ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="post-content">
                          <div class="main-content">
                            <h3 class="post-title">
                              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>
                            <div class="news-meta">
                                <span class="author">
                                    <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
                                        <i class="fa fa-user"></i> <?php echo esc_html( get_the_author() ); ?>
                                    </a>
                                </span>
                                <span class="date">
                                    <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                    <a href="<?php echo esc_url( get_permalink() ); ?>">
                                       <i class="far fa-calendar-alt"></i> <?php echo esc_html( get_the_date( 'd M Y' ) ); ?>
                                    </a>
                                </span>
                                <span class="comments">
                                    <span class="seperator"><?php echo esc_html__( ' / ', 'news-current-affair' ); ?></span>
                                    <a href="<?php echo esc_url( get_comments_link() ); ?>">
                                        <i class="far fa-comment-alt"></i> <?php echo esc_html( get_comments_number() ); ?>
                                    </a>
                                </span>
                            </div>
                            <p class="post-para"><?php echo wp_trim_words(get_the_content(), 25); ?></p>
                          </div>
                        </div>
                      </div>
                    <?php endwhile;
                    wp_reset_postdata();
                  endif;
                  ?>
                </div>
              </div>
            <?php 
              } // end if not empty tab
            } // end for loop 
            ?>
          </div>
          <div class="post-box-right">
            <?php if ( is_active_sidebar( 'home-page-sidebar' ) ) { ?>
                <aside id="secondary" class="widget-area sidebar-width">
                    <?php dynamic_sidebar( 'home-page-sidebar' ); ?>
                </aside>
            <?php } ?>
          </div>
        </div>
      </div>
    </section>
    <?php endif; ?>
</main>
<?php
get_footer();
?>