<?php
/**
 * defaultpro 移动端个人中心
 */
$currentPage = 'profile';
$pageType = 'profile';
$bodyClass = 'profile-page';
$this->setLayout('app');

$userModel = new UserModel();
$profileUser = $userModel->find($_SESSION['user_id']);
?>
<div class="user-profile-header">
    <img class="user-avatar-large" src="<?= Helper::avatar($profileUser['avatar'], $profileUser['username']) ?>" alt="头像">
    <div class="user-name-large"><?= $this->escape($profileUser['username']) ?></div>
    <div class="user-stats-row">
        <div class="user-stat">
            <div class="user-stat-num"><?= $profileUser['following_count'] ?? 0 ?></div>
            <div class="user-stat-label">关注</div>
        </div>
        <div class="user-stat">
            <div class="user-stat-num"><?= $profileUser['follower_count'] ?? 0 ?></div>
            <div class="user-stat-label">粉丝</div>
        </div>
        <div class="user-stat">
            <div class="user-stat-num"><?= $profileUser['post_count'] ?? 0 ?></div>
            <div class="user-stat-label">动态</div>
        </div>
    </div>
</div>

<div class="profile-menu">
    <a href="<?= $this->url('mobile/favorites') ?>" class="menu-item">
        <i class="menu-icon fa-solid fa-star" style="color:#ffad1f;"></i>
        <span class="menu-text">我的收藏</span>
        <span class="menu-arrow">›</span>
    </a>
    <a href="<?= $this->url('mobile/follows') ?>" class="menu-item">
        <i class="menu-icon fa-solid fa-user-plus"></i>
        <span class="menu-text">我的关注</span>
        <span class="menu-arrow">›</span>
    </a>
    <a href="<?= $this->url('mobile/fans') ?>" class="menu-item">
        <i class="menu-icon fa-solid fa-users"></i>
        <span class="menu-text">我的粉丝</span>
        <span class="menu-arrow">›</span>
    </a>
</div>

<div class="logout-section">
    <button class="logout-btn" id="logoutBtn">退出登录</button>
</div>

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

<script>
document.getElementById('logoutBtn').addEventListener('click', function() {
    if (confirm('确定要退出登录吗？')) {
        window.location.href = '<?= $this->url('mobile/logout') ?>';
    }
});

(function() {
    $.get(App.baseUrl + '/mobileApi/userPosts', {user_id: <?= $_SESSION['user_id'] ?>}, function(res) {
        if (res.code === 0) {
            var html = (res.data.items || []).map(function(post) {
                return App.renderPostCard(post);
            }).join('');
            $('#myPosts').html(html || '<div class="empty-state"><i class="fa-solid fa-inbox"></i>暂无动态</div>');
        }
    });
})();
</script>
