{% extends 'django_spire/card/title_card.html' %}

{% block card_title %}
	Entries
{% endblock %}

{% block card_button %}
    {% if AuthController.knowledge.can_add %}
        <div class='container'>
            <div class='row'>
                <div class='col-auto'>
                    {% url 'django_spire:knowledge:entry:form:import' collection_pk=collection.pk as import_url %}
                    {% include 'django_spire/button/primary_button.html' with button_text='Import' button_href=import_url button_icon='bi bi-upload' %}
                </div>
                <div class='col-auto'>
                    {% url 'django_spire:knowledge:entry:form:create' collection_pk=collection.pk as create_url %}
                    {% include 'django_spire/button/primary_button.html' with button_text='Add' button_href=create_url button_icon='bi bi-plus' %}
                </div>
            </div>
        </div>
    {% endif %}
{% endblock %}

{% block card_title_content %}
    <div x-data="{
            async reorder_entries({entry_id, order}){
                let response = null
                try {
                    response = await django_glue_fetch(
                        '{% url "django_spire:knowledge:entry:json:reorder" %}',
                        {payload: {entry_id: entry_id, order: order, collection_id: {{ collection.id }}}}
                    )
                } catch (e) {
                    window.location.reload()
                }

                if (response.type === 'error') {
                    $dispatch('notify', {'type': response.type, 'message': response.message})
                }
            }
        }"
         x-sort="await reorder_entries({entry_id: $item, order: $position})"
    >
        {% for entry in current_entries %}
            <div x-sort:item="{{ entry.pk }}">
                {% include 'django_spire/knowledge/entry/item/list_item.html' %}
            </div>
        {% empty %}
            {% include 'django_spire/item/no_data_item.html' %}
        {% endfor %}
    </div>
{% endblock %}
