<?php
/**
 * defaultpro 移动端收藏页
 */
$currentPage = 'favorites';
$pageType = 'favorites';
$bodyClass = 'favorites-page';
$showBack = true;
$headerTitle = '我的收藏';
$this->setLayout('app');
?>
<div class="post-list" id="favoritesList">
    <div class="loading-indicator">
        <div class="loading-spinner"></div>
    </div>
</div>
<button class="load-more-btn" id="loadMoreBtn" style="display:none;">加载更多</button>

<script>
(function() {
    var page = 1;
    var loading = false;
    var hasMore = true;
    
    function loadFavorites() {
        if (loading || !hasMore) return;
        loading = true;
        
        $.get(App.baseUrl + '/mobileApi/userFavorites', {page: page}, function(res) {
            if (res.code === 0) {
                var items = res.data.items || [];
                var html = items.map(function(post) {
                    return App.renderPostCard(post);
                }).join('');
                if (page === 1 && items.length === 0) {
                    $('#favoritesList').html('<div class="empty-state"><i class="fa-solid fa-star"></i>暂无收藏</div>');
                } else {
                    $('#favoritesList').append(html);
                }
                hasMore = res.data.pagination ? res.data.pagination.has_more : false;
                page++;
                $('#loadMoreBtn').toggle(hasMore);
            }
            loading = false;
        });
    }
    
    loadFavorites();
    
    $(window).on('scroll', function() {
        if ($(window).scrollTop() + $(window).height() >= $(document).height() - 100) {
            loadFavorites();
        }
    });
    
    $('#loadMoreBtn').on('click', loadFavorites);
})();
</script>
