{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}

{% block title %}{% trans "Nutrition plan" %} – {{ plan.creation_date}}{% endblock %}


{% block header %}
<script>
function custom_modal_init()
{
    // Init the autocompleter after loading the modal dialog
    init_ingredient_autocompleter();
}

$(document).ready(function() {

    // Check if the user wants to see ingredients in english
    init_english_ingredients();

    /*
     * At the moment (Oct. 2013), firefox OS has no possibility to download
     * authenticated files, like these PDFs: when using web activities, we
     * can only pass the PDF's URL to the reader but this will fail, since that
     * application is not authenticated.
     */
    if(typeof MozActivity != 'undefined') {
        $(".pdf-button").remove();
    }
});
</script>
{% endblock %}



{% block content %}
{% if plan.description %}
<p>
    <span class="dimmed">{{plan.description}}</span>
</p>
{% endif %}


{% for meal in plan.meal_set.select_related %}
<div data-role="popup"
     id="editoptions-meal-{{ meal.id }}"
     data-theme="d"
     data-overlay-theme="a">
    <div data-role="header" data-theme="b">
        <h3>{% trans "Options" %}</h3>
    </div>

    <div role="main" class="ui-content">
    <a href="{% url 'wger.nutrition.views.meal.delete_meal' meal.id %}"
       title="{% trans 'Delete' %}"
       data-role="button">
       <img src="{% static 'images/icons/trash.svg' %}"
            width="22"
            height="22"
            alt="{% trans 'Delete meal' %}"
            title="{% trans 'Delete meal' %}">
            {% trans "Delete meal" %}
    </a>
    <a href="{% url 'meal-edit' meal.id %}"
       title="{% trans 'Edit' %}"
       data-role="button">
        <img src="{% static 'images/icons/edit.svg' %}"
             width="22"
             height="22"
             alt="{% trans 'Edit meal' %}"
             title="{% trans 'Edit meal' %}">
             {% trans "Edit meal" %}
    </a>
    <a href="{% url 'mealitem-add' meal.id %}"
       title="{% trans 'Add' %}"
       data-role="button"
       data-ajax="false">
        <img src="{% static 'images/icons/add.svg' %}"
             width="22"
             height="22"
             alt="{% trans 'Add item to meal' %}"
             title="{% trans 'Add item to meal' %}">
             {% trans "Add item to meal" %}
   </a>
   </div>
</div>

{% for item in meal.mealitem_set.select_related %}
    <div data-role="popup"
     id="editoptions-item-{{ item.id }}"
     data-theme="d"
     data-overlay-theme="a">
    <div data-role="header" data-theme="b">
        <h3>{% trans "Options" %}</h3>
    </div>


    <div role="main" class="ui-content">
    <h3>{{item.ingredient}}</h3>

    <table>
    <thead>
        <tr>
            <th colspan="2">{% trans "Nutritional data" %}</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td>{% trans "Energy" %}</td>
        <td class="align-right">{{item.get_nutritional_values.energy|floatformat}} kcal</td>
    </tr>
    <tr>
        <td>{% trans "Protein" %}</td>
        <td class="align-right">{{item.get_nutritional_values.protein|floatformat}} g</td>
    </tr>
    <tr>
        <td>{% trans "Carbohydrates" %}</td>
        <td class="align-right">{{item.get_nutritional_values.carbohydrates|floatformat}} g</td>
    </tr>
    <tr>
        <td>{% trans "Fat" %}</td>
        <td class="align-right">{{item.get_nutritional_values.fat|floatformat}} g</td>
    </tr>
    </tbody>
    </table>

    <a href="{% url 'wger.nutrition.views.meal_item.delete_meal_item' item.id %}"
       title="{% trans 'Delete' %}"
       data-role="button">
       <img src="{% static 'images/icons/trash.svg' %}"
            width="16"
            height="16"
            alt="{% trans 'Delete' %}">
            {% trans "Delete" %}
    </a>
    <a href="{% url 'mealitem-edit' item.id %}"
       title="{% trans 'Edit' %}"
       data-role="button">
        <img src="{% static 'images/icons/edit.svg' %}"
             width="16"
             height="16"
             alt="{% trans 'Edit' %}">
             {% trans "Edit" %}
    </a>
    </div>
    </div>
{% endfor %}

{% endfor %}


{% for meal in plan.meal_set.select_related %}
<ul data-role="listview"
    data-split-icon="grid"
    data-split-theme="b"
    style="margin-top: 3em;">

    <li data-theme="b" data-icon="false">
        <a href="#editoptions-meal-{{ meal.id }}" data-rel="popup">
        {% trans "Nr."%} {{ forloop.counter }}
            {% if meal.time %} &ndash; {{meal.time|time:"H:i"}}{% endif %}
        </a>
    </li>
    {% for item in meal.mealitem_set.select_related %}
        <li data-icon="false">
            <a href="#editoptions-item-{{ item.id }}" data-rel="popup">
                <h3>{{ item.ingredient.name }}</h3>
                <p>
                    {% if item.get_unit_type == MEALITEM_WEIGHT_GRAM %}{{ item.amount|floatformat:"0" }}g
                    {% else %}
                        {{ item.amount|floatformat:"0" }} × {{ item.weight_unit.unit.name }}
                    {% endif %}
                </p>
            </a>
        </li>
    {% if forloop.last %}
        <li>
            <a href="{% url 'mealitem-add' meal.id %}"
               data-ajax="false">{% trans "Add item to meal" %}</a>
        </li>
    {% endif %}
    {% empty %}
    <li>
        <a href="{% url 'mealitem-add' meal.id %}"
           title="{% trans 'Add one now.' %}"
           data-ajax="false">
            {% trans "No items for this meal." %}<br>
            {% trans "Add one now." %}
        </a>
    </li>
    {% endfor %}
</ul>
{% empty %}
<a href="{% url 'meal-add' plan.id %}"
   data-role="button">
        {% trans "No meals for this plan." %}<br>
        {% trans "Add one now." %}
</a>
{% endfor %}

<p style="margin-top:2em;">
    <a href="{% url 'meal-add' plan.id %}"
       data-role="button"
       data-mini="true">{% trans "Add a new meal" %}</a>
</p>


{% if language.short_name != 'en' %}
    <p>
    {% trans "If you find the ingredient list too short, you might want to activate the preference to also show English ingredients." %}
    <a href="{% url 'core:preferences' %}" id="ajax-english-ingredients">{% trans "That's done here" %}</a>:
    {% if user.userprofile.show_english_ingredients %}
    <img src="{% static 'images/icons/status-on.svg' %}"
         width="20"
         height="20"
         alt=""
         id="english-ingredients-status"
         data-show-ingredients="true">
    {% else %}
    <img src="{% static 'images/icons/status-off.svg' %}"
         width="20"
         height="20"
         alt=""
         id="english-ingredients-status"
         data-show-ingredients="false">
    {% endif %}
    </p>
{% endif %}

{% endblock %}



{% block sidebar %}
<h3>{% trans "Energy" %}</h3>
<table id="nutritional-data-energy">
    <thead>
        <tr>
            <th>{% trans "Energy" %}</th>
            <th class="align-right">{{nutritional_data.total.energy|floatformat:"0"}} kcal</th>
        </tr>
    </thead>
    <tbody>
        {% if user.userprofile.calories and plan.has_goal_calories %}
        {% with total=plan.get_calories_approximation %}
        <tr style="background-color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}">
            <td>{% trans "Goal" %}</td>
            <td class="align-right">
                <strong>{{user.userprofile.calories|floatformat:"0"}} kcal</strong>
            </td>
        </tr>
        {% endwith %}
        {% endif %}
    </tbody>
</table>


{% if plan.has_goal_calories %}
    <p>{% blocktrans %}You have selected that this nutritional plan has a
    goal amount of calories. Use the calculator or enter the value yourself.{% endblocktrans %}
    <a href="{% url 'calories-calculator' %}">{% trans "That's done here" %}</a>
    </p>
{% endif %}

<h3>{% trans "Nutritional data" %}</h3>
<table>
    <thead>
        <tr style="height:4em;">
            <th>{% trans "Macronutrients" %}</th>
            <th class="align-right">{% trans "Total" %}</th>
            <th class="align-right">{% trans "Percent of energy" %}</th>
            <th class="align-right">{% trans "g per body kg" %}</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>{% trans "Energy" %}</td>
            <td class="align-right">{{nutritional_data.total.energy|floatformat}} kcal</td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>{% trans "Protein" %}</td>
            <td class="align-right">{{nutritional_data.total.protein|floatformat}} g</td>
            <td class="align-right">{{nutritional_data.percent.protein|floatformat}} %</td>
            <td class="align-right">{{nutritional_data.per_kg.protein|floatformat}}</td>
        </tr>
        <tr>
            <td>{% trans "Carbohydrates" %}</td>
            <td class="align-right">{{nutritional_data.total.carbohydrates|floatformat}} g</td>
            <td class="align-right">{{nutritional_data.percent.carbohydrates|floatformat}} %</td>
            <td class="align-right">{{nutritional_data.per_kg.carbohydrates|floatformat}}</td>
        </tr>
        <tr>
            <td style="padding-left: 2em;">{% trans "Sugar content in carbohydrates" %}</td>
            <td class="align-right">{{nutritional_data.total.carbohydrates_sugar|floatformat}} g</td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>{% trans "Fat" %}</td>
            <td class="align-right">{{nutritional_data.total.fat|floatformat}} g</td>
            <td class="align-right">{{nutritional_data.percent.fat|floatformat}} %</td>
            <td class="align-right">{{nutritional_data.per_kg.fat|floatformat}}</td>
        </tr>
        <tr>
            <td style="padding-left: 2em;">{% trans "Saturated fat content in fats" %}</td>
            <td class="align-right">{{nutritional_data.total.fat_saturated|floatformat}} g</td>
            <td></td>
            <td></td>
        </tr>


        <tr>
            <th colspan="4">{% trans "Others" %}</th>
        </tr>
        <tr>
            <td>{% trans "Fibres" %}</td>
            <td class="align-right">{{nutritional_data.total.fibres|floatformat}} g</td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>{% trans "Sodium" %}</td>
            <td class="align-right">{{nutritional_data.total.sodium|floatformat}} g</td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>



<h3>{% trans "Options" %}</h3>
<a href="{% url 'wger.nutrition.views.plan.export_pdf' plan.id %}"
   class="pdf-button"
   data-role="button">
    <img src="{% static 'images/icons/save.svg' %}"
         width="32"
         height="32"
         alt="{% trans 'Download as PDF' %}"
         title="{% trans 'Download as PDF' %}">
    {% trans "Download as PDF" %}
</a>

<a href="{% url 'nutrition-edit' plan.id %}"
   data-role="button">
    <img src="{% static 'images/icons/edit.svg' %}"
         width="32"
         height="32"
         alt="{% trans 'Edit nutrition plan' %}">
    {% trans "Edit nutrition plan" %}
</a>

<a href="{% url 'nutrition-copy' plan.id %}"
   data-role="button">
    <img src="{% static 'images/icons/copy.svg' %}"
         width="32"
         height="32"
         alt="{% trans 'Make a copy of this nutrition plan' %}">
    {% trans "Make a copy of this nutrition plan" %}
</a>

<a href="{% url 'nutrition-delete' plan.id %}"
   data-role="button">
    <img src="{% static 'images/icons/trash.svg' %}"
         width="32"
         height="32"
         alt="{% trans 'Delete nutrition plan' %}">
    {% trans "Delete nutrition plan" %}
</a>

{% endblock %}
