<?php
// Silence is golden.

	$settings = $this->get_settings_for_display();
    $id = $this->get_id();
    
    if( class_exists('SFWD_LMS') ) :
        $elementor_editor = \Elementor\Plugin::instance()->editor->is_edit_mode();
        if( $elementor_editor ) {
            $course_id = learndash_post_arr()[0];
        } else {
            $course_id = learndash_get_course_id();
        }

        if( is_user_logged_in() ) {
            $user_id = get_current_user_id();
        } else {
            $user_id = 0;
        }

        $coursecat = get_the_terms($course_id,'ld_course_category');
        $coursetag = get_the_terms($course_id,'ld_course_tag');

        
        $coursecatarr = [];
        $coursetagarr = [];

        if( $coursecat && ! is_wp_error($coursecat) ) {
            foreach($coursecat as $singlecat) {
                $coursecatarr[] = $singlecat->slug;
            }
        }

        if( $coursetag && ! is_wp_error($coursetag) ) {
            foreach($coursetag as $singletag) {
                $coursetagarr[] = $singletag->slug;
            }
        }


        /**
         * Query Argument
         */
        $query_args = array(
            "post_type" => "sfwd-courses",
            "posts_per_page"   => $settings['posts_per_page'],
            'orderby' => $settings['orderby'],
            'order'   => $settings['order'],
            "post__not_in"     => array($course_id),
            "tax_query" => array(
                "relation"  =>  "OR",
                array(
                    "taxonomy"  => "ld_course_category",
                    "field" => "slug",
                    "terms" => $coursecatarr
                ),
                array(
                    "taxonomy"  => "ld_course_tag",
                    "field" => "slug",
                    "terms" => $coursetagarr
                ),
            )
        );

        $query = new WP_Query($query_args);
        if( $query->have_posts() ) :
            ?>
                <div class="ld-related-course-wrapper">
                    <div class="row">
                        <?php 
                            while( $query->have_posts() ) :
                                $query->the_post();
                                
                                $relatedcoursecat = get_the_terms(get_the_ID(),'ld_course_category');
                                $relatedcoursecatarr = [];

                                if( $relatedcoursecat && ! is_wp_error($relatedcoursecat) ) {
                                    foreach($relatedcoursecat as $singlecat) {
                                        $relatedcoursecatarr[] = array(
                                            "id"    => $singlecat->term_id,
                                            "slug"    => $singlecat->slug,
                                            "name"    => $singlecat->name
                                        );
                                    }
                                }
                        ?>
                        <div class="col-md-<?php echo esc_attr($settings['columns']); ?> col-sm-6">
                            <article <?php post_class(array('ld-single-course-item')); ?> id="post-<?php the_ID(); ?>">
                                <?php if( has_post_thumbnail() ) : ?>
                                <figure class="ld-course-post-thumbnail">
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail(array(600,400)); ?>
                                    </a>
                                </figure><!-- .course-thumbnail -->
                                <?php endif; ?>
                                <div class="ld-course-content">
                                    <?php if( !empty( $relatedcoursecatarr ) ) : ?> 
                                        <div class="ld-course-meta">
                                            <a href="<?php echo esc_url( get_term_link($relatedcoursecatarr[0]["id"],"ld_course_category" ) ); ?>"> <?php echo esc_html( $relatedcoursecatarr[0]["name"] ); ?> </a>
                                        </div><!-- .course-title -->
                                    <?php endif; ?>
                                    <h2 class="ld-course-title">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php the_title(); ?>
                                        </a>
                                    </h2><!-- .course-title -->
                                </div>
                            </article>
                        </div>
                        <?php 
                            endwhile;
                            wp_reset_postdata();
                        ?>
                    </div>
                </div>
        <?php 
            else :
        ?>
            <h3><?php esc_html_e('No related Course Found', 'widgetkit-pro');?></h3>
        <?php
            endif;
        ?>
        
    <?php
        else :
    ?>
        <div class="plugin-active-massage">
            <h3><?php esc_html_e('Please activate LearnDash LMS plugin first', 'widgetkit-pro');?></h3>
        </div>
    <?php
    endif;