<?php 
/**
* Home pages slider block
*/

/**
* Function - Home slider block
*/
if ( !function_exists( 'store_expert_home_slider_block' ) ) {
    // Function
    function store_expert_home_slider_block() {
    /**
     * Check if slider is enabled
     */
    if ( get_theme_mod( 'store_expert_slider_menu_enable', 1 ) == 1 ) : 
    ?>
    <section id="content" class="slider container home-section">
        <div class="row">
            <?php
            /**
            * Check if navigation menu is enabled and menu is not empty
            */
            if ( get_theme_mod( 'store_expert_slider_nav_menu_enable' ) && get_theme_mod( 'store_expert_slider_navigation_menu', 'none' ) !== 'none' ) : 
            ?>
            <div class="col-md-3 col-lg-3 slider-navigation">
                <?php
                    // Echo the menu
                    wp_nav_menu( array(
                        'menu' => get_theme_mod( 'store_expert_slider_navigation_menu', 'none' ),
                        'fallback_cb' => false,
                        'container' => false,
                        'menu_class' => 'ms-product-menu',
                        'theme_location' => '',
                    ));
                ?>
            </div>
            <?php 
            endif; // end of slider navigational menu

            /**
            * Main slider begins
            * Check if slider is active and not empty
            */
            if ( get_theme_mod( 'store_expert_slider_nav_menu_enable' ) && get_theme_mod( 'store_expert_slider_navigation_menu', 'none' ) !== 'none' ) : 

            ?>
            <div class="col-md-12 col-lg-9">
                <div class="row">
            <?php endif; ?>

            <?php 
                // Get data from Customizer
                $post_one = get_theme_mod( 'store_expert_slider_banner_one', 'none' );
                $post_two = get_theme_mod( 'store_expert_slider_banner_two', 'none' );
                // Get thumbnail image url from posts
                $image_one = get_the_post_thumbnail_url( $post_one, 'store-expert-slider' );
                $image_two = get_the_post_thumbnail_url( $post_two, 'store-expert-slider' );
                // add class if slider image is enabled
                $slider_class = 'col-lg-12 col-md-12';
                if ( get_theme_mod( 'store_expert_slider_images_enable', 1 ) == 1 ) {
                    if ( $image_one || $image_two ) {
                        $slider_class = 'col-lg-8 col-md-8';
                    }

                } 

            ?>

            <div class="<?php echo $slider_class; ?>">
                <div class="slidercont">
                <?php
                    $counter = 0;
                    /**
                     * Get post category from Customizer to show in slider
                     */
                    $category = '';
                    if ( get_theme_mod( 'store_expert_slider_category', 'none' ) != 'none' ) {
                        $category = get_theme_mod( 'store_expert_slider_category' );
                    }
                    /**
                     * Arguments of sliders, We choose to select latest three posts from the selected category to display in slider
                     * Want more posts in slider? Change 'posts_per_page' => 3, 
                     *
                     */
                    $args = array(
                        'cat' => $category,
                        'post_type' => 'post',
                        'ignore_sticky_posts' => true,
                        'posts_per_page' => 3,
                        'orderby' => 'date',
                        'order' => 'DESC',
                        'post_status' => 'publish',
                        'meta_query' => array( 
                            array(
                                'key' => '_thumbnail_id'
                            ) 
                        )
                    );

                    $latestloop = new WP_Query($args);
                ?>  
                    <div class="nivoSlider slider-overlay clearfix" id="slider">
                        <?php 
                            /**
                             * Query to get posts
                             */
                            if ($latestloop->have_posts()) : 
                            while ($latestloop->have_posts()) : 
                            $latestloop->the_post(); 
                            $counter++; 
                        ?>
                        <?php $property_id = get_the_ID(); ?>
                            <a href="<?php the_permalink(); ?>">
                                <img src="<?php echo esc_url( get_the_post_thumbnail_url( $property_id, 'store-expert-slider' ) );?>" title="#slidecaption<?php echo $counter; ?>">
                            </a>
                        <?php 
                            /**
                             * End the query
                             */
                            endwhile;
                            endif;
                        ?>
                    </div>
                </div>
            </div>          
            <?php 
            /**
             * Right side image banners of Slider area
             * Upto two images can be added
             */
            if ( get_theme_mod( 'store_expert_slider_images_enable', 1 ) == 1 ) :
            
            /**
             * Check if any out of two selected posts has thumbnail to display
             */
            if ( $image_one || $image_two ) : ?>
                <div class="col-md-4 col-lg-4  col-sm-12 slider-right">
                    <?php 
                    /**
                     * Display Banner one image
                     */
                    if ( $image_one ) : 
                    ?>
                    <div class="slider-right-first">
                        <a href="<?php echo esc_url( get_permalink( $post_one ) ); ?>">
                            <img src="<?php echo esc_url( $image_one );?> ">
                        </a>
                    </div>
                    <?php endif; ?>
                    <?php 
                    /**
                     * Display Banner two image
                     */
                    if ( $image_two ) : 
                    ?>
                    <div class="slider-right-second">
                        <a href="<?php echo esc_url( get_permalink( $post_two ) ); ?>">
                            <img src="<?php echo esc_url( $image_two );?>">
                        </a>
                    </div>
                    <?php endif; ?>
                </div>
            <?php 
                endif; 
                endif; 
            ?>

        </div>
        <?php if ( get_theme_mod( 'store_expert_slider_nav_menu_enable' ) && get_theme_mod( 'store_expert_slider_navigation_menu', 'none' ) !== 'none' ) : ?>
                </div>
            </div>
        <?php endif; ?>
    </section>
    <?php 

    endif;

    }

}
add_action( 'store_expert_home_slider_block_hook', 'store_expert_home_slider_block', 0 );