{% load djacore_tags default_tags %}<div class="list">    
    {% for item in articles %}
        <div class="border rounded p-md-3 p-2 mt-3 d-flex  flex-nowrap">
            <a href="{{ item.get_absolute_url }}" rel="nofollow"><img src="{% if item.thumb_image %}{{ uploadpath }}{{item.thumb_image}}{%else%}{% nopic cate=item.category.slug %}{% endif %}" alt="{{ item.title }}"  class="rounded me-3"  onerror="javascript:this.style.display='none'"></a>
            <div class="flex-column d-flex">
                <p class="mb-3 fw-bold">{% if not category.is_leaf_node %}[<a href="{{ item.category.get_absolute_url }}">{{item.category.name}}</a>]{% endif %}<a href="{{ item.get_absolute_url }}">{{ item.title }}</a></p>
                <p class="text-secondary hidden-md">{{ item.meta_description|slice:'120' }}</p>
                <p class="text-secondary mt-auto"> <small>{{ item.update_time }}</small></p>
            </div>
        </div> 
    {% endfor %}
</div>
{% if json_ld_enabled %}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "ItemList",
  "@id": "{{ request.build_absolute_uri }}#list",
  "name": "{% if obj.seo_title %}{{obj.seo_title}}{%  else %}{{ obj.name }}{% endif %}",
  "description": "{{ obj.description }}",
  "numberOfItems": "{{ articles|length }}",
  "itemListElement": [
    {% for item in articles %}
    {
      "@type": "ListItem",
      "position": {{ forloop.counter }},
      "url": "{{ site_url }}{{ item.get_absolute_url }}"
    }{% if not forloop.last %},{% endif %}
    {% endfor %}
  ]
}
</script>
{% endif %}
{% if has_next %}
<div class="mt-3 text-center"> <button class="load-more btn btn-primary">加载更多</button></div>
{% endif %}
<script>
 
// 全局分页参数配置
const params = {
    category: {% if category %}{{category.pk}}{% else %}null{% endif %} ,
    includeChildren: {% if category.is_leaf_node %}false{% else %}true{% endif %},
    pageSize: 10,
    cursor: {% if next_cursor %}{{ next_cursor}}{% else %}null{% endif %},
    hasNext: false
};

function loadArticles() {
    const container = $('.list');
    const loadMoreBtn = $('.load-more');
    
    $.ajax({
        url: '/api/articles/',
        data: {
            category: params.category,
            include_children: params.includeChildren,
            page_size: params.pageSize,
            cursor: params.cursor,
        },
        beforeSend: function() {
            loadMoreBtn.prop('disabled', true).text('加载中...');
        },
        success: function(response) {
            // 更新分页参数
            params.hasNext = response.has_next;
            params.cursor = response.next_cursor;
            
            response.results.forEach(article => {
                const articleHtml = `
                    <div class="border rounded p-md-3 p-2 mt-3 d-flex flex-nowrap">
                        <a href="${article.absolute_url}" rel="nofollow">
                            <img src="${article.thumb_image}" alt="${article.title}" class="rounded me-3" onerror="this.style.display='none'">
                        </a>
                        <div class="flex-column d-flex">
                            <p class="mb-3 fw-bold">
                                {% if not category.is_leaf_node %}[<a href="${article.category.url}">${article.category.name}</a>]{% endif %}
                                <a href="${article.absolute_url}">${article.title}</a>
                            </p>
                            <p class="text-secondary hidden-md">${article.meta_description}</p>
                            <p class="text-secondary mt-auto"><small>${new Date(article.update_time).toLocaleString()}</small></p>
                        </div>
                    </div>
                `;
                container.append(articleHtml);
            });

            if (params.hasNext) {
                loadMoreBtn.prop('disabled', false)
                    .text('加载更多')
                    .data('cursor', params.cursor);
            } else {
                loadMoreBtn.remove();
            }
        },
        error: function() {
            loadMoreBtn.prop('disabled', false).text('加载失败，重试');
        }
    });
}

// 初始化加载
$(document).ready(function() {
    
    $(document).on('click', '.load-more', function() {
        loadArticles();
    });
});

    
</script>