<?php
/**
 * Career section
 *
 * This is the template for the content of career section
 *
 * @package Theme Palace
 * @subpackage Enlightme Pro
 * @since Enlightme Pro 1.0.0
 */
if ( ! function_exists( 'enlightme_pro_add_career_section' ) ) :
    /**
    * Add career section
    *
    *@since Enlightme Pro 1.0.0
    */
    function enlightme_pro_add_career_section() {
        $options = enlightme_pro_get_theme_options();

        // Check if career is enabled on frontpage
        $career_enable = apply_filters( 'enlightme_pro_section_status', true, 'career_section_enable' );

        if ( true !== $career_enable ) {
           return false;
        }
        // Get career section details
        $section_details = array();
        $section_details = apply_filters( 'enlightme_pro_filter_career_section_details', $section_details );
        if ( empty( $section_details ) ) {
           return;
        }
        // Render career section now.
        enlightme_pro_render_career_section( $section_details );
    }
endif;

if ( ! function_exists( 'enlightme_pro_get_career_section_details' ) ) :
    /**
    * Career section details.
    *
    * @since Enlightme Pro 1.0.0
    * @param array $input career section details.
    */
    function enlightme_pro_get_career_section_details( $input ) {
        $options = enlightme_pro_get_theme_options();

        // Content type.
        $career_content_type  = $options['career_content_type'];
        $career_count = ! empty( $options['career_count'] ) ? $options['career_count'] : 5;
        $content = array();
        $args = array();
        $content['cats'] = array();
        switch ( $career_content_type ) {
        	
            case 'category':
                $cat_ids = ! empty( $options['career_content_category'] ) ? $options['career_content_category'] : array();

                if ( empty( $cat_ids ) )
                    return;

                foreach ( $cat_ids as $cat_id ) :
                    $category_args = array(
                        'post_type'         => 'post',
                        'posts_per_page'    => absint( $career_count ),
                        'cat'               => absint( $cat_id ),
                        'ignore_sticky_posts'   => true,
                        );
                    array_push( $args, $category_args );
                    array_push( $content['cats'], $cat_id );
                endforeach;               
                $content['taxonomy'] = 'category';
            break;

            case 'course-category': // course-category
                if ( ! class_exists( 'TP_Education' ) )
                    return;

                $cat_ids = ! empty( $options['career_content_course_category'] ) ? $options['career_content_course_category'] : array();
                if ( empty( $cat_ids ) )
                    return;

                foreach ( $cat_ids as $cat_id ) :
                    $category_args = array(
                        'post_type'      => 'tp-course',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'tp-course-category',
                                'field'    => 'id',
                                'terms'    => $cat_id,
                            ),
                        ),
                        'posts_per_page'  => absint( $career_count ),
                        );
                    array_push( $args, $category_args );
                    array_push( $content['cats'], $cat_id );
                endforeach;                
                $content['taxonomy'] = 'tp-course-category';
            break;

            default:
            break;
        }

        $i = 0;
        foreach ( $args as $arg ) :
            $content['details'][$i] = array();
            // Run The Loop.
            $query = new WP_Query( $arg );
            if ( $query->have_posts() ) : 
                while ( $query->have_posts() ) : $query->the_post();
                    $page_post[$i]['id']        = get_the_id();
                    $page_post[$i]['title']     = get_the_title();
                    $page_post[$i]['url']       = get_the_permalink();
                    $page_post[$i]['image']  	 = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'post-thumbnail' ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-590x650.jpg';

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


if ( ! function_exists( 'enlightme_pro_render_career_section' ) ) :
  /**
   * Start Career section
   *
   * @return string Career content
   * @since Enlightme Pro 1.0.0
   *
   */
   function enlightme_pro_render_career_section( $content_details = array() ) {
        $options = enlightme_pro_get_theme_options();
        $career_title = ! empty( $options['career_title'] ) ? $options['career_title'] : '';
        $career_sub_title = ! empty( $options['career_sub_title'] ) ? $options['career_sub_title'] : '';
        $career_btn = ! empty( $options['career_btn_title'] ) ? $options['career_btn_title'] : '';
        $career_content_type = $options['career_content_type'];
        $career_autoplay = ($options['career_autoplay']) ? 'true' : 'false';
        
        ?>
        <div id="enlightme_pro_career_section">
        
        <div id="career" class="relative page-section">
            <div class="wrapper">
            <?php if ( is_customize_preview()):
                enlightme_pro_section_tooltip( 'career' );
                endif; ?>
                <div class="section-header-wrapper clear">
                    <div class="section-header">
                        <?php if( !empty( $career_sub_title ) ): ?>
                            <p class="section-subtitle"><?php echo esc_html( $career_sub_title ); ?></p>
                        <?php endif;
                        if( !empty( $career_title ) ):
                            ?>
                        <h2 class="section-title"><?php echo esc_html( $career_title ); ?></h2>
                    <?php endif; ?>
                </div><!-- .section-header -->

                <ul class="filter-tabs">

                    <?php 
                    foreach ( $content_details['cats'] as $i=>$content ) :
                        $terms =  get_term_by( 'id', $content, $content_details['taxonomy'] ); 
                    if(!is_wp_error( $terms ) && $terms): ?>
                    <li class="<?php echo ( $i==0 ? 'active' : ''); ?>"><a href="#" data-slug="<?php echo esc_attr($terms->slug); ?>"><?php echo esc_html( $terms->name ); ?></a></li>
                <?php endif; 
                endforeach; ?>
            </ul>
        </div>

        <div class="filtering-posts" data-slick='{"slidesToShow": 4, "slidesToScroll": 1, "infinite": true, "speed": 800, "dots": false, "arrows": true, "autoplay": <?php echo esc_attr( $career_autoplay ); ?>, "draggable": true, "fade": false }'>

            <?php foreach ( $content_details['details'] as $i => $content_detail ) : 
            $catid =  $content_details['cats'][$i];
            $cat = get_term_by( 'id', $catid, $content_details['taxonomy']);
            foreach ( $content_detail as $content ) : ?>

            <article class="has-post-thumbnail <?php echo esc_attr($cat->slug); ?>">
                <div class="featured-image" style="background-image: url('<?php echo esc_url($content['image']); ?>');">
                    <a href="<?php echo esc_url($content['url']); ?>" class="post-thumbnail-link"></a>
                </div><!-- .featured-image -->

                <div class="entry-container">
                    <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( $career_content_type == 'course-category' ){?>
                    <div class="entry-meta">
                        <span><ins><?php tp_course_price( $content['id'] ); ?></ins></span>
                        <?php tp_course_duration( $content['id'] ); ?>

                    </div><!-- .entry-meta -->
                    <?php } ?>


                    <div class="entry-content">
                        <div class="courses">
                            <a href="<?php echo esc_url($content['url']); ?>"><span class="courses"><?php echo esc_html( $options['career_post_btn'] ); ?>
                                <svg viewBox="0 0 476.213 476.213">
                                    <polygon points="476.213,238.105 400,161.893 400,223.106 0,223.106 0,253.106 400,253.106 400,314.32 "/>
                                </svg>


                            </span></a>
                        </div>

                        <div class="number">
                            <?php if( $career_content_type == 'course-category' ){?>
                            <span>
                                <?php echo enlightme_pro_get_svg( array( 'icon' => 'user' ) ) ; ?>
                                <?php tp_course_students( $content['id'] ); ?>
                            </span>
                            <?php } ?>
                            <span>
                                <?php echo enlightme_pro_get_svg( array( 'icon' => 'comment' ) ) ; ?>
                                <?php echo absint( get_comments_number( $content['id'] ) ); ?>
                            </span> 
                        </div>

                    </div><!-- .entry-content -->

                </div><!-- .entry-container -->
            </article>
        <?php endforeach;

        endforeach; ?>

    </div><!-- .filtering -->

    <?php if( !empty( $options['career_btn_url'] ) && !empty( $career_btn ) ): ?>
        <div class="read-more">
            <a href="<?php echo esc_url( $options['career_btn_url'] ); ?>" class="btn"><?php echo esc_html( $career_btn ); ?></a>
        </div><!-- .read-more -->
    <?php endif; ?>

</div><!-- .wrapper -->
</div><!-- #career -->

</div>

    <?php }
endif;