{% extends 'timeseer.html' %}

{% from 'macros.html' import alerter, bootstrap_icon, info_popover %}
{% from 'resources.html' import resource_breadcrumb, resource_menu with context %}

{% block menu %}
{{ resource_menu('Series set templates') }}
{% endblock %}

{% block breadcrumb %}
{{
    resource_breadcrumb(["Series set templates"])
}}
{% endblock %}

{% block main %}
<div class="ts-section pb-3 mb-3">
    <div class="row justify-content-between">
        <h2 class="col-sm-6 h4">Manage series set templates</h2>
        <div class="col-sm-auto">
            <a  href="{{ url_for('.create_template', sourcename=source_name) }}"
                class="btn btn-primary">
                Create new
            </a>
        </div>
    </div>
</div>

<div class="my-3 py-3">
    {{ alerter() }}

    {% if series_set_templates %}

    <form>
        {% if source_name %}
        <input type="hidden" name="sourcename" value="{{ source_name }}">
        {% endif %}
        <div class="row g-2 pb-3">
            {% if series_set_templates|selectattr('origin.value', '==', 'ui')|list|count > 0 %}
            <div class="col-sm-auto">
                <div class="btn-group ts-dropdown-select-button">
                    <a class="btn border border-secondary dropdown-toggle"
                       href="#"
                       role="button"
                       id="selectionDropdown"
                       data-bs-toggle="dropdown"
                       aria-haspopup="true"
                       aria-expanded="false">
                    </a>
                    <div class="dropdown-menu" aria-labelledby="selectionDropdown">
                        <button type="submit"
                                class="dropdown-item ts-dropdown-action"
                                form="seriessettemplateselection"
                                formmethod="POST"
                                formaction="{{ url_for('.remove_templates') }}"
                                >
                                Remove selected
                        </button>
                    </div>
                </div>
            </div>
            {% endif %}
            {% if series_set_templates|count > 1 or template_pattern %}
            <div class="col">
                <label class="visually-hidden" for="templateFilterInput">Filter series set templates by name</label>
                <input class="form-control" id="templateFilterInput" name="templatepattern" value="{{ template_pattern }}" placeholder="Filter series set templates by name">
            </div>
            <div class="col-sm-auto">
                <button class="btn btn-secondary" type="submit">Filter</button>
            </div>
            {% endif %}
        </div>
    </form>

    <form id='seriessettemplateselection'>
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        {% if source_name %}
        <input type="hidden" name="sourcename" value="{{ source_name }}">
        {% endif %}
        <table class="ts-table ts-table-checkbox table table-borderless mt-3" data-ts-target="selectionDropdown">
            <thead class="table-secondary">
                <tr class="border-top border-bottom">
                    <th scope="col">
                        {% if series_set_templates|selectattr('origin.value', '==', 'ui')|list|count > 0 %}
                        <input type="checkbox" class="form-check-input">
                        {% endif %}
                    </th>
                    <th scope="col">Series set template name</th>
                </tr>
            </thead>
            <tbody>
                {% for template in series_set_templates %}
                <tr class="border-bottom">
                    <td>
                        {% if template.origin.value == 'ui' %}
                        <input class="form-check-input" type="checkbox" name="{{ template.name }}" {% if template_pattern %}checked{% endif %}>
                        {% else %}
                        {{ info_popover('Generated by Timeseer or created in the configuration.', 'lock', 'text-muted', placement="top") }}
                        {% endif %}
                    </td>
                    <td>
                        <input type="hidden" name="templatenames" value="{{ template.name }}">
                        <a class="ts-table-link text-decoration-none text-dark" href="{{ url_for('.preview_template', templatename=template.name, sourcename=source_name) }}">
                            {{ template.name }}
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </form>
    {% else %}
    <p class="alert alert-info">No series set templates found.</p>
    {% endif %}
</div>
{% endblock %}
