{% extends "base.html" %}
{% comment %}
SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %}
{% load i18n static pretty_money django_tables2 %}

{# Use a fluid-width container #}
{% block containertype %}container-fluid{% endblock %}

{% block content %}
    <div class="row mt-4">
        <div class="col-sm-5 col-md-4" id="infos_div">
            <div class="row justify-content-center justify-content-md-end">
                {# User details column #}
                <div class="col picture-col">
                    <div class="card bg-light mb-4 text-center">
                        <a id="profile_pic_link" href="#">
                            <img src="{% static "member/img/default_picture.png" %}"
                                 id="profile_pic" alt="" class="card-img-top d-none d-sm-block">
                        </a>
                        <div class="card-body text-center text-break p-2">
                            <span id="user_note"><i class="small">{% trans "Please select a note" %}</i></span>
                        </div>
                    </div>
                </div>

                {# User selection column #}
                <div class="col-xl" id="user_select_div">
                    <div class="card bg-light border-success mb-4">
                        <div class="card-header">
                            <p class="card-text font-weight-bold">
                                {% trans "Consum" %}
                            </p>
                        </div>
                        <div class="card-body p-0" style="min-height:125px;">
                            <ul class="list-group list-group-flush" id="note_list">
                            </ul>
                        </div>

                        {# User search with autocompletion #}
                        <div class="card-footer">
                            <input class="form-control mx-auto d-block"
                                placeholder="{% trans "Name or alias..." %}" type="text" id="note" autofocus />
                        </div>
                    </div>
                </div>

                {# Summary of consumption and consume button #}
                <div class="col-xl-5 d-none" id="consos_list_div">
                    <div class="card bg-light border-info mb-4">
                        <div class="card-header">
                            <p class="card-text font-weight-bold">
                                {% trans "Select consumptions" %}
                            </p>
                        </div>
                        <div class="card-body p-0" style="min-height:125px;">
                            <ul class="list-group list-group-flush" id="consos_list">
                            </ul>
                        </div>
                        <div class="card-footer text-center">
                            <span id="consume_all" class="btn btn-primary">
                                {% trans "Consume!" %}
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            {# Show last used buttons #}
            <div class="card bg-light mb-4">
                <div class="card-header">
                    <p class="card-text font-weight-bold">
                        {% trans "Highlighted buttons" %}
                    </p>
                </div>
                <div class="card-body text-nowrap" style="overflow:auto hidden">
                    <div class="d-inline-flex flex-wrap justify-content-center" id="highlighted">
                        {% for button in highlighted %}
                            {% if button.display %}
                                <button class="btn btn-outline-dark rounded-0 flex-fill"
                                        id="highlighted_button{{ button.id }}" name="button" value="{{ button.name }}">
                                    {{ button.name }} ({{ button.amount | pretty_money }})
                                </button>
                            {% endif %}
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>

        {# Buttons column #}
        <div class="col">
            {# Regroup buttons under categories #}

            <div class="card bg-light border-primary text-center mb-4">
                {# Tabs for button categories #}
                <div class="card-header">
                    <ul class="nav nav-tabs nav-fill card-header-tabs">
                        {% for category in categories %}
                            <li class="nav-item">
                                <a class="nav-link font-weight-bold" data-toggle="tab" href="#{{ category.name|slugify }}">
                                    {{ category.name }}
                                </a>
                            </li>
                        {% endfor %}
                            <li class="nav-item">
                                <a class="nav-link font-weight-bold" data-toggle="tab" href="#search">
                                    {% trans "Search" %}
                                </a>
                            </li>
                    </ul>
                </div>

                {# Tabs content #}
                <div class="card-body">
                    <div class="tab-content">
                        {% for category in categories %}
                            <div class="tab-pane" id="{{ category.name|slugify }}">
                                <div class="d-inline-flex flex-wrap justify-content-center">
                                    {% for button in category.templates_filtered %}
                                        {% if button.display %}
                                            <button class="btn btn-outline-dark rounded-0 flex-fill"
                                                    id="button{{ button.id }}" name="button" value="{{ button.name }}">
                                                {{ button.name }} ({{ button.amount | pretty_money }})
                                            </button>
                                        {% endif %}
                                    {% endfor %}
                                </div>
                            </div>
                        {% endfor %}
                            <div class="tab-pane" id="search">
                                <input class="form-control mx-auto d-block mb-3"
                                       placeholder="{% trans "Search button..." %}" type="search" id="search-input"/>
                                <div class="d-inline-flex flex-wrap justify-content-center" id="search-results">
                                    {% for button in all_buttons %}
                                        {% if button.display %}
                                            <button class="btn btn-outline-dark rounded-0 flex-fill" hidden
                                                    id="search_button{{ button.id }}" name="button" value="{{ button.name }}">
                                                {{ button.name }} ({{ button.amount | pretty_money }})
                                            </button>
                                        {% endif %}
                                    {% endfor %}
                                </div>
                            </div>
                    </div>
                </div>

                {# Mode switch #}
                <div class="card-footer border-primary">
                    <a class="btn btn-sm btn-secondary float-left" href="{% url 'note:template_list' %}">
                        <i class="fa fa-edit"></i> {% trans "Edit" %}
                    </a>
                    <div class="btn-group btn-group-toggle float-right" data-toggle="buttons">
                        <label for="single_conso" class="btn btn-sm btn-outline-primary active">
                            <input type="radio" name="conso_type" id="single_conso" checked>
                            <i class="fa fa-long-arrow-left" aria-hidden="true"></i>
                            {% trans "Single consumptions" %}
                        </label>
                        <label for="double_conso" class="btn btn-sm btn-outline-primary">
                            <input type="radio" name="conso_type" id="double_conso">
                            <i class="fa fa-arrows-h" aria-hidden="true"></i>
                            {% trans "Double consumptions" %}
                        </label>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {# history of transaction #}
    <div class="card shadow mb-4" id="history">
        <div class="card-header">
            <p class="card-text font-weight-bold">
                {% trans "Recent transactions history" %}
            </p>
        </div>
        {% render_table table %}
    </div>
{% endblock %}

{% block extrajavascript %}
    <script type="text/javascript" src="{% static "note/js/consos.js" %}"></script>
    <script type="text/javascript">
        {% for button in highlighted %}
            {% if button.display %}
                document.getElementById("highlighted_button{{ button.id }}").addEventListener("click", function() {
                    addConso({{ button.destination_id }}, {{ button.amount }},
                        {{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
                        {{ button.id }}, "{{ button.name|escapejs }}");
                });
            {% endif %}
        {% endfor %}

        {% for category in categories %}
            {% for button in category.templates_filtered %}
            {% if button.display %}
                document.getElementById("button{{ button.id }}").addEventListener("click", function() {
                    addConso({{ button.destination_id }}, {{ button.amount }},
                        {{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
                        {{ button.id }}, "{{ button.name|escapejs }}");
                });
            {% endif %}
            {% endfor %}
        {% endfor %}

        {% for button in all_buttons %}
            {% if button.display %}
                document.getElementById("search_button{{ button.id }}").addEventListener("click", function() {
                    addConso({{ button.destination_id }}, {{ button.amount }},
                        {{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
                        {{ button.id }}, "{{ button.name|escapejs }}");
                });
            {% endif %}
        {% endfor %}
    </script>
{% endblock %}
