{#
SPDX-FileCopyrightText: 2026 Martin Kirchgessner

SPDX-License-Identifier: AGPL-3.0-or-later
#}

{% extends 'layout.jinja' %}
{% block title %}Carts{% endblock title %}

{% from 'carts/macros.jinja' import cart_summary %}
{% set can_carts = current_user.user.can_carts() %}
{% block content %}
<section class="content">
<div class="container">
<h1>
    Carts
</h1>

<p>
    {% if can_carts %}
    <a
        class="button is-primary"
        href="{{ url_for('carts.add_form')}}"
        aria-label="Create cart"
        >
        Create cart
    </a>
    {% endif %}
</p>

<table class="table is-fullwidth is-striped is-hoverable" aria-label="Carts list">
    <thead>
        <tr>
            <th scope="col">Title</th>
            <th scope="col">Notes</th>
            <th scope="col">Sounds</th>
            <th scope="col">Average duration</th>
            <th scope="col">Schedule</th>
            <th scope="col">Actions</th>
        </tr>
    </thead>
    <tbody id="cartsrows">
        {% for cart in carts %}
        <tr class="fade-in fade-out" title="{{ cart.title }}">
            <td>
                {% if can_carts %}
                <a href="{{ url_for('carts.edit_form', cart_id=cart.id) }}">
                {% endif %}
                    {{ cart.title | safe }}
                {% if can_carts %}
                </a>
                {% endif %}
            </td>
            <td>{{ cart.notes | safe }}</td>
            <td>
                {% if cart.mode == CartMode.RELAY%}
                    <a href="{{cart.url}}" target="_blank">
                        {{ hostname_span(cart.url) }}
                    </a>
                {% else %}
                    {% if cart.sounds_count %}
                        <a href="{{ url_for('carts.sounds_list', cart_id=cart.id) }}">
                            {% if cart.mode == CartMode.PLAYLIST %}
                                <span aria-role="tooltip" title="Playlist">
                                    {{ icon('⬇️') }}
                                </span>
                            {% elif cart.mode == CartMode.PLAYLIST_LOOP %}
                                <span aria-role="tooltip" title="Looping playlist">
                                    {{ icon('🔃') }}
                                </span>
                            {% elif cart.mode == CartMode.RANDOM %}
                                <span aria-role="tooltip" title="Random">
                                    {{ icon('🔀') }}
                                </span>
                            {% endif %}

                            {{ cart_summary(cart, swap=False) }}
                        </a>
                    {% elif can_carts %}
                        <label for="addsound">Add :</label>
                        <input id="addsound"
                            type="file"
                            name="sounds"
                            multiple
                            hx-post="{{ url_for('carts.add_sounds', cart_id=cart.id) }}"
                            hx-encoding="multipart/form-data"
                            hx-trigger="change"
                        >
                    {% endif %}
                {% endif %}
            </td>
            <td>
                {% if cart.mode != CartMode.RELAY%}
                    {{ duration_span(cart.average_duration) }}
                {% endif %}
                {% if cart.max_duration %}
                    (limit: {{ cart.max_duration // 60 }}:{{ '%02d' % (cart.max_duration % 60) }})
                {% endif %}
            </td>
            <td class="{{ 'is-danger' if not cart.schedule_correct else
                'is-warning' if cart.schedule_mode == ScheduleMode.JINGLES else
                'is-primary' if cart.schedule_mode == ScheduleMode.TIMED else '' }}">
                {% if cart.schedule_mode == ScheduleMode.JINGLES %}
                    Jingles
                {% else %}
                    Timed
                {% endif %}
                {% if not cart.schedule_correct %}
                    error
                {% endif %}
            </td>
            <td>
                {% if cart.mode != CartMode.RELAY%}
                    <a title="Manage sounds" href="{{ url_for('carts.sounds_list', cart_id=cart.id) }}">
                        {{ icon('🗃️') }}
                    </a>
                {% endif %}
                {% if can_carts %}
                    <a title="Edit" href="{{ url_for('carts.edit_form', cart_id=cart.id) }}">
                        {{ icon('✏️') }}
                    </a>
                    <button title="Delete"
                        hx-delete="{{ url_for('carts.delete', cart_id=cart.id) }}"
                        hx-on::confirm="confirmWithMessage(`Really delete cart {{cart.title}}?`)"
                        hx-swap="outerHTML"
                        hx-target="closest tr"
                    >
                        {{ icon('❌') }}
                    </button>
                {% endif %}
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
</div>
</section>
{% endblock content %}
