<turbo-frame id="chapter_list">
    <div class="p-4" {{ turbo_stream_listen('App\\Entity\\Chapter') }}>
        {% for volume, chapters in chapters_by_volume %}
            {% set is_first = loop.first %}
            {% set volume_cbz_path = chapters|first.cbzPath %}
            {% set all_chapters_same_cbz = chapters|reduce((carry, chapter) => carry and chapter.cbzPath == volume_cbz_path, true) %}
            {% set available_chapters = chapters|filter(chapter => chapter.cbzPath is not null) %}
            {% set total_chapters = chapters|filter(chapter => chapter.visible)|length %}

            <div data-controller="table" data-table-open-value="{{ is_first ? 'true' : 'false' }}">
                <div class="bg-white rounded-sm shadow mb-4">
                    <div class="relative flex items-center justify-between bg-white p-4 rounded-t-sm">
                        <!-- Partie gauche -->
                        <div class="flex items-center space-x-4">
                            <i class="fas fa-bookmark text-gray-500 text-3xl w-8"></i>
                            <h2 class="text-xl font-semibold w-28">Volume {{ '%02d'|format(volume) }}</h2>
                            <div class="flex items-center w-16">
                                <span class="px-2 py-1 text-sm rounded w-full text-center
                                    {% if available_chapters|length == 0 %}
                                        bg-red-500 text-white
                                    {% elseif available_chapters|length < total_chapters %}
                                        bg-yellow-500 text-white
                                    {% else %}
                                        bg-green-500 text-white
                                    {% endif %}">
                                    {{ available_chapters|length }} / {{ total_chapters }}
                                </span>
                            </div>
                        </div>

                        <div class="absolute left-1/2 top-1/2 transform -translate-x-1/2 -translate-y-1/2">
                            <i data-table-target="toggleIcon" data-action="click->table#toggle"
                               class="bg-gray-400 rounded-full font-bold text-sm text-white p-1 hover:bg-green-500 fas fa-chevron-{{ is_first ? 'up' : 'down' }} cursor-pointer"></i>
                        </div>

                        <div class="flex space-x-2 text-xl text-bold">
                            <a href="#"
                               data-controller="download"
                               data-action="download#download"
                               data-download-url-value="{{ path('search_volume', {'mangaSlug': manga.slug, 'volume': volume}) }}"
                               class="w-8 text-center">
                                <i data-download-target="icon"
                                   class="fas fa-search text-gray-500 hover:text-green-500"></i>
                            </a>
                            <a href="#"
                               data-controller="download"
                               data-action="download#download"
                               data-download-url-value="{{ path('download_volume', {'mangaSlug': manga.slug, 'volume': volume}) }}"
                               class="w-8 text-center">
                                <i data-download-target="icon"
                                   class="fas fa-download text-gray-500 hover:text-green-500"></i>
                            </a>
                        </div>
                    </div>

                    <div data-table-target="body"
                         class="p-4 border-t" {{ not is_first ? 'style="display: none;"' : '' }}>
                        <table class="min-w-full table-auto">
                            <thead>
                            <tr>
                                <th class="px-4 py-2 text-left">#</th>
                                <th class="px-4 py-2 text-left">Title</th>
                                <th class="px-4 py-2 text-right">Actions</th>
                            </tr>
                            </thead>
                            <tbody id="volume-{{ volume }}">
                            {% if all_chapters_same_cbz and volume_cbz_path is not null %}
                                <tr class="border-t hover:bg-green-100">
                                    <td class="px-4 py-2 text-green-500">
                                        <a data-turbo-frame="_top"
                                           href="{{ path('app_reader', { mangaSlug: manga.slug, chapterNumber: chapters|first.number, pageNumber: 1 }) }}">
                                            {{ '%02d'|format(volume) }}
                                        </a>
                                    </td>
                                    <td class="px-4 py-2 w-full text-left">
                                        <a data-turbo-frame="_top"
                                           href="{{ path('app_reader', { mangaSlug: manga.slug, chapterNumber: chapters|first.number, pageNumber: 1 }) }}">
                                            Volume {{ '%02d'|format(volume) }}
                                        </a>
                                    </td>
                                    <td class="px-4 py-2 flex justify-end gap-2">
                                        <a href="#"
                                           data-controller="download"
                                           data-action="download#download"
                                           data-download-url-value="{{ path('download_cbz', {chapterId: chapters|first.id}) }}"
                                           class="w-8 text-center">
                                            <i data-download-target="icon"
                                               class="fas fa-download text-gray-500 hover:text-green-500"></i>
                                        </a>
                                    </td>
                                </tr>
                            {% else %}
                                {% for chapter in chapters %}
                                    {% include 'manga/_chapter_row.html.twig' with {'chapter': chapter, 'manga': manga} %}
                                {% endfor %}
                            {% endif %}
                            </tbody>
                        </table>
                        <div class="relative flex items-center justify-between bg-white mt-4 mb-2 rounded-t-sm">
                            <div
                                class="absolute left-1/2 top-1/2 transform -translate-x-1/2 -translate-y-1/2">
                                <i data-table-target="toggleIcon" data-action="click->table#toggle"
                                   class="bg-gray-400 rounded-full font-bold text-sm text-white p-1 hover:bg-green-500 fas fa-chevron-up cursor-pointer"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        {% endfor %}
    </div>
</turbo-frame>
