<?php
// 管理中心动态页面 - 显示用户的文章、片刻、圈子、评论、收藏
$user_id = get_current_user_id();
$tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'posts';

function admin_dynamic_nav($type, $uid) {
    $html = '';
    $counts = get_user_content_counts($uid);

    $args = array();

    $args['posts'] = array(
        'title'     => '文章',
        'tab'     => 'posts',
        'icon'     => '<i class="ri-article-line"></i>',
        'count'     => $counts['posts'],
        'skeleton' => 'post-list',
    );

    $args['moment'] = array(
        'title'     => '片刻',
        'tab'     => 'moment',
        'icon'     => '<i class="ri-donut-chart-line"></i>',
        'count'     => $counts['moment'],
        'skeleton' => 'moment-list',
    );

    $args['moments'] = array(
        'title'     => '圈子',
        'tab'     => 'moments',
        'icon'     => '<i class="ri-bubble-chart-line"></i>',
        'count'     => $counts['moments'],
        'skeleton' => 'moments-list',
    );

    $args['comments'] = array(
        'title'     => '评论',
        'tab'     => 'comments',
        'icon'     => '<i class="ri-chat-3-line"></i>',
        'count'     => $counts['comments'],
        'skeleton' => 'user-comment',
    );

    $args['collect'] = array(
        'title'     => '收藏',
        'tab'     => 'collect',
        'icon'     => '<i class="ri-star-smile-line"></i>',
        'count'     => $counts['collect'],
        'skeleton' => 'post-list',
    );

    foreach($args as $key => $value){
        $active = $type === $key ? 'active' : '';
        $tab_url = '/wp-json/ppo/v1/user-'.$key.'?page=1&user_id='.$uid.'&target='.urlencode('#admin-dynamic-content').'&push_url_base='.urlencode(home_url('/dashboard'));
        $html .= '<a href="'.esc_url($tab_url).'"
            hx-get="'.esc_url($tab_url).'"
            hx-target="#admin-dynamic-content"
            hx-swap="innerHTML"
            hx-history="false"
            data-skeleton="'.$value['skeleton'].'"
            class="user-tab user-'.$key.'-tab '.$active.'">
            <span class="icon">'.$value['icon'].'</span>
            <span class="title">'.$value['title'].'<small>'.$value['count'].'</small></span>
        </a>';
    }

    return '<div class="ppo-navtab big-rounded uk-flex-center">'.$html.'</div>';
}

$api_url = '/wp-json/ppo/v1/user-'.$tab.'?page=1&user_id='.$user_id.'&target='.urlencode('#admin-dynamic-content').'&push_url_base='.urlencode(home_url('/dashboard'));
?>
<div class="user-index">
    <div class="admin-dynamic">
        <?php echo admin_dynamic_nav($tab, $user_id); ?>
        <div id="admin-dynamic-content" class="admin-dynamic-content dash-dynamic user-dash-box"
            hx-get="<?php echo esc_url($api_url); ?>"
            hx-trigger="load"
            hx-swap="innerHTML">
        </div>
    </div>
</div>
