<?php
/**
 * Layout 2
 * =========
 * side style for post
 * ===================
 */
    $wkp_placeholder_image = WKP_URL . 'dist/images/placeholder.jpg';

    $settings = $this->get_settings();
    $layout = widgetkit_pro_array_get($settings, 'post_tab_layout');
    $layout_title = widgetkit_pro_array_get($settings, 'layout_title');
    $nav_tab_show = widgetkit_pro_array_get($settings, 'nav_tab_show');
    
    $multiple_category_id = widgetkit_pro_array_get($settings, 'multiple_category_id');
    $number_of_post_to_show = widgetkit_pro_array_get($settings, 'number_of_post_to_show');

    $primary_thumbnail_size = widgetkit_pro_array_get($settings, 'primary_thumbnail_size');
    $primary_content_position = widgetkit_pro_array_get($settings, 'primary_content_position');
    $primary_show_meta = widgetkit_pro_array_get($settings, 'primary_show_meta');
    $primary_show_excerpt = widgetkit_pro_array_get($settings, 'primary_show_excerpt');
    if($primary_show_excerpt){
        $primary_excerpt_length = widgetkit_pro_array_get($settings, 'primary_excerpt_length');
    }else{
        $primary_excerpt_length = -1;
    }

    
    $secondary_post_column = widgetkit_pro_array_get($settings, 'secondary_post_column');
    $secondary_image_position = widgetkit_pro_array_get($settings, 'secondary_image_position');
    $secondary_thumbnail_size = widgetkit_pro_array_get($settings, 'secondary_thumbnail_size');
    $secondary_show_meta = widgetkit_pro_array_get($settings, 'secondary_show_meta');
    $secondary_show_excerpt = widgetkit_pro_array_get($settings, 'secondary_show_excerpt');
    if($secondary_show_excerpt){
        $secondary_excerpt_length = widgetkit_pro_array_get($settings, 'secondary_excerpt_length');
    }
    $first_cat_ID = '';
?>
<div id="dynamic-layout" class="dynamic-layout dynamic-layout-<?php echo $this->get_id(); ?>">
    <?php if(empty($multiple_category_id)):?>
        <div class="empty-cat">
            <h3>Please select a category first</h3>
        </div>
    <?php endif;?>
    <div id="dynamic-category">
        <?php if($multiple_category_id): $a = 0; ?>
            <div class="dynamic-category-wrapper">

                <?php if($layout_title): ?>
                <div class="dynamic-layout-header">
                    <h4>
                        <?php echo esc_html($layout_title); ?>
                    </h4>
                </div>
                <?php endif; ?>

                <?php if($nav_tab_show):?>
                    <div id="<?php echo $this->get_id(); ?>" class="multiple-cat">
                        <?php
                            foreach ($multiple_category_id as $cat):
                                $tax_data = get_term($cat);
                                $cat_id = $tax_data->term_id;
                                if($a == 0){
                                    $first_cat_ID = $cat_id;
                                }
                                ?>

                                <a href="<?php echo admin_url( 'admin-ajax.php?action=dynamicCat&term_id=' . $tax_data->term_id . '&pn='.$number_of_post_to_show) ?>"
                                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 endif;?>
                
            </div>
        <?php endif; ?>
    </div>
        <!--layout-header-->
    <?php
    /**
     * Argument
     * for query post
     * ================
     */
    $count = 0;
    $args = array(
        'cat' => $first_cat_ID,
        'post_type' =>  'post',
        'posts_per_page' => $number_of_post_to_show,
    );
    $tax_term = get_term_meta($first_cat_ID);
    // $tax_tag_bg_color = widgetkit_pro_array_get($tax_term, 'magmax_tax_tag_bg_color')[0];

    $query = new WP_Query($args);

    ?>

    <div
            data-style="<?php echo $layout; ?>"
            data-primayThumbSize="<?php echo $primary_thumbnail_size; ?>"
            data-primaryContentPosition="<?php echo $primary_content_position; ?>"
            data-primaryMeta="<?php echo $primary_show_meta; ?>"
            data-primaryExcerpt="<?php echo $primary_show_excerpt; ?>"
            data-primaryExcerptLength="<?php echo $primary_excerpt_length; ?>"
            data-seondaryColumn="<?php echo $secondary_post_column; ?>"
            data-secondaryImagePosition="<?php echo $secondary_image_position; ?>"
            data-secondaryThumbSize="<?php echo $secondary_thumbnail_size; ?>"
            data-secondaryMeta="<?php echo $secondary_show_meta; ?>"
            data-secondaryExcerpt="<?php echo $secondary_show_excerpt; ?>"
            data-secondaryExcerptLength="<?php echo $secondary_excerpt_length; ?>"
        class="cat-post-list dynamic-cat-post-list animated fadeIn <?php echo 'layout-'.$layout ?> <?php echo 'grid-'.$secondary_post_column; ?> <?php echo 'regular-grid-'.$secondary_image_position; ?>">

        <?php
        if($query->have_posts()):
            ?>
            <div class="cat-post-list-wrapper <?php echo 'featured-style-'. $primary_content_position; ?>">

                <?php
                while($query->have_posts()):
                    $query->the_post();
                    ?>
                    <?php if($count == 0): ?>
                    <div class="featured-post" style="grid-row: span <?php echo $number_of_post_to_show - 1 ;?>">
                        <?php
                            if($primary_content_position === 'before-image'){
                                ?>
                                <div class="post-data">
                                    <div class="post-title">
                                        <a href="<?php the_permalink(); ?>" class="highlight-post-title highlight-title">
                                            <h3><?php the_title(); ?></h3>
                                        </a>
                                    </div>
                                    <?php if($primary_show_meta):?>
                                        <div class="wkp-post-meta">
                                            <div class="post-author"><?php echo get_the_author(); ?></div>
                                            <div class="seperator"> <span><?php esc_html_e('|', 'widgetkit-pro'); ?></span></div>
                                            <div class="post-date"><?php echo get_the_date(); ?></div>
                                        </div>
                                    <?php endif;?>
                                </div>
                                <?php
                            }
                        ?>
                        <?php if(has_post_thumbnail()): ?>
                            <div class="post-thumb">
                                <a href="<?php the_permalink() ?>">
                                    <?php the_post_thumbnail($primary_thumbnail_size); ?>
                                </a>
                            </div>
                        <?php else: ?>
                            <div class="post-thumb">
                                <a href="<?php the_permalink() ?>">
                                    <img src="<?php echo esc_url($wkp_placeholder_image);?>" alt="">
                                </a>
                            </div>
                        <?php endif; ?>
                        <?php if( $primary_content_position == 'overlay' || $primary_content_position == 'after-image' ): ?>
                        <div class="post-data">
                            <div class="post-title">
                                <a href="<?php the_permalink(); ?>" class="highlight-post-title highlight-title">
                                    <h3><?php the_title(); ?></h3>
                                </a>
                            </div>
                            <?php if($primary_show_meta):?>
                                <div class="wkp-post-meta">
                                    <div class="post-author"><?php echo get_the_author(); ?></div>
                                    <div class="seperator"> <span><?php esc_html_e('|', 'widgetkit-pro'); ?></span></div>
                                    <div class="post-date"><?php echo get_the_date(); ?></div>
                                </div>
                            <?php endif; ?>
                            <?php if($primary_show_excerpt):?>
                                <div class="post-excerpt">
                                    <?php echo wp_trim_words(get_the_excerpt(), $primary_excerpt_length, ''); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>

                        <?php if($primary_show_excerpt && $primary_content_position === 'before-image' ):?>
                            <div class="post-excerpt">
                            <?php echo wp_trim_words(get_the_excerpt(), $primary_excerpt_length, ''); ?>
                            </div>
                        <?php endif; ?>
                        
                    </div>
                <?php else: ?>
                    <div class="regular-post">
                        <div class="regular-post-wrapper">
                            <?php if(has_post_thumbnail()): ?>
                                <div class="post-thumb col-md-4">
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail($secondary_thumbnail_size ); ?>
                                    </a>
                                </div>
                            <?php endif; ?>
                            <div class="post-data col-md-8">
                                <div class="post-title">
                                    <a href="<?php the_permalink(); ?>" class="regular-post-title regular-title">
                                        <h3><?php the_title(); ?></h3>
                                    </a>
                                </div>
                                <?php if($secondary_show_meta):?>
                                <div class="wkp-post-meta">
                                    <div class="post-author"><?php echo get_the_author(); ?></div>
                                    <div class="seperator"> <span><?php esc_html_e('|', 'widgetkit-pro'); ?></span></div>
                                    <div class="post-date"><?php echo get_the_date(); ?></div>
                                </div>
                                <?php endif;?>
                                <?php if($secondary_show_excerpt):?>
                                    <div class="post-excerpt">
                                    <?php echo wp_trim_words(get_the_excerpt(), $secondary_excerpt_length, ''); ?>
                                    </div>
                                <?php endif;?>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
                    <?php
                    $count++;
                endwhile;
                wp_reset_postdata();
                ?>
            </div>
            <?php
        endif;
        ?>
    </div>
    <?php //wp_reset_query(); ?>
    <!--cat-post-list-->
</div>