<?php
if ( ! function_exists( 'moment_home_showcase' ) ) :
    /**
     * showcase Slider
     *
     * @since Moment Pro 1.0.0
     *
     * @param null
     * @return null
     *
     */
    function moment_home_showcase() {
        global $moment_customizer_all_values;

        $moment_home_showcase_title = $moment_customizer_all_values['moment-home-showcase-title'];
        $moment_home_showcase_sub_title = $moment_customizer_all_values['moment-home-showcase-sub-title'];
        $moment_home_showcase_number = $moment_customizer_all_values['moment-home-showcase-number'];
        $moment_home_showcase_button_text = $moment_customizer_all_values['moment-home-showcase-button-text'];
        $moment_home_showcase_button_url = $moment_customizer_all_values['moment-home-showcase-button-url'];
        $repeated_category = array('moment-home-showcase-category-ids');
        if( 1 != $moment_customizer_all_values['moment-home-showcase-enable'] ){
            return null;
        }
        ?>
        <section class="wrapper wrap-latestpost">
            <div class="container overhidden">
                <div class="row">
                    <div class="column-md-12">
                        <header class="title-section salientthemes-animate slideInDown" data-wow-delay="0s">
                            <h2><?php echo esc_html( $moment_home_showcase_title );?></h2>
                            <h3><?php echo esc_html( $moment_home_showcase_sub_title );?></h3>
                        </header>
                    </div>
                </div>
            </div>
            <div class="container overhidden">
                <div class="row">
                    <?php
                        $moment_showcase_category = salientthemes_customizer_get_repeated_all_value(3 , $repeated_category);
                        $moment_showcase_cat_posts_ids=array();
                        foreach( $moment_showcase_category as $moment_showcase_cat_post ) {
                            if( 0 != $moment_showcase_cat_post['moment-home-showcase-category-ids'] ){
                                $moment_showcase_cat_posts_ids[] = $moment_showcase_cat_post['moment-home-showcase-category-ids'];
                            }
                        }
                        if( !empty( $moment_showcase_cat_posts_ids) ){
                            $moment_home_about_args = array(
                                'post_type' => 'post',
                                'cat' => $moment_showcase_cat_posts_ids,
                                'ignore_sticky_posts' => true,
                                'posts_per_page' => absint( $moment_home_showcase_number ),
                            );
                        } ?>
                        <div id="filters" class="button-group"> 
                            <button class="button is-checked" data-filter="*">show all</button>
                            <?php for ($j=0; $j < count($moment_showcase_cat_posts_ids) ; $j++) {
                                $moment_category = get_cat_name( $moment_showcase_cat_posts_ids[$j]);
                                $moment_category_id = get_cat_id($moment_category);
                                if (!empty($moment_category)) { ?>
                                    <button class="button" data-filter=".<?php echo esc_attr($moment_category_id)?>"><?php echo esc_attr( $moment_category, 'moment' )?></button>
                            <?php }     
                            } ?>
                        </div>
                        <?php if (!empty ($moment_showcase_cat_posts_ids)){ ?>
                            <div class="grid">
                                <?php 
                                $moment_home_about_post_query = new WP_Query($moment_home_about_args);
                                if ($moment_home_about_post_query->have_posts()) :
                                    $data_delay = 0;
                                    while ($moment_home_about_post_query->have_posts()) : $moment_home_about_post_query->the_post();
                                        $moment_cat_id = array();
                                        if(has_post_thumbnail()){
                                            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
                                            $url = $thumb['0'];
                                        }
                                        else{
                                            $url = get_template_directory_uri().'/assets/img/no-image-430-300.png';
                                        }
                                        $data_wow_delay = 'data-wow-delay='.$data_delay.'s';
                                        ?> 
                                        <?php 
                                        $moment_categories = get_the_category();
                                        foreach ($moment_categories as $moment_cat) {
                                            $moment_cat_id[] = $moment_cat->term_id;
                                        }
                                        $cat_ids = implode(' ',$moment_cat_id);
                                        ?>
                                        <div class="popup-link">
                                                <div class="element-item <?php echo esc_attr($cat_ids); ?> " data-category="transition"> 
                                                <a href="<?php echo esc_url( $url ); ?>" rel="momentpopup[<?php echo esc_attr($cat_ids); ?>]">
                                                    <img src="<?php echo esc_url( $url ); ?>" alt="<?php the_title_attribute();?>">
                                                </a>
                                                    <div class="hover-content">  
                                                        <a href="<?php the_permalink(); ?>"><i class="fa fa-link"></i></a> 
                                                    </div>
                                                </div>
                                        </div>
                                    <?php endwhile; ?>
                                    <?php endif; wp_reset_postdata(); 
                                ?>
                            </div>
                        <?php } else {
                        $url = get_template_directory_uri().'/assets/img/no-image-430-300.png';
                         ?>
                         <div class="grid">
                             <div class="popup-link">
                                     <div class="element-item" data-category="transition"> 
                                     <a href="<?php echo esc_url( $url ); ?>" rel="momentpopup">
                                         <img src="<?php echo esc_url( $url ); ?>" alt="<?php the_title_attribute();?>">
                                     </a>
                                         <div class="hover-content">  
                                             <a href="<?php echo esc_url( $url ); ?>" rel="momentpopup"><i class="fa fa-eye"></i></a>
                                             <a href="<?php the_permalink(); ?>"><i class="fa fa-link"></i></a> 
                                         </div>
                                     </div>
                             </div>
                         </div>
                        <?php } ?>
                </div>
            </div>
                <?php if (!empty($moment_home_showcase_button_url)) { ?>
                    <div class="btn-holder">
                        <a href="<?php echo esc_url($moment_home_showcase_button_url); ?>" class="showcase-button">
                            <?php echo esc_html($moment_home_showcase_button_text);?>
                        </a>
                    </div>
                <?php } ?>
        </section>

        <?php
    }
endif;
add_action( 'moment_homepage', 'moment_home_showcase', 40 );