{% extends 'user/form.html.twig' %}
{% import "macros/widgets.html.twig" as widgets %}

{% block form_body %}
    <div class="card-body">

        <div class="row mb-3">
            <div class="col-12 col-md-6">
                <p>
                    {{ 'api_password.intro'|trans }}
                </p>
                <p>
                    <strong>URL</strong>: {{ api_url }}
                </p>
            </div>

            <div class="col-12 col-md-6 text-md-end">
                <a class="btn action-create modal-ajax-form" href="{{ path('user_profile_access_token', {'username': user.userIdentifier}) }}">{{ icon('create', true) }} {{ 'create'|trans }}</a>
                <a class="btn" target="_blank" href="{{ path('api.swagger_ui') }}">{{ icon('documentation', true) }} Swagger Docs</a>
                <a class="btn" target="_blank" href="{{ 'rest-api.html'|docu_link }}">{{ icon('help', true) }} {{ 'help'|trans }}</a>
            </div>
        </div>

        {% if created_token is not null %}
            <div class="card mb-4">
                <div class="ribbon bg-red">{{ 'status.new'|trans }}</div>
                <div class="card-body">
                    <h3 class="card-title">{{ created_token.name }}</h3>
                    <p class="text-secondary"></p>
                    <p>{{ 'api_token_hidden'|trans }}</p>
                    <div class="codeblock">
                        <div class="codeblock-copy" style="position: absolute; left: 30px; padding-top: 5px">
                            <button class="btn btn-icon btn-dark" data-toggle="tooltip" title="{{ 'copy_to_clipboard'|trans }}" onclick="navigator.clipboard.writeText('{{ created_token.token }}');"><svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clipboard icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2"></path><path d="M9 3m0 2a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z"></path></svg></button>
                        </div>
                        <pre style="padding-left: 65px"><code>{{ created_token.token }}</code></pre>
                    </div>
                    {% if qr_code_payload is not null %}
                        <p class="mt-3 mb-1">
                            <img style="max-width: 200px; max-height: 200px;" alt="API QR code" src="{{ qr_code_data_uri(qr_code_payload) }}" />
                        </p>
                        <p class="text-secondary">
                            <i class="{{ 'info'|icon(false) }}"></i>
                            {{ 'api_token_qr_hint'|trans }}
                        </p>
                    {% endif %}
                </div>
            </div>
        {% endif %}

        {% if access_tokens|length > 0 %}
            <table class="table table-vcenter">
                <thead>
                <tr>
                    <th>{{ 'name'|trans }}</th>
                    <th>{{ 'last_usage'|trans }}</th>
                    <th>{{ 'expires'|trans }}</th>
                    <th class="w-min actions"></th>
                </tr>
                </thead>
                <tbody>
                {% for token in access_tokens %}
                    <tr>
                        <td>{{ token.name }}</td>
                        <td>
                            {% if token.lastUsage is not null %}
                                {{ token.lastUsage|date_time }}
                            {% endif %}
                        </td>
                        <td>
                            {% if token.expiresAt is not null %}
                                {{ token.expiresAt|date_short }}
                            {% endif %}
                        </td>
                        <td>
                            {{ widgets.action_button('trash', {'url': path('delete_api_token', {id: token.id}), class: 'api-link', 'attr': {
                                'data-question': 'confirm.delete',
                                'data-event': 'kimai.accessToken',
                                'data-method': 'DELETE',
                                'data-question': 'confirm.delete',
                                'data-msg-error': 'action.delete.error',
                            }}) }}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% endif %}

    </div>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script type="text/javascript">
        document.addEventListener('kimai.initialized', function() {
            KimaiReloadPageWidget.create('kimai.accessToken');
        });
    </script>
{% endblock %}
