<?php
/**
 * Special Menu section
 *
 * This is the template for the content of special menu section
 *
 * @package Theme Palace
 * @subpackage Mezze
 * @since Mezze 1.0.0
 */
if ( ! function_exists( 'mezze_add_special_menu_section' ) ) :
    /**
    * Add special menu section
    *
    *@since Mezze 1.0.0
    */
    function mezze_add_special_menu_section() {
    	$options = mezze_get_theme_options();
        // Check if special menu is enabled on frontpage
        $special_menu_enable = apply_filters( 'mezze_section_status', true, 'special_menu_section_enable' );

        if ( true !== $special_menu_enable ) {
            return false;
        }
        // Get special menu section details
        $section_details = array();
        $section_details = apply_filters( 'mezze_filter_special_menu_section_details', $section_details );

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

        // Render special menu section now.
        mezze_render_special_menu_section( $section_details );
    }
endif;

if ( ! function_exists( 'mezze_get_special_menu_section_details' ) ) :
    /**
    * special menu section details.
    *
    * @since Mezze 1.0.0
    * @param array $input special menu section details.
    */
    function mezze_get_special_menu_section_details( $input ) {
        $options = mezze_get_theme_options();

        // Content type.
        $special_menu_content_type  = $options['special_menu_content_type'];
        $content = array();
        switch ( $special_menu_content_type ) {
        	
            case 'page':
                $page_ids = array();

                for ( $i = 1; $i <= 4; $i++ ) {
                    if ( ! empty( $options['special_menu_content_page_' . $i] ) )
                        $page_ids[] = $options['special_menu_content_page_' . $i];
                }
                
                $args = array(
                    'post_type'         => 'page',
                    'post__in'          => ( array ) $page_ids,
                    'posts_per_page'    => 4,
                    'orderby'           => 'post__in',
                    );                    
            break;

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

                for ( $i = 1; $i <= 4; $i++ ) {
                    if ( ! empty( $options['special_menu_content_product_' . $i] ) )
                        $page_ids[] = $options['special_menu_content_product_' . $i];
                }
                
                $args = array(
                    'post_type'         => 'product',
                    'post__in'          => ( array ) $page_ids,
                    'posts_per_page'    => 4,
                    'orderby'           => 'post__in',
                    );                    
            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['url']       = get_the_permalink();
                    $page_post['excerpt']   = mezze_trim_content( 15 );

                    // Push to the main array.
                    array_push( $content, $page_post );
                endwhile;
            endif;
            wp_reset_postdata();

            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// special menu section content details.
add_filter( 'mezze_filter_special_menu_section_details', 'mezze_get_special_menu_section_details' );


if ( ! function_exists( 'mezze_render_special_menu_section' ) ) :
  /**
   * Start special menu section
   *
   * @return string special menu content
   * @since Mezze 1.0.0
   *
   */
   function mezze_render_special_menu_section( $content_details = array() ) {
        $options = mezze_get_theme_options();
        $special_menu_content_type  = $options['special_menu_content_type'];
        $background = ! empty( $options['special_menu_image'] ) ? $options['special_menu_image'] : get_template_directory_uri() . '/assets/uploads/menus-bg.jpg';

        if ( empty( $content_details ) ) {
            return;
        } ?>

        <div id="mezze_special_menu_section">

        <div id="special-menu" class="relative page-section" style="background-image: url('<?php echo esc_url( $background ); ?>');">
            <div class="overlay"></div>
            <div class="wrapper">
                <?php if ( ! empty( $options['special_menu_title'] ) ) : ?>
                    <div class="section-header">
                        <?php if ( ! empty( $options['special_menu_title'] ) ) : ?>
                            <h2 class="section-title"><?php echo esc_html( $options['special_menu_title'] ); ?></h2>
                        <?php endif; ?>
                    </div><!-- .section-header -->
                <?php endif; ?>

                <div class="section-content">
                    <?php foreach ( $content_details as $content ) : ?>
                        <article>
                            <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>

                            <?php if ( in_array( $special_menu_content_type, array( 'product', 'product-category' ) ) ) : ?>
                                <span class="price">
                                    <?php  
                                        $product = new WC_Product( $content['id'] );
                                        echo $product->get_price_html();
                                    ?>
                                </span><!-- .price -->
                            <?php endif; ?>

                            <div class="entry-content">
                                <p><?php echo esc_html( $content['excerpt'] ); ?></p>
                            </div><!-- .entry-content -->

                            <div class="read-more">
                                <a href="<?php echo esc_url( $content['url'] ); ?>" class="btn btn-fill" tabindex="-1">
                                    <span class="more-icon">
                                        <?php echo mezze_get_svg( array( 'icon' => 'more' ) ); ?>
                                    </span><!-- .more-icon -->
                                </a>
                            </div><!-- .read-more -->
                        </article>
                    <?php endforeach; ?>
                </div><!-- .section-content -->
            </div><!-- .wrapper -->
        </div><!-- .special-menu -->

        </div>
        
    <?php }
endif;