<?php
/**
 * defaultpro 移动端用户主页
 */
$currentPage = 'user';
$pageType = 'user';
$bodyClass = 'user-page';
$showBack = true;
$headerTitle = $username ?? '用户主页';
$pageData = ['targetId' => $targetId ?? 0];
$this->setLayout('app');
?>
<div class="user-profile-header" id="userProfile">
    <div class="loading-indicator">
        <div class="loading-spinner"></div>
    </div>
</div>

<div class="post-list" id="userPosts">
    <div class="loading-indicator">
        <div class="loading-spinner"></div>
    </div>
</div>

<script>
(function() {
    var targetId = document.body.dataset.targetId;
    var username = '<?= $this->escape($username ?? '') ?>';
    
    // 加载用户信息
    var profileUrl = targetId ? App.baseUrl + '/mobileApi/userProfile?id=' + targetId : App.baseUrl + '/mobileApi/userProfile?username=' + encodeURIComponent(username);
    $.get(profileUrl, function(res) {
        if (res.code === 0) {
            var user = res.data.user;
            var stats = res.data.stats || {};
            $('#userProfile').html(
                '<img class="user-avatar-large" src="'+user.avatar+'" alt="'+user.username+'">'+
                '<div class="user-name-large">'+user.username+'</div>'+
                '<div style="margin-bottom:12px;color:#666;font-size:13px;">'+ (user.bio || '这个人很懒，什么都没写') +'</div>'+
                '<div class="user-stats-row">'+
                    '<div class="user-stat"><div class="user-stat-num">'+(stats.following || 0)+'</div><div class="user-stat-label">关注</div></div>'+
                    '<div class="user-stat"><div class="user-stat-num">'+(stats.followers || 0)+'</div><div class="user-stat-label">粉丝</div></div>'+
                    '<div class="user-stat"><div class="user-stat-num">'+(stats.post_count || 0)+'</div><div class="user-stat-label">动态</div></div>'+
                '</div>'
            );
        }
    });
    
    // 加载用户动态
    if (targetId) {
        $.get(App.baseUrl + '/mobileApi/userPosts', {user_id: targetId}, function(res) {
            if (res.code === 0) {
                var html = (res.data.items || []).map(function(post) {
                    return App.renderPostCard(post);
                }).join('');
                $('#userPosts').html(html || '<div class="empty-state"><i class="fa-solid fa-inbox"></i>暂无动态</div>');
            }
        });
    }
})();
</script>
