<?php
    $user_id = get_current_user_id();
    $catid = $args['catid'];
    $show = get_term_meta( $catid, 'mo_show_type', true);
    $show = $show ? $show : 'show';
    $show_num = get_term_meta( $catid, 'show_num', true);
    $hide_moments = get_option('moments_hide');
    $hide_moments = $hide_moments ? $hide_moments : '';
    $merge_hide = get_mo_hide_unjoin($user_id);
    $show_nav = true;
    $nav_type = get_op('moment_nav','btn');
?>
<div class="cls-blog-cat-filter" data="<?php echo $catid ?>">
    <?php //echo post_cat_filter(); ?>
</div>


<div id="moment-item" class="p-item box-p" uk-height-viewport="expand: true">

    <?php

        // 基础数组
        $sticky = get_option('sticky_posts');

        $mo_args = array(
            'post_type' => 'moment', 
            'post_status' => 'publish',
            'tax_query' => array(
                'relation' => 'AND',
            ),
            );
        
        // 如果是片刻首页 term_id为0    
       if($catid == 0){
            echo moment_sticky_loop();
            $mo_args['post__not_in'] = $sticky;
            
            if(!empty($merge_hide)){
                array_push( $mo_args['tax_query'], array (
                    'taxonomy' => 'moments',
                    'field' => 'term_id',
                    'terms' => $merge_hide,
                    'operator' => 'NOT IN',
                ));
            } else {
                $mo_args['tax_query'] = null;
            }

        }

        // 为圈子分类页面时
        if($catid != 0 && $catid > 0){
            array_push( $mo_args['tax_query'], array (
                'taxonomy' => 'moments',
                'field' => 'term_id',
                'terms' => $catid,
            ));

            if(!check_mo_joined() && $show == 'join'){
                if($show_num > 0){
                    $mo_args['posts_per_page'] = $show_num;
                   
                }
    
                if($show_num == 0 || $show_num == ''){
                    $mo_args = array();
                }

                echo '<div class="join-tips"><i class="ri-search-eye-line"></i>当前只可查看部分内容</div>';
    
                $show_nav = false;
            }

        }

    

        $my_query = new WP_Query($mo_args);

      
        
        if( $my_query->have_posts() ) {
            
            while ($my_query->have_posts()) : $my_query->the_post();

            get_template_part( 'tpl/content','moment');

            endwhile; 
            wp_reset_postdata(); 
        } else {
            echo '<div class="no-moment"><img src="'.THEME_URL.'/img/empty.png"></div>';
        }
      
        //print_r($my_query);
        $max = $my_query->max_num_pages;
        $cat = get_cu('cls_show_cats_de','0');
    ?>

</div>

<?php if($nav_type == 'btn' || $nav_type == 'scroll') { ?>
<div class="pagenav-box">
    <?php if ( $my_query->max_num_pages > 1 && $show_nav){ ?>
        <div class="number-pager">
            <div class="pager-info">
                <div class="paged-number">当前：<span class="current">1</span> / <span class="total"><?php echo $max ?></span></div>
                <div class="go-top"><a class="hint--top hint--rounded" href="#page" uk-scroll data-hint="回到顶部"><i class="ri-arrow-up-line"></i></a></div>
            </div>
        </div>
    
        <div class="load-more-box">
            <div class="loadmore-btn" data-paged="1" data-action="cls_load_moments" data-append="#moment-item" data-max="<?php echo $max ?>" data-cat="<?php echo $catid ?>">加载更多</div>
        </div>   
    <?php } ?>  
</div>
<?php } else { ?>

<div class="pagination-box" data-action="cls_load_moments" data-cat="<?php echo $catid ?>" data-max="<?php echo $max ?>" data-append="#moment-item" > 
<?php if($show_nav){
    $paged = max(1, get_query_var('paged'));
    echo paginate_links(array(
        'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
        'format' => '?paged=%#%',
        'current' => $paged,
        'total' => $max,
        'prev_text' => '上一页',
        'next_text' => '下一页',
        'type' => 'list'
    ));
}
    ?>
</div>
<?php } ?>

<div id="comment_form_tmp"><?php get_template_part( 'inc/layouts/moment-comment'); ?></div>