{% extends "@ChillMain/layout.html.twig" %}

{% block css %}
    {{ parent() }}
    {{ encore_entry_link_tags('mod_saved_export_button') }}
    <style lang="css">
        .export-title {
            margin-top: 2rem;
            margin-bottom: 1rem;
        }
    </style>
{% endblock %}

{% block js %}
    {{ parent() }}
    {{ encore_entry_script_tags('mod_saved_export_button') }}
{% endblock %}

{% block title %}{{ 'saved_export.Saved exports'|trans }}{% endblock %}


{% macro render_export_card(saved, export, export_alias, generations) %}
    <div class="col">
        <div class="card h-100">
            <div class="card-header">
                {{ export.title|trans }}
            </div>
            <div class="card-body">
                <h2 class="card-title">{{ saved.title }}</h2>
                {% if app.user is same as saved.user %}
                    <p class="card-text tags">
                        {% if app.user is same as saved.user %}<span class="badge bg-primary">{{ 'saved_export.Owner'|trans }}</span>{% endif %}
                        {% if saved.isShared() %}<span class="badge bg-info">{{  'saved_export.Shared with others'|trans }}</span>{% endif %}
                    </p>
                {% else %}
                    <p class="card-text tags">
                        Partagé par <span class="badge-user">{{ saved.user|chill_entity_render_box }}</span>
                    </p>
                {% endif %}
                <p class="card-text my-3">{{ saved.description|chill_markdown_to_html }}</p>
            </div>
            {% if generations|length > 0 %}
                <ul class="list-group list-group-flush">
                    {% for generation in generations %}
                        <li class="list-group-item">
                            <a href="{{ chill_path_add_return_path('chill_main_export-generation_wait', {'id': generation.id}) }}">{{ generation.createdAt|format_datetime('short', 'short') }}</a>
                            {% if generation.status == 'pending' %}
                                &nbsp;<span class="badge bg-info">{{ 'export.generation.Export generation is pending_short'|trans }}</span>
                            {% elseif generation.status == 'failure' %}
                                &nbsp;<span class="badge bg-warning">{{ 'export.generation.Error_short'|trans }}</span>
                            {% endif %}
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
            <div class="card-footer">
                <ul class="record_actions slim">
                    <li>
                        <div class="" data-generate-export-button data-saved-export-uuid="{{ saved.id|escape('html_attr') }}"></div>
                    </li>
                    <li>
                        <div class="dropdown">
                            <button class="btn btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                {{ 'Actions'|trans }}
                            </button>
                            <ul class="dropdown-menu">
                                {% if is_granted('CHILL_MAIN_EXPORT_SAVED_EDIT', saved) %}
                                    <li><a href="{{ chill_path_add_return_path('chill_main_export_saved_edit', {'id': saved.id }) }}" class="dropdown-item"><i class="fa fa-pencil"></i> {{ 'saved_export.update_title_and_description'|trans }}</a></li>
                                {% endif %}
                                {# reminder: the controller already checked that the user can generate saved exports #}
                                <li><a href="{{ chill_path_add_return_path('chill_main_export_new', {'alias': saved.exportAlias,'from_saved': saved.id }) }}" class="dropdown-item"><i class="fa fa-pencil"></i> {{ 'saved_export.update_filters_aggregators_and_execute'|trans }}</a></li>
                                {% if is_granted('CHILL_MAIN_EXPORT_SAVED_DUPLICATE', saved) %}
                                    <li><a href="{{ chill_path_add_return_path('chill_main_export_saved_duplicate', {'id': saved.id}) }}" class="dropdown-item"><i class="fa fa-copy"></i> {{ 'saved_export.Duplicate'|trans }}</a></li>
                                {% endif %}
                                {% if is_granted('CHILL_MAIN_EXPORT_SAVED_DELETE', saved) %}
                                    <li><a href="{{ chill_path_add_return_path('chill_main_export_saved_delete', {'id': saved.id }) }}" class="dropdown-item"><i class="fa fa-trash"></i> {{ 'Delete'|trans }}</a></li>
                                {% endif %}
                            </ul>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
{% endmacro %}

{% block content %}
    <div class="col-md-10 exports-list">

        {{ include('@ChillMain/Export/_navbar.html.twig', {'current' : 'my'}) }}

        <div class="container mt-4">
            {{ filter|chill_render_filter_order_helper }}

            {% if total == 0 %}
                <p class="chill-no-data-statement" >{{ 'saved_export.Any saved export'|trans }}</p>
            {% endif %}

            {% for group, saveds in grouped_exports %}
                {% if group != '_' %}
                    <h1 class="display-6 export-title">{{ group }}</h1>
                    <div class="row row-cols-1 row-cols-md-3 g-2">
                        {% for s in saveds %}
                            {{ _self.render_export_card(s.saved, s.export, s.saved.exportAlias, last_executions[s.saved.id.toString()]) }}
                        {% endfor %}
                    </div>
                {% endif %}
            {% endfor %}

            {% if grouped_exports|keys|length > 1 and grouped_exports['_']|default([])|length > 0 %}
                <h2 class="display-6 export-title">{{ 'Ungrouped exports'|trans }}</h2>
            {% endif %}

            <div class="row flex-bloc">
                {% for saveds in grouped_exports['_']|default([]) %}
                    {% for s in saveds %}
                        {{ _self.render_export_card(s.saved, s.export, s.saved.exportAlias, last_executions[s.saved.id.toString()]) }}
                    {% endfor %}
                {% endfor %}
            </div>
        </div>
    </div>
{% endblock %}
