{% if errors %}
<article class="error">
    <header>Wystąpiły błędy</header>
    <ul>
        {% for error in errors %}
        <li>{{ error }}</li>
        {% endfor %}
    </ul>
</article>
{% endif %}

{% if urls %}
{# Check if any item has month or param #}
{% set has_month = urls | selectattr('month', 'defined') | selectattr('month') | list | length > 0 %}
{% set has_param = urls | selectattr('param', 'defined') | selectattr('param') | list | length > 0 %}

<article>
    <header>
        <h3>Wygenerowane linki ({{ count }})</h3>
    </header>

    <p>
        <small>Kliknij w link, aby pobrać plik bezpośrednio z serwera IMGW.</small>
    </p>

    <div class="download-actions">
        <button class="secondary outline" onclick="copyAllLinks()">
            Kopiuj wszystkie linki
        </button>
        <button class="secondary outline" onclick="downloadWgetScript()">
            Pobierz skrypt wget
        </button>
    </div>

    <table class="download-table">
        <thead>
            <tr>
                <th>Rok</th>
                {% if has_month %}
                <th>Miesiąc</th>
                {% endif %}
                {% if has_param %}
                <th>Parametr</th>
                {% endif %}
                <th>Plik</th>
                <th>Akcja</th>
            </tr>
        </thead>
        <tbody>
            {% for item in urls %}
            <tr>
                <td>{{ item.year }}</td>
                {% if has_month %}
                <td>{{ item.month if item.month else '-' }}</td>
                {% endif %}
                {% if has_param %}
                <td>{{ item.param if item.param else '-' }}</td>
                {% endif %}
                <td>
                    <code>{{ item.filename }}</code>
                </td>
                <td>
                    <a href="{{ item.url }}" target="_blank" role="button" class="outline">
                        Pobierz
                    </a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <textarea id="all-links" style="display: none;">{% for item in urls %}{{ item.url }}
{% endfor %}</textarea>

    <script>
        function copyAllLinks() {
            const textarea = document.getElementById('all-links');
            navigator.clipboard.writeText(textarea.value).then(() => {
                alert('Skopiowano ' + {{ count }} + ' linków do schowka!');
            });
        }

        function downloadWgetScript() {
            const links = document.getElementById('all-links').value.trim().split('\n');
            let script = '#!/bin/bash\n# IMGWTools - wget download script\n\n';
            script += 'mkdir -p imgw_data\ncd imgw_data\n\n';
            links.forEach(url => {
                script += 'wget -c "' + url + '"\n';
            });

            const blob = new Blob([script], { type: 'text/plain' });
            const a = document.createElement('a');
            a.href = URL.createObjectURL(blob);
            a.download = 'download_imgw.sh';
            a.click();
        }
    </script>
</article>
{% else %}
{% if not errors %}
<article>
    <p>Brak wyników dla wybranych parametrów.</p>
</article>
{% endif %}
{% endif %}
