<?php
use Elementor\Icons_Manager;

$wkp_placeholder_image = WKP_URL . 'dist/images/placeholder.jpg';

$settings = $this->get_settings();
$cat_multi_slug = widgetkit_pro_array_get($settings, 'cat_dynamic_multiple_id');
$course_thumbnail_size = widgetkit_pro_array_get($settings, 'image_size');
$post_num_to_show = widgetkit_pro_array_get($settings, 'post_num_to_show');

$sensei_instructor = widgetkit_pro_array_get($settings, 'sensei_instructor');
$show_meta = widgetkit_pro_array_get($settings, 'show_meta');
$show_description = widgetkit_pro_array_get($settings, 'show_description');
$excerpt_desc_word = widgetkit_pro_array_get($settings, 'excerpt_desc_word');
$show_lesson = widgetkit_pro_array_get($settings, 'show_lesson');
$read_more_button = widgetkit_pro_array_get($settings, 'read_more_button');


// $sensei_course_price = $settings['sensei_course_price'];
// $sensei_course_desc = widgetkit_pro_array_get($settings, 'sensei_course_desc');
// $sensei_course_btn = widgetkit_pro_array_get($settings, 'sensei_course_btn');

$nav_visibility = widgetkit_pro_array_get($settings, 'nav_visibility'); 
$posts_column = $settings['posts_column'];
$layout = widgetkit_pro_array_get($settings, 'courses_layout_style');
$active_carousel = $layout == 'active_carousel' ? 'yes' : '';
//$tax_term = get_term_meta($cat_id);
$a = '';

$sensei_lesson_custom_icon = widgetkit_pro_array_get($settings, 'sensei_lesson_add_custom_icon');
$sensei_lesson_icon = widgetkit_pro_array_get($settings, 'sensei_lesson_icon');
?>
<?php if (class_exists('Sensei_Main')) : // Check Sensei LMS plugin active?>

<div id="dynamic-category-content-<?php echo $this->get_id(); ?>" class="dynamic-category-content-<?php echo $this->get_id(); ?> dynamic-category-body">
    <div id="dynamic-layout" class="dynamic-layout dynamic-layout-<?php echo $this->get_id(); ?>">
        <div id="dynamic-category">
         

	               <div class="sensei-dynamic-category-wrapper">
                    <?php if ($cat_multi_slug): ?>
                        <div id="<?php echo $this->get_id(); ?>" class="multiple-cat">
                            
                            <?php
                                /**
                                 * get custom post type
                                 * data from slug
                                 */
                                foreach ($cat_multi_slug as $cat):
                                    $tax_data = get_term_by('slug', $cat, 'course-category');
                                    $cat_id = $tax_data->term_id;

                                    if ($a == 0) {
                                        $first_cat_ID = $cat_id;
                                    }
                                    /**
                                     * pn = post number to show
                                     * rts= regular thumb size
                                     */
                            ?>
                            <a href="<?php echo admin_url('admin-ajax.php?action=senseiDynamicCat&term_id=' . $tax_data->term_id . '&pn=' . $post_num_to_show . '&rts=' . $course_thumbnail_size) ?>"
                               class="cat-item <?php echo $a == 0 ? ' active' : ''; ?>"
                            >
                                <span class="tab-text"> <?php echo esc_html($tax_data->name); ?> </span>
                            </a>

                            <?php
                                $a++;
                            endforeach;

                            ?>
                        </div>
                    <?php else: ?>
                        <div id="<?php echo $this->get_id(); ?>" class="multiple-cat">
                            <a href="<?php echo admin_url('admin-ajax.php?action=senseiDynamicCat&term_id=' . $tax_data->term_id . '&pn=' . $post_num_to_show . '&rts=' . $course_thumbnail_size) ?>"
                               class="cat-item <?php echo $a == 0 ? ' active' : ''; ?>"
                            >
                                <span class="tab-text"> <?php echo esc_html('Most Recent'); ?> </span>
                            </a>
                        </div>
                    <?php endif ?>
                    </div>

            
        </div>
        <!-- category content -->
        <div
                data-showInstructor = "<?php echo $sensei_instructor; ?>"
                data-showMeta = "<?php echo $show_meta; ?>"
                data-showDescription = "<?php echo $show_description; ?>"
                data-excerptWord = "<?php echo $excerpt_desc_word; ?>"
                data-showLesson = "<?php echo $show_lesson; ?>"
                data-enrollButton = "<?php echo $read_more_button; ?>"
                data-postsColumn = "<?php echo $posts_column; ?>"
                data-activeCarousel = "<?php echo $active_carousel; ?>"
                data-senseiLessonCustomIcon = "<?php echo $sensei_lesson_custom_icon; ?>"
                class="dynamic-category-content cat-post-list dynamic-cat-post-list animated fadeIn">

                <div class="hidden-data" style="display:none;">
                    <?php 
                        Icons_Manager::render_icon( $sensei_lesson_icon, [ 'aria-hidden' => 'true', 'class' => 'lesson-custom-icon' ] ); 
                    ?>
                </div>

            <div id="sensei-dynamic-course-carousel" class="wkp-sensei-course-tab-content-wrapper <?php echo ($active_carousel=='yes') ? 'sensei-dynamic-course-active owl-carousel' : ''; ?>">
      <?php
        /**
         * Query for the post
         * custom post type
         * post type : sensei_course
         * category taxonomy : course_category
         */
        $count = 0;
        /**
        * if select any category for drop down
        * then assign that value to the $first_cat_id
        * if not then set it to null
        */
        if (isset($first_cat_ID)) {
            $first_cat_ID = $first_cat_ID;
        } else {
            $first_cat_ID = '';
        }
        if ($cat_multi_slug) :
            $args = array(
                'post_type' => 'course',
                'posts_per_page' => $post_num_to_show,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'course-category',
                        'field'    => 'slug',
                        'terms' => $cat_multi_slug[0],
                    ),
                ),
            );

        else :
            $args = array(
                'post_type' => 'course',
                'posts_per_page' => $post_num_to_show
            );  
        endif;
        
        $query = new WP_Query($args);
        if ($query->have_posts()):
            while ($query->have_posts()):
                $query->the_post();

            ?>

	                  <div class="sensei-single-courses <?php echo $active_carousel ? 'col-md-12' : ''; ?> col-md-<?php echo $posts_column; ?> col-sm-6">
                      <article id="post-<?php the_ID(); ?>" <?php post_class('sensei-single-course'); ?>>
                        <?php //if ( has_post_thumbnail() ):?>
                            <figure class="sensei-course-thumbnail">
                                <div class="sensei-img">
                                    <?php if ( has_post_thumbnail() ):?>
                                        <a href="<?php the_permalink(); ?>">
                                            <?php  the_post_thumbnail();?>
                                        </a>
                                    <?php else: ?>
                                        <a href="<?php the_permalink() ?>">
                                            <img src="<?php echo esc_url($wkp_placeholder_image);?>" alt="">
                                        </a>
                                    <?php endif; ?>
                                </div>
                            <?php if($sensei_instructor == 'yes'): ?>
                                <div class="author-wrapper">
                                    <div class="tx-author-img"><?php echo get_avatar( get_the_author_meta( 'ID' ), 55 ); ?></div>
                                </div>
                            <?php endif; ?>
                            </figure>
                        <?php //endif; ?>

                        <div class="course-content-body">

                            <header class="wkp-entry-header">
                                <h4 class="course-entry-title">
                                    <a href="<?php the_permalink();?>">
                                        <?php echo ucfirst(get_the_title(get_the_ID())); ?>                       
                                    </a>
                                </h4>       
                            </header><!-- .entry-header -->
                            
                            <?php if($show_meta):?>
                                <div class="post-cat">
                                    <ul>
                                    <?php 
                                    $course_cats = get_the_terms(get_the_ID(), 'course-category');
                                    foreach($course_cats as $cat):
                                    ?>
                                        <li><?php echo $cat->name; ?></li>
                                    <?php endforeach;?>
                                    </ul>
                                </div>
                            <?php endif;?>

                            <?php if ($show_description && $excerpt_desc_word):?>
                                <div class="wkp-entry-content">
                                    <p><?php esc_html_e(wp_trim_words( get_the_excerpt(), $excerpt_desc_word, ''), 'widgetkit-pro'); ?></p>
                                </div><!-- .entry-content -->
                            <?php endif; ?>

                            <footer class="wkp-entry-footer">
                                <div class="footer-body">
                                    <?php if($show_lesson): ?>
                                        <span class="sensei-enroll">
                                            <?php if( $sensei_lesson_custom_icon && $sensei_lesson_icon ):?>
                                                <?php Icons_Manager::render_icon( $sensei_lesson_icon, [ 'aria-hidden' => 'true', 'class' => 'lesson-custom-icon' ] ); ?>
                                            <?php else: ?>
                                                <i class="fa fa-play-circle"></i>
                                            <?php endif;?>
                                            <?php echo Sensei()->course->course_lesson_count( get_the_ID() ) . '&nbsp;' . esc_html__( 'Lessons', 'widgetkit-pro' ); ?>
                                        </span>
                                    <?php endif; ?>
                                    <?php if($read_more_button): ?>
                                        <span class="sensei-enroll-btn">
                                            <a href="<?php the_permalink();?>"><?php esc_html_e('Enroll', 'widgetkit-pro') ?></a>   
                                        </span>
                                    <?php endif; ?>
                                </div><!-- .entry-meta -->
                            </footer>
                        </div><!-- .course details -->

                    </article><!-- #post-## -->
                  </div>
            <?php $count++;
                endwhile;
               wp_reset_postdata(); wp_reset_query(); 
            endif;
        ?>
        </div>
        </div>
    </div><!-- div -->


  <script type='text/javascript'>
            <?php
                if ($active_carousel == 'yes'):
                $column = 12 / $posts_column;
                $rootId = '#dynamic-category-content-' . $this->get_id();
            ?>
	        jQuery(document).ready(function($) {
	          console.log();
	          $('<?php echo $rootId; ?> ').find('.sensei-dynamic-course-active').owlCarousel({
	              loop:false,
	              nav:true,
	              dots:false,
	              items: <?php echo $column ?>,
	              navClass: ['owl-carousel-left','owl-carousel-right'],
	              navText: ['<i class="fa fa-chevron-left" aria-hidden="true"></i>', '<i class="fa fa-chevron-right" aria-hidden="true"></i>'],
	              responsive:{
	                  0:{
	                      items:1
	                  },
	                  600:{
	                      items:2
	                  },
	                  1000:{
	                      items:<?php echo $column ?>
	                  }
	              }
	          });
	      });
	    <?php endif;?>


  </script>

  <!-- Start css style -->
    <style>
    <?php if ($settings['nav_visibility'] == 'always_visible'): ?>
        .widgetkit-sensei-course-1 .owl-nav .owl-carousel-left, .widgetkit-sensei-course-1 .owl-nav .owl-carousel-right, .widgetkit-course-1 .owl-nav .owl-carousel-left, .widgetkit-course-1 .owl-nav .owl-carousel-right, .dynamic-category-body .owl-nav .owl-carousel-left, .dynamic-category-body .owl-nav .owl-carousel-right {
            opacity: 1;
        }
    <?php endif;?>
  </style>

</div>
<?php else:  ?>
    <div class="plugin-active-massage">
        <h3><?php esc_html_e('Please activate Sensei LMS plugin first', 'widgetkit-pro');?></h3>

    </div>
<?php endif;  ?>
