{% extends 'charlink/base.html' %}
{% load django_bootstrap5 %}
{% load charlinkutils %}
{% load charlink_versioned_static %}
{% load i18n %}

{% block page_title %}{% translate "CharLink" %}{% endblock page_title %}

{% block extra_css %}
    <link rel="stylesheet" type="text/css" href="{% charlink_static 'charlink/css/added-icons.css' %}">
{% endblock extra_css %}

{% block charlink_page_header %}<h1 class="page-header text-center">{% translate "Character Linking" %}</h1>{% endblock charlink_page_header %}

{% block charlink_content %}
    <div class="card">
        <div class="card-header">
            <ul class="nav nav-tabs card-header-tabs">
                <li class="nav-item">
                    <button class="nav-link active" id="login-tab" data-bs-toggle="tab" data-bs-target="#loginForm" type="button" role="tab" aria-controls="loginForm" aria-selected="true">
                        {% translate "Login Form" %}
                    </button>
                </li>
                <li class="nav-item">
                    <button class="nav-link" id="characters-tab" data-bs-toggle="tab" data-bs-target="#linkedCharacters" type="button" role="tab" aria-controls="linkedCharacters" aria-selected="false">
                        {% translate "Linked Characters" %}
                    </button>
                </li>
            </ul>
        </div>
        <div class="card-body">
            <div class="tab-content">
                <div class="tab-pane fade text-center container-fluid show active" id="loginForm" role="tabpanel" aria-labelledby="login-tab" tabindex="0">
                    <div class="row justify-content-center">
                        <div class="col-12 my-3">
                            <p>{% translate "Select the apps you want to link from the list. You will be redirected to eveonline website to authenticate and provide the token with all the scopes needed." %}</p>
                        </div>
                        <div class="col-lg-4">
                            <form method="post">
                                {% csrf_token %}
                                {% bootstrap_form form %}
                                <button type="submit" class="btn btn-primary mt-3">{% translate "Login" %}</button>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="tab-pane fade text-center" id="linkedCharacters" role="tabpanel" aria-labelledby="characters-tab" tabindex="0">
                    <div class="table-responsive">
                        <table class="table table-aa align-middle">
                            <thead>
                                <th>{% translate "Character" %}</th>
                                {% for app_imports in characters_added.apps.values %}
                                    {% for login_import in app_imports.imports %}
                                        <th scope="col">{{ login_import.field_label }}</th>
                                    {% endfor %}
                                {% endfor %}
                            </thead>
                            <tbody>
                                {% for char in characters_added.characters %}
                                    <tr>
                                        <td scope="row">{{ char }}</td>
                                        {% for imports in characters_added.apps.values %}
                                            {% for login_data in imports.imports %}
                                                {% with is_added=char|get_char_attr:login_data.get_query_id %}
                                                    {% if is_added %}
                                                        <td><i class="fas fa-check fa-lg"></i></td>
                                                    {% else %}
                                                        <td><i class="fas fa-times fa-lg"></i></td>
                                                    {% endif %}
                                                {% endwith %}
                                            {% endfor %}
                                        {% endfor %}
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="card mt-3">
        <div class="card-header">
            <ul class="nav nav-tabs card-header-tabs">
                {% for import_data in characters_added.apps.values %}
                    {% for login_data in import_data.imports %}
                        <li class="nav-item">
                            <button class="nav-link{% if forloop.parentloop.first and forloop.first %} active{% endif %}" id="{{ import_data.app_label }}-{{ login_data.unique_id }}-tab" data-bs-toggle="tab" data-bs-target="#{{ import_data.app_label }}-{{ login_data.unique_id }}" type="button" role="tab" aria-controls="{{ import_data.app_label }}-{{ login_data.unique_id }}" aria-selected="{% if forloop.parentloop.first and forloop.first %}true{% else %}false{% endif %}">
                                {{ login_data.field_label }}
                            </button>
                        </li>
                    {% endfor %}
                {% endfor %}
            </ul>
        </div>
        <div class="card-body">
            <div class="tab-content">
                {% for import_data in characters_added.apps.values %}
                    {% for login_data in import_data.imports %}
                        <div class="tab-pane fade {% if forloop.parentloop.first and forloop.first %}show active {% endif %}text-center" id="{{ import_data.app_label }}-{{ login_data.unique_id }}" role="tabpanel" aria-labelledby="{{ import_data.app_label }}-{{ login_data.unique_id }}-tab" tabindex="0">
                            {% translate "Scopes required:" %}
                            {{ login_data.scopes|unordered_list|safe }}
                        </div>
                    {% endfor %}
                {% endfor %}
            </div>
        </div>
    </div>
{% endblock charlink_content %}