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

        if ( ( true !== $collection_enable) || ! class_exists( 'WooCommerce' ) ) {
            return false;
        }

        // Render collection section now.
        raise_charity_pro_render_collection_section();
    }
endif;

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

    ?>
    <div id="raise_charity_pro_collection_section">
          <div id="products-collection" class="relative page-section same-background">
                <div class="wrapper">
                <?php if ( is_customize_preview()):
                raise_charity_pro_section_tooltip( 'products-collection' );
                endif; ?>
                    <div class="section-header">
                        <?php if( !empty( $options['collection_subtitle'] ) ): ?>
                            <p class="section-subtitle"><?php echo esc_html( $options['collection_subtitle'] ); ?></p>
                        <?php endif;
                        if( !empty( $options['collection_title'] ) ):
                           ?>
                       <h2 class="section-title"><?php echo esc_html( $options['collection_title'] ); ?></h2>
                   <?php endif; ?>
               </div><!-- .section-header -->

                    <div class="section-content grid col-2">
                        
                <?php for ( $i=1; $i <= 4 ; $i++ ) { 
                    if(empty($options['collection_content_product_category_'.$i])) continue ;
                $thumbnail_id = get_term_meta( $options['collection_content_product_category_'.$i], 'thumbnail_id', true );
                $image = wp_get_attachment_url( $thumbnail_id );
                $cat_name = get_term_by( 'id', $options['collection_content_product_category_'.$i], 'product_cat' )->name;
                $banner_title = empty( $options['collection_banner_title_'.$i] ) ? '' : $options['collection_banner_title_'.$i];
                $cat_link = get_category_link( $options['collection_content_product_category_'.$i] );
            ?>
                <?php if (!empty($options['collection_content_product_category_'.$i])): ?>
                      <article class="grid-item">
                            <div class="featured-image" style="background-image: url('<?php echo esc_url( $image ); ?>');">
                                <div class="entry-container">
                                    <span class="cat-links">
                                        <ul class="post-categories">
                                            <li><a href="<?php echo esc_url( $cat_link ) ?>"><?php echo esc_html( $cat_name ); ?></a></li>
                                        </ul>
                                    </span><!-- .cat-links -->

                                    <header class="entry-header">
                                        <h2 class="entry-title"><a href="<?php echo esc_url( $cat_link ) ?>"><?php echo esc_html( $banner_title ) ?></a></h2>
                                    </header>

                                    <?php if( !empty( $options['collection_button_'.$i] ) ): ?>
                                    <div class="read-more">
                                        <a href="<?php echo esc_url( $cat_link ) ?>" class="btn"><?php echo esc_html( $options['collection_button_'.$i] ); ?></a>
                                    </div><!-- .read-more -->
                                <?php endif; ?>
                                </div><!-- .entry-container -->
                            </div><!-- .featured-image -->
                        </article>
                <?php endif ?>
                      

                          <?php } ?>

                    </div><!-- .section-content -->
                </div><!-- .wrapper -->
            </div><!-- #products-collction -->

    </div>
  
    <?php }
endif;