{% extends 'base.html' %}

{% block header %}
    <h1>{% block title %}Admin{% endblock %}</h1>
{% endblock %}

{% block content %}
    <div id="activeRegistrationCodes">
        <table>
            <thead>
                <th>Registration Code</th>
                <th>Creation Time</th>
                <th>Expiration Time</th>
                <th>Usages</th>
                <th>Max Usages</th>
                <th>Registration Link</th>
                <th>Expire</th>
            </thead>
            <tbody>
                {% for registration_code in registration_codes %}
                    <tr>
                        <td>{{ registration_code.code|tojson|safe }}</td>
                        <td>{{ registration_code.creation_time|tojson|safe }}</td>
                        <td>{{ registration_code.expiration_time|tojson|safe }}</td>
                        <td>{{ registration_code.usages|tojson|safe }}</td>
                        <td>{{ registration_code.max_usages|tojson|safe }}</td>
                        <td>
                            {% if not registration_code.is_expired() and registration_code.has_available_uses() %}
                            <a href="/register?registrationCode={{ registration_code.code }}">link</a>
                            {% endif %}
                        <td class="borderless">
                            <form method="POST" action="/admin/expire_registration_code">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                                <input type="hidden" name="registration_code" value="{{ registration_code.code }}"/>
                                {% if not registration_code.is_expired() %}
                                <input type="submit" name="expire" value="Expire">
                                {% else %}
                                <input type="submit" name="delete" value="Delete">
                                {% endif %}
                            </form>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    <hr>
    <div id="registeredUsers">
        <table>
            <thead>
            <th>Matrix ID</th>
                <th>User</th>
                <th>Registration Time</th>
                <th>Registration Code</th>
            </thead>
            <tbody>
                {% for registered_user in registered_users %}
                    <tr>
                        <td>{{ registered_user.registered_user_id|tojson|safe }}</td>
                        <td>{{ registered_user.username|tojson|safe }}</td>
                        <td>{{ registered_user.registered_time|tojson|safe }}</td>
                        <td>{{ registered_user.registration_code|tojson|safe }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    <hr>
    <div id="register">
        <form method="POST" action="/admin/add_registration_code">
            {{ add_registration_code_form.csrf_token }}
            <div class="row">
            {{ add_registration_code_form.expiration_time.label }} {{ add_registration_code_form.expiration_time() }}
            </div>
            <div class="row">
            {{ add_registration_code_form.max_usages.label }} {{ add_registration_code_form.max_usages(default="1", value="1") }}
            </div>
            <div class="formSubmit">
                <input type="submit" value="Add Token">
            </div>
        </form>
    </div>
{% endblock %}