<?php
/**
 * trending_product section
 *
 * This is the template for the content of trending_product section
 *
 * @package Theme Palace
 * @subpackage Raise Charity Pro
 * @since Raise Charity Pro 1.0.0
 */
if ( ! function_exists( 'raise_charity_pro_add_trending_product_section' ) ) :
    /**
    * Add trending_product section
    *
    *@since Raise Charity Pro 1.0.0
    */
    function raise_charity_pro_add_trending_product_section() {
    	$options = raise_charity_pro_get_theme_options();
        // Check if trending_product is enabled on frontpage
        $trending_product_enable = apply_filters( 'raise_charity_pro_section_status', true, 'trending_product_section_enable' );

        if ( true !== $trending_product_enable ) {
            return false;
        }
        if ( !class_exists('WooCommerce') ) {
            return;
        }
        // Get trending_product section details
        $section_details = array();
        $section_details = apply_filters( 'raise_charity_pro_filter_trending_product_section_details', $section_details );

        if ( empty( $section_details ) ) {
            return;
        }

        // Render trending_product section now.
        raise_charity_pro_render_trending_product_section( $section_details );
    }
endif;

if ( ! function_exists( 'raise_charity_pro_get_trending_product_section_details' ) ) :
    /**
    * trending_product section details.
    *
    * @since Raise Charity Pro 1.0.0
    * @param array $input trending_product section details.
    */
    function raise_charity_pro_get_trending_product_section_details( $input ) {
        $options = raise_charity_pro_get_theme_options();
        $content = array();
        $content[1] = array();
        $content[2] = array();
        $content[3] = array();

        for ($i=1; $i <= 3 ; $i++) { 
                // Content type.
            $trending_product_content_type  = empty( $options['trending_content_type_'.$i] ) ? 'product' : $options['trending_content_type_'.$i];
            $trending_product_count = ! empty( $options['trending_product_count'] ) ? $options['trending_product_count'] : 3;

            switch ( $trending_product_content_type ) {

                case 'product':
                    $page_ids = array();

                    for ( $j = 1; $j <=  $options['trending_product_count']; $j++ ){
                        if ( ! empty( $options['trending_product_content_page_' . $i.$j] ) ){
                            $page_ids[] = $options['trending_product_content_page_' . $i.$j];
                        }
                        
                    }
                      
                    $args = array(
                        'post_type'         => 'product',
                        'post__in'          => ( array ) $page_ids,
                        'posts_per_page'    => $options['trending_product_count'],
                        'orderby'           => 'post__in',
                        );
                break;
                case 'product_cat':
                    $cat_id = ! empty( $options['trending_product_category_'.$i] ) ? $options['trending_product_category_'.$i] : '';

                    $args = array(
                    'post_type'         => 'product',
                    'posts_per_page'    => absint( $options['trending_product_count'] ),
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'product_cat',
                            'field'     => 'id',
                            'terms'     => $cat_id
                        )
                    ) );
                break;

                default:
                break;
            }


            // Run The Loop.
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) : 
 
                while ( $query->have_posts() ) : $query->the_post();
                    $page_post['id']        = get_the_id();
                    $page_post['title']     = get_the_title();
                    $page_post['excerpt']   = raise_charity_pro_trim_content( 50 );
                    $page_post['url']       = get_the_permalink();
                    $page_post['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'post-thumbnail' ) : '';

                    // Push to the main array.
                    array_push( $content[$i], $page_post );
               
                endwhile;
            endif;
            wp_reset_postdata();
        }
            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// trending_product section content details.
add_filter( 'raise_charity_pro_filter_trending_product_section_details', 'raise_charity_pro_get_trending_product_section_details' );


if ( ! function_exists( 'raise_charity_pro_render_trending_product_section' ) ) :
  /**
   * Start trending_product section
   *
   * @return string trending_product content
   * @since Raise Charity Pro 1.0.0
   *
   */
   function raise_charity_pro_render_trending_product_section( $content_details = array() ) {
        $options = raise_charity_pro_get_theme_options();

        if ( empty( $content_details ) ) {
            return;
        } ?>
        <div id="raise_charity_pro_trending_product_section">
            <div id="trending-products" class="relative page-section same-background">
                <div class="wrapper">

                    <?php if ( is_customize_preview()):
                    raise_charity_pro_section_tooltip( 'trending-products' );
                    endif; ?>
                    <div class="section-content clear">

                        <?php for ( $i=1; $i <=3 ; $i++) { 
                            $title = empty( $options['trending_product_title_'.$i] ) ? '' : $options['trending_product_title_'.$i];
                            ?>

                            <div class="product-wrapper">
                                <h2 class="category-title"><?php echo esc_html( $title ) ; ?></h2>
                                <div class="product-items">

                                    <?php foreach ($content_details[$i] as $content): 
                                    $image = $content['image'] == '' ? get_template_directory_uri().'/assets/uploads/no-featured-image-590x650.jpg' : $content['image'] ;
                                    ?>

                                    <article>
                                        <div class="featured-image">
                                            <a href="<?php echo esc_url( $content['url'] ) ; ?>"><img src="<?php echo esc_url( $image );?>"></a>
                                        </div><!-- .featured-image -->

                                        <div class="entry-container">
                                            <span class="cat-links">
                                                <ul class="post-categories">
                                                    <?php 
                                                    $terms = get_the_terms ( $content['id'], 'product_cat' );
                                                    foreach ( $terms as $term ) { ?>
                                                    <li class="posted_in">
                                                        <a href="<?php echo esc_url( get_term_link( $term->term_id, 'product_cat' ) ) ?>"><?php echo esc_html( $term->name) ; ?></a>
                                                    </li><!-- .posted_in -->

                                                    <?php } ?>
                                                </ul>
                                            </span><!-- .cat-links -->

                                            <header class="entry-header">
                                                <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ) ; ?>"><?php echo esc_html( $content['title'] ) ; ?></a></h2>
                                            </header>

                                            <span class="price">
                                                <?php 
                                                $product = new WC_Product( $content['id'] );
                                                echo $product->get_price_html();
                                                ?>
                                            </span><!-- .price -->
                                        </div><!-- .entry-container -->
                                    </article>

                                <?php endforeach; ?>

                            </div><!-- .product-items -->
                        </div><!-- .product-wrapper -->

                        <?php } ?> 

                    </div><!-- .product-wrapper -->
                </div><!-- .section-content -->
            </div><!-- .wrapper -->
        </div><!-- .trending-products -->
        </div>
       

    <?php }
endif;