{% extends "freeform/export/_layout" %}
{% hook "freeform.prepareCpTemplate" %}

{% set title = "Export Profiles"|t('freeform') %}

{% block contextMenu %}
    {% if not isCraft5 and selectedSite %}
        {% include "_elements/sitemenu" with { urlFormat: 'freeform/export/profiles?site={handle}' } %}
    {% endif %}
{% endblock %}

{% block actionButton %}

    {% if freeform.edition.atLeast('lite') and currentUser.can("freeform-pro-exportProfilesManage") %}

        <div class="buttons">
            {% if forms|length %}
                {% if forms|length > 1 %}
                    <div class="btn submit menubtn add icon">{{ "New Export Profile"|t('freeform') }}</div>
                    <div class="menu">
                        <ul>
                            {% for form in forms %}
                                <li>
                                    <a href="{{ url('freeform/export/profiles/new/'~form.handle) }}">
                                        {% if form.color %}
                                            <span class="color-indicator" style="background-color: {{ form.color }};"></span>
                                        {% endif %}
                                        {{ form.name|t }}
                                    </a>
                                </li>
                            {% endfor %}
                        </ul>
                    </div>
                {% else %}
                    <a class="btn submit add icon" href="{{ url('freeform/export/profiles/new/'~(forms|first).handle|default) }}">
                        {{ "New Export Profile for {form}"|t({form: (forms|first).name}) }}
                    </a>
                {% endif %}
            {% endif %}
        </div>

    {% endif %}

{% endblock %}

{% set content %}

    {% if exportProfiles|length %}
        <table id="export-profiles" class="data fullwidth collapsible">
            <thead>
            <th scope="col">{{ "Name"|t('app') }}</th>
            <th scope="col">{{ "Form"|t('freeform') }}</th>
            <th scope="col">{{ "Submissions"|t('freeform') }}</th>
            <th>{{ "Actions"|t('app') }}</th>
            <td class="thin"></td>
            </thead>
            <tbody>
            {% for profile in exportProfiles %}
                <tr data-id="{{ profile.id }}" data-name="{{ profile.name|t }}">
                    <th scope="row" data-title="{{ 'Name'|t }}">
                        {% if currentUser.can("freeform-pro-exportProfilesManage") %}
                            <a href="{{ url('freeform/export/profiles/' ~ profile.id) }}">
                                {{ profile.name }}
                            </a>
                        {% else %}
                            {{ profile.name }}
                        {% endif %}
                    </th>
                    <td>
                        {% set form = profile.form %}
                        {% if form %}
                            {% set canManage = currentUser.can("freeform-formsManage") or currentUser.can("freeform-formsManage:"~form.id) %}
                            {% if canManage %}
                                <a href="{{ url('freeform/forms/' ~ form.id) }}">
                                    {{ form.name }}
                                </a>
                            {% else %}
                                {{ form.name }}
                            {% endif %}
                        {% endif %}
                    </td>
                    <td>
                        {% if profile.limit > 0 and profile.submissionCount > profile.limit %}
                            {{ profile.limit }}
                        {% else %}
                            {{ profile.submissionCount }}
                        {% endif %}
                    </td>
                    <td>
                        <div class="btngroup small file-download-list">
                            {% for type, exporter in exporters %}
                                <a data-type="{{ type }}" class="btn small">{{ exporter }}</a>
                            {% endfor %}
                        </div>
                        <form method="post" style="position: absolute; left:-9999px; top: -9999px;">
                            {{ csrfInput() }}
                            <input type="hidden" name="type" value="csv" />
                            <input type="hidden" name="profileId" value="{{ profile.id }}" />
                            <input type="hidden" name="action" value="freeform/export/profiles/export" />
                        </form>
                    </td>
                    <td class="thin">
                        {% if currentUser.can("freeform-pro-exportProfilesManage") %}
                            <a class="delete icon" title="{{ 'Delete'|t('freeform') }}" role="button"></a>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    {% else %}
        <div class="zilch">
            {% if freeform.edition.atLeast('lite') %}
                {{ "No export profiles exist yet."|t("freeform") }}
            {% else %}
                {{ "Upgrade to the Lite or Pro edition of Freeform to get access to Export Profiles."|t('freeform') }}
            {% endif %}
        </div>
    {% endif %}
{% endset %}

{% css %}
    #content { padding: 0; }
{% endcss %}

{% js %}
    var adminTable = new Craft.AdminTable({
    tableSelector: '#export-profiles',
    noObjectsSelector: '#noexport-profiles',
    newObjectBtnSelector: '#newprofilecontainer',
    deleteAction: Craft.getCpUrl('freeform/export/profiles/delete'),
    confirmDeleteMessage: '{{ "Are you sure you want to delete this profile?"|t }}'
    });

    $('.file-download-list > a').on({
        click: function() {
            var parent = $(this).parents(".btngroup:first");
            var type = $(this).data('type');

            parent.next().find("input[name=type]").val(type);
            parent.next("form").submit();
        }
    });
{% endjs %}
