{# @var user \App\Entity\UserSystem\User #}

{% import "helper.twig" as helper %}

<div class="card mt-4">
    <div class="card-header">
        <i class="fa-solid fa-plug fa-fw" aria-hidden="true"></i>
        {% trans %}user.settings.api_tokens{% endtrans %}
    </div>
    <div class="card-body">
        <span class="text-muted">{% trans %}user.settings.api_tokens.description{% endtrans %}</span><br>
        <a href="{{ path('api_doc') }}">{% trans %}user.settings.show_api_documentation{% endtrans %}</a>

        {% if user.apiTokens.empty %}
            <br><br>
            <b>{% trans %}user.settings.api_tokens.no_api_tokens_yet{% endtrans %}</b>
            <br><br>
        {% else %}
            <form action="{{ path('user_api_tokens_delete') }}" method="post"
                    {{ stimulus_controller('elements/delete_btn') }} {{ stimulus_action('elements/delete_btn', "submit", "submit") }}
                  data-delete-title="{% trans %}user.settings.api_tokens.delete.title{% endtrans %}"
                  data-delete-message="{% trans %}user.settings.api_tokens.delete.message{% endtrans %}">
                <input type="hidden" name="_method" value="DELETE">
                <input type="hidden" name="_token" value="{{ csrf_token('delete' ~ user.id) }}">
                <table class="table table-striped table-bordered table-hover table-sm mt-2">
                    <thead>
                    <tr>
                        <th>{% trans %}api_tokens.name{% endtrans %}</th>
                        <th>{% trans %}api_tokens.access_level{% endtrans %}</th>
                        <th>{% trans %}api_tokens.expiration_date{% endtrans %}</th>
                        <th>{% trans %}tfa_u2f.keys.added_date{% endtrans %}</th>
                        <th>{% trans %}api_tokens.last_time_used{% endtrans %}</th>
                        <th></th>
                    </tr>
                    </thead>

                    <tbody>
                    {% for api_token in user.apiTokens %}
                        {# @var api_token \App\Entity\UserSystem\ApiToken #}
                        <tr>
                            <td>{{ api_token.name }}<br>
                                <small class="text-muted">{% trans%}api_token.ends_with{% endtrans%} ...<i>{{ api_token.lastTokenChars }}</i></small></td>
                            <td>{{ api_token.level.translationKey|trans }}</td>
                            <td>
                                {{ helper.format_date_nullable(api_token.validUntil) }}
                                {% if api_token.valid %}
                                    <span class="badge bg-success badge-success">{% trans %}api_token.valid{% endtrans %}</span>
                                {% else %}
                                    <span class="badge bg-warning badge-warning">{% trans %}api_token.expired{% endtrans %}</span>
                                {% endif %}
                            </td>
                            <td>{{ helper.format_date_nullable(api_token.addedDate) }}</td>
                            <td>{{ helper.format_date_nullable(api_token.lastTimeUsed) }}</td>
                            <td>
                                <button type="submit" class="btn btn-danger btn-sm" name="token_id"
                                        value="{{ api_token.id }}" {% if not is_granted('@api.manage_tokens') %}disabled="disabled"{% endif %}>
                                    <i class="fas fa-trash-alt fa-fw"></i> {% trans %}user.settings.api_tokens.delete{% endtrans %}
                                </button>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </form>
        {% endif %}

        <a href="{{ path('user_api_token_create') }}" class="btn btn-success" {% if not is_granted('@api.manage_tokens') %}disabled="disabled"{% endif %}>
            <i class="fas fa-plus-square fa-fw"></i> {% trans %}api_token.create_new{% endtrans %}
        </a>

        <hr>
        <h5>{% trans %}api.api_endpoints.title{% endtrans %}:</h5>

        <div class="row">
            <div class="col-sm-2">
                <b>{% trans %}api.api_endpoints.partdb{% endtrans %}:</b>
            </div>
            <div class="col-sm-3">
                <button class="btn btn-outline-dark d-print-none btn-sm" data-clipboard-text="{{ url('api_entrypoint') }}">
                    <i class="fas fa-copy"></i>
                </button>
                <span>{{ url('api_entrypoint') }}</span>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-2">
                <b>{% trans %}api.api_endpoints.kicad_root_url{% endtrans %}:</b>
            </div>
            <div class="col-sm-10">
                <button class="btn btn-outline-dark d-print-none btn-sm" data-clipboard-text="{{ absolute_url('/en/kicad-api/') }}">
                    <i class="fas fa-copy"></i>
                </button>
                <span>{{ url("kicad_api_root") | replace({"v1/": ""}) }}</span>
            </div>
        </div>


    </div>
</div>