<?php
$settings                 = $this->get_settings();
$cat_multi_slug           = widgetkit_pro_array_get($settings, 'cat_dynamic_multiple_id');
$course_thumbnail_size    = widgetkit_pro_array_get($settings, 'course_thumbnail_size');
$post_num_to_show         = widgetkit_pro_array_get($settings, 'post_num_to_show');
$lp_course_rating          = widgetkit_pro_array_get($settings, 'lp_course_rating');
$lp_course_enroll          = widgetkit_pro_array_get($settings, 'lp_course_enroll');
$lp_course_price          = widgetkit_pro_array_get($settings, 'lp_course_price');
$lp_course_cat            = widgetkit_pro_array_get($settings, 'lp_course_cat');
$lp_course_show_rating    = widgetkit_pro_array_get($settings, 'lp_course_show_rating');
$lp_course_average_rating = widgetkit_pro_array_get($settings, 'lp_course_average_rating');
$posts_column             = widgetkit_pro_array_get($settings, 'posts_column');
$active_carousel          = widgetkit_pro_array_get($settings, 'active_carousel');
$hide_tab_menu            = widgetkit_pro_array_get($settings, 'hide_tab_menu');
$show_course_description        = widgetkit_pro_array_get($settings, 'show_course_description');
$description_word_count        = widgetkit_pro_array_get($settings, 'description_word_count');
/**
 * check learnpress
 */
$learnpress_main_file = 'learnpress/learnpress.php';
$learnpress_rating_plugin = 'learnpress-course-review/learnpress-course-review.php';
$installed_plugins = get_plugins();
$site_has_learnpress = isset($installed_plugins[$learnpress_main_file]);
$site_has_lp_rating_plugin = isset($installed_plugins[$learnpress_rating_plugin]);

?>
<?php if ($site_has_learnpress  && $site_has_lp_rating_plugin) : ?>
    <div id="learnpress-course-tab-content-<?php echo $this->get_id(); ?>" class="<?php echo empty($cat_multi_slug) ? 'no-category' : ''; ?> learnpress-course-tab-wrapper learnpress-course-tab-content-<?php echo $this->get_id(); ?> learnpress-course-tab-body">
        <?php if (class_exists('LearnPress') && class_exists('LP_Addon_Course_Review_Preload')) : ?>

            <!-- if no category in choosed -->
            <?php if (empty($cat_multi_slug)) : ?>
                <div class="empty-cat">
                    <h3>Please select a category first</h3>
                </div>
            <?php endif; ?>

            <div id="dynamic-layout" class="dynamic-layout dynamic-layout-<?php echo $this->get_id(); ?>">
                <div id="learnpress-course-tab" class="lp-course-tab-nav">
                    <?php if ($cat_multi_slug) : $a = 0; ?>

                        <div class="learnpress-course-tab-cat-wrapper" style="<?php if ($hide_tab_menu == false) : ?>display:none;<?php endif; ?>">
                            <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=learnpressCourseTab&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>
                        </div>

                    <?php endif; ?>
                </div>
                <!-- category content -->
                <div data-coursePrice="<?php echo $lp_course_price; ?>" data-lpcourseRating="<?php echo $lp_course_rating; ?>" data-lpcourseEnroll="<?php echo $lp_course_enroll; ?>" data-courseCategory="<?php echo $lp_course_cat; ?>" data-postsColumn="<?php echo $posts_column; ?>" data-showCourseDescription="<?php echo $show_course_description; ?>" data-descriptionWordCount="<?php echo $description_word_count; ?>" data-activeCarousel="<?php echo $active_carousel; ?>" data-lpCourseStyle="<?php echo $lp_course_style; ?>" class="learnpress-course-tab-content cat-post-list dynamic-cat-post-list animated fadeIn">

                    <div id="lp-dynamic-course-carousel" class="<?php echo $active_carousel ? 'lp-dynamic-course-active owl-carousel' : 'wkp-lp-course-tab-grid'; ?>">


                        <?php
                        /**
                         * Query for the post
                         * custom post type
                         * post type : lp_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 = '';
                        }
                        /**
                         * Start Query
                         */
                        $args = array(
                            'post_type'      => 'lp_course',
                            'posts_per_page' => $post_num_to_show,
                            'tax_query'      => array(
                                array(
                                    'taxonomy' => 'course_category',
                                    'field'    => 'term_id',
                                    'terms'    => $first_cat_ID,
                                ),
                            ),
                        );
                        $query = new WP_Query($args);
                        if ($query->have_posts()) :
                            while ($query->have_posts()) :
                                $query->the_post();
                        ?>
                                <div class="wkp-lp-single-course <?php echo $active_carousel ? 'col-md-12' : ''; ?> col-sm-<?php echo $posts_column; ?>">
                                    <article class="learnpress-course-tab-course">
                                        <!-- thumbnail -->
                                        <?php if (has_post_thumbnail()) : ?>
                                            <figure class="course-thumbnail">
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php the_post_thumbnail($course_thumbnail_size); ?>
                                                </a>
                                            </figure>
                                        <?php else : ?>
                                            <figure class="course-thumbnail">
                                                <a href="<?php the_permalink(); ?>">
                                                    <img src="<?php echo esc_url(WKP_PLACEHOLDER_URL); ?>" alt="widgetitpro-placeholder">
                                                </a>
                                            </figure>
                                        <?php endif; ?>
                                        <!-- content -->
                                        <div class="course-details">
                                            <!-- title -->
                                            <header class="wkp-entry-header course-card-title">
                                                <h4 class="course-entry-title">
                                                    <a class="course-title-link" href="<?php the_permalink(); ?>">
                                                        <?php the_title(); ?>
                                                    </a>
                                                </h4>
                                            </header>

                                            <div class="course-meta addons-course-meta">
                                                <div class="price-and-cat">
                                                    <!-- price -->
                                                    <?php if ($lp_course_price == 'yes') : ?>
                                                        <?php $course = LP_Global::course(); ?>
                                                        <?php if ($price_html = $course->get_price_html()) : ?>
                                                            <span class="addon-price">
                                                                <?php echo $price_html; ?>
                                                                <?php if ($course->get_origin_price() != $course->get_price()) : ?>
                                                                    <?php $origin_price_html = $course->get_origin_price_html(); ?>
                                                                    <span class="addon-origin-price"><?php echo $origin_price_html; ?></span>
                                                                <?php endif; ?>
                                                            </span>
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                    <!-- category -->
                                                    <?php if ($lp_course_cat == 'yes') : ?>
                                                        <span class="post-date">
                                                            <?php learn_press_course_categories(); ?>
                                                        </span>
                                                    <?php endif; ?>
                                                </div>
                                                <div class="enroll-student">
                                                    <!-- enrolled student -->
                                                    <?php if ($lp_course_enroll == 'yes') : ?>
                                                        <?php $lp_students = get_post_meta(get_the_ID(), '_lp_students'); ?>
                                                        <span class="post-view">
                                                            <i class="fas fa-user-o"></i><?php echo esc_html($lp_students[0]);  ?>
                                                        </span>
                                                    <?php endif; ?>
                                                </div>

                                            </div>

                                            <!-- rating -->
                                            <?php if (class_exists('LP_Addon_Course_Review_Preload')) : ?>
                                                <?php if ($lp_course_rating) : ?>
                                                    <div class="rating">
                                                        <?php
                                                        $course_id       = get_the_ID();
                                                        $course_rate_res = learn_press_get_course_rate($course_id, false);
                                                        $course_rate     = $course_rate_res['rated'];
                                                        $total           = $course_rate_res['total'];
                                                        ?>

                                                        <div class="lp-course-rate-wapper">
                                                            <ul class="lp-course-rate clearfix">
                                                                <li>
                                                                    <?php echo '<div class="star-rating" title="' . sprintf(__('Rated %s out of 5', 'widgetkit-pro'), $course_rate) . '"><span style="width:' . (($course_rate / 5) * 100) . '%"><strong itemprop="ratingValue" class="rating">' . $course_rate . '</strong> ' . __('out of 5', 'widgetkit-pro') . '</span></div>'; ?>
                                                                    <?php printf(__(' %1.2f ', 'widgetkit-pro'), $course_rate); ?>
                                                                </li>
                                                            </ul>
                                                        </div>
                                                    </div>
                                                <?php endif; ?>
                                            <?php endif; ?>

                                            <?php if ($show_course_description) : ?>
                                                <div class="wkp-entry-content course-card-description">
                                                    <p>
                                                        <?php esc_html_e(wp_trim_words(get_the_content(), $description_word_count, ''), 'widgetkit-pro'); ?>
                                                    </p>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                    </article>
                                </div>
                            <?php $count++;
                            endwhile;
                            wp_reset_postdata();
                        else :
                            ?>
                            <h3 class="no-course">You have no course on this category. Please add some course on this category</h3>
                        <?php
                        endif;
                        ?>
                    </div>
                </div>
            </div><!-- div -->


            <script type='text/javascript'>
                <?php
                if ($settings['active_carousel'] == 'yes') :
                    $column = 12 / $posts_column;
                    $rootId = '#learnpress-course-tab-content-' . $this->get_id();
                ?>
                    jQuery(document).ready(function($) {
                        $('<?php echo $rootId; ?> ').find('.lp-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-angle-left fa-fw"></i>', '<i class="fa fa-angle-right fa-fw"></i>'],
                            responsive: {
                                0: {
                                    items: 1
                                },
                                600: {
                                    items: 2
                                },
                                1000: {
                                    items: <?php echo $column ?>
                                }
                            }
                        });
                    });
                <?php endif; ?>
            </script>

            <!-- Start css style -->
            <style>
                <?php if ($settings['carousel_nav_visible'] == 'yes') : ?>.learnpress-course-tab-body .owl-carousel-left {
                    opacity: 1;
                }

                .learnpress-course-tab-body .owl-carousel-right {
                    opacity: 1;
                }

                .learnpress-course-tab-body .owl-carousel-left {
                    opacity: 1;
                }

                .learnpress-course-tab-body .owl-carousel-right {
                    opacity: 1;
                }

                <?php endif; ?>
            </style>
        <?php else : ?>
            <div class="plugin-is-deactive">
                <div class="wrapper">
                    <?php if (!class_exists('LearnPress')) : ?>
                        <h3><?php esc_html_e('Please active LearnPress First', 'widgetkit-pro') ?></h3>
                    <?php endif; ?>
                    <?php if (!class_exists('LP_Addon_Course_Review_Preload')) : ?>
                        <h3><?php esc_html_e('Please active learnpress course review plugin for this layout', 'widgetkit-pro') ?></h3>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
<?php else : ?>
    <div class="plugin-is-not-exists">
        <div class="wrapper">
            <h3>Please install & active LearnPress First</h3>
        </div>
    </div>
<?php endif; ?>