{% extends request.is_ajax|yesno:'nullcont.htm,abonapp/ext.htm' %}
{% load i18n %}
{% block content %}

    <div class="row">
        <div class="col-sm-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">{% trans 'Services for buy' %}</h3>
                </div>
                <div class="panel-body">
                    <table class="table table-condensed">
                        <thead>
                        <tr>
                            <th>{% trans 'Pick a service' %}</th>
                            <th>{% trans 'Service' %}</th>
                            <th>{% trans 'Price' %}</th>
                            <th>{% trans 'Speed In' %}</th>
                            <th>{% trans 'Speed Out' %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% with can_ch_trf=perms.tariff_app.change_tariff %}
                        {% for service in services %}
                        <tr>
                            <td>
                                {% if abon_tariff %}
                                    <a href="#" class="btn btn-sm btn-default disabled">
                                        <span class="glyphicon glyphicon-shopping-cart"></span>
                                    </a>
                                {% else %}
                                    <a href="{% url 'abonapp:pick_tariff' group.pk abon.username %}?selected_tariff={{ service.pk }}"
                                       class="btn btn-sm btn-default" title="{{ service.get_calc_type_display }}" data-toggle="tooltip">
                                        <span class="glyphicon glyphicon-shopping-cart"></span>
                                    </a>
                                {% endif %}
                            </td>
                            <td>
                                {% if can_ch_trf %}
                                    <a href="{% url 'tarifs:edit' service.pk %}" title="{{ service.descr }}" data-toggle="tooltip"><b>{{ service.title }}</b></a>
                                {% else %}
                                    {{ service.title }}
                                {% endif %}
                            </td>
                            <td>{{ service.amount }} {% trans 'currency' %}</td>
                            <td>{{ service.speedIn }}</td>
                            <td>{{ service.speedOut }}</td>
                        </tr>
                        {% empty %}
                        <tr><td colspan="5">
                            {% trans 'This group has no services' %}
                            <a href="{% url 'abonapp:ch_group_tariff' group.pk %}" class="btn btn-sm btn-default" title="{% trans 'User groups' %}">
                                <span class="glyphicon glyphicon-cog"></span> {% trans 'Tariffs in groups' %}
                            </a>
                        </td></tr>
                        {% endfor %}
                        {% endwith %}
                        </tbody>
                    </table>
                    <a href="{% url 'abonapp:ch_group_tariff' group.pk %}" class="btn btn-sm btn-primary" title="{% trans 'User groups' %}">
                        <span class="glyphicon glyphicon-cog"></span> {% trans 'Attach services to group' %}
                    </a>
                </div>
            </div>
        </div>
        <div class="col-sm-6">

            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">{% trans "Subscriber's service" %}</h3>
                </div>
                <div class="panel-body">
                    <dl class="dl-horizontal">
                        {% if abon_tariff %}
                            <dt>{% trans 'Service' %}</dt>
                            <dd>
                                {% if abon_tariff.tariff %}
                                    {% if perms.tariff_app.change_tariff %}
                                        <a href="{% url 'tarifs:edit' abon_tariff.tariff.pk %}" title="{{ abon_tariff.time_start|default:'' }}">
                                            {{ abon_tariff.tariff.title }}
                                        </a>
                                    {% else %}
                                        {{ abon_tariff.tariff.title }}
                                    {% endif %}
                                {% else %}
                                    <b class="text-danger">{% trans 'We have a problem in DB: AbonTariff instance has no related to service' %}</b>
                                {% endif %}
                            </dd>
                            <dt>{% trans 'Sum' %}</dt>
                            <dd>{{ abon_tariff.tariff.amount }} {% trans 'currency' %}.</dd>

                            <dt>{% trans 'Input speed' %}</dt>
                            <dd>{{ abon_tariff.tariff.speedIn }}</dd>

                            <dt>{% trans 'Output speed' %}</dt>
                            <dd>{{ abon_tariff.tariff.speedOut }}</dd>

                            <dt>{% trans 'Date of start' %}</dt>
                            <dd>{{ abon_tariff.time_start|date:"d E Y, l H:i" }}</dd>

                            <dt>{% trans 'Works until' %}</dt>
                            <dd>{{ abon_tariff.deadline|date:"d E Y, l H:i" }}</dd>
                        {% else %}
                            <dt>{% trans 'Subscriber has no service' %}</dt>
                            <dd>
                                <a href="{% url 'abonapp:pick_tariff' group.pk abon.username %}">
                                    {% trans 'Buy service' %}
                                </a>
                            </dd>
                        {% endif %}

                        {% if abon.last_connected_tariff %}
                            <dt>{% trans 'Last connected service' %}</dt>
                            <dd><a href="{{ abon.last_connected_tariff.get_absolute_url }}">{{ abon.last_connected_tariff.title }}</a></dd>
                        {% endif %}

                        <dt>{% trans 'Auto continue service.' %}</dt>
                        <dd>
                            <input type="checkbox" data-url="{% url 'abonapp:set_auto_continue_service' group.pk abon.username %}" class="autosave" {{ abon.autoconnect_service|yesno:'checked,' }}>
                            <a href="https://github.com/nerosketch/djing/blob/devel/docs/tarifs.md" title="{% trans 'Help' %}" data-toggle="tooltip">?</a>
                        </dd>
                    </dl>

                    {% if abon_tariff.tariff.descr %}
                        <p>{{ abon_tariff.tariff.descr }}</p>
                    {% endif %}

                    {% if abon_tariff %}
                        {% if perms.abonapp.can_complete_service %}
                            <a href="{% url 'abonapp:unsubscribe_service' group.pk abon.username abon_tariff.pk %}" class="btn btn-sm btn-danger">
                                <span class="glyphicon glyphicon-remove-circle"></span> {% trans 'Finish service' %}
                            </a>
                        {% else %}
                            <a href="#" class="btn btn-sm btn-danger disabled">
                                <span class="glyphicon glyphicon-remove-circle"></span> {% trans 'Finish service' %}
                            </a>
                        {% endif %}
                    {% endif %}
                </div>
            </div>
        </div>

        {% if perms.tariff_app.view_periodic_pay %}
        <div class="col-sm-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">{% trans 'Periodic pay' %}</h3>
                </div>
                <div class="panel-body">
                    {% if periodic_pay %}
                        <dl class="dl-horizontal">
                            <dt>{% trans 'Pay logic' %}</dt>
                            <dd>{{ periodic_pay.periodic_pay }}</dd>
                            <dt>{% trans 'Last pay' %}</dt>
                            <dd>{{ periodic_pay.last_pay|default:'Not yet paid' }}</dd>
                            <dt>{% trans 'Next time to pay' %}</dt>
                            <dd>{{ periodic_pay.next_pay|date:'d E Y' }}</dd>
                        </dl>
                        <a href="{% url 'abonapp:del_periodic_pay' group.pk abon.username periodic_pay.pk %}" class="btn btn-sm btn-danger">
                            <span class="glyphicon glyphicon-remove-circle"></span> {% trans 'Remove periodic pay' %}
                        </a>
                    {% else %}
                        <a href="{% url 'abonapp:add_periodic_pay' group.pk abon.username %}" class="btn btn-primary btn-sm btn-modal">
                            <span class="glyphicon glyphicon-plus"></span> {% trans 'Add periodic pay' %}
                        </a>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}

    </div>

{% endblock %}
