{% extends "base.html" %}
{% load i18n static wger_extras django_bootstrap_breadcrumbs %}

{% block title %}{{current_user|format_username}}{% endblock %}


{% block breadcrumbs %}
    {% if current_user.userprofile.gym %}
        {{ block.super }}

        {% if perms.gym.manage_gyms %}
            {% breadcrumb "Gyms" "gym:gym:list" %}
        {% endif %}
        {% breadcrumb_raw current_user.userprofile.gym "gym:gym:user-list" current_user.userprofile.gym.pk %}
        {% breadcrumb_raw current_user|format_username "core:user:overview" current_user.pk %}
    {% endif %}
{% endblock %}


{% block content %}
{% url 'core:user:trainer-login' current_user.pk as trainer_login %}

{% if perms.gym.gym_trainer %}

<h4>{% trans "Workouts" %} <small>({% blocktrans with number=10 %}last {{ number }}{% endblocktrans %})</small></h4>
{% if perms.gym.gym_trainer %}
<a href="{{trainer_login}}?next={% url 'manager:workout:overview' %}" class="btn btn-default btn-sm">{% trans "Overview" %}</a>
<a href="{{trainer_login}}?next={% url 'manager:workout:add' %}" class="btn btn-default btn-sm">{% trans "Add workout" %}</a>
{% endif %}

<table class="table">
<thead>
<tr>
    <th style="width: 25%;">{% trans "Date" %}</th>
    <th>{% trans "Description" %}</th>
    <th>{% trans "Number of logs (days)" %}</th>
    <th>{% trans "Last activity" %}</th>
</tr>
</thead>
<tbody>
{% for workout in workouts %}
<tr>
    <td>
        {{workout.workout.creation_date}}
    </td>
    <td>
        <a href="{{trainer_login}}?next={{ workout.workout.get_absolute_url }}">{{workout.workout}}</a>
    </td>
    <td>
        {{workout.logs|default:'-/-'}}
    </td>
    <td>
        {{workout.last_log.date|default:'-/-'}}
    </td>
</tr>
{% empty %}
<tr>
    <td colspan="2">{% trans "No workouts found." %}</td>
</tr>
{% endfor %}
</tbody>
</table>





<h4>{% trans "Log" %} <small>({% blocktrans with number=10 %}last {{ number }}{% endblocktrans %})</small></h4>
{% if perms.gym.gym_trainer %}
<a href="{{trainer_login}}?next={% url 'manager:workout:calendar' %}" class="btn btn-default btn-sm">{% trans "Calendar" %}</a>
{% endif %}

<table class="table">
<thead>
<tr>
    <th style="width: 25%;">{% trans "Date" %}</th>
    <th>{% trans "General impression" %}</th>
    <th>{% trans "Notes" %}</th>
    <th>{% trans "Time" %}</th>
</tr>
</thead>
<tbody>
{% for log in session %}
<tr>
    <td>
        {{log.date}}
    </td>
    <td>
        {{log.get_impression_display}}
    </td>
    <td>
        {{log.notes|default:'-/-'|truncatewords:6}}
    </td>
    <td>
        {% if log.time_start and log.time_end %}
            {{log.time_start|time:"H:i"}} - {{log.time_end|time:"H:i"}}
        {% else %}
            -/-
        {% endif %}
    </td>
</tr>
{% empty %}
<tr>
    <td colspan="3">{% trans "Nothing found" %}</td>
</tr>
{% endfor %}
</tbody>
</table>



<h4>{% trans "Weight" %} <small>({% blocktrans with number=5 %}last {{ number }}{% endblocktrans %})</small></h4>
{% if perms.gym.gym_trainer %}
<a href="{{trainer_login}}?next={% url 'weight:overview' current_user.username %}" class="btn btn-default btn-sm">{% trans "Weight overview" %}</a>
{% endif %}

<table class="table">
<thead>
<tr>
    <th style="width: 25%;">{% trans "Date" %}</th>
    <th>{% trans "Weight" %}</th>
</tr>
</thead>
<tbody>
{% for weight in weight_entries %}
<tr>
    <td>
        {{weight.date}}
    </td>
    <td>
        {{weight.weight}} {% trans_weight_unit 'kg' current_user %}
    </td>
</tr>
{% empty %}
<tr>
    <td colspan="3">{% trans "Nothing found" %}</td>
</tr>
{% endfor %}
</tbody>
</table>



<h4>{% trans "Nutrition plans" %} <small>({% blocktrans with number=5 %}last {{ number }}{% endblocktrans %})</small></h4>
{% if perms.gym.gym_trainer %}
    <a href="{{trainer_login}}?next={% url 'nutrition:plan:overview' %}" class="btn btn-default btn-sm">{% trans "Overview" %}</a>
    <a href="{{trainer_login}}?next={% url 'nutrition:plan:add' %}" class="btn btn-default btn-sm">{% trans "Add nutrition plan" %}</a>
{% endif %}

<table class="table">
<thead>
<tr>
    <th style="width: 25%;">{% trans "Date" %}</th>
    <th>{% trans "Description" %}</th>
    <th>{% trans "Energy" %}</th>
    <th>{% trans "Protein" %}</th>
    <th>{% trans "Carbohydrates" %}</th>
    <th>{% trans "Fat" %}</th>
</tr>
</thead>
<tbody>
{% for nutrition_plan in nutrition_plans %}
<tr>
    <td>
        {{nutrition_plan.creation_date}}
    </td>
    <td>
        <a href="{{trainer_login}}?next={{ nutrition_plan.get_absolute_url }}">{{nutrition_plan}}</a>
    </td>
    <td>
        {{nutrition_plan.get_nutritional_values.total.energy|floatformat}} {% trans "kcal" %}
    </td>
    <td>
        {{nutrition_plan.get_nutritional_values.total.protein|floatformat}} {% trans_weight_unit 'g' current_user %}
    </td>
    <td>
        {{nutrition_plan.get_nutritional_values.total.carbohydrates|floatformat}} {% trans_weight_unit 'g' current_user %}
    </td>
    <td>
        {{nutrition_plan.get_nutritional_values.total.fat|floatformat}} {% trans_weight_unit 'g' current_user %}
    </td>
</tr>
{% empty %}
<tr>
    <td colspan="6">
        {% trans "Nothing found" %}
    </td>
</tr>
{% endfor %}
</tbody>
</table>


{% endif %}
{% endblock %}


{% block sidebar %}
{% if perms.gym.manage_gyms or perms.gym.manage_gym %}
<div class="btn-group pull-right">
    <button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown">
        {% trans "Actions" %} <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">
        <li>
            <a href="{% url 'core:user:edit' current_user.pk %}" class="wger-modal-dialog">{% trans "Edit"%}</a>
        </li>
        <li class="divider"></li>
        <li>
            {% if current_user.is_active %}
                <a href="{% url 'core:user:deactivate' current_user.pk %}" >{% trans "Deactivate user"%}</a>
            {% else %}
                <a href="{% url 'core:user:activate' current_user.pk %}">{% trans "Activate user"%}</a>
            {% endif %}
        </li>
        <li>
            <a data-url="{% url 'gym:gym:reset-user-password' current_user.pk %}" data-toggle="modal" data-target="#confirmation-modal">{% trans "Reset user password" %}</a>
        </li>
        {% if perms.gym.manage_gym or perms.gym.manage_gyms %}
        {% if current_user.userprofile.gym %}
        <li>
            <a href="{% url 'gym:gym:edit-user-permission' current_user.pk %}" class="wger-modal-dialog">{% trans "Roles"%}</a>
        </li>
        {% endif %}
        {% endif %}
        <li>
            <a href="{% url 'core:user:delete' current_user.pk %}" class="wger-modal-dialog">{% trans "Delete"%}</a>
        </li>
    </ul>
</div>

<div id="confirmation-modal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">{% trans 'Password reset confirmation' %}</h4>
            </div>
            <div class="modal-body">
                <p>{% trans 'Do you really want to change this user&#39;s password?' %}</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">{% trans 'No' %}</button>
                <button type="button" class="btn btn-success">{% trans 'Yes' %}</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#confirmation-modal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);
            var url = button.data('url');
            var modal = $(this);

            modal.find('button.btn-success').click(function() {
                window.location.href = url;
            });
        });
    });
</script>

{% endif %}
<h4>{% trans "Details" %}</h4>

<table class="table">
    <tr>
        <td>{% trans "Nr." %}</td>
        <td>{{current_user.pk}}</td>
    </tr>
    <tr>
        <td>{% trans "Name" %}</td>
        <td>{{current_user.first_name}}</td>
    </tr>
    <tr>
        <td>{% trans "Last name" %}</td>
        <td>{{current_user.last_name}}</td>
    </tr>
    <tr>
        <td>{% trans "Email" %}</td>
        <td>
            {% if current_user.email %}
                <a href="mailto:{{current_user.email}}">{{current_user.email}}</a>
            {% else %}
                -/-
            {% endif %}
        </td>
    </tr>
    <tr>
        <td>{% trans "Phone" %}</td>
        <td> {{ current_user.userprofile.address.phone|default:'-/-' }}</td>
    </tr>
    <tr>
        <td>{% trans "Address" %}</td>
        <td>
            {% if not current_user.userprofile.address.zip_code and not current_user.userprofile.address.city and not current_user.userprofile.address.street%}
                -/-
            {% else %}
                {{current_user.userprofile.address.zip_code}} {{current_user.userprofile.address.city}}<br>
                {{current_user.userprofile.address.street}}
            {% endif %}
        </td>
    </tr>
    <tr>
        <td>{% trans "Registered" %}</td>
        <td>{{current_user.date_joined}}</td>
    </tr>
    <tr>
        <td>{% trans "Last login" %}</td>
        <td>{{current_user.last_login}}</td>
    </tr>
{% if perms.gym.manage_gym or perms.gym.manage_gyms %}
    <tr {% if not current_user.is_active %}class="danger"{% endif%}>
        <td>{% trans "Status" %}</td>
        <td>
            {% if current_user.is_active %}
                {% trans "Active" %}
            {% else %}
                {% trans "Inactive" %}
            {% endif %}
        </td>
    </tr>
{% endif %}
</table>

{#          #}
{# Contract #}
{#          #}
{% if perms.gym.add_contract %}
<div class="btn-group pull-right">
    <button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown">
        {% trans "Actions" %} <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">
        <li>
            <a href="{% url 'gym:contract:list' current_user.id %}">{% trans "Overview"%}</a>
        </li>
        <li>
            <a href="{% url 'gym:contract:add' current_user.pk %}">{% trans "Add"%}</a>
        </li>
    </ul>
</div>

<h4>{% trans "Contracts" %} <small>({% blocktrans with number=5 %}last {{ number }}{% endblocktrans %})</small></h4>
<table class="table">
    {% for contract in contracts %}
    <tr>
        <td>
            {{ contract }}
        </td>
        <td {% if not contract.is_active %}style="text-decoration: line-through;" {% endif %}>
            <a href="{{ contract.get_absolute_url }}">{{ contract.date_start }}</a>
        </td>
        <td>
            {{ contract.amount }}
        </td>
    </tr>
    {% empty %}
    <tr>
        <td>
            {% trans "Nothing found" %}
        </td>
    </tr>
    {% endfor %}
</table>
{% endif %}


{#             #}
{# Admin notes #}
{#             #}
{% if perms.gym.add_adminusernote %}
<div class="btn-group pull-right">
    <button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown">
        {% trans "Actions" %} <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">
        <li>
            <a href="{% url 'gym:admin_note:list' current_user.pk %}">{% trans "Overview" %}</a>
        </li>
        <li>
            <a href="{% url 'gym:admin_note:add' current_user.pk %}" class="wger-modal-dialog">{% trans "Add"%}</a>
        </li>
    </ul>
</div>

<h4>{% trans "Notes" %} <small>({% blocktrans with number=5 %}last {{ number }}{% endblocktrans %})</small></h4>
<table class="table">
    {% for note in admin_notes %}
    <tr>
        <td>
            {{ note.timestamp_created }}
        </td>
        <td>
            {{ note.note|truncatewords:10 }}
        </td>
    </tr>
    {% empty %}
    <tr>
        <td>
            {% trans "Nothing found" %}
        </td>
    </tr>
    {% endfor %}
</table>
{% endif %}


{#           #}
{# Documents #}
{#           #}
{% if perms.gym.add_userdocument %}
<div class="btn-group pull-right">
    <button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown">
        {% trans "Actions" %} <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">
        <li>
            <a href="{% url 'gym:document:list' current_user.pk %}">{% trans "Overview" %}</a>
        </li>
        <li>
            <a href="{% url 'gym:document:add' current_user.pk %}">{% trans "Add"%}</a>
        </li>
    </ul>
</div>

<h4>{% trans "Documents" %} <small>({% blocktrans with number=5 %}last {{ number }}{% endblocktrans %})</small></h4>
<table class="table">
    {% for document in current_user.userdocument_member.all %}
    <tr>
        <td>
            <a href="{{ document.document.url }}"
               class="btn btn-default btn-xs"
               title="{% trans 'Download' %}"
               download="{{ document.original_name }}">
                <span class="glyphicon glyphicon-download"></span>
            </a>
        </td>
        <td>
            {{ document.timestamp_created }}
        </td>
        <td>
            {{ document.name }}
        </td>
    </tr>
    {% empty %}
    <tr>
        <td>
            {% trans "Nothing found" %}
        </td>
    </tr>
    {% endfor %}
</table>
{% endif %}



{#                   #}
{# Gym configuration #}
{#                   #}
{% if perms.gym.change_gymuserconfig and current_user.gymuserconfig %}
<div class="btn-group pull-right">
    <button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown">
        {% trans "Actions" %} <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">

        <li>
            <a href="{% url 'gym:user_config:edit' current_user.gymuserconfig.pk %}" class="wger-modal-dialog">{% trans "Edit"%}</a>
        </li>
    </ul>
</div>
{% endif %}

<h4>{% trans "Configuration" %}</h4>
<table class="table">
    <tr>
        <td>{% trans 'Include in inactive overview' %}</td>
        <td style="text-align: right;">
            {% if current_user.gymuserconfig.include_inactive %}
                <span class="{% fa_class 'check' %}"></span>
            {% else %}
                <span class="{% fa_class 'times' %}"></span>
            {% endif %}
        </td>
    </tr>
</table>
{% endblock %}


{#         #}
{# Options #}
{#         #}
{% block options %}
{% if perms.gym.gym_trainer %}
    <a href="{% url 'core:user:trainer-login' current_user.pk %}" class="btn btn-success btn-sm">{% trans "Log in as this user" %}</a>
{% endif %}
{% endblock %}
